forked from mcneel/rhino-developer-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SampleDrawExtrudedCurves.py
84 lines (72 loc) · 3 KB
/
SampleDrawExtrudedCurves.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
################################################################################
# SampleDrawExtrudedCurves.py
# Copyright (c) 2019 Robert McNeel & Associates.
# See License.md in the root of this repository for details.
################################################################################
import scriptcontext as sc
import Rhino
import System
# GetObject-derived class that limits object picking to planar curves
class GetPlanarCurve(Rhino.Input.Custom.GetObject):
def CustomGeometryFilter(self, rh_object, geometry, component_index):
if not rh_object:
return False
if not geometry:
return False
if not isinstance(geometry, Rhino.Geometry.Curve):
return False
tol = rh_object.Document.ModelAbsoluteTolerance
rc, plane = geometry.TryGetPlane(tol)
return rc
# DisplayConduit-derived class that draws Breps
class DrawBrepConduit(Rhino.Display.DisplayConduit):
def __init__(self, breps):
self.breps = breps
def CalculateBoundingBox(self, e):
for brep in self.breps:
e.IncludeBoundingBox(brep.GetBoundingBox(False))
def PreDrawObjects(self, e):
att = e.Display.DisplayPipelineAttributes
if att.ShadingEnabled:
material = Rhino.Display.DisplayMaterial(System.Drawing.Color.DarkGray)
for brep in self.breps:
e.Display.DrawBrepShaded(brep, material)
for brep in self.breps:
e.Display.DrawBrepWires(brep, System.Drawing.Color.Black)
# Sample function that select planar curve, extrudes them in some fixed
# direction, and then draws them in a custom display conduit.
def SampleDrawExtrudedCurves():
go = GetPlanarCurve()
go.SetCommandPrompt("Select planar curves")
go.SubObjectSelect = False
go.GetMultiple(1, 0)
if go.CommandResult() != Rhino.Commands.Result.Success:
return
tol = sc.doc.ModelAbsoluteTolerance
breps = []
for objref in go.Objects():
crv = objref.Curve();
rc, plane = crv.TryGetPlane(tol)
dir = plane.ZAxis * 3
srf = Rhino.Geometry.Surface.CreateExtrusion(crv, dir)
brep = srf.ToBrep()
brep.Faces.SplitKinkyFaces(Rhino.RhinoMath.DefaultAngleTolerance, True)
if crv.IsClosed:
solid = brep.CapPlanarHoles(tol)
if Rhino.Geometry.BrepSolidOrientation.Inward == solid.SolidOrientation:
solid.Flip();
breps.append(solid)
else:
breps.append(brep)
objref.Object().Select(False)
conduit = DrawBrepConduit(breps)
conduit.Enabled = True
sc.doc.Views.Redraw()
rs.GetString('Press <Enter> to continue')
conduit.Enabled = False
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__":
SampleDrawExtrudedCurves()