Replace component and set dynamic attributes?
-
OK - here's the challenge.
I have a model with several non-dynamic components (doors).
I have a dynamic component (door) called LDOOR.I can select all the non-dynamic components -- and then what I want to do, for each such instance is:
- grab the height, width, depth and X,Y,Z, and rotation of the non-dynamic component
- assign height, width and depth and X, Y, Z and rotation to attributes: LenX, LenY and LenZ,X,Y,Z,RotX etc.. of the dynamic component.
- have the dynamic component update itself (i.e. react to the changes in #2)
- replace the non-dynamic component with the new instance of the dynamic component.
It MUST be possible...and I have carefully studied TIG's component replacer code - but try as I might I can't get it to work.
So far, I can easily select the dynamic component:
model = Sketchup.active_model definitions = model.definitions component = definitions['LDOOR']
and I can happily iterate through my selected "non dynamic components":
sel=model.selection for instance in sel h= instance.bounds.height d= instance.bounds.depth w= instance.bounds.width x= instance.transformation.origin.x y= instance.transformation.origin.y z= instance.transformation.origin.z r= Math.asin(instance.transformation.to_a[4]).radians.round next
But how to now create a new instance of the dynamic component and fill in the values!!!! H-E-L-P!
-
OK - getting close -- very close -- but stuck on getting the right rotation (and I'm sure there's a cleaner way of doing all this):
The issue is that I'm retrieving the wrong rotation. If I create a rotZ attribute on the entity I'm trying to replace, it shows (for example) 135 degrees. But the instance.transformation array gives me a z-rotation of -45. I'm stumped! I've attached a sample scene. The dynamic door is RED and my targets are, well, not.
` model = Sketchup.active_model
entities = Sketchup.active_model.entities
definitions = model.definitions
component = definitions['LDOOR']model.start_operation("Create Door")
#create a new instance
transformation = Geom::Transformation.new([0,0,0])
componentinstance = entities.add_instance(component, transformation)
puts 'we have added a new instance (GUID:' + componentinstance.guid + ')'
$dc_observers.get_latest_class.redraw(componentinstance)
model.commit_operationmodel.start_operation("XForm Door")
sel=model.selection
instance = sel[0]x= instance.transformation.origin.x y= instance.transformation.origin.y z= instance.transformation.origin.z d= instance.definition.bounds.height h= instance.definition.bounds.depth w= instance.definition.bounds.width r= Math.asin(instance.transformation.to_a[4]).radians.round componentinstance.transformation = Geom::Transformation.new([x,y,z]) d=2 ;#override depth #instance.attribute_dictionaries["dynamic_attributes"].each_pair{|k,v|p k;p v;puts} ad = componentinstance.attribute_dictionary "dynamic_attributes" ad["_lenx_formula"]=w.inspect ad["_leny_formula"]=d.inspect ad["_lenz_formula"]=h.inspect ad["_rotz_formula"]=r.inspect $dc_observers.get_latest_class.redraw(componentinstance) entities.erase_entities instance
model.commit_operation
#` -
There are a few things that I had to deal with.
I found it easier to put the dynamic attributes in a nested group as opposed to the outside group. This way you can rotate the entire door and jamb to fit it into the door opening and you don't have to keep changing the dynamic rotz. These doors can be copied, flipped and or rotated while preserving the desired angles of rotation.
Advertisement