Skip to content

Instantly share code, notes, and snippets.

@pgolay
Created March 11, 2022 21:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pgolay/3cc28d1c239b5c8e210673451c1d6d03 to your computer and use it in GitHub Desktop.
Save pgolay/3cc28d1c239b5c8e210673451c1d6d03 to your computer and use it in GitHub Desktop.
import rhinoscriptsyntax as rs
import scriptcontext as sc
import Rhino
def FitTextToBoundary():
"""
Get the BBs and center the text in the curve
Get the text BB.
Line from BB center through each corner - extend by curve BB diagaonal
intersections of these corner lines to the curve
set text height to suit
Move to center again
replace object
"""
tol = sc.doc.ModelAbsoluteTolerance
while True:
idCrv = rs.GetObject('Select the bounding curve', filter = 4, preselect=True)
if not idCrv: return
crv = rs.coercecurve(idCrv)
rc, crvPlane = crv.TryGetPlane()
crvBB = crv.GetBoundingBox(True)
crvCen = crvBB.Center
distance = (crvBB.Diagonal.Length)/2
idText = rs.GetObject('Select the text', filter=rs.filter.annotation)
if not idText: return
textObj = rs.coercerhinoobject(idText)
geo = rs.coercegeometry(idText)
bb = geo.GetBoundingBox(True)
textCen = crvPlane.ClosestPoint(bb.Center)
geo.Translate(crvCen-textCen)
bb = geo.GetBoundingBox(True)
textCen = crvPlane.ClosestPoint(bb.Center)
pts = bb.GetCorners()
pts = [crvPlane.ClosestPoint(pt) for pt in pts]
ints = []
for n in range(0, 4):
vecDir = textCen-pts[n]
vecDir *= distance
lineCrv = Rhino.Geometry.LineCurve(textCen, pts[n]+vecDir )
info = Rhino.Geometry.Intersect.Intersection.CurveCurve(crv, lineCrv, tol, tol)
if len(info) ==0: continue
pt = info[0].PointA
ints.append(pt)
vec = ints[3]-ints[0]
h = vec.Length
h *= .9
geo.TextHeight = h
textCen = crvPlane.ClosestPoint(geo.GetBoundingBox(True).Center)
geo.Translate(crvCen-textCen)
sc.doc.Objects.Replace(idText, geo)
sc.doc.Views.Redraw()
if __name__ == '__main__':FitTextToBoundary()
@pgolay
Copy link
Author

pgolay commented Mar 11, 2022

Note this does not work with multi line text.

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