I am having an issue with some definitions that I don’t understand, and hope you can help me explaining why this is happening. I want to extract the centroids of every instance in the model. This is my code:
model = Sketchup.active_model
coord_hash = {}
model.definitions.each do |cd|
name = cd.name
n_instances = cd.count_instances
n_used_instances = cd.count_used_instances
next if n_instances.zero?
cd.instances.each do |ci|
coord = ci.bounds.center
v = ci.visible?
coord_hash[ci.name] = coord
end
end
According to the API doc, count_used_instances counts the total number of component instances in a model using the current component definition, so theoretically, n_used_instances
must be always major than n_instances
.
Most of the cases are fine, but for some definitions this is not true. It seems that there are some instances for specific definitions that are counted with count_instances and not with count_used_instances. These definitions don’t appear in the Outliner list, and when I check the visibility in the code: v = true
. If I go to the 3D point, there is nothing there.
For example, in one iteration of the loop:
name = cd.name # name = '6520_Faucet_CH'
n_instances = cd.count_instances # n_instances = 1
n_used_instances = cd.count_used_instances # n_used_instances = 0
next if n_instances.zero?
cd.instances.each do |ci|
coord = ci.bounds.center # coord = (-0,001793", -0,027353", 4,271654")
v = ci.visible? # v = true
coord_hash[ci.name] = coord
end
Of course there is nothing in that 3D point (very close to origin, btw). It’s like a component has been imported and then erased, but somehow the info is still there, but not in the Outliner list or using the count_used_instances method.
FYI, I did not create the model so I don’t know where these unused definitions come from, but they shouldn’t be in the model.definitions
list.
Apart from that, I have another question. How to iterate through all instances of a definition in the full hierarchy? Shoudn’t be a method in the API called cd.used_instances or so?