Ruby definition list

Hello,

Can someone tell me what’s wrong?
Because the result gives all the instances …

mod.options['UnitsOptions']['LengthUnit'] = 0
ents = mod.definitions.select{ |definition| definition.count_instances > 0 }
savepath = modpath.chomp('.skp')+'.txt'
if savepath != nil
    File.open(savepath, 'w+') do |f|
        lister_pieces(ents, f)
    end
    system %{open '#{savepath}'}
end

ents = mod.definitions.select{ |definition| definition.count_instances > 0 }

Why this line of code lists the items that are not in my project. Is not it supposed to list only elements with at least 1 instance?

TY

typo - it’s:

mod.path

Consider the differences between:

definition.count_instances
and
definition.count_used_instances
and
definition.instances
and
definition.instances.length

And remember that definitions can have instances within their entities that do not necessarily appear in the model at all…

Thanks TIG!

definition.count_used_instance

Was the command I need. But it’s for Sketchup 2016+
I have Sketchup 2015

Is there another way to?

Consider:
definition.instances.length
to find if a definition has any instances - i.e. ==[0]

If so then you need to see if these instances are ‘used’ in the model, rather than nested inside other definitions…
You can test for each problematical instance to see its ‘.parent
if it’s another definition you might wish to skip it ??

1 Like

Thank you TIG! :handshake:
It’s so easy when you know.

So here is the modification and it work :

ents = mod.definitions.select{ |definition| definition.instances.length > 0 }

This lists only the components that are in the model.

It can also list definitions whose instances are within UNUSED definitions.

This will prevent that …

cdefs = mod.definitions.select{ |definition| 
  definition.count_used_instances > 0
}
1 Like

TY DanRathbun, for the info, I will use your solution for Sketchup 2016+

1 Like