Help with Components
-
If you are rotating (or tranforming) everything inside a componentinstance, apply your tranformation to all the entities by using
Entities.transform_entitieshttp://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
Enumerablemodule, which theArrayclass 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) endSorry 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.selectwill return a new array will all the elements ofarr1when 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 theDrawingElementclass. So we use the.selectmethod to extract only the items that inherits from that classe.is_a?(Sketchup::Drawingelement). -
Yes.
When you see in the API manual, for the
Faceclass for example: http://code.google.com/intl/nb/apis/sketchup/docs/ourdoc/face.html
Notice it says: Parent: DrawingelementAnd the
Drawingelementclass says Parent: EntityAnd the
Loopclass: Parent: EntitySo a Face
.is_a?(Sketchup::Face)==true
.is_a?(Sketchup::Drawingelement)==true
.is_a?(Sketchup::Entity)==trueBut a Loop
.is_a?(Sketchup::Loop)==true
.is_a?(Sketchup::Drawingelement)==false<- Notice
.is_a?(Sketchup::Entity)==trueBecause the relationship of a
Faceis:
Sketchup::Face<Sketchup::Drawingelement<Sketchup::Entity<ObjectWhile a
Loopis:
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 > endIn your examples, what you mean to do is:
comp_entities.each do |e| if e.is_a? Sketchup;;Drawingelement comp_new.push e end endNotice the
.eachinstead of.select.eachis a simple iterator - it does not return any values.
.selectreturns 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 endDoes the exact same thing as:
comp_new = comp_entities.select do |e| e.is_a?(Sketchup;;Drawingelement) endCan 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.selectsaves 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 endAddenda: Corrected as noted below.
Advertisement