Skip to content

Instantly share code, notes, and snippets.

@pgolay
Last active April 14, 2021 01:45
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/f1db284bfa9d59997321e1190dcdcaa9 to your computer and use it in GitHub Desktop.
Save pgolay/f1db284bfa9d59997321e1190dcdcaa9 to your computer and use it in GitHub Desktop.
Change the UV or isocurve orientation on a planar brep face.
import Rhino
import scriptcontext as sc
import rhinoscriptsyntax as rs
import System
import math
def PlaneAlignedBB(geo, plane):
wxy = Rhino.Geometry.Plane.WorldXY
xform = Rhino.Geometry.Transform.ChangeBasis( wxy, plane)
bb = geo.GetBoundingBox(plane)
box = Rhino.Geometry.Box(bb)
xform = Rhino.Geometry.Transform.ChangeBasis( plane, wxy)
box.Transform(xform)
return box
def PlanarFaceDirection():
pi = math.pi
X_color = Rhino.ApplicationSettings.AppearanceSettings.GridXAxisLineColor
Y_color = Rhino.ApplicationSettings.AppearanceSettings.GridYAxisLineColor
#shape_color = System.Drawing.Color.FromArgb(150,0,50)
f_color = Rhino.ApplicationSettings.AppearanceSettings.FeedbackColor
def GetPointDynamicDrawFunc( sender, args ):
pt = args.CurrentPoint
args.Display.DrawPoint(plane.Origin)
args.Display.DrawCircle(Rhino.Geometry.Circle(plane, dX/2), f_color, 1)
vecX = pt-plane.Origin
vecY = pt-plane.Origin
vecY.Unitize()
vecY *= dY/2
vecY.Rotate(pi/2, vecZ)
args.Display.DrawLine(plane.Origin, plane.Origin + (plane.XAxis*dX/2), X_color, 2)
args.Display.DrawLine(plane.Origin, plane.Origin + (plane.YAxis*dY/2), Y_color, 4)
args.Display.DrawLine( plane.Origin, plane.Origin + vecX, System.Drawing.Color.Red,4)
args.Display.DrawLine(plane.Origin, plane.Origin + vecY, System.Drawing.Color.Green, 4)
def filter_planar( rhino_object, geometry, component_index):
if geometry.IsPlanar():
return True
return False
objRef = rs.GetObject('Select a planar surface.', filter = 8, preselect=True,subobjects=True, custom_filter = filter_planar)
if not objRef: return
brep = objRef.Brep()
single=False
if objRef.GeometryComponentIndex.Index == -1:
single = True
face = brep.Faces[0]
else:
face = brep.Faces[objRef.GeometryComponentIndex.Index]
indices = [brep.Faces[n].FaceIndex for n in range(brep.Faces.Count) if brep.Faces[n].FaceIndex != objRef.GeometryComponentIndex.Index ]
id = objRef.ObjectId
outer = face.OuterLoop.To3dCurve()
inner = None
if face.Loops.Count > 1:
inner = [face.Loops[n].To3dCurve() for n in range(1,face.Loops.Count)]
srf = face.UnderlyingSurface()
rc, plane = srf.TryGetPlane()
if not rc:
print('Could not find a plane.')
return
box = PlaneAlignedBB(srf, plane)
dX = box.X.Length
dY = box.Y.Length
vecZ = plane.ZAxis
plane.Origin = box.Center
while True:
useCPlane = False
if sc.sticky.has_key('USE_CPLANE_ORTHO'):
useCPlane = sc.sticky['USE_CPLANE_ORTHO']
gp = Rhino.Input.Custom.GetPoint()
opCPlane = Rhino.Input.Custom.OptionToggle(useCPlane, 'No', 'Yes')
gp.AddOptionToggle('CPlaneOrtho', opCPlane)
if useCPlane:
circle_plane = Rhino.Geometry.Plane(rs.ViewCPlane())
circle_plane.Origin = plane.Origin
else:
circle_plane = plane
gp.Constrain(Rhino.Geometry.Circle(circle_plane, dX/2))
gp.DynamicDraw += GetPointDynamicDrawFunc
rc = gp.Get()
if ( gp.CommandResult() != Rhino.Commands.Result.Success ):
return
elif rc == Rhino.Input.GetResult.Option:
useCPlane = opCPlane.CurrentValue
sc.sticky['USE_CPLANE_ORTHO'] = useCPlane
continue
elif rc ==Rhino.Input.GetResult.Point:
pt = gp.Point()
break
vecX = pt-plane.Origin
xform = Rhino.Geometry.Transform.Rotation(plane.XAxis, vecX, plane.Origin)
box.Transform(xform)
new_brep = Rhino.Geometry.Brep.CreateTrimmedPlane(box.Plane, outer)
if inner is not None:
for item in inner:
new_brep.Loops.AddPlanarFaceLoop(0, Rhino.Geometry.BrepLoopType.Inner, [item])
if not single:
copyBrep = brep.DuplicateSubBrep(indices)
new_brep.Append(copyBrep)
new_brep.Compact()
if new_brep.IsValid:
sc.doc.Objects.Replace(id, new_brep)
sc.doc.Views.Redraw()
pass
if __name__ == '__main__':PlanarFaceDirection()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment