Hi,
I am a newbie to SketchUp development. I am using SketchUP for some time, and have many years of programming experience in multiple languages.
I got a complex 3D model as a .stp file (STEP) with thousands of objects. I have used the SimLab Step Importer extension to import them into SketchUp. The model comes with about 500 groups in a hierarchy each has 10-20 components of geometry objects. The issue is the axis of the model, but also each of the thousands of objects is off, and that causes each of these objects to be rounded by a boxer in a weird direction, that doesn’t change, unless the object is exploded and regroup. Doing it manually is almost impossible due to the numbers. I was looking to write a Ruby code to do that.
I found a skeleton, which I modified to run recursively over the entire hierarchy of a selected object, but when it comes to creating the new group I am confused.
The basic concept is:
- Run down into each of the groups/components until you get to one that is just geometry.
- Move back to the parent and explode.
- Get the exploded children and regroup them into the parent-parent. I don’t need the components as they are all 1 instance components.
My issue is that entity doesn’t have add_group, only entities.
Anyone can help? To focus my issue, it’s in 3 lines of explode_deeply. Thanks in advance, Shai
require 'Sketchup'
module REGROUP
def self.container_type(entity)
result = 0
if(entity.is_a?(Sketchup::Group))
result = 3
elsif (entity.is_a?(Sketchup::ComponentInstance))
result = 2
end
if (result == 2)
definitionA = entity.definition
number = definitionA.count_used_instances
if (number > 1)
result = 1
end
end
return result
end
def self.is_geometry(entity)
return entity.is_a?(Sketchup::Edge) ||
entity.is_a?(Sketchup::Face)
end
def self.get_children(entity)
result = nil
if (entity.is_a?(Sketchup::Group))
result = entity.entities
elsif (entity.is_a?(Sketchup::ComponentInstance))
result = entity.definition.entities
end
return result
end
def self.explode_deeply(entity)
result = !(is_geometry(entity))
if (result)
children = get_children(entity)
if (children)
children.each do |child|
if explode_deeply(child)
nameA = child.name
type = container_type(child)
if (type >= 2) # if type is component with 1 instance of group
##!!!!! Here is teh code which is uncorrect, that explode and add to new group of the parent
members = child.explode
group = entity.add_group(members)
group.name = nameA
##!!!!!
end
end
end
end
end
return result
end
def self.regroup_selected_hierarchy
puts "Regroup Start1"
mod = Sketchup.active_model
sel = mod.selection
unless sel.empty?
sel.each do |entity|
puts "Testing: #{entity.name}"
if (container_type(entity) > 0)
result = explode_deeply(entity)
end
end
mod.commit_operation
end
puts "Regroup End1"
end
end
SKETCHUP_CONSOLE.show
SKETCHUP_CONSOLE.clear
date_a = DateTime.now()
puts "Regroup Start: #{date_a}"
REGROUP.regroup_selected_hierarchy()
puts "Regroup End"