My code is working, but in some SketchUp models it is extremly slow.
faces ||= Sketchup.active_model.selection.select{ |e| e.is_a?(Sketchup::Face) }
faces.each_with_index{ |face,index| label_face(face, index) }
def entities
@entities ||= Sketchup.active_model.entities
end
def label_face(face, index)
# The following code line takes up to 9 seconds
group = entities.add_group(face)
# Some more code
...
end
I have no idea why the statement “entities.add_group(face)” takes so much time. Can anyone help or has a workaround? It would be very appreciated.
@model = Sketchup.active_model
@entities = @model.active_entities # it's safe - selection is in entities context
@model.selection.grep(Sketchup::Face).each_with_index{|face, index|
label_face(face, index)
}
def label_face(face, index)
group = @entities.add_group(face)
# Some more code -
# like adding some text to the now grouped face, using index ?
end
Obviously you need to wrap it in an ‘operation’ so it’s one-step undo-able…
Thanks for your reply! I changed to code as you recommeded, but the execution time did not decrease. The code is wrapped in an operation and the disable_ui flag is true. The file im working with is 300 MB big. In a smaller test file the group = @entities.add_group(face) command runs fast. But why should the filesize affect the execution time of this command? I don’t see the problem.
Since I use selection, I can select just one face. The execution time per face is not affected by the number of faces selected. I checked with ‘faces.length’ that really just one element is selected.
When I have labled just a few faces, the code runs fast, in every model. It runs slower as more faces are labled. If you do the ‘entities.add_group(face)’ command maybe a thousand times it takes at the end over 20 seconds.
I solved the problem. Instead of:
group = entities.add_group(face)
I used:
group = entities.add_group
group.entities.add_face(face.vertices.map{ |v| Geom::Point3d.new([v.position.x, v.position.y, v.position.z]) })
and deleted the face afterwards. This is much much faster.