Description

Rend un ou plusieurs bloc(s) unique(s).

Le script suivant fonctionne sur :

  • Rhino pour Windows ;
  • Rhino pour macOS.

Script par Ejnar Brendsdal sous licence MIT.

Code

#******* Imports ********************
#************************************
 
import rhinoscriptsyntax as rs
import scriptcontext as sc
import Rhino.Geometry as G
import re
 
#******* Main function ********************
#******************************************
 
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
 
RunCommand(True) #Run script
 
#END MakeUnique