Created
June 26, 2011 03:30
-
-
Save bengolder/1047195 to your computer and use it in GitHub Desktop.
bricks
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 System | |
import Rhino | |
from scriptcontext import doc | |
tol = 0.001 | |
class Brick(object): | |
centerOfMass = None | |
xSize = Rhino.Geometry.Interval(-1.0, 1.0) | |
ysize = Rhino.Geometry.Interval(-0.5, 0.5) | |
zSize = Rhino.Geometry.Interval(0.0, 0.5) | |
def __init__(self, plane): | |
self.plane = plane | |
self.makeShape() | |
def makeShape(self): | |
self.box = Rhino.Geometry.Box(self.plane, self.xSize, self.ysize, self.zSize) | |
def evalCenterOfMass(self, bricks): | |
firstBbox = bricks[0].box.BoundingBox | |
xs = [b.box.Center.X for b in bricks] | |
ys = [b.box.Center.Y for b in bricks] | |
self.centerOfMass = Rhino.Geometry.Point3d(sum(xs)/len(xs), sum(ys)/len(ys), 0.25 ) | |
self.ok = firstBbox.Contains(self.centerOfMass) | |
def setColor(self): | |
if self.ok: | |
self.color = System.Drawing.Color.Green | |
else: | |
self.color = System.Drawing.Color.Red | |
def evalPrevious(self, others): | |
pass | |
def findNext(self, curve): | |
newLevel = self.plane | |
newLevel.OriginZ += self.zSize.T1 | |
crvIntx = Rhino.Geometry.Intersect.Intersection.CurvePlane( curve, newLevel, tol) | |
if crvIntx: | |
return True, Rhino.Geometry.Plane(crvIntx[0].PointA, Rhino.Geometry.Plane.WorldXY.ZAxis) | |
else: | |
return False, None | |
def makeBricks(curve): | |
worldXY = Rhino.Geometry.Plane.WorldXY | |
crvIntx = Rhino.Geometry.Intersect.Intersection.CurvePlane( curve, worldXY, tol) | |
plane = Rhino.Geometry.Plane(crvIntx[0].PointA, worldXY.ZAxis) | |
bricks = [] | |
onCurve = True | |
while onCurve: | |
b = Brick(plane) | |
bricks.append(b) | |
b.evalCenterOfMass(bricks) | |
b.setColor() | |
onCurve, plane = b.findNext(curve) | |
# make the bricks | |
for brick in bricks: | |
att = Rhino.DocObjects.ObjectAttributes() | |
att.ObjectColor = brick.color | |
att.ColorSource = Rhino.DocObjects.ObjectColorSource.ColorFromObject | |
brep = Rhino.Geometry.Brep.CreateFromBox( brick.box ) | |
doc.Objects.AddBrep(brep, att) | |
if __name__=='__main__': | |
curve = doc.Objects.FindByLayer('curve')[0].Geometry | |
makeBricks( curve ) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment