-
Notifications
You must be signed in to change notification settings - Fork 0
/
make2d.py
executable file
·245 lines (213 loc) · 6.89 KB
/
make2d.py
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
import os
import shelve
import Rhino
import cPickle as pickle
from rhinopythonscripts import LayerTools, FileTools
from rhinopythonscripts.GeomTools import bakeMany
import System
from scriptcontext import doc
from System.Drawing import Rectangle
import rhinoscriptsyntax as rs
def rcmd(commandstring, echo=True, mask=True):
if mask:
premask = '-_%s '
try:
cmd = premask % commandstring
except:
print commandstring, 'failed to mask'
else:
cmd = commandstring
result = Rhino.RhinoApp.RunScript(cmd, echo)
if not result:
print 'The following command failed:'
print ' ',cmd
return result
def rcmds(commands, echo=True, mask=True):
for cmd in commands:
rcmd( cmd, echo, mask )
def formatCmd( tup ):
if tup[1]:
return tup[0] % tup[1]
else:
return tup[0]
def storeNearRect(filePath):
near = list(doc.Views.ActiveView.ActiveViewport.GetNearRect())
far = list(doc.Views.ActiveView.ActiveViewport.GetFarRect())
print 'view stored in %s' % filePath
f = open( filePath, 'wb' )
near.extend(far)
pickle.dump( near, f )
f.close()
def storeViewPoints(sids):
outMask = '/amigos/db/%sviewPoints'
for sid in sids:
setView( sid )
fPath = outMask % sid
storeNearRect( fPath )
def chopCurvesToFrustum(curves):
view = doc.Views.ActiveView.ActiveViewport
nearPts = view.GetNearRect()
farPts = view.GetFarRect()
near = view.GetFrustumNearPlane()
far = view.GetFrustumFarPlane()
left = view.GetFrustumLeftPlane()
right = view.GetFrustumRightPlane()
top = view.GetFrustumTopPlane()
bottom = view.GetFrustumBottomPlane()
frustPts = nearPts.extend(farPts)
frustBox = Rhino.Geometry.BoundingBox( frustPts )
frustum = Rhino.Geometry.Brep.CreatefromBox( frustBox )
def bakeRaw( data, attributes = None, transVect=None):
if type(data) not in (list, tuple):
data = [data]
elif type(data[0]) in (list, tuple, List):
data = data[0]
if attributes:
att = attributes
else:
att = Rhino.DocObjects.ObjectAttributes()
for thing in data:
bakeMany(thing, att, transVect)
doc.Views.ActiveView.Redraw()
def setView( sid ):
# still don't know how to set up the size of the viewport.
# this needs to be done manually
f = open('/amigos/db/%sview' % sid, 'rb')
view = pickle.load(f)
f.close()
if len(view) == 3:
geom = view[0]
vect = Rhino.Geometry.Vector3d(view[1])
targ = view[2]
else:
return 'bad view data'
vp = doc.Views.ActiveView.ActiveViewport
vp.SetCameraDirection( vect, True)
vp.SetCameraTarget(targ, True)
bb = geom.GetBoundingBox(True)
vp.ZoomBoundingBox( bb)
doc.Views.ActiveView.Redraw()
return view
def bakePickle( path, layer=None, attributes=None, transVector=None):
if os.path.exists(path):
try:
f = open( path, 'rb' )
data = pickle.load(f)
f.close()
bakeRaw(data, attributes, transVector)
print 'baked %s to layer "%s"' % (path, layer)
except:
print 'trouble unpickling', path
else:
print '%s does not exist' % path
def getModelLayers(filePath, layers ):
data = {}
print 'getting %s' % filePath
model = Rhino.FileIO.File3dm.Read( filePath )
for layer in layers:
geoms = []
objs = model.Objects.FindByLayer( layer )
for obj in objs:
geom = obj.Geometry
geom.EnsurePrivateCopy()
geoms.append(geom)
data[layer] = geoms
model.Dispose()
return data
def crossMatch(iter1, iter2):
pairs = []
for n in iter1:
for m in iter2:
pair = (n, m)
pairs.append(pair)
return pairs
def frustumX():
view = doc.Views.ActiveView.ActiveViewport
near = view.GetNearRect()
far = view.GetFarRect()
pairs = crossMatch( near, far )
crvs = []
for pair in pairs:
crvs.append(Rhino.Geometry.Curve.CreateControlPointCurve(pair, 1))
return crvs
def make2D(sid):
# create frustum X
frustumLines = frustumX()
att = LayerTools.layerAttributes("viewportFramework", System.Drawing.Color.Cyan )
bakeMany( frustumLines, att)
cmds = [
'SelAll',
'Make2D Enter',
'Invert Delete SetView World Top',
'SelNone',
'SelLayer Make2D::visible::lines::viewportframework',
'SelLayer Make2D::hidden::lines::viewportframework',
'ZS',
'Delete',
]
rcmds( cmds )
def exportPerspective( path ):
# export the current view as an illustrator file
cmds = [
'SellAll',
'Export "%s"' % path,
'Enter',
]
rcmds( cmds )
if __name__=='__main__':
'''This Make2D script needs to do the following things:
export linework for the topolines
export linework for the site outline
export linework for the other parcels
export linework of the nearby busstops (and create circles for them)
export linework for the terrain analysis curves and the drain arrows.
'''
scenes = [
('topo',), # maybe
('site','siteoutline'), # no
('parcels',), # maybe
('busstops',), # no
('terraindrain','terrainmeshlines','drainarrows',), # maybe
('plantwindows',), # no
]
from site_ids import ids
aiMask = 'C:\\amigos\\make2ds\\%s-%s.ai'
for sid in ids[:1]:
rcmd('SetView World Perspective')
# set view
setView(sid)
for scene in scenes[:1]:
outPath = aiMask % ( sid, scene )
# delete everything
FileTools.deleteAll()
# get data
if scene[0] == 'topo':
layers = [
'bigcontours',
'smallcontours',
]
importPath = '/amigos/dump/%s-terrain.3dm' % sid
data = getModelLayers( importPath, layers )
print ', '.join(['%s:%s' % (k, len(data[k])) for k in data])
else:
data = {}
if len(scene) > 1:
layers = scene[1:]
else:
layers = scene
for layer in layers:
fmask = '/amigos/db/%s%s' % (sid, layer)
f = open(fmask, 'rb')
data[layer] = pickle.load(f)
f.close()
# bake data
for key in data:
layerAtt = LayerTools.layerAttributes( key )
bakeMany( data[key], layerAtt )
# do make2d or export AI file of current view
exportPerspective( outPath )
#make2D(sid)
#cmds = [
#'SelAll',
#'Export "%s" Enter' % outPath,
#]