How to improve performance?

Hello everyone, I am developping an extension, the models are so big, each of them contains so many geometry objects, and I meet some performance issues.
(1) I need explode all the groups and components, the codes as below:

  def self.explodeComponentInstance(entList) 
    entList.each{|ent|
      next unless ent.valid?
      if [Sketchup::Group, Sketchup::ComponentInstance].include?(ent.class)
        explodeComponentInstance(ent.explode) 
      end
    }
  end

The models have hundreds of groups or component instances, and the groups have sub groups etc. So when I explode all of them, it’s so slow~~~~~~

(2) With the same models I need move one group using API “transform”, and it’s also slow~~~~ the code as below:

def changePosition2(posDir, unitOfPosition)
  uop = unitOfPosition.cm
  case posDir
  when 1 #left
    tr = Geom::Transformation.new([-uop, 0, 0])
  when 2 #right
    tr = Geom::Transformation.new([uop, 0, 0])
  when 3 #down
    tr = Geom::Transformation.new([0, -uop, 0])
  when 4 #up
    tr = Geom::Transformation.new([0, uop, 0])
  when 5 #down2
    tr = Geom::Transformation.new([0, 0, -uop])
  when 6 #up2
    tr = Geom::Transformation.new([0, 0, uop])
  end
  Sketchup.active_model.start_operation("ChangePosition", true)
  @faceGroup.transform!(tr)
  Sketchup.active_model.commit_operation
end

I think the problem is not the codes but collision. I wonder if it has a better solution.

The method name to check inclusion in a collection is #include?, not includes.

slow, not slowly.

Do you wrap the first call to explodeComponentInstance() within an undo operation with the disable UI flag set true ?

def self.explode_everything
  model = Sketchup.active_model
  model.start_operation('Explode everything', true)
    #
    explodeComponentInstance(model.entities)
    #
  model.commit_operation
end

This is a recursive method call which might error out if the stack level gets too deep on a machine with low memory available.


An alternative is to instead iterate the model’s DefinitionList collection and if each member is not an Image definition, then if it’s #count_used_instances is > 0 then explode all the definition’s instances.

def self.explode_all_used_objects
  model = Sketchup.active_model
  model.start_operation('Explode everything', true)
    #
    deflist = model.definitions.to_a
    deflist.each do |definition|
      next if definition.image?
      if definition.count_used_instances > 0
        definition.instances.each { |inst| inst.explode }
      end
    end
    #
  model.commit_operation
end

(2) I cannot think of any way to speed up the group move.

I would perhaps pass the group into the method as an argument.

Groups and Component instances do not collide.

That is their whole purpose to separate geometry, so it does not collide.


Are you still using SU 2020 as your profile says ?

Thanks Dan.

The method name to check inclusion in a collection is #include?, not includes .
slow, not slowly.

I’m sorry for my typo and poor English.

Do you wrap the first call to explodeComponentInstance() within an undo operation with the disable UI flag set true ?

Yes.

An alternative is to instead iterate the model’s DefinitionList collection and if each member is not an Image definition, then if it’s #count_used_instances is > 0 then explode all the definition’s instances.

I’ll try to use your suggestion. Thanks again !

Groups and Component instances do not collide.

That is their whole purpose to separate geometry, so it does not collide.

The operation is:
(1) User selects the translucent cube(it’s a group) in the model.
(2) User clicks a button with direction sign to move the group.
The method “changePosition2” runs fast, but the group moves slowly. I think It is executed asynchronously.
Sorry, I can’t upload this model for commercial reasons.

Are you still using SU 2020 as your profile says ?

I’m using SU Pro 2022.

Yes, many methods are asynchronous in the API.

Are you using a fast style ?


If you do not need to move the group back you could use #move! instead of #transform!

faceGroup.move!( faceGroup.transformation.origin.transform(tr) )

Are you using a fast style ?

I don’t know how to use Fast Style or how to switch on it.

If you do not need to move the group back you could use #move! instead of #transform!

faceGroup.move!( faceGroup.transformation.origin.transform(tr) )

Users need ‘undo’ and ‘redo’ actions. So I can’t use #move! .

See the Help Center article:

Speeding Up Rendering with Fast Styles | SketchUp Help

See the Help Center article:

Speeding Up Rendering with Fast Styles | SketchUp Help

Thanks. I will learn and try.