Deleting duplicate components

Deleting duplicate components

Postby sabre » Fri Nov 28, 2008 5:32 pm

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.
0

sabre 
 

Re: Deleting duplicate components

Postby Matt666 » Fri Nov 28, 2008 5:38 pm

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 ?
;)
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 !
0
Frenglish at its best !
My scripts

Matt666 
 

Re: Deleting duplicate components

Postby sabre » Fri Nov 28, 2008 5:54 pm

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.
0

sabre 
 

Re: Deleting duplicate components

Postby Didier Bur » Sat Nov 29, 2008 12:55 am

Hi,
So you want to remove all duplicate instances refering to the same component definition and that have the same transformation as well:

Code: Select all
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...
0
Didier Bur (Bytes Farmer)
Ecole Nationale Supérieure d'Architecture de Nancy (F)
http://rld.crai.archi.fr/rubylibrarydepot/
User avatar
Didier Bur 
 

Re: Deleting duplicate components

Postby sabre » Sat Nov 29, 2008 4:59 pm

Many thanks Didier.
You have saved me a lot of time :sketchstatic:
0

sabre 
 

Re: Deleting duplicate components

Postby Gianfranco » Wed Jun 06, 2012 2:31 pm

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.
1

Gianfranco 
 

Re: Deleting duplicate components

Postby bjornburgh » Tue Mar 07, 2017 10:23 am

Pasting the Script in the Ruby console doesn't work. Is there another way?
0

bjornburgh 
 

Re: Deleting duplicate components

Postby sdmitch » Tue Mar 07, 2017 3:37 pm

bjornburgh wrote:Pasting the Script in the Ruby console doesn't work. Is there another way?


Code: Select all
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.
0
Nothing is worthless, it can always be used as a bad example.

http://sdmitch.blogspot.com/
User avatar
sdmitch 
PluginStore Author
PluginStore Author
 

SketchUcation One-Liner Adverts

by Ad Machine » 5 minutes ago



Ad Machine 
Robot
 



 

Return to Developers' Forum

Who is online

Users browsing this forum: Architure, Google Bot, kyu seon and 6 guests

Visit our sponsors: