• Login
sketchucation logo sketchucation
  • Login
🤑 SketchPlus 1.3 | 44 Tools for $15 until June 20th Buy Now

Deleting duplicate components

Scheduled Pinned Locked Moved Developers' Forum
8 Posts 6 Posters 3.5k Views 6 Watching
Loading More Posts
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    sabre
    last edited by 28 Nov 2008, 16:32

    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.

    1 Reply Last reply Reply Quote 0
    • M Offline
      Matt666
      last edited by 28 Nov 2008, 16:38

      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 !

      Frenglish at its best !
      My scripts

      1 Reply Last reply Reply Quote 0
      • S Offline
        sabre
        last edited by 28 Nov 2008, 16:54

        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.

        1 Reply Last reply Reply Quote 0
        • D Offline
          Didier Bur
          last edited by 28 Nov 2008, 23:55

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

          DB

          1 Reply Last reply Reply Quote 0
          • S Offline
            sabre
            last edited by 29 Nov 2008, 15:59

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

            1 Reply Last reply Reply Quote 0
            • G Offline
              Gianfranco
              last edited by 6 Jun 2012, 13:31

              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 Reply Last reply Reply Quote 0
              • B Offline
                bjornburgh
                last edited by 7 Mar 2017, 09:23

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

                1 Reply Last reply Reply Quote 0
                • S Offline
                  sdmitch
                  last edited by 7 Mar 2017, 14:37

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

                  Nothing is worthless, it can always be used as a bad example.

                  http://sdmitch.blogspot.com/

                  1 Reply Last reply Reply Quote 0
                  • 1 / 1
                  • First post
                    Last post
                  Buy SketchPlus
                  Buy SUbD
                  Buy WrapR
                  Buy eBook
                  Buy Modelur
                  Buy Vertex Tools
                  Buy SketchCuisine
                  Buy FormFonts

                  Advertisement