sketchucation logo sketchucation
    • Login
    Oops, your profile's looking a bit empty! To help us tailor your experience, please fill in key details like your SketchUp version, skill level, operating system, and more. Update and save your info on your profile page today!
    πŸ«› Lightbeans Update | Metallic and Roughness auto-applied in SketchUp 2025+ Download

    Check if component exists before making it

    Scheduled Pinned Locked Moved Developers' Forum
    6 Posts 2 Posters 361 Views 2 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.
    • M Offline
      MCorgano
      last edited by

      I have a script that rasterizes a line into cubes. My problem is, I want to make sure it doesn't generate duplicate cubes. How do I do that?

      require 'sketchup.rb'
      require 'extensions.rb'
      
      cCube = Sketchup.active_model.definitions.add("Cube") ;
      
      newface = cCube.entities.add_face( [ [0.5, -0.5, 0.5], [0.5, -0.5, -0.5], [-0.5, -0.5, -0.5], [-0.5, -0.5, 0.5] ] ) ;
      newface.reverse! if newface.normal.z < 0 ;
      newface.pushpull(1) ; # extrude it up to make a cube
      
      Sketchup.active_model.selection.each { |entity|
      	x = entity.start.position.x
      	y = entity.start.position.y
      	z = entity.start.position.z
      	l = entity.length
      	dx = (x-entity.end.position.x)/l
      	dy = (y-entity.end.position.y)/l
      	dz = (z-entity.end.position.z)/l
      	for i in 0..l.to_int
      		cx = (x - (dx*i)).round
      		cy = (y - (dy*i)).round
      		cz = (z - (dz*i)).round
      		# a bit of code to make sure a cCube doesnt exist here already
      		Sketchup.active_model.active_entities.add_instance(cCube, Geom;;Transformation.new([cx,cy,cz]))
      	end
      }
      
      1 Reply Last reply Reply Quote 0
      • TIGT Offline
        TIG Moderator
        last edited by

        name="Cube" name=Sketchup.active_model.definitions.unique_name(name)

        if 'Cube' is already taken it becomes 'Cube#1'

        if 'Cube#1' is already taken it becomes 'Cube#2' etc...

        Sketchup.active_model.definitions.add(name)

        TIG

        1 Reply Last reply Reply Quote 0
        • M Offline
          MCorgano
          last edited by

          @tig said:

          name="Cube" name=Sketchup.active_model.definitions.unique_name(name)

          if 'Cube' is already taken it becomes 'Cube#1'

          if 'Cube#1' is already taken it becomes 'Cube#2' etc...

          Sketchup.active_model.definitions.add(name)

          That solves a different question I had (lol) but not the one I'm asking here.
          Let's take point [1, 2, 3]. I want to add an instance of cCube at that point
          Sketchup.active_model.active_entities.add_instance(cCube, Geom::Transformation.new( [1, 2, 3] ))
          What I want to know, is how do I check that that instance of cCube does not already exist?
          How do I prevent creating duplicate instances of a component?
          If there is not a cCube at [1, 2, 3] then{ Sketchup.active_model.active_entities.add_instance(cCube, Geom::Transformation.new( [1, 2, 3] )) }

          1 Reply Last reply Reply Quote 0
          • TIGT Offline
            TIG Moderator
            last edited by

            point=Geom;;Point3d.new(1,2,3
            )### or whatever 'point' you are testing for
            exists=false
            compo=Sketchup.active_model.definitions['Cube']
            compo.instances.each{|ins|
              if ins.transformation.origin==point
                exists=true
                break
              end
            }
            if exists
              puts 'That Cube exists already!'
            else
              ### do your stuff to add a new instance of compo at point
              Sketchup.active_model.active_entities.add_instance(cCube, Geom;;Transformation.new( [1, 2, 3] )
            end
            

            TIG

            1 Reply Last reply Reply Quote 0
            • M Offline
              MCorgano
              last edited by

              @tig said:

              point=Geom;;Point3d.new(1,2,3
              > )### or whatever 'point' you are testing for
              > exists=false
              > compo=Sketchup.active_model.definitions['Cube']
              > compo.instances.each{|ins|
              >   if ins.transformation.origin==point
              >     exists=true
              >     break
              >   end
              > }
              > if exists
              >   puts 'That Cube exists already!'
              > else
              >   ### do your stuff to add a new instance of compo at point
              >   Sketchup.active_model.active_entities.add_instance(cCube, Geom;;Transformation.new( [1, 2, 3] )
              > end
              

              How will this effect performance if I had hundreds of them? I figured something like this must be possible, but as the number of cubes grows, this would become less efficient. I'm talking on the scale of ~2000 cubes. Is this method efficient enough for the speed to be negligible?

              1 Reply Last reply Reply Quote 0
              • TIGT Offline
                TIG Moderator
                last edited by

                At best it will find a match immediately 1/2000, at worst at go 2000/2000, on average at go 1000/2000.
                You can but test it and see...
                If you are adding all cube-instances in the same SketchUp session then why not make a @points array to push [ <<] all of the points you will use during the placements - these need to be as arrays so they are 'comparable' [use point.to_a] - points are comparable as == but include? is faster [most probably] with array comparisons ?
                Then... instead of finding all instances and comparing each 'instance.origin' to 'point' use:
                @points.include?(point.to_a)
                If the tested point is in the current list then it's taken and you skip the operation...
                Of course this doesn't work when instances have been erased or across SketchUp sessions πŸ˜•

                TIG

                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