Script to remove a small bit from the middle of edges

Hi! I’m looking to write a script that will remove a small segment in the middle of an edge. I use SketchUp to design for a laser cutter and I have some a lot of design I want to cut up but leave the pieces connected just barely. I’ve been needing to create a joining segment on an edge, copy-move the segment 0.2 mm, and then erase both segments and the middle. This gets tiring for a design with a lot of pieces.

Ideally, I’d like a script that takes all edges over a certain length (say, 20mm), divides them into three with the middle piece at a set length (say 0.2mm) and deletes the middle, or some variation.

I’m a beginning at ruby scripting and been poking around other scripts and tutorials. If anyone could share some tips or code snips, I’d really appreciate it!

Thank you!

You can start with something like the below.It will add a gap to selected edges according to the parameters in the main method.

module MiddleSegmentRemover

	def self.mid_segment(pt0, pt1, seg_length) 
		d = pt0.distance(pt1)
		ve = (pt1 - pt0)
		return [pt0.offset(ve, 0.5 * (d - seg_length)),
				pt0.offset(ve, 0.5 * (d + seg_length))]			
	end

	def self.get_edges(ents, threshold)
		return ents.grep(Sketchup::Edge).select { |e| e.length >= threshold }
	end

	def self.divide(e, gap)
		segement = mid_segment(e.start.position, e.end.position, gap)
		e = e.parent.entities.add_line(segement[0], segement[1])
		e.erase!
	end

	def self.main
		mod = Sketchup.active_model
		sel = mod.selection

		#### PARAMETERS ####
		threshold = 10.mm
		gap = 2.mm
		####################

		mod.start_operation("Gapper", true)
		get_edges(sel, threshold).each { |e| divide(e, gap) }
		mod.commit_operation
	end

	main

end
1 Like

That works perfectly. Thank you so much – you’re awesome! I will study this and learn.

It is assumed that any example module posted needs to be wrapped within a unique author or company namespace module.