Skip to content

Instantly share code, notes, and snippets.

@pgolay

pgolay/SeamMarker.py

Last active Jul 21, 2018
Embed
What would you like to do?
import Rhino
import rhinoscriptsyntax as rs
import scriptcontext as sc
import System.Guid
import math
"""
Get the edge
if there are not two adjacent faces, ask for any other faces to mark.
Flip the curve is needed
Present the Divide options:
CurrentNumber int
Mode (Length/Number) bool
DivLength dbl
DivNum int
Marker size dbl
Add numbers bool
"""
def SeamMarkerDivide():
pi = math.pi
num =0
lFonts = [item.lower() for item in sorted(list(set(Rhino.DocObjects.Font.AvailableFontFaceNames())))]
fonts = [item.title() for item in lFonts] #to use in a list box if needed
for item in fonts:
if Rhino.DocObjects.Font(item).IsSingleStrokeFont:
fonts.insert(0, fonts.pop(fonts.index(item)))
if sc.sticky.has_key("SEW_NUMBER"):
num = sc.sticky["SEW_NUMBER"]
rc, objref = Rhino.Input.RhinoGet.GetOneObject("Select an edge.", False, Rhino.DocObjects.ObjectType.EdgeFilter)
if not objref or rc!=Rhino.Commands.Result.Success: return
obj = objref.Object()
if isinstance(obj.Geometry, Rhino.Geometry.Extrusion):
brep = obj.Geometry.ToBrep()
else:
brep = obj.Geometry
idx = objref.GeometryComponentIndex.Index
crv = brep.Trims[idx].Edge.ToNurbsCurve()
eIdx = brep.Trims[idx].Edge.EdgeIndex
edge = brep.Edges[eIdx]
faces = [brep.Faces[edge.AdjacentFaces()[n]] for n in range(edge.AdjacentFaces().Count)] #brep face indexes
if faces.Count<2:
pass
#Ask for another face to mark
sc.doc.Objects.UnselectAll()
go = Rhino.Input.Custom.GetObject()
go.GeometryFilter = Rhino.DocObjects.ObjectType.Surface
go.SetCommandPrompt("Select a second surface to mark.")
results = go.Get()
if go.CommandResult()!=Rhino.Commands.Result.Success:
return go.CommandResult()
if results == Rhino.Input.GetResult.Object:
objref2 = go.Object(0)
idx2 = objref2.GeometryComponentIndex.Index
if idx2 == -1:
idx2 = 0
faces.append(objref2.Brep().Faces[idx2])
sc.doc.Objects.UnselectAll()
while True:
while True:
addNum = False
if sc.sticky.has_key("ADD_SEW_NUMBERS"):
addNum = sc.sticky["ADD_SEW_NUMBERS"]
dblSize = 1
if sc.sticky.has_key("SEAM_MARKER_SIZE"):
dblSize = sc.sticky["SEAM_MARKER_SIZE"]
fontName= "Arial"
if sc.sticky.has_key("SEW_FONTNAME"):
fontName = sc.sticky ["SEW_FONTNAME"].title()
gp = Rhino.Input.Custom.GetPoint()
gp.AcceptNumber(True, True)
opSize = Rhino.Input.Custom.OptionDouble(dblSize)
opNum = Rhino.Input.Custom.OptionInteger(num)
opAddNum = Rhino.Input.Custom.OptionToggle(addNum, "No", "Yes")
gp.AddOptionInteger("CurrentNumber", opNum)
gp.AddOptionDouble("MarkerSize", opSize)
gp.AddOptionToggle("Numbers", opAddNum)
opFont = gp.AddOption("Font", fontName)
gp.Constrain(crv, False)
result = gp.Get()
if( gp.CommandResult() != Rhino.Commands.Result.Success ):
return
if result == Rhino.Input.GetResult.Number:
dblSize = gp.Number()
sc.sticky["SEAM_MARKER_SIZE"] = dblSize
continue
if result == Rhino.Input.GetResult.Point:
pt = gp.Point()
break
if result == Rhino.Input.GetResult.Option:
opIdx = gp.OptionIndex()
if opIdx == opFont:
#fontRC, tempFont = Rhino.Input.RhinoGet.GetString("Font name", True, fontName)
gf = Rhino.Input.Custom.GetString()
gf.SetCommandPrompt("Set new marker font.")
gf.AddOption("List")
fontRC = gf.Get()
if fontRC == Rhino.Input.GetResult.Option:
tempFont = rs.ListBox(fonts, "Set marker font", fonts[fonts.index(fontName)])
else:
tempFont == gf.String()
if tempFont is not None:
if tempFont.lower() in lFonts:
fontName = tempFont
sc.sticky["SEW_FONTNAME"]= fontName.title()
dblSize = opSize.CurrentValue
num = opNum.CurrentValue
addNum = opAddNum.CurrentValue
sc.sticky["ADD_SEW_NUMBERS"] = addNum
sc.sticky["SEW_NUMBER"] = num
sc.sticky["SEAM_MARKER_SIZE"] = dblSize
continue
plane = None
intCrvs =[]
if pt: rc, plane = crv.PerpendicularFrameAt(crv.ClosestPoint(pt)[1])
if rc:
circle = Rhino.Geometry.Circle(plane, dblSize)
brepCircle = Rhino.Geometry.Brep.CreatePlanarBreps([circle.ToNurbsCurve()]) [0]
srfCircle = brepCircle.Faces[0].DuplicateSurface()
count = 0
for face in faces:
ret, crvs, pts = Rhino.Geometry.Intersect.Intersection.BrepBrep(face.ToBrep(), brepCircle, .001)
count += 1
if addNum:
if crvs:
srf = face.DuplicateSurface()
intCrvs.append(crvs[0])
if pt.DistanceTo(crvs[0].PointAtStart) < pt.DistanceTo(crvs[0].PointAtEnd):
tPt = crvs[0].PointAtEnd
vecY = crvs[0].TangentAtEnd
else:
tPt = crvs[0].PointAtStart
vecY = crvs[0].TangentAtStart
vecY.Reverse()
srfRc,parU, parV = srf.ClosestPoint(tPt)
planeRc, tPlane = srf.FrameAt(parU, parV)
planeAxis = Rhino.Geometry.Vector3d(tPlane.ZAxis)
planeAxis.Reverse()
vecX= Rhino.Geometry.Vector3d(vecY)
vecX.Rotate( pi/2,planeAxis)
tPlane = Rhino.Geometry.Plane(tPlane.Origin,vecX,vecY)
tPlane.Origin = tPt
blnClose = True
if Rhino.DocObjects.Font(fontName).IsSingleStrokeFont: blnClose = False
text = Rhino.Geometry.Curve.CreateTextOutlines(str(num),fontName, dblSize, 0, blnClose, tPlane,.8*dblSize, tolerance=.001)
if text:
textBB = Rhino.Geometry.BoundingBox()
for txtCrv in text:
textBB.Union(txtCrv.GetBoundingBox(tPlane))
pass
corners = textBB.GetCorners()
pToP = Rhino.Geometry.Transform.PlaneToPlane(Rhino.Geometry.Plane.WorldXY,tPlane)
for u in range(8):
corners[u] = pToP*corners[u]
tempPt = (corners[1]+corners[0])/2
xXform = tPt- tempPt
#xXform.Reverse()
yXform = tPlane.YAxis*.1*dblSize
tempIds = []
for txtCrv in text:
txtCrv.Translate(xXform)
txtCrv.Translate(yXform)
tempIds.append(sc.doc.Objects.AddCurve(txtCrv))
rs.AddObjectsToGroup(tempIds, rs.AddGroup())
pass
if ret:
for item in crvs:
id = System.Guid.NewGuid()
crvId = sc.doc.Objects.AddCurve(item)
rs.SetUserText(crvId, "SeamMarkerId", str(id))
id = None
if addNum: num += 1
sc.sticky["SEW_NUMBER"] = num
sc.doc.Views.Redraw()
if __name__ == "__main__": SeamMarkerDivide()
@pgolay

This comment has been minimized.

Copy link
Owner Author

@pgolay pgolay commented Jul 20, 2018

Added the ability to set the font for numbering and accommodate single stroke fonts.

@pgolay

This comment has been minimized.

Copy link
Owner Author

@pgolay pgolay commented Jul 21, 2018

Filter single-stroke fonts to the top of the list.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment