Open tomboul26 opened 3 months ago
also the documentation https://ruby.sketchup.com/Sketchup/Model.html#drawing_element_visible%3F-instance_method is wrong
module Example
def self.instance?(entity)
entity.is_a?([Sketchup](https://ruby.sketchup.com/Sketchup.html)::[ComponentInstance](https://ruby.sketchup.com/Sketchup/ComponentInstance.html)) || entity.is_a?([Sketchup](https://ruby.sketchup.com/Sketchup.html)::[Group](https://ruby.sketchup.com/Sketchup/Group.html))
end
# Walk the visible entities in the model, taking into account
# "DrawHiddenGeometry" and "DrawHiddenObjects" rendering options.
def self.walk(entities, transformation = [IDENTITY](https://ruby.sketchup.com/top-level-namespace.html#IDENTITY-constant), path = [], &block)
entities.each { |entity|
entity_path = path + [entity]
next unless entity.model.drawing_element_visible?(entity_path)
block.call(entity, transformation, path)
if instance?(entity)
child_entities = entity.definition.entities
child_transformation = transformation * entity.transformation
walk(child_entities, child_transformation, entity_path, &block)
end
}
end
end
model = [Sketchup](https://ruby.sketchup.com/Sketchup.html).[active_model](https://ruby.sketchup.com/Sketchup.html#active_model-class_method)
Example.walk(model.entities) do |entity, transformation, path|
# Do something to every visible entity in the model...
end
before entity.model.drawing_element_visible?(entity_path)
you have to test if the leaf of instancePath is nil or not
A leaf is a drawing element that is not a containers (group / component). That's per design. Whether that was the best choice is a different matter, but if we change this now it might break existing extensions. But we can at least improve documentation.
Btw, avoid string comparison of class names when checking types - it quickly becomes a bottleneck.
# instead of:
if(cm != "Group" && entity.respond_to?(:definition))
entity = entity.definition
end
# prefer type checking:
if entity.is_a?(Sketchup::Group) && entity.respond_to?(:definition)
entity = entity.definition
end
Thank you thomthom
A leaf is a drawing element that is not a containers (group / component). That's per design. Whether that was the best choice is a different matter, but if we change this now it might break existing extensions. But we can at least improve documentation.
Yes you have to change the documentation for leaf instance method :
The leaf of an instance path is the last element which can be any entity that can be represented in the model. This is normally a Drawingelement, but could be a Vertex. An instance can also be a leaf.
because it's seem that "group" or "component" are also Drawingelement.
group.is_a?(Sketchup::Drawingelement)
or component.is_a?(Sketchup::Drawingelement)
return true
There si also a problem with the use of drawing_element_visible?(entity_path)
you have to correct the documentation (this fabulous documentation very well done š š š ) but also the example "Traversing every visible entity in the model".
Btw, avoid string comparison of class names when checking types - it quickly becomes a bottleneck.
Yes you're absolutely right, i'm total beginner in ruby sletchup and this getName function was only used to display the name of a group or component on the sketchup ruby console, I had to indicate it here so that everyone can run the code I gave
and
drawing_element_visible?
has the errorLeaf must be a type of Sketchup::Drawingelement
when last item of instancePath is a group or a component for the same reason with this codeand this simple sketchup project (i can't upload a sketchup project here) i have this result :
leaf is always empty when group or component is the last item of instancePath
I commented this line
#vis = ent.model.drawing_element_visible?(pa)
, otherwise I always had this errorLeaf must be a type of Sketchup::Drawingelement
as leaf return nil, this is perhaps the reason why we have this error with
ent.model.drawing_element_visible?(pa)
when the last item of the instancePath is a group or a component