Exemple #1
0
def blkObjs(blkid):
    blockName = rs.BlockInstanceName(blkid)
    objref = rs.coercerhinoobject(blkid)
    idef = objref.InstanceDefinition
    idefIndex = idef.Index

    lvl = levels[rs.GetUserText(blkid, 'level')]
    height = float(lvl['height'])
    xform = rs.BlockInstanceXform(blkid)
    objects = rs.BlockObjects(blockName)
    # masses = map(lambda x: massFromSrf(x, height), objects)
    # newblk = rs.AddBlock(masses, (0,0,0), name=name, delete_input=True)

    # objects.extend(masses)

    newGeometry = []
    newAttributes = []
    for object in objects:
        newGeometry.append(rs.coercegeometry(object))
        ref = Rhino.DocObjects.ObjRef(object)
        attr = ref.Object().Attributes
        attr.SetUserString('blkname', blockName)
        newAttributes.append(attr)

    InstanceDefinitionTable = sc.doc.ActiveDoc.InstanceDefinitions
    InstanceDefinitionTable.ModifyGeometry(idefIndex, newGeometry,
                                           newAttributes)
    # rs.TransformObjects(masses, xform)
    rs.DeleteObjects(masses)
def RemoveFromBlock():
    block = rs.GetObject("Select Block to extract objects from",
                         rs.filter.instance,
                         preselect=True)
    if not block: return

    blockName = rs.BlockInstanceName(block)
    objref = rs.coercerhinoobject(block)
    idef = objref.InstanceDefinition
    idefIndex = idef.Index

    XformBlock = rs.BlockInstanceXform(block)
    blockObjects = rs.BlockObjects(blockName)
    blockInstanceObjects = rs.TransformObjects(blockObjects, XformBlock, True)

    objs = rs.GetObjects("Select Objects to extract from Block",
                         objects=blockInstanceObjects)
    if not objs:
        rs.DeleteObjects(blockInstanceObjects)
        return

    keep = []  #List to keep in block
    delete = []  #list to delete from block and add to doc

    rs.EnableRedraw(False)

    for object in blockInstanceObjects:
        if object in objs: delete.append(object)
        else: keep.append(object)

    if rs.IsBlockReference(blockName):
        print "Block is referenced from file; unable to modify block"
        rs.DeleteObjects(keep)
        return

    rs.TransformObjects(keep, rs.XformInverse(XformBlock), False)

    newGeometry = []
    newAttributes = []
    for object in keep:
        newGeometry.append(rs.coercegeometry(object))
        ref = Rhino.DocObjects.ObjRef(object)
        attr = ref.Object().Attributes
        newAttributes.append(attr)

    InstanceDefinitionTable = sc.doc.ActiveDoc.InstanceDefinitions
    InstanceDefinitionTable.ModifyGeometry(idefIndex, newGeometry,
                                           newAttributes)

    rs.DeleteObjects(keep)
    rs.EnableRedraw(True)
def IntersectGeo(obj, level):
    tolerance = rs.UnitAbsoluteTolerance()
    plane = rc.Geometry.Plane(rs.coerce3dpoint((0, 0, level)),
                              rs.coerce3dvector((0, 0, 1)))
    finalCurves = []
    #BLOCKS
    if rs.IsBlockInstance(obj):
        matrix = rs.BlockInstanceXform(obj)
        blockObjs = rs.BlockObjects(rs.BlockInstanceName(obj))
        for eachBlockObj in blockObjs:
            newCopy = rs.CopyObject(eachBlockObj)
            xformedObj = rs.TransformObject(newCopy, matrix)

            #EXTRUSIONS
            if isinstance(xformedObj, rc.Geometry.Extrusion):
                temp = sc.doc.Objects.AddBrep(xformedObj.ToBrep(False))
                xformedObj = rs.coercebrep(temp)
                rs.DeleteObject(temp)

            #BREPS IN BLOCK
            result = IntersectBrepPlane(xformedObj, plane)
            if result is None: continue
            for each in result:
                if each is not None:
                    finalCurves.append(each)
            rs.DeleteObject(xformedObj)

            #MESHES IN BLOCK <---This code might not be necessary
            result = IntersectMeshPlane(xformedObj, plane)
            if result is None: continue
            for each in result:
                if each is not None:
                    finalCurves.append(each)
            rs.DeleteObject(xformedObj)

    #BREPS
    elif rs.IsBrep(obj):
        result = IntersectBrepPlane(obj, plane)
        if result is None: return None
        for each in result:
            if each is not None:
                finalCurves.append(each)

    #MESHES
    elif rs.IsMesh(obj):
        result = IntersectMeshPlane(obj, plane)
        if result is None: return None
        for each in result:
            if each is not None:
                finalCurves.append(each)
    return finalCurves
Exemple #4
0
def BlockTxt(block, str = False, cstr = False, boolFlipHeb=True):
    if not block: return
    
    blockName = rs.BlockInstanceName(block)
    objref = rs.coercerhinoobject(block)
    idef = objref.InstanceDefinition
    idefIndex = idef.Index
    
    XformBlock = rs.BlockInstanceXform(block)
    blockObjects = rs.BlockObjects(blockName)
    
    txtobjs = False
    
    for obj in blockObjects:
        if rs.IsText(obj):
            if not str: txtobjs = True
            elif rs.TextObjectText(obj) == str: txtobjs = True
    
    if txtobjs:
        
        blockInstanceObjects = rs.TransformObjects(blockObjects, XformBlock, True)
        
        keep = []
        
        rs.EnableRedraw(False)
        
        for obj in blockInstanceObjects:
            if rs.IsText(obj):
                if not str and not cstr:
                    str = rs.TextObjectText(obj)
                    cstr = ConvTxt(str, boolFlipHeb)
                if not cstr == str:
                    rs.TextObjectText(obj, cstr)
                keep.append(obj)
            else: keep.append(obj)
        
        rs.TransformObjects(keep, rs.XformInverse(XformBlock), False)
        
        newGeometry = []
        newAttributes = []
        for object in keep:
            newGeometry.append(rs.coercegeometry(object))
            ref = Rhino.DocObjects.ObjRef(object)
            attr = ref.Object().Attributes
            newAttributes.append(attr)
        
        InstanceDefinitionTable = sc.doc.ActiveDoc.InstanceDefinitions
        InstanceDefinitionTable.ModifyGeometry(idefIndex, newGeometry, newAttributes)
        
        rs.DeleteObjects(keep)
Exemple #5
0
def blkObjs(blkid):
    blockName = rs.BlockInstanceName(blkid)
    # objref = rs.coercerhinoobject(blkid)
    # idef = objref.InstanceDefinition
    # idefIndex = idef.Index
    
    lvl = levels[rs.GetUserText(blkid, 'level')]
    height = lvl['height']
    xform = rs.BlockInstanceXform(blkid)
    objects = [x for x in rs.BlockObjects(blockName) if rs.IsPolysurfaceClosed(x)]

    objects = map(lambda x: rs.SetUserText(x, 'level', lvl), objects)
    # map(lambda x: rs.SetUserText(x, 'height', lvl))

    blockInstanceObjects = rs.TransformObjects(objects, xform, True)
Exemple #6
0
def blkObjs(blkid):
    blockName = rs.BlockInstanceName(blkid)
    # objref = rs.coercerhinoobject(blkid)
    # idef = objref.InstanceDefinition
    # idefIndex = idef.Index

    # lvl = levels[rs.GetUserText(blkid, 'level')]
    # height = lvl['height']
    xform = rs.BlockInstanceXform(blkid)
    # objects = [x for x in rs.BlockObjects(blockName) if 'mass' in rs.ObjectLayer(x)]
    objects = filter(lambda x: 'mass' in rs.ObjectLayer(x),
                     rs.BlockObjects(blockName))
    # objects = filter(lambda x: 'mass' in rs.ObjectLayer(x) and rs.GetUserText(x, 'class') != 'na', rs.BlockObjects(blockName))
    # objects = map(lambda x: rs.SetUserText(x, 'level', lvl), objects)
    # map(lambda x: rs.SetUserText(x, 'height', lvl))

    blockInstanceObjects = rs.TransformObjects(objects, xform, True)
    masses.extend(blockInstanceObjects)
Exemple #7
0
def assignBlockToPanel(obj):
    """
    Assigns block containing a base surface to a surface with the matching name.
    Block, base surface, and target surface must all have the same name.
    
    input: target surface (with name)
    returns: None
    """
    allBlockNames = rs.BlockNames()
    for eachBlockName in allBlockNames:
        if rs.ObjectName(obj) == eachBlockName:
            blockName = eachBlockName
            print "Matching Block Found"

            objBasePt = rs.SurfaceEditPoints(obj)[0]
            objYPt = rs.SurfaceEditPoints(obj)[1]
            objXPt = rs.SurfaceEditPoints(obj)[2]
            objXVec = rs.VectorCreate(objXPt, objBasePt)
            objXLength = rs.VectorLength(objXVec)
            objYLength = rs.Distance(objBasePt, objYPt)

            blockObjs = rs.BlockObjects(blockName)
            for blockObj in blockObjs:
                if rs.ObjectName(blockObj) == blockName:
                    print "Contains base plane"
                    if rs.IsSurface(blockObj):
                        blockBasePt = rs.SurfaceEditPoints(blockObj)[0]
                        blockYPt = rs.SurfaceEditPoints(blockObj)[1]
                        blockXPt = rs.SurfaceEditPoints(blockObj)[2]
                        blockXVec = rs.VectorCreate(blockXPt, blockBasePt)
                        rotAngle = rs.VectorAngle(objXVec, blockXVec)
                        blockXLength = rs.VectorLength(blockXVec)
                        blockYLength = rs.VectorLength(
                            rs.VectorCreate(blockYPt, blockBasePt))
                        xScale = objXLength / blockXLength
                        yScale = objYLength / blockYLength
                        newScale = [yScale, xScale, 1]
                        rs.InsertBlock(blockName,
                                       objBasePt,
                                       scale=newScale,
                                       angle_degrees=rotAngle)
                        break
                    else:
                        print "Error: Base plane was not a surface"
Exemple #8
0
def GetAllBlockObjectsInPosition(obj):
    """Recursively get all objects from a block (and blocks in blocks)
    inputs:
        obj (block instance)
    returns:
        objs (list guids): Geometry is a copy of the instance geometry
    """
    blockObjs = rs.BlockObjects(rs.BlockInstanceName(obj))
    xform = rs.BlockInstanceXform(obj)
    objList = []
    for eachblockObj in blockObjs:
        if rs.IsBlockInstance(eachblockObj):
            thisBlockObjects = GetAllBlockObjectsInPosition(eachblockObj)
            for eachObject in thisBlockObjects:
                transformedObj = rs.TransformObject(eachObject, xform, False)
                objList.append(transformedObj)
        else:
            transformedObj = rs.TransformObject(eachblockObj, xform, True)
            objList.append(transformedObj)
    return objList
Exemple #9
0
def AddToBlock():
    objects = rs.GetObjects("Choose Objects to Add to Block", preselect=True)
    if not objects: return

    block = rs.GetObject("Choose Block to Add Object to", rs.filter.instance)
    if not block: return

    rs.EnableRedraw(False)

    blockName = rs.BlockInstanceName(block)
    objref = rs.coercerhinoobject(block)
    idef = objref.InstanceDefinition
    idefIndex = idef.Index

    if rs.IsBlockReference(blockName):
        print "Block is referenced from file; unable to add object(s)"
        return

    blnCopy = False

    XformBlock = rs.BlockInstanceXform(block)

    rs.TransformObjects(objects, rs.XformInverse(XformBlock), blnCopy)

    objects.extend(rs.BlockObjects(blockName))

    newGeometry = []
    newAttributes = []
    for object in objects:
        newGeometry.append(rs.coercegeometry(object))
        ref = Rhino.DocObjects.ObjRef(object)
        attr = ref.Object().Attributes
        newAttributes.append(attr)

    InstanceDefinitionTable = sc.doc.ActiveDoc.InstanceDefinitions
    InstanceDefinitionTable.ModifyGeometry(idefIndex, newGeometry,
                                           newAttributes)

    rs.DeleteObjects(objects)

    rs.EnableRedraw(True)
Exemple #10
0
def BlockTxtSim(block, boolFlipHeb=True):
    
    if not block: return
    
    blockName = rs.BlockInstanceName(block)
    blockObjects = rs.BlockObjects(blockName)
    
    obj = blockObjects[0]
    str= ""
    cstr = ""
    
    if rs.IsText(obj):
        str = rs.TextObjectText(obj)
        cstr = ConvTxt(str, boolFlipHeb)
    else: return
    
    blocks = rs.ObjectsByType(4096, False, 0)
    
    for blockRef in blocks:
        BlockTxt(blockRef, str, cstr, boolFlipHeb)
    
    rs.EnableRedraw(True)
Exemple #11
0
def RunCommand(is_interactive):
    if sc.escape_test(False):
        print "script cancelled"  #do something

    print "Making unique..."

    #******* Get blocks *****************
    #************************************

    objectIds = rs.GetObjects("Pick some blocks", 4096, preselect=True)
    if not objectIds:
        print "No objects"
        return False

    #pause viewport redraw
    rs.EnableRedraw(False)

    #******* Sort blocks by type ********
    #************************************
    blockTypes = {}
    for id in objectIds:
        blockName = rs.BlockInstanceName(id)
        if blockName not in blockTypes:
            blockTypes[blockName] = []
        blockTypes[blockName].append(id)

    #***** Define new block and add *****
    #************************************

    #Get block names
    blockNames = rs.BlockNames()

    #gather all new objects when done
    finalObjs = []

    for blockType in blockTypes:
        for id in blockTypes[blockType]:
            #Get the block transformation matrix and name
            blockXForm = rs.BlockInstanceXform(id)
            blockName = rs.BlockInstanceName(id)

            #Get objects in the block
            exObjs = rs.BlockObjects(blockName)

            #create new block name

            # if the string ends in digits m will be a Match object, or None otherwise.
            strippedName = re.sub(r'#[0-9]+$', '', blockName)

            #test if block name exist and add to the end number if true.
            x = 0
            tryAgain = True
            while tryAgain:
                x += 1
                newerBlockName = strippedName + "#" + str(x)
                if newerBlockName not in blockNames:
                    tryAgain = False
                    break

            #insert exObjs as new block
            rs.AddBlock(exObjs, [0, 0, 0], newerBlockName, delete_input=True)
            newerBlock = rs.InsertBlock(newerBlockName, [0, 0, 0])

            #match properties from original
            rs.MatchObjectAttributes(newerBlock, id)

            #transform new block
            rs.TransformObject(newerBlock, blockXForm)

            #append for final selection
            finalObjs.append(newerBlock)

        #add name to list of used blocknames.
        blockNames.append(newerBlockName)

    #Delete original block
    rs.DeleteObjects(objectIds)

    #Select all new objects
    rs.SelectObjects(finalObjs)

    rs.EnableRedraw(True)

    print "...aaand its done."
    #End RunCommand()

    #end sane
    return 0
def GetCustomSpacing(blockName):
    guids = rs.BlockObjects(blockName)
    pts = rs.BoundingBox(guids)
    return rs.Distance(pts[0], pts[2])
Exemple #13
0
import rhinoscriptsyntax as rs
import trkRhinoPy as trp
# walls = rs.GetObjects("Select solids", rs.filter.polysurface, preselect=True)
blk = rs.GetObject("Select blocks", rs.filter.instance, preselect=True)

objects = rs.BlockObjects(blk)
map(lambda x: rs.Area(x), objects)

# rs.EnableRedraw(False)
# trp.redefineBlockScale(blk)
# rs.EnableRedraw(True)