Skip to content

Instantly share code, notes, and snippets.

@pgolay
Last active January 24, 2019 17:05
Show Gist options
  • Save pgolay/50fcf76169c75268ce53942a8c94b2d4 to your computer and use it in GitHub Desktop.
Save pgolay/50fcf76169c75268ce53942a8c94b2d4 to your computer and use it in GitHub Desktop.
import rhinoscriptsyntax as rs
import scriptcontext as sc
import Rhino
def MoveBlockInsertionPoint():
id = rs.GetObject("Select a block instance", 4096, preselect=True)
if not id: return
name = rs.BlockInstanceName(id)
xform = rs.BlockInstanceXform(id).TryGetInverse()
#check for linking
if rs.BlockStatus(name) != -3 :
print "Sorry, can't handle linked blocks..."
return
ids = rs.BlockInstances(name)
obj = sc.doc.Objects.Find(id)
pt = obj.InsertionPoint
geo = obj.Geometry
p2 = rs.GetPoint("Set new insertion point", pt)
if not p2: return
vecDir = pt-p2
#Transform the vector by the instance (reverse) transform,
# i.e. back to the origin
vecDir.Transform(xform[1])
idef = obj.InstanceDefinition
if idef:
idx = idef.Index
containers = idef.GetContainers()
rhino_objects = idef.GetObjects()
attrList = [rhino_objects[i].Attributes for i in range(len(rhino_objects))]
geoList = []
for r_obj in rhino_objects:
#Move the definition geometry
#by the vector
r_obj.Geometry.Translate(vecDir)
geoList.append(r_obj.Geometry)
pass
#Modify the definition
sc.doc.InstanceDefinitions.ModifyGeometry(idx, geoList, attrList)
rs.EnableRedraw(False)
#modify instances that are contained in other instances.
if containers:
for container in containers:
c_idx = container.Index
c_objects = container.GetObjects()
c_geoList = []
c_attrList = []
mod = False
for c_obj in c_objects:
c_geoList.append(c_obj.Geometry)
c_attrList.append(c_obj.Attributes)
if isinstance(c_obj, Rhino.DocObjects.InstanceObject):
if c_obj.InstanceDefinition.Name == name:
mod = True
vec = Rhino.Geometry.Vector3d(vecDir)
vec.Reverse()
vec.Transform(c_obj.InstanceXform)
c_obj.Geometry.Translate(vec)
if mod:
sc.doc.InstanceDefinitions.ModifyGeometry(c_idx, c_geoList, c_attrList)
#modify top level instances
for i in range (len(ids)):
#get new copy of the transformed-to-the-origin move vector
vec = Rhino.Geometry.Vector3d(vecDir)
vec.Reverse()
#Get the instance transform,
#Transform the reversed vector back to the instance
vec.Transform(rs.BlockInstanceXform(ids[i]))
#Move the instance
rs.MoveObject(ids[i], vec)
rs.EnableRedraw(True)
if __name__=='__main__':MoveBlockInsertionPoint()
@pgolay
Copy link
Author

pgolay commented Jan 23, 2019

Added check for linked and nesting - those are not handled.

@pgolay
Copy link
Author

pgolay commented Jan 23, 2019

Handles nested and transformed blocks, better at least. I do not see how it will be possible to do linked blocks.

@pgolay
Copy link
Author

pgolay commented Jan 24, 2019

Note: if this block is used in another block, the blocks using it will change this block's position within the parent block if this block's IP is moved. I do not see any easy way to avoid that - there might be a complicated way.

@pgolay
Copy link
Author

pgolay commented Jan 24, 2019

Now handles nesting this block in parent blocks correctly, I think.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment