Question

I am making a plugin to sum up the area of all the material in a Sketch. I have succeeded in getting all the faces and such, but now the Components come into the picture.

Im using the term single or multi leveled component as i dont know any better way to explain the occurence of having a component inside an component and so on.

I have noticed that some components also have more to i than just 1 level. So if you go inside one component there may be components embedded inside this component that also have materials. So what i want is to sum up all of the material of a specific component and get all the "recursive" materials, if any, inside the component.

So, how do I count the area of all the material inside an component(single or multileveled)?

Was it helpful?

Solution

Here is what I would do, let's suppose that you loop through all entities and check for type of entity.

if entity.is_a? Sketchup::ComponentInstance
  entity.definition.entities.each {|ent|
    if ent.is_a? Sketchup::Face
      #here do what you have to do to add area to your total
    end
  }
end

You can do the same with a Group with:

if entity.is_a? Sketchup::Group
  entity.entities.each {|ent|
    if ent.is_a? Sketchup::Face
      #here do what you have to do to add area to your total
    end
  }
end

Hope it helps Ladislav

OTHER TIPS

Ladislav's example doesn't dig into all the levels.

For that you need a recursive method:

def sum_area( material, entities, tr = Geom::Transformation.new )
  area = 0.0
  for entity in entities
    if entity.is_a?( Sketchup::Group )
      area += sum_area( material, entity.entities, tr * entity.transformation )
    elsif entity.is_a?( Sketchup::ComponentInstance )
      area += sum_area( material, entity.definition.entities, tr * entity.transformation )
    elsif entity.is_a?( Sketchup::Face ) && entity.material == material
      # (!) The area returned is the unscaled area of the definition.
      #     Use the combined transformation to calculate the correct area.
      #     (Sorry, I don't remember from the top of my head how one does that.)
      #
      # (!) Also not that this only takes into account materials on the front
      #     of faces. You must decide if you want to take into account the back
      #     size as well.
      area += entity.area
    end
  end
  area
end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top