Advertisement
Guest User

planefit PolygonArea

a guest
Dec 6th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.28 KB | None | 0 0
  1. import Rhino
  2. import clr
  3. clr.AddReferenceToFile("KangarooSolver.dll")
  4. import KangarooSolver as ks
  5. from System import Array, Double, MissingMemberException
  6.  
  7. class PolygonArea(ks.GoalObject):
  8.     def __init__(self, P, area, strength=1):
  9.         try: # convert polylinecurve
  10.             _,P = P.TryGetPolyline()
  11.         except MissingMemberException:
  12.             pass
  13.         self.PPos = Array[Rhino.Geometry.Point3d](P)
  14.         self.Move = Array.CreateInstance(Rhino.Geometry.Vector3d, len(self.PPos))
  15.         self.Weighting = Array[Double]([strength] * len(self.PPos))
  16.         self.Strength = strength
  17.         self.TargetArea = area
  18.  
  19.     def Calculate(self, p):
  20.         Array.Clear(self.Move, 0, self.Move.Length)
  21.         Array.Clear(self.Weighting, 0, self.Weighting.Length)
  22.  
  23.         xfpts = [Rhino.Geometry.Point3d(p[self.PIndex[i]].Position) for i in range(self.PIndex.Length)]
  24.         pfresult, plane = Rhino.Geometry.Plane.FitPlaneToPoints(xfpts)
  25.         if pfresult == Rhino.Geometry.PlaneFitResult.Failure:
  26.             raise RuntimeError("Couldn't fit plane to points")
  27.  
  28.         # keep stable plane orientation
  29.         if plane.Normal.CompareTo(Rhino.Geometry.Plane.WorldXY.Normal) == -1:
  30.             plane.Flip()
  31.         plane = Rhino.Geometry.Plane(Rhino.Geometry.Point3d.Origin, plane.Normal)
  32.  
  33.         xform = Rhino.Geometry.Transform.ChangeBasis(Rhino.Geometry.Plane.WorldXY, plane)
  34.         for pt in xfpts:
  35.             pt.Transform(xform)
  36.  
  37.         areadoubled = 0
  38.         totallength = 0
  39.         L = self.PIndex.Length
  40.         for i in range(L):
  41.             p1, p2 = xfpts[i], xfpts[(i+1)%L]
  42.             areadoubled += Rhino.Geometry.Vector3d.CrossProduct(
  43.                 Rhino.Geometry.Vector3d(p1),
  44.                 Rhino.Geometry.Vector3d(p2)).Z
  45.             totallength += p1.DistanceTo(p2)
  46.         areashortage = self.TargetArea - 0.5 * areadoubled
  47.         offset = areashortage / totallength
  48.  
  49.         for i in range(L):
  50.             nexti = (i+1) % L
  51.             edge = p[self.PIndex[nexti]].Position - p[self.PIndex[i]].Position
  52.             edge.Unitize()
  53.             pressure = offset * Rhino.Geometry.Vector3d.CrossProduct(edge, plane.ZAxis)
  54.             self.Move[i] += pressure
  55.             self.Move[nexti] += pressure
  56.             self.Weighting[i] = self.Strength
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement