Help with Components
-
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
-
@chris fullmer said:
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
They are not the same.
Enumerable.collect
(aliasEnumerable.map
)arr = [0, 1, 2, 3, 4] arr.collect { |i| i * 2 }
returns:[0, 2, 4, 6, 8]
I use it for instance to collect Point3ds from vertices collections.
points = face.vertices.collect { |vertex| vertex.position }
Enumerable.select
(aliasEnumerable.find_all
)
arr = [0, 1, 2, 3, 4] arr.select { |i| i%2 > 0 } # block returns true if i is an odd number
returns:[1, 3]
I recently noticed this:
Enumerable.partition
arr = [0, 1, 2, 3, 4] arr.partition { |i| i%2 > 0 } # block returns true if i is and odd number
returns:[[1, 3], [0, 2, 4]]
Also rather handy.
The
Enumerable
module, which theArray
class includes, has a number of very nice methods which I've found do many of the things I often do with arrays. I just haven't noticed them, partly because not everything in the Enumerable module isn't listed in the Array class docs.model.selection.all? { |e| e.is_a?(Sketchup::Edge) }
or
[ruby:af7phbud]model.selection.any? { |e| e.is_a?(Sketchup::Edge) }[/ruby:af7phbud]
-
Wow those are great Thom. I see the difference now there with collect and select. And I really like that partition method. Not sure where I'd use it right now, but I have a feeling it might come in handy eventually.
Thanks!
Chris
-
Those five methods there has cut down many lines in my older codes.
-
Hi Guy, Finally got around to working on my program, and thanks to you it works. I have one question. How do I understand the following bit of code?
comp_new = comp_entities.select do |e| e.is_a?(Sketchup;;Drawingelement) end
Sorry for the long-hand, but it helps me to read my code, perhaps when I get better:-}
-
your variable name is a bit confusing. "comp_entities" is the resulting array from
.explode
? Assuming it is:"comp_new" sounds like it's a ComponentInstance or ComponentDefinition, but in this case it will be a collection of entities.
Example of
.select
:arr1.select
will return a new array will all the elements ofarr1
when the block returnstrue
.In your case, you have an array returned from
.explode
. That array contains lots of thing you don't want. But the common denominator for the items you do want is that they inherit from theDrawingElement
class. So we use the.select
method to extract only the items that inherits from that classe.is_a?(Sketchup::Drawingelement)
. -
Yes.
When you see in the API manual, for the
Face
class for example: http://code.google.com/intl/nb/apis/sketchup/docs/ourdoc/face.html
Notice it says: Parent: DrawingelementAnd the
Drawingelement
class says Parent: EntityAnd the
Loop
class: Parent: EntitySo a Face
.is_a?(Sketchup::Face)
==true
.is_a?(Sketchup::Drawingelement)
==true
.is_a?(Sketchup::Entity)
==true
But a Loop
.is_a?(Sketchup::Loop)
==true
.is_a?(Sketchup::Drawingelement)
==false
<- Notice
.is_a?(Sketchup::Entity)
==true
Because the relationship of a
Face
is:
Sketchup::Face
<Sketchup::Drawingelement
<Sketchup::Entity
<Object
While a
Loop
is:
Sketchup::Loop
<Sketchup::Entity
<Object
-
The Object Diagram is a nice graphical overview of the classes relationships: http://code.google.com/intl/nb/apis/sketchup/docs/diagram.html
-
@honoluludesktop said:
Yes, "comp_entities" is the exploded array. OK, so when I see is_a? in this manner, the do loop is "select(ing) the entity(s)" in the array that is a (are) %(#FF0000)Sketchup::Drawingelement. Is it faster (better) then:
comp_entities.select do |e| > if e.is_a? Sketchup;;Drawingelement > comp_new.push e > end > end
In your examples, what you mean to do is:
comp_entities.each do |e| if e.is_a? Sketchup;;Drawingelement comp_new.push e end end
Notice the
.each
instead of.select
.each
is a simple iterator - it does not return any values.
.select
returns a new array - where the content depends on when the block yields true.comp_new = [] comp_entities.each do |e| if e.is_a? Sketchup;;Drawingelement comp_new.push e end end
Does the exact same thing as:
comp_new = comp_entities.select do |e| e.is_a?(Sketchup;;Drawingelement) end
Can also we written as one line:
comp_new = comp_entities.select { |e| e.is_a?(Sketchup;;Drawingelement) }
There are many ways to do things - but Ruby has many useful methods for repeated tasks that saves your from typing the same code structure over and over again.
In these examples - I'm not sure if anyone is any significantly faster than the other. But.select
saves some characters. -
Opps, I meant comp_entities.each. Thanks for the lesson.
-
Yes, "comp_entities" is the exploded array. OK, so when I see is_a? in this manner, the do loop is "select(ing) the entity(s)" in the array that is a (are) %(#FF0000)Sketchup::Drawingelement. Is it faster (better) then:
comp_entities.each do |e| if e.is_a? Sketchup;;Drawingelement comp_new.push e end end
Addenda: Corrected as noted below.
Advertisement