def MakeBlockUnique(block, newName): """ Explodes a block and makes a new one with 'newName' """ xform = rs.BlockInstanceXform(block) insertPt = rs.BlockInstanceInsertPoint(block) objs = rs.ExplodeBlockInstance(block, False) rs.TransformObjects(objs, rs.XformInverse(xform)) pt = rs.TransformObject(insertPt, rs.XformInverse(xform)) rs.AddBlock(objs, insertPt, newName, True) newBlock = rs.InsertBlock2(newName, xform) rs.DeleteObject(pt) return newBlock
def resetBlockScale(block): block_name = rs.BlockInstanceName(block) blockXform = rs.BlockInstanceXform(block) plane = rs.PlaneTransform(rs.WorldXYPlane(), blockXform) # newplane = rs.CreatePlane(plane.Origin, plane.XAxis, plane.YAxis) # cob = rs.XformChangeBasis(newplane, rs.WorldXYPlane()) cob = rs.XformChangeBasis(plane, rs.WorldXYPlane()) newBlock = rs.InsertBlock2(block_name, cob) copySourceLayer(newBlock, block) try: copySourceData(newBlock, block) except: pass rs.DeleteObjects(block) return newBlock
def addResultToBlock(obj, result): name = rs.BlockInstanceName(obj) i_point = rs.BlockInstanceInsertPoint(obj) xform = rs.BlockInstanceXform(obj) block_content = rs.ExplodeBlockInstance(obj) bc=[] for c in block_content: bc.append(c) bc.append(result) rs.AddBlock(bc, i_point, name, True) rs.InsertBlock2(name, xform)
def redefineBlockScale(block): block_name = rs.BlockInstanceName(block) # rs.RenameBlock (block_name, "{}-old".format(block_name)) blockXform = rs.BlockInstanceXform(block) plane = rs.PlaneTransform(rs.WorldXYPlane(), blockXform) cob = rs.XformChangeBasis(plane, rs.WorldXYPlane()) cob_inverse = rs.XformChangeBasis(rs.WorldXYPlane(), plane) refBlock = rs.TransformObjects(block, cob_inverse, True) exploded = rs.ExplodeBlockInstance(refBlock) rs.AddBlock(exploded, rs.WorldXYPlane().Origin, block_name, True) newBlock = rs.InsertBlock2(block_name, cob) copySourceLayer(newBlock, block) try: copySourceData(newBlock, block) except: pass rs.DeleteObjects(block)
def ReplicateBlock(blockObj): #Copy block copiedBlock = rs.CopyObject(blockObj) #Get new block name origName = rs.BlockInstanceName(blockObj) defaultName = origName for i in range(100): defaultName = utils.UpdateString(defaultName) if origName == defaultName: break if rs.IsBlock(defaultName) == False: break looping = True while looping: newBlockName = rs.StringBox("Enter new block name", default_value=defaultName, title='Iterate Design Option') if newBlockName is None: rs.DeleteObject(copiedBlock) return if rs.IsBlock(newBlockName): print "Block name already exists" elif len(newBlockName) == 0: print "Must specify a name" else: looping = False if newBlockName is None: rs.DeleteObject(copiedBlock) return #Get previous base point xform = rs.BlockInstanceXform(copiedBlock) basePoint = rs.BlockInstanceInsertPoint(copiedBlock) #Explode block objsInside = rs.ExplodeBlockInstance(copiedBlock) rs.AddBlock(objsInside, basePoint, newBlockName, True) #Create new block instance = rs.InsertBlock2(newBlockName, xform) return instance
import rhinoscriptsyntax as rs crvIds = rs.GetObjects("Pick curves", rs.filter.curve) ptIds = rs.GetObjects("Pick points", rs.filter.point) blockId = rs.GetObject("Pick Block", rs.filter.instance) if rs.IsBlockInstance(blockId): block_name = rs.BlockInstanceName(blockId) for crv in crvIds: for pt in ptIds: if rs.IsPointOnCurve(crv, pt): param = rs.CurveClosestPoint(crv, pt) tangent = rs.CurveTangent(crv, param) tangent = rs.VectorUnitize(tangent) normal = rs.WorldXYPlane().ZAxis # normal = rs.VectorUnitize(normal) plane = rs.PlaneFromNormal(pt, normal, tangent) # plane = rs.CurvePerpFrame(crv, param) xform = rs.XformChangeBasis(plane, rs.WorldXYPlane()) rs.InsertBlock2(block_name, xform)
def PopulatePath(): numObjects = 0 type = None try: crvs = rs.GetObjects("Select curves to populate", rs.filter.curve, True) if crvs is None: return None crvObjs = [] for crv in crvs: crvObjs.append(rs.coercecurve(crv)) #GET POPULATION TYPE type = GetPopulationType() if type is None: return None #GET BLOCK NAMES if type == 'Custom Block': blockNames, instances = GetCustomBlockNames() else: blockNames = GetBlockNames(type) if blockNames is None: return #GET NUMBER OF OBJECTS numObjects = GetNumObjects() if numObjects is None: return None rs.EnableRedraw(False) if type == '2D People' or type == '3D People': spacing = 42 elif type == '2D Trees': spacing = 100 elif type == '3D Trees': spacing = 200 elif type == '3D Vehicles': spacing = 240 else: spacing = GetCustomSpacing( blockNames[0]) #currently just selects the first block #GET PTS upVec = rc.Geometry.Vector3d(0, 0, 1) plane0 = rc.Geometry.Plane(rc.Geometry.Point3d(0, 0, 0), upVec) plane0.Rotate(math.pi / 2, upVec) crvData = [] for i, crvObj in enumerate(crvObjs): lengths = [] frames = [] curveLength = crvObj.GetLength() counter = 0 while len(frames) < numObjects: t = utils.Remap(random.uniform(0, 1), 0, 1, 0, curveLength) posOkay = True if len(lengths) > 0: counter += 1 for eachPrevPos in lengths: if abs(eachPrevPos - t) < spacing: posOkay = False if posOkay: lengths.append(t) pt = crvObj.PointAtLength(t) param = crvObj.LengthParameter(t)[1] tan = crvObj.TangentAt(param) xAxis = rc.Geometry.Vector3d.CrossProduct(tan, upVec) xAxis.Reverse() frames.append(rc.Geometry.Plane(pt, tan, xAxis)) if counter > int(curveLength / numObjects): print "Curve {}: Could only fit {} of the requested {} objects".format( i, len(frames), numObjects) break crvData.append(frames) scaleVariation = .1 #PLACE THE BLOCKS for i, crvObj in enumerate(crvObjs): for i, frame in enumerate(crvData[i]): blockName = blockNames[random.randint(0, len(blockNames) - 1)] if TryLoadBlock(type, blockName): xform = rc.Geometry.Transform.PlaneToPlane(plane0, frame) eachBlock = rs.InsertBlock2(blockName, xform) try: if type == '2D People' or type == '3D People': layerName = layers.GetLayerNameByNumber(2200) elif type == '2D Trees' or type == '3D Trees': layerName = layers.GetLayerNameByNumber(2300) elif type == '3D Vehicles': layerName = layers.GetLayerNameByNumber(2400) elif type == 'Custom Block': layerName = rs.ObjectLayer(instances[0]) else: layers.AddLayerByNumber(2000) layerName = layers.GetLayerNameByNumber(2000) rs.ObjectLayer(eachBlock, layerName) if type != '3D Vehicles': xyScale = random.uniform(1 - scaleVariation, 1 + scaleVariation) zScale = random.uniform(1 - scaleVariation, 1 + scaleVariation) else: xyScale = 1 zScale = 1 rs.ScaleObject(eachBlock, frame, (xyScale, xyScale, zScale)) except: pass rs.EnableRedraw(True) result = True except: result = False utils.SaveFunctionData('Blocks-Populate Path', [__version__, numObjects, type, result])
def add_rivet(rivet): # *********************************************** # ******** ADDING THE RIVET TO THE SCENE ********* # *********************************************** old_osnap_state = ModelAidSettings.OsnapModes #record Osnap state to reset later rivets = 0 rs.OsnapMode(32 + 134217728) while True: Rhino.UI.MouseCursor.SetToolTip("select surface or face") # this function ask the user to select a point on a surface to insert the bolt on # Surface to orient on gs = Rhino.Input.Custom.GetObject() gs.SetCommandPrompt("Surface to orient on") gs.GeometryFilter = Rhino.DocObjects.ObjectType.Surface gs.Get() if gs.CommandResult() != Rhino.Commands.Result.Success: ModelAidSettings.OsnapModes = old_osnap_state #reset to previous Osnap state print str(rivets) + " " + rivet.__repr__( ) + " rivet(s) added to the document" Rhino.UI.MouseCursor.SetToolTip("") return objref = gs.Object(0) # get selected surface object obj = objref.Object() if not obj: ModelAidSettings.OsnapModes = old_osnap_state #reset to previous Osnap state print str(rivets) + " " + rivet.__repr__( ) + " bolt(s) added to the document" return # get selected surface (face) global surface surface = objref.Surface() if not surface: return Rhino.Commands.Result.Failure # Unselect surface obj.Select(False) # Point on surface to orient to / activate center Osnap gp = Rhino.Input.Custom.GetPoint() gp.SetCommandPrompt("Point on surface to orient to") gp.Constrain(surface, False) #display the geometry to be created gp.DynamicDraw += drawbreps gp.Get() if gp.CommandResult() != Rhino.Commands.Result.Success: ModelAidSettings.OsnapModes = old_osnap_state #reset to previous Osnap state print str(rivets) + " " + rivet.__repr__( ) + " bolt(s) added to the document" return getrc, u, v = surface.ClosestPoint(gp.Point()) if getrc: getrc, target_plane = surface.FrameAt(u, v) if getrc: # Build transformation source_plane = Rhino.Geometry.Plane.WorldXY xform = Rhino.Geometry.Transform.PlaneToPlane( source_plane, target_plane) #check if layer Block_Definitions exist, else create it if not rs.IsLayer("Block_Definitions"): rs.AddLayer("Block_Definitions") #check if layer with block name exists, else create it if not rs.IsLayer("Block_Definitions::" + rivet.name): block_layer = rs.AddLayer("Block_Definitions::" + rivet.name, color=(120, 210, 210)) block_layer = "Block_Definitions::" + rivet.name layer_id = rs.LayerId(block_layer) layer_index = sc.doc.Layers.Find(layer_id, True) # Do the transformation rs.EnableRedraw(False) temp_layer = rs.CurrentLayer() rs.CurrentLayer(block_layer) objs = rivet.breps rhobj = [] for brep in objs: attribs = Rhino.DocObjects.ObjectAttributes() attribs.WireDensity = -1 attribs.LayerIndex = layer_index rhobj.append(sc.doc.Objects.AddBrep(brep, attribs)) rs.AddBlock(rhobj, [0, 0, 0], rivet.name, True) newrivet = rs.InsertBlock2(rivet.name, xform) rs.CurrentLayer(temp_layer) rs.EnableRedraw(True) rivets += 1