How to add components to another component? [ruby]
-
I want to select several component instances in my model, based on name, and group them in a bigger component.
Something like this:Component#1 |
Component#2 |--> Parent_Component
Component#3 |Test#1 |
Test#2 |
Test#3 |--> Parent_Test
Test#4 |
Test#5 |But I really don't understand how. I thought I have to use entities.add_instance method, but I can't make it work. And there is a Transformation attribute which I don't understand. All I can do is to add a New empty component definition in model.
Any help, please? Thanks.
-
Add component 1, 2 & 3 to selection; make selection component ... perhaps?
-
Can you explain how you select the components?
The easiest way would to be what chrisglasier suggested. However, if you want to do it in a ruby script, one way would be:
add a group, add the instances inside the group (group.entities.add_instance(instance.definition,instance.transformation)), delete the previous instances, and use group.to_component
if you add a group, you don't have to worry about transformations.
-
The easiest thing to do..
model = Sketchup.active_model selection = model.selection # use any array of entities g = model.entities.add_group(selection.to_a) g.to_component
Note the API warns against using add_group in this way, but I don't think it will be a problem when adding Instances.
-
@jim said:
The easiest thing to do..
> model = Sketchup.active_model > selection = model.selection > # use any array of entities > g = model.entities.add_group(selection.to_a) > g.to_component >
You put the code right into my words!
-
If you want to 'select' something ...
### Let's assume you have an array [list] of the items to add to the component, called 'list'... ### If you have selection made that has 'raw' faces in it then add those faces' edges too to the list, if those edges then have other faces those faces and edges too - '.all_connected. ??? ### This is what causes bugsplats - when parts of 'connected things' are put into different entities sets... model=Sketchup.active_model selection=model.selection selection.clear selection.add(list) ### OR list.each{|e|selection.add(e)} ### then the code... g=model.active_entities.add_group(selection.to_a) inst=g.to_component inst.name="MyInstancesName" defn=inst.definition defn.name="MyComponentsName" ### check it hasn't incremented; using dname=defn.name; compare dname to "MyComponentsName", e.g. it might now be "MyComponentsName#1" ### etc...
Advertisement