def AxialScale(self): Allpts = self.GetPoints() curves = [] for pts in Allpts: pts.append(pts[0]) minDist = self.GetDim('W', pts) new = [] for i in range(len(pts) - 1): dist = rh.Point3d.DistanceTo(pts[i], pts[i + 1]) if round(minDist, 3) == round(dist, 3): mid = rh.Point3d((pts[i].X + pts[i + 1].X) / 2, (pts[i].Y + pts[i + 1].Y) / 2, 0) vec1 = rh.Vector3d(pts[i + 1].X - mid.X, pts[i + 1].Y - mid.Y, 0) n2 = rh.Point3d(pts[i + 1].X + vec1.X, pts[i + 1].Y + vec1.Y, 0) n1 = rh.Point3d(pts[i].X - vec1.X, pts[i].Y - vec1.Y, 0) #lst.append(rh.Point3d(pts[i].X + mid.X, pts[i].Y + mid.Y, 0)) #lst.append(rh.Point3d(pts[i+1].X + mid.X, pts[i+1].Y + mid.Y, 0)) new.append(n1) new.append(n2) else: pass new.append(new[0]) curves.append(GetPolyline(new)) return curves
def RoomBoundCL(self, rhinoCrvs): """ this must be run from Python 3.7xx The mathmatical basis for this can be found here: https://www.designingbuildings.co.uk/wiki/Measurement cl is centerline """ # centerline = internalGirth + numCorners * ((2*wallWidth)/2) # internalGirth = pt = rh.Point3d(0, 0, 0) print(pt) print(pt.X) print(type(pt)) pts = [rh.Point3d(i, j, 0) for i in range(3) for j in range(3)] print(pts) crv = rh.NurbsCurve.Create(False, 1, pts) print(crv) ## what elements would only have a single boundary segment #for nestedCrvList in rhinoCrvs: #for crvList in nestedCrvList: ## this should work like this: rh.Geometry.Curve.JoinCurves(crvList) #joinedCrv = rc.Curve.JoinCurves(crvList) #print(joinedCrv) #for i in crvList: #print(i) clLength = 98945.59 DE.JSONTools().WriteJSON( clLength, "{0}\JSON_talk\\".format(os.path.dirname(__file__)), self.jsonFileName)
def CreateOutlet(pts, width, height): rects = [] for p in pts: pt = [] pt.append(rh.Point3d(p.X - width / 2, p.Y - height / 2, 0)) pt.append(rh.Point3d(p.X + width / 2, p.Y - height / 2, 0)) pt.append(rh.Point3d(p.X + width / 2, p.Y + height / 2, 0)) pt.append(rh.Point3d(p.X - width / 2, p.Y + height / 2, 0)) pt.append(pt[0]) rects.append(GetPolyline(pt)) return rects
def avgPt(ptList): ptX = list(map(lambda p: p.X, ptList)) ptY = list(map(lambda p: p.Y, ptList)) ptZ = list(map(lambda p: p.Z, ptList)) return rg.Point3d(sum(ptX)/len(ptList), sum(ptY)/len(ptList), sum(ptZ)/len(ptList))
def getFaceCenter(mesh, meshFace): faceVertex = [] for i in range(len(meshFace)): p3f = mesh.Vertices[meshFace[i]] faceVertex.append(rg.Point3d(p3f.X, p3f.Y, p3f.Z)) return avgPt(faceVertex)
def SolveInterference(outlets, pucks, wall): puckCen = [] for p in pucks: puckCen.append(GetCurveCenter(p)) cloud = rh.PointCloud(puckCen) rects = [] for o in outlets: pts = [] index = int(rh.PointCloud.ClosestPoint(cloud, GetCurveCenter(o))) if compute_rhino3d.Curve.PlanarCurveCollision(o, pucks[index], rh.Plane.WorldXY(), 0.0001): #start2 = time.time() inter = CI.CurveCurve(o, pucks[index], 0.0001, 0.0) #end2 = time.time() #print("Intersection" + str(end2 - start2)) for i in inter: #print(i) pts.append(i['PointA']) ptA = rh.Point3d(pts[0]['X'], pts[1]['Y'], 0) ptB = rh.Point3d(pts[1]['X'], pts[0]['Y'], 0) param = compute_rhino3d.Curve.ClosestPoint(wall, GetCurveCenter(o))[1] direction = rh.Curve.TangentAt(wall, param) #print(GetShapelyPolygon(pucks[index].ToPolyline())) if GetShapelyPolygon(pucks[index].ToPolyline()).contains( sh.Point(ptA.X, ptA.Y)) == True: pt = ptA else: pt = ptB dir1 = rh.Vector3d(pt.X - pts[0]['X'], pt.Y - pts[0]['Y'], 0) dir2 = rh.Vector3d(pt.X - pts[1]['X'], pt.Y - pts[1]['Y'], 0) oc = o if AreParallel(dir1, direction): oc.Transform(rh.Transform.Translation(dir1.X, dir1.Y, 0)) else: oc.Transform(rh.Transform.Translation(-dir2.X, -dir2.Y, 0)) moved = oc rects.append(moved) else: rects.append(o) return rects
def GetPoints(self): newLst = [] for d in self.data[self.string]: lst = [] for k in d: lst.append(rh.Point3d(k[0], k[1], k[2])) newLst.append(lst) return newLst
def getFaceCenterTri(mesh, meshFace): #i think only for triangles faceVertex = [] for i in range(len(meshFace)): p3f = mesh.Vertices[meshFace[i]] faceVertex.append(rg.Point3d(p3f.X, p3f.Y, p3f.Z)) mid1 = rg.Point3d((faceVertex[0].X + faceVertex[1].X) / 2, (faceVertex[0].Y + faceVertex[1].Y) / 2, (faceVertex[0].Z + faceVertex[1].Z) / 2) mid2 = rg.Point3d((faceVertex[1].X + faceVertex[2].X) / 2, (faceVertex[1].Y + faceVertex[2].Y) / 2, (faceVertex[1].Z + faceVertex[2].Z) / 2) line1 = rg.Line(mid1, faceVertex[2]) line2 = rg.Line(mid2, faceVertex[0]) center_parameter = rg.Intersection.LineLine(line1, line2)[1] center = rg.Line.PointAt(line1, center_parameter) return center
def GetCurveCenter(curve): x = [] y = [] z = [] for c in curve.ToPolyline(): x.append(c.X) y.append(c.Y) z.append(c.Z) x_avg = sum(x[1:]) / len(x[1:]) y_avg = sum(y[1:]) / len(y[1:]) z_avg = sum(z[1:]) / len(z[1:]) return rh.Point3d(x_avg, y_avg, z_avg)
def makeSampleMesh(U, V): #creating a simple mesh from a grid of points mesh = rg.Mesh() for i in range(U): for j in range(V): p = rg.Point3d(i, j, 0) mesh.Vertices.Add(p.X, p.Y, p.Z) for i in range(len(mesh.Vertices) - (V)): if (i % V != V - 1): mesh.Faces.AddFace(i, i + 1, i + V + 1, i + V) return mesh
def _coerce_input(item): data = json.loads(item['data']) itemtype = item['type'] coercers = { 'System.Double': lambda x: float(x), 'Rhino.Geometry.Point2d': lambda x: rhino3dm.Point2d(x['X'], x['Y']), 'Rhino.Geometry.Point3d': lambda x: rhino3dm.Point3d(x['X'], x['Y'], x['Z']), 'Rhino.Geometry.Vector3d': lambda x: rhino3dm.Vector3d(x['X'], x['Y'], x['Z']) } if itemtype in coercers: rc = coercers[itemtype](data) return rc if itemtype.startswith('Rhino.Geometry.'): rc = rhino3dm.CommonObject.Decode(data) return rc return data
def GetPucks(data, boundary): pucklines = [] pucks = data['pucks'] dist = [] for puck in pucks: pts = [] for p in puck: #pt = rh.Point3d(p[0], p[1], 0) pt = sh.Point(p[0], p[1]) if boundary.contains(pt) == False: pts.append(rh.Point3d(pt.x, pt.y, 0)) else: pass for i in range(len(pts) - 1): d = rh.Point3d.DistanceTo(pts[i], pts[i + 1]) dist.append(d) #pucklines.append(GetPolyline(pts)) curve = GetPolyline(pts) if curve.IsClosed: pucklines.append(curve) else: pass return pucklines, max(dist)
import rhino3dm import random import compute_rhino3d import compute_rhino3d.Util import compute_rhino3d.Curve import compute_rhino3d.Mesh compute_rhino3d.Util.authToken = "" #compute_rhino3d.Util.url = "http://127.0.0.1:8081/" model = rhino3dm.File3dm() curves = [] for i in range(20): pt = rhino3dm.Point3d(random.uniform(-10, 10), random.uniform(-10, 10), 0) #model.Objects.AddPoint(pt) circle = rhino3dm.Circle(pt, random.uniform(1, 4)) #model.Objects.AddCircle(circle) curve = circle.ToNurbsCurve() curves.append(curve) bcurves = compute_rhino3d.Curve.CreateBooleanUnion(curves) for bcurve in bcurves: bcurveObj = rhino3dm.CommonObject.Decode(bcurve) extrusion = rhino3dm.Extrusion.Create(bcurveObj, 5, True) #model.Objects.AddExtrusion(extrusion) brep = extrusion.ToBrep(True) meshes = compute_rhino3d.Mesh.CreateFromBrep(brep) for i in range(len(meshes)):
import json import random authToken = "" with open('./projectinfo.json') as f: df = json.load(f) authToken = df["auth_token"] compute_rhino3d.Util.authToken = authToken #compute_rhino3d.Util.url = "http://192.168.20.2:8081/" for n in range(1): model = rhino3dm.File3dm() curves = [] for i in range(20): s = 10 pt = rhino3dm.Point3d(random.uniform(-s, s), random.uniform(-s, s), 0) r = random.uniform(1, 3) circle = rhino3dm.Circle(pt, r) curve = circle.ToNurbsCurve() curves.append(curve) unionCrvsC = compute_rhino3d.Curve.CreateBooleanUnion(curves) unionCrvs = [] height = random.uniform(1, 5) for unionCrvC in unionCrvsC: unionCrv = rhino3dm.CommonObject.Decode(unionCrvC) extrusion = rhino3dm.Extrusion.Create(unionCrv, height, True) model.Objects.AddExtrusion(extrusion) model.Write('outputs/bubble{0}.3dm'.format(str(n)), 5)
def DecodeToPoint3d(item): if item is None: return None if isinstance(item, list): return [DecodeToPoint3d(x) for x in item] return rhino3dm.Point3d(item['X'], item['Y'], item['Z'])
""" Design NXT 2019 Workshop Sample 6: Offset and trim """ import rhino3dm import compute_rhino3d.Util import compute_rhino3d.Intersection compute_rhino3d.Util.authToken = 'ADD_TOKEN_HERE' points = [] points.append(rhino3dm.Point3d(0.417, 0.262, -10)) points.append(rhino3dm.Point3d(11.718, 1.645, 0)) points.append(rhino3dm.Point3d(0.656, 4.029, 0)) points.append(rhino3dm.Point3d(11.814, 8.178, 10)) curve1 = rhino3dm.Curve.CreateControlPointCurve(points, 3) sphere = rhino3dm.Sphere(rhino3dm.Point3d(5, 5, 0), 5) brep = sphere.ToBrep() # create a 3dm file with results model = rhino3dm.File3dm() model.Objects.AddCurve(curve1) model.Objects.AddSphere(sphere) intersection_results = compute_rhino3d.Intersection.CurveBrep( curve1, brep, 0.01) intersection_points = intersection_results[2] for intersection in intersection_points: x = intersection["X"]
# Simple example import rhino3dm center = rhino3dm.Point3d(1, 2, 3) arc = rhino3dm.Arc(center, 10, 1) nc = arc.ToNurbsCurve() start = nc.PointAtStart print(start)
import compute_rhino3d.Util import compute_rhino3d.Grasshopper as gh import rhino3dm import json compute_rhino3d.Util.authToken = ADD_TOKEN_HERE pt1 = rhino3dm.Point3d(0, 0, 0) circle = rhino3dm.Circle(pt1, 5) angle = 20 # convert circle to curve and stringify curve = json.dumps(circle.ToNurbsCurve().Encode()) # create list of input trees curve_tree = gh.DataTree("RH_IN:curve") curve_tree.Append([0], [curve]) rotate_tree = gh.DataTree("RH_IN:rotate") rotate_tree.Append([0], [angle]) trees = [curve_tree, rotate_tree] output = gh.EvaluateDefinition('workshop_step5.ghx', trees) # print(output) # decode results branch = output['values'][0]['InnerTree']['{ 0; }'] lines = [ rhino3dm.CommonObject.Decode(json.loads(item['data'])) for item in branch ] # create a 3dm file with results
""" Design NXT 2019 Workshop Sample 5: Output to svg instead of 3dm """ import rhino3dm import compute_rhino3d.Util import compute_rhino3d.Intersection import svgwrite # pip install svgwrite compute_rhino3d.Util.authToken = 'ADD_TOKEN_HERE' points = [] points.append(rhino3dm.Point3d(0.417, 0.262, 0)) points.append(rhino3dm.Point3d(11.718, 1.645, 0)) points.append(rhino3dm.Point3d(0.656, 4.029, 0)) points.append(rhino3dm.Point3d(11.814, 8.178, 0)) curve1 = rhino3dm.Curve.CreateControlPointCurve(points, 3) circle = rhino3dm.Circle(rhino3dm.Point3d(5, 5, 0), 5) curve2 = circle.ToNurbsCurve() intersection_results = compute_rhino3d.Intersection.CurveCurve( curve1, curve2, 0.01, 0.01) intersection_points = [] for intersection in intersection_results: x = intersection["PointA"]["X"] y = intersection["PointA"]["Y"] z = intersection["PointA"]["Z"] intersection_points.append(rhino3dm.Point3d(x, y, z))
import rhino3dm import random model = rhino3dm.File3dm() for i in xrange(10): pt = rhino3dm.Point3d(random.randint(0, 10), random.randint(0, 10), 0) circle = rhino3dm.Circle(pt, float(random.randint(0, 5))) model.Objects.AddCircle(circle) model.Write("add_random_circles.3dm", 5)
import rhino3dm import random import math import os # Initial parameters theta_min = 0.0 theta_max = math.pi alpha_min = 0.0 alpha_max = math.pi * 2.0 sphere_radius = 100.0 num_lines = 1500 # Create a center point center_pt = rhino3dm.Point3d(0.0, 0.0, 0.0) # Create a File3dm object model = rhino3dm.File3dm() for i in range(num_lines): # Calculate random line end point random.seed(i * 100) theta = random.uniform(theta_min, theta_max) alpha = random.uniform(alpha_min, alpha_max) x = sphere_radius * math.sin(theta) * math.cos(alpha) y = sphere_radius * math.sin(theta) * math.sin(alpha) z = sphere_radius * math.cos(theta) end_pt = rhino3dm.Point3d(x, y, z) # Create line curve line_curve = rhino3dm.LineCurve(center_pt, end_pt)