Extract geometrical information from a .skp file

  1. Exploding a whole model is not ideal, and for reading information it’s certainly should not be needed. But in order to provide information to what to do instead we need some more information to what the task here is.
    I’m going to make a guess to what is going on, are you using model.entities to read the entities of the model? If so, then note that you only get the root level entities via that. In order to traverse the whole model you need to look for groups and components and recursively dig into them.

Something like this:

module Example

  def self.do_something
    model = Sketchup.active_model
    self.read_entities(model.entities)
  end

  def self.read_entities(entities, transformation = IDENTITY)
    entities.each { |entity|
      case entity
      when Sketchup::Group, Sketchup::ComponentInstance
        # Note: Older versions of the SketchUp API did not have .definition for
        # groups.
        definition = entity.definition
        # When you read the positions of vertices inside component definitions
        # they are local to the definition. In order to get the global
        # coordinates for each vertex in each instance you need to combine
        # the transformation to the instance path.
        tr = entity.transformation * transformation
        # Recurse into child-instances.
        self.read_entities(definition.entities, tr)
      else
        # All other entities...
      end
    }
  end

end
  1. The Ruby API is single process only. You can only call it from the main thread. Explode is in itself very slow. That along with it being a destructive operation makes it something you want to avoid when you really just need to read data from the model. Traversing the model without modifying it will be the fastest you get.
2 Likes