Deleting duplicate components
-
Hi
I am try to find a way to locate duplicate component instances and then deleting all but 1 instance by just using ruby.
The defintion of duplicate is where two or more of the same component definition have the exactly the same origin.
Thanks for any suggestions. -
Hi Sabre, and welcome to this forum !
I think Com def has her proper origin... So how can you choose which comp instance will not be deleted ?
@unknownuser said:
The defintion of duplicate is where two or more of the same component definition have the exactly the same origin.
So you want to delete duplicate component definitions, right ? you want to make component definitions unique ? I don't understand... Sorry !
-
As you noticed I am new to the forum (thanks for the welcome) and I am having difficulites the sketchup terminology.
I will try explaining the problem in another way:
The user selects several components and builds the first part of a frame. He then copies these components and pastes them to expand the framework. Sometimes he overlaps the existing components with the pasted copy hence he has the two of the same components sharing the same origin. In order to accurately price up the framework I need to remove the duplicate component as in real life I cannot put more than one component in the same place.
Hope that helps. -
Hi,
So you want to remove all duplicate instances refering to the same component definition and that have the same transformation as well:def remove_instances_duplicates defs=Sketchup.active_model.definitions to_delete=[] defs.each do |compo_def| next if compo_def.internal? instances=compo_def.instances instances2=instances instances2.each do |i2| keeping_instance=instances.shift instances.each do |instance| if instance != keeping_instance and instance.transformation.to_a == keeping_instance.transformation.to_a and instance.definition.insertion_point.transform(instance.transformation)==keeping_instance.definition.insertion_point.transform(keeping_instance.transformation) to_delete.push(instance) puts "Erasing duplicate instance of " + instance.definition.name end end end end to_delete.each { |inst| inst.erase! } end end
I'm not sure it works as is but it is near it...
-
Many thanks Didier.
You have saved me a lot of time -
Good day
I know this is an old thread but this seems to be exactly what I'm looking for. I would like to ask, how can I use the script that Didier posted?
Thanks.
-
Pasting the Script in the Ruby console doesn't work. Is there another way?
-
@bjornburgh said:
Pasting the Script in the Ruby console doesn't work. Is there another way?
def remove_instances_duplicates defs=Sketchup.active_model.definitions to_delete=[] defs.each do |compo_def| next if compo_def.internal? instances=compo_def.instances instances2=instances instances2.each do |i2| keeping_instance=instances.shift instances.each do |instance| if instance != keeping_instance and instance.transformation.to_a == keeping_instance.transformation.to_a and instance.definition.insertion_point.transform(instance.transformation)==keeping_instance.definition.insertion_point.transform(keeping_instance.transformation) to_delete.push(instance) puts "Erasing duplicate instance of " + instance.definition.name end end end end to_delete.each { |inst| inst.erase! } end
There was an extra "end" in the code. Removing that should fix the problem.
After pasting into the Ruby Console, press Enter then type in "remove_instances_duplicates" to execute.
Advertisement