Help with Components
-
Need help. I tried to do the following with limited success. Couldn't figure out the api, so some questions about components:
- Can I remove, modify, and return a entity to a component instance? How?
- I saved a component instance's entities in a array then exploded the component. How do I make a new component out of a array of entities?
- How do I return the array to the original definition?
Going out, and will not get back until later today, thanks for any insight.
-
Well, I hoping to get a better version of this out for the holidays. Requests (if any for modification) will be later then you are used to, as I am still stumbling over oops and the api:-) The plugin, will point any component (not group) along its zaxis to a selected target. I use it for my light fixtures, prior to render.
Right click a selected component, then menu select to point on zaxis. Happy holidays!!!
-
#1 - you don't add entities to the instance, you add them to the definition. And the definition is always located where its origin is aligned with the real world origin.
#2 - you do something like:
my_group = entities.add_group(entities_array) my_comp = my_group.to_component
I don't have the API handy or SketchUp, but I think those are the exact method names. Can I ask where you got the entities array from? You might find that you actually have an array of the definition entities, not the actual entities inside your instance. But you might have more luck capturing the array of entities when you explode the component instance. This is very handy:
exploded_array = my_comp_instance.explode
Exploding a group or instance returns an array of the entities (plus other stuff so you have to filter out the non entities (such as loop objects)).
#3 - ? You want to turn that comp back into an instance of the original? Easiest would be to just insert another instance of the definition. So you would want to capture the transformation of the instance before exploding it. So when you go to insert a new one, you have the exact transformation you want to insert it with.
Hope that gets you pointed in the right direction.
Chris
-
Hi Chris, Thanks for the direction. I did not think that I would have to make a group first. Actually, I don't remember the first way I created an array of entities, but eventually found ComponentInstance.explode. I think I made a blank array, then copied the selected component's entities to it. Filter out?, hmm.., OK. Thanks again, I will run with what you have provided, and see if I can finish my program.
-
OK, I found, filtered out "EdgeUse", "Loop", and attempted my_group = entities.add_group(comp_ok_entities), and got Error: #<ArgumentError: All Entities must have a common parent>. How do I do that?
-
That is interesting. It sounds like somehow your array of ents still has something in there that SU does not want. Specifically that error is one that would be given if you tried to add a face and edges from the main model space, plus a face and edges nested inside a component already to a new group.
So when you explode the component, are there sub components in it? If so, it sounds like perhaps exploding it is returning the edge and face objectID's of nested geometry. When you really can't use that. You need it to return only edge and face objectID's of geometry in the uppermost level and componentinstancesID's and groupID's from the same level.
I'd test it out myself, but I'm not on a SU machine right now
Good luck. You might need to get your array differently I suppose?
Chris
-
Chris, you are right about the sub-component. Actually it is a critical part of the parent component, and I can't lose the attributes assigned to it. Let me try a few things :-{
-
Try iterating through all the entities that are returned in the "explosion". And make an array of all entities that are groups, comp_instances, and edges and faces. And you will need to check the parent of each of these to verify it is the same as the current level of editing. I think you should be able to tell if you are iterating over a face that is not grouped vs. one that is actually a part of an instance. Then if a face or edge or group or whatever does have a different parent than the current level, then you would obviously not want to add it to your final array of entities. So this would effectively weed out all entities that are being listed, even though they are inside an sub-component.
See if you can get that code up and running,
Chris
-
Chris, I have filtered out everything, and visually checked the parent of each entity. All have the model as their parent, but the error still persist.
The entities takes the form of #Sketchup::Face:0xe4d85c8, etc. A previous error messages made reference to requiring the form as Sketchup::Entity, and I wondered if I had to edit out "<#", and ">" from the variable. I tried to use v1.chop, but that failed, and the compiler reported that it was a "private" method. In any case, do I need to edit the form the array takes?
-
@honoluludesktop said:
Chris, I have filtered out everything, and visually checked the parent of each entity. All have the model as their parent, but the error still persist.
The entities takes the form of #Sketchup::Face:0xe4d85c8, etc. A previous error messages made reference to requiring the form as Sketchup::Entity, and I wondered if I had to edit out "<#", and ">" from the variable. I tried to use v1.chop, but that failed, and the compiler reported that it was a "private" method. In any case, do I need to edit the form the array takes?
No - not need to edit out <# and >. This is just what the string
Entity.to_s
orEntity.inspect
method outputs. It's automatically called when youputs entity
to the console.What would help is if you posted more of the script so we can see the how it's working. Much easier than guessing.
-
I basically tried it two ways:
if comp.is_a? Sketchup;;ComponentInstance if @sel == "yes" comp.definition.entities.each do |subcomp| ###if entity is a sub componant if subcomp.is_a? Sketchup;;ComponentInstance comp_entities = [] comp_entities = comp.definition.entities #do stuff with entities here my_group = entities.add_group(comp_entities) #my_comp = my_group.to_component #my_comp.name= my_comp_name end end
andif comp.is_a? Sketchup;;ComponentInstance if @sel == "yes" comp.definition.entities.each do |subcomp| ###if entity is a sub componant if subcomp.is_a? Sketchup;;ComponentInstance comp_entities = comp.explode #do stuff here comp_entities.each do |filter| if filter.to_s.split(';0x').first != "#<Sketchup;;Loop" and filter.to_s.split(';0x').first != "#<Sketchup;;EdgeUse" and filter.to_s.split(';0x').first != "#<Sketchup;;AttributeDictionary" and filter.to_s.split(';0x').first != "#<Sketchup;;AttributeDictionaries" comp_ok_entities.push filter end end my_group = entities.add_group(comp_ok_entities) #my_comp = my_group.to_component #my_comp.name= my_comp_name end end
-
Instead of adding the entities to a new group and converting it back to a component, make the component unique. http://code.google.com/apis/sketchup/docs/ourdoc/componentinstance.html#make_unique
(note, the API says it returntrue
/false
, but it really returns a newComponentInstance
.)if comp.is_a? Sketchup;;ComponentInstance if @sel == "yes" comp.definition.entities.each do |subcomp| ###if entity is a sub componant if subcomp.is_a? Sketchup;;ComponentInstance my_comp = subcomp.make_unique comp_entities = my_comp.definition.entities #do stuff with entities here end end
-
Understood, however my problem came about because I was having difficulty understanding transformations of individual ComponentInstance.definition.entities, and was only able to do it after exploding the component. Which brought me back to returning it into a component form. I assume that I can not return to the original definition (some component entities have been relocated), so I was trying to create a new component.
When I do the following with a component selection that has no sub-components:
comp_entities = [] selection.each do |g| if g.is_a? Sketchup;;ComponentInstance comp_entities = g.definition.entities #do stuff here my_group = entities.add_group(comp_entities) end end
I get (eval):10:in βadd_groupβ: wrong argument type (expected Sketchup::Entity). When I add after #do stuff here
comp_entities.each do |f| puts f.parent end
Puts echo's #Sketchup::ComponentDefinition:0xe4ba1a8 for each entity to the display, demonstrating that the entities have the same parent. So what is a "wrong argument type" for my_group = entities.add_group(comp_entities)?
-
Are you doing anything to the
comp_entities
variable?if you add right before your
add_group
adebug = comp_entities.to_a.select { |e| !e.is_a?(Sketchup;;Entity) } adebug.inspect
What does it output? That snippet should return an array of everything in that collection which isn't inherited from
Sketchup::Entity
. -
At this time, I am not doing anything, just trying to group the array of entities. Once I figure how to do that, I will place my procedure (rotates one of the entities) in the if-end "block" (is that the right word?).
-
In your snippet there, the variable
comp_entities
is pointing to an entities object. That is not what.add_group
is expecting. That is what the error is..add_group
is expecting a single entitiy OR an array of entities. So just change the loine to this and see if it works:my_group = entities.add_group(comp_entities.to_a)
I just added a
.to_a
to turn the entities object into an actual aray. Now it should work, I think.Chris
-
ok, and I just got onto a machine with SU on it. I tested what happens if I explode a group and then try to add the entities returned in the explosion to a group. And I got the error about adding entities with the same parent. So far so good.
I found that exploding the component instance does not return the inner geometry of each sub-component like I thought it might be doing. So there really is no need to test the parent of each entity. It is just a matter of making an array of only the parts you want.
So I exploded the component instance, and I then made an array of just faces, edges, groups and componentinstances. And then added that array to a new group and it worked just fine. Here is what that code looks like:
model = Sketchup.active_model ents = model.active_entities explode_array = model.selection[0].explode good_ents = [] explode_array.each do |e| good_ents << e if e.is_a? Sketchup;;Face good_ents << e if e.is_a? Sketchup;;Edge good_ents << e if e.is_a? Sketchup;;Group good_ents << e if e.is_a? Sketchup;;ComponentInstance end ents.add_group( good_ents )
And that works from the web console with a single component Instance selected, and it can have sub components and sub groups (but not sub-images since I didn't test for those.
Anyhow, I hope it helps. But in the end I think you might be better off approaching this entirely differently.
Chris
-
If you are rotating (or tranforming) everything inside a componentinstance, apply your tranformation to all the entities by using
Entities.transform_entities
http://code.google.com/apis/sketchup/docs/ourdoc/entities.html#transform_entitiesChris, that snippet can be made easier if you check for inheritance from the Drawingelements instead of individual classes.
model = Sketchup.active_model ents = model.active_entities explode_array = model.selection[0].explode good_ents = explode_array.select { |e| e.is_a?(Sketchup;;Drawingelement) } ents.add_group( good_ents )
-
Thanks, Guys. Not able to get back to this tonight, will try tomorrow night.
-
I like that Thom. I've also seen you use "select" a few times recently. I'll try to look at it, but is it different than collect? I recently ran across that one, and I never rememeber to use it. But they both appear to do about the same thing, and are remarkably useful for populating an array.
Chris
Advertisement