Nested Components
-
I am using the following code from Todd as a starting point
# Written by Todd Burch www.smustard.com June 23, 2007. # First, create a new Component Definition. new_comp_def = Sketchup.active_model.definitions.add("MyCube") ; # Now, define some points for an arbitrary face. points = Array.new ; points[0] = ORIGIN ; points[1] = [100, 0 , 0] ; points[2] = [100, 100, 0] ; points[3] = [0 , 100, 0] ; # Now, add the face to the new component definition. newface = new_comp_def.entities.add_face(points) ; # returns a Face # If the blue face is pointing up, reverse it. newface.reverse! if newface.normal.z < 0 ; # flip face to up if facing down # pushpull the face into a Cube newface.pushpull(100) ; # extrude it up to make a cube # To add the component, you have to define a transformation. We can define # a transformation that does nothing to just get the job done. trans = Geom;;Transformation.new ; # an empty, default transformation. # Now, insert the Cube component. Sketchup.active_model.active_entities.add_instance(new_comp_def, trans) ; #done.
What I would like to do is actually add a new component within the component I am creating. I looked at lost of scripts, but everybody seems to just do add_group.
The idea would be to create new_comp_def = model.definitions.add
then create new_sub_comp_def = new_comp_def.definitions.addnow at the end one should probably have
new_comp_def.entities.add_instance(new_sub_comp_def,trans)
model.active_entities.add_instance(new_comp_def,trans)or similar.
Anybody with guidance here?
-
If you add a group inside the component, then put in the bits of geometry etc... you can then use the method group.to_component... it does what it says on the label. You can always change that new compo-def / instance name etc later...
-
@unknownuser said:
The idea would be to create new_comp_def = model.definitions.add
then create new_sub_comp_def = new_comp_def.definitions.addYour second line will fail. There is no .definitions method for a definition. You will need to create the second definition exactly like the first, using model.definitions.add
Then, you can add an instance to the entities of your parent component:
new_comp_def.entities.add_instance(new_sub_comp_def, transformation)
-
Thanks guys, will try both ideas.
RickW: The line was meant to be pseudo code. It sort of indicated what I wanted to do, but I also noted that there was no definitions within a definitions.
Both Ideas make sense, ie it is similar to how you actually create it in SketchUp itself. Add a component inside the component. Logically I thought you had to create it inside, then add the geometry, but it is actually the other way around, create the definition, then add it to the parent.
TIG's idea might actually be the simplest, because most sample code uses the add_group approach. So if you can later just change the group to a component, that seems rather simple.
-
Be careful, though, with groups. If you create an empty group in the definition and then add geometry to it, you'll be okay. But if you try to create the group with specified geometry, your group will end up in the main model entities space. That's because in ruby, things that are impossible when drawing manually become (theoretically) possible, such as using FollowMe on a path outside the current entities space (will work), or creating groups inside of groups when you aren't inside the group to begin with (won't work).
So, just be mindful when working with groups & nested geometry, that things may not happen the way you expect.
Advertisement