Created
March 11, 2022 21:17
-
-
Save pgolay/3cc28d1c239b5c8e210673451c1d6d03 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Note this does not work with multi line text.