forked from mcneel/rhino-developer-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SampleMake2dSnapshots.py
179 lines (148 loc) · 6.94 KB
/
SampleMake2dSnapshots.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
################################################################################
# SampleMake2dSnapshots.py
# Copyright (c) 2018 Robert McNeel & Associates.
# See License.md in the root of this repository for details.
################################################################################
import Rhino
import System
import scriptcontext as sc
################################################################################
# Creates a unique root layer
################################################################################
def __CreateRootLayer():
for i in range(1, 99):
layer_name = "Make2d " + str(i).zfill(2)
layer_index = sc.doc.Layers.FindByFullPath(layer_name, -1)
if layer_index < 0:
layer_index = sc.doc.Layers.Add(layer_name, System.Drawing.Color.Black)
return sc.doc.Layers[layer_index].Id
return System.Guid.Empty
################################################################################
# Creates some sublayer to the root layer
################################################################################
def __CreateSubLayers(layer_name, parent_id):
# Create a layer named snapshot
layer = Rhino.DocObjects.Layer();
layer.Name = layer_name
layer.ParentLayerId = parent_id
layer_index = sc.doc.Layers.Add(layer)
layer_id = sc.doc.Layers[layer_index].Id
# Create a sublayer named "Silhouettes" that is hidden
sublayer0 = Rhino.DocObjects.Layer();
sublayer0.Name = "Silhouettes"
sublayer0.IsVisible = False
sublayer0.PlotWeight = 0.5
sublayer0.ParentLayerId = layer_id
sublayer0_index = sc.doc.Layers.Add(sublayer0)
# Create a sublayer named "Curves" that is hidden
sublayer1 = Rhino.DocObjects.Layer();
sublayer1.Name = "Curves"
sublayer1.IsVisible = False
sublayer1.ParentLayerId = layer_id
sublayer1_index = sc.doc.Layers.Add(sublayer1)
return sublayer0_index, sublayer1_index
################################################################################
# Computes hidden line drawing curves for a snapshot
################################################################################
def __ComputeHiddenLineDrawing(snapshot, viewport):
# Restore the snapshot
script = "_-Snapshots _Restore \"" + snapshot + "\" _Enter _Enter"
Rhino.RhinoApp.RunScript(script, False)
Rhino.RhinoApp.Wait()
# Create and define hidden line drawing parameters
parameters = Rhino.Geometry.HiddenLineDrawingParameters()
parameters.AbsoluteTolerance = sc.doc.ModelAbsoluteTolerance
parameters.Flatten = True
parameters.IncludeHiddenCurves = False
parameters.IncludeTangentEdges = False
#parameters.IncludeTangentSeams = False
parameters.SetViewport(viewport)
# Process all of the document objects
for obj in sc.doc.Objects:
if obj.IsSelectable(True, False, False, False):
parameters.AddGeometry(obj.Geometry, None)
# Create the hidden line drawing geometry
return Rhino.Geometry.HiddenLineDrawing.Compute(parameters, True)
################################################################################
# Main function
################################################################################
def SampleMake2dSnapshots():
# Unselect everything
sc.doc.Objects.UnselectAll()
# Verify there are objects in the document
if 0 == sc.doc.Objects.Count:
print "No objects to make a 2-D drawing."
return
# Verify there are snapshots in the document
snapshots = sc.doc.Snapshots.Names
if 0 == snapshots.Length:
print "No snapshots to restore."
return
# Get the active view
view = sc.doc.Views.ActiveView
if view is None:
return
# Verify the active viewport is in a perspective projection
viewport = view.ActiveViewport
if not viewport.IsPerspectiveProjection:
print "Set a perspective view active before running script."
return
# Store the results of each operation on a layer, with the name
# of the snapshot, under this layer.
parent_id = __CreateRootLayer()
# Use this to offset the results of each operation
origin = Rhino.Geometry.Point3d.Origin
# Save indices of new layers, that are initially hidden,
# so we can turn them on later.
layer_indices = []
# Do this for every snapshot...
for snapshot in snapshots:
# Create the hidden line drawing (Make2D) geometry
hld = __ComputeHiddenLineDrawing(snapshot, viewport)
if hld is None:
continue
# Create a transformation that moves the objects to the origin
bbox = hld.BoundingBox(True)
dir = origin - Rhino.Geometry.Point3d(bbox.Min.X, bbox.Min.Y, 0.0)
xform = Rhino.Geometry.Transform.Translation(dir)
# Update the origin, for next use, by offsetting it the length
# of the bounding box along the xaxis
dir = bbox.Corner(False, True, True) - bbox.Corner(True, True, True)
origin = origin + Rhino.Geometry.Vector3d.XAxis * (dir.Length * 1.1)
# Create a sublayer with the name of the snapshot
silhouette_layer_index, curve_layer_index = __CreateSubLayers(snapshot, parent_id)
layer_indices.append(silhouette_layer_index)
layer_indices.append(curve_layer_index)
# Create object attributes that use the sublayer
attributes = sc.doc.CreateDefaultAttributes()
# Add the results to the document
for hld_curve in hld.Segments:
if not hld_curve:
continue
if not hld_curve.ParentCurve:
continue
if hld_curve.ParentCurve.SilhouetteType == Rhino.Geometry.SilhouetteType.None:
continue
if hld_curve.SegmentVisibility == Rhino.Geometry.HiddenLineDrawingSegment.Visibility.Visible:
if hld_curve.IsSceneSilhouette:
attributes.LayerIndex = silhouette_layer_index
else:
attributes.LayerIndex = curve_layer_index
# Copy the curve
curve = hld_curve.CurveGeometry.DuplicateCurve()
# Flatten the curve
curve.Transform(xform)
# Add it to the document
sc.doc.Objects.AddCurve(curve, attributes)
# Turn on the new, hidden layers
for layer_index in layer_indices:
sc.doc.Layers[layer_index].IsVisible = True
# Redraw the document
sc.doc.Views.Redraw()
################################################################################
# Check to see if this file is being executed as the "main" python
# script instead of being used as a module by some other python script
# This allows us to use the module which ever way we want.
################################################################################
if __name__ == "__main__":
SampleMake2dSnapshots()