sketchucation logo sketchucation
    • Login
    1. Home
    2. MCorgano
    3. Posts
    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
    M
    Offline
    • Profile
    • Following 0
    • Followers 0
    • Topics 1
    • Posts 3
    • Groups 1

    Posts

    Recent Best Controversial
    • RE: Check if component exists before making it

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

      posted in Developers' Forum
      M
      MCorgano
    • RE: Check if component exists before making it

      @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] )) }

      posted in Developers' Forum
      M
      MCorgano
    • Check if component exists before making it

      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
      }
      
      posted in Developers' Forum
      M
      MCorgano
    • 1 / 1