Entering groups.Again...
-
Hello. I know this topics been answered before in a similar fascion,
but I'm a little confused about the subject.I was under the strict impression that groups could not be entered no matter what, through Ruby code.
However after reading through "Automatic Sketchup" I went back to page and 148 it said(a portion of it):
@unknownuser said:
Each Group stores its Entity objects in its own separate Entities container, and you can access this by invoking the Group's entities method. With this collection, you can add Edges, Faces, and other Entity objects to the Group as though it was the top-level SketchUp model. For example, the following code adds a circular array of Edges to group1:
ent_g = group1.entities
circle = ent_g.add_circle [0.5, 0.5, 1], [0, 0, 1], 0.25So what does this mean? One CAN enter the group and add geometry after the groups been created?
And this would apply to components as well?
Or am I missunderstanding some fundamential rule in the example.English is not my native language so, I might have gotten it all wrong..
-
Yes, you can add entities to a group after creation.
Maybe you are thinking of making the Group's context active - like when you double-click a Group. This can not be done.
But you can access a Group's entities anytime and make modifications, regardless if the Group is the "active_entities" or not.
-
Keep in mind that you don't work in the same manner when you write plugin to create geometry as when you use SketchUp's tools.
When you model in SketchUp you model the geometry - then group
In Ruby you create the group, then add the geometryWhen modelling and you want to edit a group you have to open it
In Ruby you can access any group/component regardless -
You always add objects to an entities collection.
This can be
model.entities
= the entities in the model itself
model.active_entities
= the active entities - which might be the model.entities BUT if you are in the middle of doing an edit on a group or component-instance when the code is run then the active_entities are the current definition's entities, rather than the model's.
group.entities
= the entities contained within the group's definition
definition.entities
= the entities contained within the definition - a definition is a component - when it's been added to an entities collection it's a component-instance, so to access an instance's entities you use instance.definition.entities,as the instance has no entities of its own.
A group is a special kind of component - its entities are accessible directly, but you can find it's definition in a convoluted way defn=group.entities.parent [which returns a definition rather than the group itself] if needed...So each of the 'entities' collections can have objects added [see the API entities section http://code.google.com/apis/sketchup/docs/ourdoc/entities.html]
model.entities.add_cpoint([0,0,1])
adds a cpoint [guide] 1" above the origin - it is in the model's entities.
group.entities.add_cpoint([0,0,1])
adds a cpoint [guide] 1" above the origin - it is in the group's entities, because the group's origin may have been moved the cpoint appears relative to the group's current origin [insertion-point] and not the model's.
To add a new group to a model use
group=model.add_group()
now you can add objects to group_entities, you can even add a group inside a group with
group1=group.entities.add_group()
Then you can add add objects into either group.entities or group1.entities.
Do not try and add existing geometry etc directly into a new group as it can cause Bugsplats if the 'entities-barrier' is crossed and the active_entities is not the source/target. So e.g. group.entities.add(model.selection[0]) may work if 'group' is in the active_entities [i.e. where the [pre]selection is] but group1.entities.add(model.selection[0]) will probably crash if 'group1' is inside 'group' so removed from the 'active_entities' by a level of nesting. If you are at all uncertain of this then 'clone' the objects and replicate them within a new 'empty' group, copying their points, properties etc, if you want to copy a group to be inside a new group's entities then you get it's definition [as explained above] and then use ..entities.add_instance(group_defn, group.transformation) on it, as you would with a component. To 'move' it to be inside the new group's entities simply add a copy as above and then erase the original... -
You guys are great!
This is more logical. Opens up more possibilities compared to what I thought was doable in Ruby.
No wonder I did not understand how some plugins was constructed if I thought one could not alter geometry in groups/comp.. Need to get back to basics..Not ready to fool around yet.So if I got this right. On top of adding/del. geometry, one can alter it as well(transl., scaling etc)?
@unknownuser said:
Group's context active
If group is not open in modelspace or in Rubycode, one cannot use tools on geometry, pushpull for ex?
Have to get a grasp of the term "context active". (Note to myself)Thank you all for your help. And mr TIG's detailed explanation. I have to reread that one a couple of times to get it into my thick skull.
-
Of course you can use any API tools within ANY entities context.
So if you have a face inside a group and you have a reference to it - either because you just added it with
face=group.entities.add_face(p0,p1,p2,p3)
OR because you've found it by some test of preexisting entities - like this one that looks for a flat 'up' face in the entities...
face=nil group.entities.each{|e|face=e if e.class==Sketchup::Face and e.normal.z==1}
then you can manipulate it like any other face.
For exampleface.pushpull(100.mm)
will pushpull the face by 100mm [irrespective of the current SKP's unit settings]... All the newly made geometry is within the same entities as the original face - exactly like when you do it manually...There is nothing special about an entities context except that you must get you head around the transformation of the 'container'.
So if you have a group and you want to add something to it you need to think where the group's origin is, and where its insertion-point is relative to the model's origin IF you are calculating locations relative to one and not the other. This is where 'transformations' come in. Often you don't need to do much to make things work but if for example you have a user picking a point in the model that is to be translated into a point inside a group and that is no longer placed at the model's origin, then to insert a cpoint you get the picked-point in the model and and then use something likepoint.transform!(group.transformation.inverse)
then add the cpoint at the adjusted 'point', into the group.entities and it appears in the corresponding location to the point picked in the model - otherwise just adding it 'untransformed' will cause it to jump by the group's own transformation when it's used to place it inside that group - try it an see... -
Note: if you deal with
model.active_entities
- then the currently open group/component's co-ordinates are temporarily transformed into world space. To get the local co-ords you need to transform the points bymodel.edit_transform.reverse
. -
@unknownuser said:
Of course you can use any API tools within ANY entities context.
Thats even better.
I learned a lot from this, thanks all!
Have to analyze this info and make me understand it fully. Local and Global coordinate system can be a little daunting to dig into for a newbie, but you explained it very well.
Advertisement