-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathFileTools.py
More file actions
163 lines (147 loc) · 5.29 KB
/
FileTools.py
File metadata and controls
163 lines (147 loc) · 5.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import os
import sys
import System
import System.Collections.Generic as SCG
import Rhino
from Rhino.FileIO import FileWriteOptions, FileReadOptions
import scriptcontext
import rhinoscriptsyntax as rs
import xlrd
import LayerTools
def xlsToObjs(filePath, headers=True):
'''converts xls rows to a list of dictionaries. first row headers become
the attribute names.'''
wb = xlrd.open_workbook(filePath)
sheet = wb.sheet_by_index(0)
if headers:
# get the first row
head = sheet.row_values(0)
objs = []
# get the rows
for r in range(sheet.nrows - 1):
r += 1 # shift down a row
obj = {}
vals = sheet.row_values(r)
for c, v in enumerate(vals):
# lookup key by column index
obj[head[c]] = v
objs.append(obj)
return objs
def exportFile(filePath,
version=4,
geomOnly=False,
selectedOnly=False,
):
'''Export a file.'''
opt = FileWriteOptions()
opt.FileVersion = version
opt.WriteGeometryOnly = geomOnly
opt.WriteSelectedObjectsOnly = selectedOnly
return scriptcontext.doc.WriteFile(filePath, opt)
def importFiles(filePathList):
'''Import a list of files'''
opt = FileReadOptions()
opt.ImportMode = True
for f in filePathList:
#print 'Importing %s' % f
scriptcontext.doc.ReadFile(f, opt)
def silentImport(filePathList):
"""Takes one or more file paths and returns RhinoCommon
File3dm objects for each file, if they exist. Returns an empty
list if no file is found.
"""
models = []
if type(filePathList) == str: # assume it's one path
filePathList = [filePathList]
for f in filePathList:
model = Rhino.FileIO.File3dm.Read(f)
if not model: continue
models.append(model)
return models
def modelsToLayerGeometryDict(models):
out = {}
for m in models:
for layer in m.Layers:
name = layer.Name
out[name] = []
objs = m.Objects.FindByLayer(name)
for obj in objs:
geom = obj.Geometry
geom.EnsurePrivateCopy()
out[name].append(geom)
m.Dispose()
return out
def fileGeometryDict(fileNames):
models = silentImport(fileNames)
return modelsToLayerGeometryDict(models)
def importFile(filePath):
'''import one file.'''
importFiles([filePath])
def deleteAll():
"""Deletes everything in the current Rhino scriptcontext.doc. Returns nothing."""
guidList = []
objType = Rhino.DocObjects.ObjectType.AnyObject
objTable = scriptcontext.doc.Objects
objs = objTable.GetObjectList(objType)
for obj in objs:
guidList.append(obj.Id)
for guid in guidList:
objTable.Delete(guid, True)
def importLayers(filePaths, layerNames):
'''Input a list of filePaths and a list of layerNames, in order to import all the
files and return a list of RhinoObjects on each layer, in corresponding layers.
Layers that do not exist or contain no objects will return empty lists.'''
outObjs = []
importFiles(filePaths)
for ln in layerNames:
objs = scriptcontext.doc.Objects.FindByLayer(ln)
outObjs.append(objs)
return outObjs
def importLayerGeometryDict(filePaths, layerNames=None, silent=True):
'''Input a list of filePaths and a list of layerNames, in order to import all the
files and return a list of RhinoObjects on each layer, in corresponding layers.
Layers that do not exist or contain no objects will return empty lists.'''
outObjs = []
importFiles(filePaths)
if not layerNames:
layerNames = []
# get all the layers
lyrTable = scriptcontext.doc.Layers
for lyr in lyrTable:
layerNames.append(lyr.FullPath)
for ln in layerNames:
objs = scriptcontext.doc.Objects.FindByLayer(ln)
if objs:
outObjs.append([obj.Geometry for obj in objs])
else:
outObjs.append([])
return dict(zip(layerNames, outObjs))
def importSmartLayerDict(filePaths, layerNames):
'''Input a list of filePaths and a list of layerNames, in order to import all the
files and return a list of SmartFeatures corresponding to each layer.
Layers that do not exist or contain no objects will return empty lists.'''
importFiles(filePaths)
outFeatures = []
for ln in layerNames:
features = LayerTools.getLayerSmartFeatures(ln)
outFeatures.append(features)
return dict(zip(layerNames, outFeatures))
def exportLayers(layerNames, filePath, version=4):
'''Export only the items on designated layers to a file.'''
# save selection
oldSelection = rs.SelectedObjects()
# clear selection
rs.UnselectAllObjects()
# add everything on the layers to selection
for name in layerNames:
objs = scriptcontext.doc.Objects.FindByLayer(name)
guids = [obj.Id for obj in objs]
scriptcontext.doc.Objects.Select.Overloads[SCG.IEnumerable[System.Guid]](guids)
# export selected items
exportFile(filePath, version, selectedOnly=True)
#clear selection
rs.UnselectAllObjects()
# restore selection
if oldSelection:
scriptcontext.doc.Objects.Select.Overloads[SCG.IEnumerable[System.Guid]](oldSelection)
#print 'exported %s' % filePath