Skip to content

Commit

Permalink
Added some new Python samples
Browse files Browse the repository at this point in the history
  • Loading branch information
dalefugier committed Feb 8, 2019
1 parent 3b8d81f commit 7e8996f
Show file tree
Hide file tree
Showing 2 changed files with 165 additions and 0 deletions.
99 changes: 99 additions & 0 deletions rhinopython/SampleGetPolyline.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
################################################################################
# SampleGetPolyline.py
# Copyright (c) 20197 Robert McNeel & Associates.
# See License.md in the root of this repository for details.
################################################################################
import System
import Rhino
import scriptcontext as sc

# GetPoint-derived class
class GetPolylinePoint(Rhino.Input.Custom.GetPoint):

def __init__(self):
self.color = Rhino.ApplicationSettings.AppearanceSettings.DefaultLayerColor
self.points = []

def Count(self):
return len(self.points)

def PointArray(self):
return System.Array[Rhino.Geometry.Point3d](self.points)

def PointFromIndex(self, index):
return self.points[index]

def AppendPoint(self, point):
self.points.append(point)

def OnDynamicDraw(self, e):
point_count = len(self.points)
if point_count > 1:
for i in range(1, point_count):
e.Display.DrawLine(self.points[i-1], self.points[i], self.color)
for i in range(point_count-1):
e.Display.DrawPoint(self.points[i])
Rhino.Input.Custom.GetPoint.OnDynamicDraw(self, e)

# Function to pick and add a polyline to the document.
# Note, this function isn't nearly as fancy as RhinoCommon's
# Rhino.Input.Custom.GetPolyline.
def SampleGetPolyline():

got_pline = False

gp = GetPolylinePoint()
while not got_pline:
gp.ClearConstructionPoints()
gp.ClearSnapPoints()
gp.ConstrainDistanceFromBasePoint(Rhino.RhinoMath.UnsetValue)
gp.PermitFromOption(True)

point_count = gp.Count()
if point_count > 0:
gp.AddSnapPoints(gp.PointArray())
gp.SetBasePoint(gp.PointFromIndex(-1), False)
if point_count >= 3:
gp.AddConstructionPoint(gp.PointFromIndex(0))

if point_count == 0:
gp.SetCommandPrompt("Start of polyline")
gp.AcceptNothing(False)
gp.SetBasePoint(Rhino.Geometry.Point3d.Unset, False)
gp.EnableDrawLineFromPoint(False)
else:
if point_count >= 2:
gp.SetCommandPrompt("Next point of polyline. Press Enter when done")
gp.AcceptNothing(True)
else:
gp.SetCommandPrompt("Next point of polyline")
gp.SetBasePoint(Rhino.Geometry.Point3d.Unset, False)
gp.DrawLineFromPoint(gp.PointFromIndex(-1), True)

gp.EnableNoRedrawOnExit(True)
res = gp.Get()

if res == Rhino.Input.GetResult.Nothing:
got_pline = True
break

if res != Rhino.Input.GetResult.Point:
break

if gp.Count() == 0:
gp.AppendPoint(gp.Point())
else:
p = gp.PointFromIndex(-1)
if p.DistanceTo(gp.Point()) >= Rhino.RhinoMath.SqrtEpsilon:
gp.AppendPoint(gp.Point())

if got_pline:
sc.doc.Objects.AddPolyline(gp.PointArray())

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__":
SampleGetPolyline()
66 changes: 66 additions & 0 deletions rhinopython/SampleTextBox.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
################################################################################
# SampleTextBox.py
# Copyright (c) 20197 Robert McNeel & Associates.
# See License.md in the root of this repository for details.
################################################################################
import Rhino
import rhinoscriptsyntax as rs
import scriptcontext as sc
import clr

# GetObject-derived class for limiting object picking to text
class GetTextObject(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.TextEntity):
return False
return True

# Draws a bounding rectangle around selected text
def SampleTextBox():
go = GetTextObject()
go.SetCommandPrompt("Select text")
go.Get()
if go.CommandResult() != Rhino.Commands.Result.Success:
return

obj = go.Object(0).Object()
text = extrusion_obj = clr.Convert(obj, Rhino.DocObjects.TextObject )
if not text:
return

plane = text.TextGeometry.Plane
xform = Rhino.Geometry.Transform.ChangeBasis(Rhino.Geometry.Plane.WorldXY, plane)
curves = text.TextGeometry.Explode()

tbox = Rhino.Geometry.BoundingBox.Empty
for crv in curves:
bbox = crv.GetBoundingBox(xform)
tbox.Union(bbox)

rmin = Rhino.Geometry.Interval(tbox.Min.X, tbox.Max.X)
rmax = Rhino.Geometry.Interval(tbox.Min.Y, tbox.Max.Y)
rect = Rhino.Geometry.Rectangle3d(plane, rmin, rmax)
pline = rect.ToPolyline()
curve = Rhino.Geometry.PolylineCurve(pline)

# Scale based on a percentage of the height of the rectangle.
# Change this value to adjust padding
scale = 0.1
height = rect.Corner(0).DistanceTo(rect.Corner(3)) * scale

tol = sc.doc.ModelAbsoluteTolerance
corner_style = Rhino.Geometry.CurveOffsetCornerStyle.Sharp
offsets = curve.Offset(plane, height, tol, corner_style)

sc.doc.Objects.AddCurve(offsets[0])
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__":
SampleTextBox()

0 comments on commit 7e8996f

Please sign in to comment.