Force rounding current model dimensions to nearest 1mm

Using Sketchup Pro 2017. I have two questions:

  1. My current model has a lot of ~ in it (~12mm, ~1000mm, etc) . I’ve already enabled length snapping to nearest 5mm in Model Info, but the current model still has a lot of ~ (possibly due to careless modeling before). Is there any way to just force everything to the nearest 1mm? (or even 5mm) I’m modeling a house, I don’t need measurements below 1mm. And the ~ is creating a lot of edge joining problems in my current model.

  2. The “enable length snapping” works great when drawing shapes, lines, etc. But it doesn’t work on a few other tools like ‘offset’ edges. When you draw offsets, you still get ~ and it simply does not snap to the nearest 5mm that I want. Anyway to force snap on offsets?

Thanks.

Length snapping won’t fix the errors in your modeling. You can mask them by changing the Precision to 0mm. That will make dimensions show without anything beyond the decimal.

Thank you. I do have the precision set to 0mm, but there are still a lot of ~ in my current model, creating edge joining problems.

That’s the best you can do without fixing the bad dimensions. There’s no automatic fix. Basically it’s a case of GIGO.

1 Like

I see. It’s gonna be a nightmare trying to fix it all. Guess I’ll just have to cope with it, or start a fresh model. Thanks Dave.

Maybe you can make the next model with more precision.

Ya, though sometimes, like the offset tool, it still creates measurements with ~ in front. I guess you just have to type in the numbers in those situations. Hopefully this area can be improved in future sketchups, the ~ is a pain.

Try this, paste as fix.rb into your plugins folder…
Open your model, right click and select ‘Other Tools’ → ‘Fix’
It moves every point to the nearest mm, save your model first!!
Also it would pay to run ‘model info’ → ‘fix problems’ afterwards, in case of merged geometry.

require ‘sketchup.rb’

class Fix

def Fix::MovePoint(entities, point, diff)
movement = Geom::Point3d.new diff[0],diff[1],diff[2]
transform = Geom::Transformation.new movement
entities.transform_entities transform, point
end

def Fix::RoundToMm(v)
v = v * 25.4 # convert mm to inches
v = v.round(0) # round to nearest
v = v / 25.4 # convert back to mm
end

def Fix::RoundPoint(p)
x = p.position.x
y = p.position.y
z = p.position.z
pNew = [RoundToMm(x),RoundToMm(y),RoundToMm(z)]
pNew[0] = pNew[0] - p.position.x
pNew[1] = pNew[1] - p.position.y
pNew[2] = pNew[2] - p.position.z
return pNew
end

def Fix::Move(entities, p1,p2)
p3 = RoundPoint(p1)
p4 = RoundPoint(p2)
MovePoint(entities, p1,p3)
MovePoint(entities, p2,p4)
end

def Fix::FixGeometry(entities)
entities.each { |e|
if(e.class == Sketchup::Group)
FixGeometry(e.entities)
end
if(e.class == Sketchup::ComponentInstance)
FixGeometry(e.definition.entities)
end
if(e.class == Sketchup::Edge)
p1 = e.vertices[0]
p2 = e.vertices[1]
Move(entities, p1,p2)
end
}
end

def Fix::Execute()
Sketchup.active_model.start_operation “Fix”,true ,false ,false
FixGeometry(Sketchup.active_model.entities)
Sketchup.active_model.commit_operation
Sketchup.active_model.active_view.invalidate
end
end # Class Fix
if(not file_loaded?(“Fix.rb”))
UI.add_context_menu_handler do |menu|
tools = menu.add_submenu(“Other Tools”)
tools.add_item(“Fix”) { Fix.Execute() }
end
end
file_loaded(“Fix.rb”)

Edit: I don’t know why the code has been split into badly formatted sections.

Thank you. I’m new to this script/codes. Had to do some learning just then.

I have the sketchup.rb in the ‘tools’ folder.

I copied and pasted the codes into a text document and changed the document name from fix.txt to fix.rb (hope that was right), and placed it in plugins folder. But when I open the model, it came up with an error window:

Error Loading File fix.rb
Error: #<NameError: uninitialized constant Fix>
C:/Users/Kevin/AppData/Roaming/SketchUp/SketchUp 2017/SketchUp/Plugins/fix.rb:1:in `<top (required)>’

Did something go wrong during the paste?

Also, where do I right click to select ‘other tools’ > ‘fix’ ?

Everything you did was correct, so it might be a copy/paste error.
I copied the same code back in and it still runs.

Make sure the code starts with

require ‘sketchup.rb’

and ends with

file_loaded(“Fix.rb”)

and everything in between!

Right-click on any object in your model to bring up the context menu, then look for ‘Other Tools’ added in there.

require 'sketchup.rb'

class Fix

	def Fix::MovePoint(entities, point, diff)
		movement = Geom::Point3d.new diff[0],diff[1],diff[2]
		transform = Geom::Transformation.new movement
		entities.transform_entities transform, point
	end

	def Fix::RoundToMm(v)
		v = v * 25.4	# convert mm to inches
		v = v.round(0)	# round to nearest
		v = v / 25.4	# convert back to mm
	end

	def Fix::RoundPoint(p)
		x = p.position.x
		y = p.position.y
		z = p.position.z
		pNew = [RoundToMm(x),RoundToMm(y),RoundToMm(z)]
		pNew[0] = pNew[0] - p.position.x
		pNew[1] = pNew[1] - p.position.y
		pNew[2] = pNew[2] - p.position.z
		return pNew
	end

	def Fix::Move(entities, p1,p2)
		p3 = RoundPoint(p1)
		p4 = RoundPoint(p2)
		MovePoint(entities, p1,p3)
		MovePoint(entities, p2,p4)
	end

	def Fix::FixGeometry(entities)
		entities.each { |e|
			if(e.class == Sketchup::Group)
				FixGeometry(e.entities)
			end
			if(e.class == Sketchup::ComponentInstance)
				FixGeometry(e.definition.entities)
			end
			if(e.class == Sketchup::Edge)
				p1 = e.vertices[0]
				p2 = e.vertices[1]
				Move(entities, p1,p2)
			end
		}
	end

	def Fix::Execute()
		Sketchup.active_model.start_operation "Fix",true ,false ,false
		FixGeometry(Sketchup.active_model.entities)
		Sketchup.active_model.commit_operation
		Sketchup.active_model.active_view.invalidate
	end
end

if(not file_loaded?("Fix.rb"))
	UI.add_context_menu_handler do |menu|
	tools = menu.add_submenu("Other Tools")
	tools.add_item("Fix") { Fix.Execute() }
	end
end
file_loaded("Fix.rb")

Ahh, I see, didn’t paste ‘require sketchup.rb’ part. Now there’s no error.

It’s still working on my current model (taking ages to calculate whatever, after clicking ‘other tools’ > ‘fix’). But I made a new rectangular plane with ~ measurements, and it works! The ~ are gone, with the exact measurements in mm. However, a simple rectangular plane took about 30sec. And my current model is still “Not Responding”. Guess I’ll come back in an hour and see if it’s finished whatever it’s doing. Thanks!

It might need some optimization for a larger model, I only quickly tested it on a 3MB model to test that it worked (and it finished in 3 seconds, so maybe I have a faster laptop :slight_smile: )
If your model has many component instances then it is going to repeat each one, rather than just do the single component.

This script should finish a lot quicker in that case (Tried it with a 60MB model and it finished in under 30 seconds).

require 'sketchup.rb'

class Fix

	def Fix::MovePoint(entities, point, diff)
		movement = Geom::Point3d.new diff[0],diff[1],diff[2]
		transform = Geom::Transformation.new movement
		entities.transform_entities transform, point
	end

	def Fix::RoundToMm(v)
		v = v * 25.4	# convert mm to inches
		v = v.round(0)	# round to nearest
		v = v / 25.4	# convert back to mm
	end

	def Fix::RoundPoint(p)
		x = p.position.x
		y = p.position.y
		z = p.position.z
		pNew = [RoundToMm(x),RoundToMm(y),RoundToMm(z)]
		pNew[0] = pNew[0] - p.position.x
		pNew[1] = pNew[1] - p.position.y
		pNew[2] = pNew[2] - p.position.z
		return pNew
	end

	def Fix::Move(entities, p1,p2)
		p3 = RoundPoint(p1)
		p4 = RoundPoint(p2)
		MovePoint(entities, p1,p3)
		MovePoint(entities, p2,p4)
	end

	def Fix::FixGeometry(entities)
		entities.each { |e|
			if(e.class == Sketchup::Group)
				FixGeometry(e.entities)
			end
			if(e.class == Sketchup::Edge)
				p1 = e.vertices[0]
				p2 = e.vertices[1]
				Move(entities, p1,p2)
			end
		}
	end

	def Fix::Execute()
		Sketchup.active_model.start_operation "Fix",true ,false ,false
		Sketchup.active_model.definitions.each { |component|
			FixGeometry(component.entities)
		}
		FixGeometry(Sketchup.active_model.entities)
		Sketchup.active_model.commit_operation
		Sketchup.active_model.active_view.invalidate
	end
end

if(not file_loaded?("Fix.rb"))
	UI.add_context_menu_handler do |menu|
	tools = menu.add_submenu("Other Tools")
	tools.add_item("Fix") { Fix.Execute() }
	end
end
file_loaded("Fix.rb")
1 Like

I came back to the model hours later and sketchup crashed, lol. It was probably too big, over 200mb. So I copy and pasted just the parts I needed, and it was done within a minute (your new script only took a few seconds). Apparently it fixes everything in the .skp model, not just the particular objects you select.

There were a lot of diagonal lines on my model after the fix. But I deleted them without trouble.

This is a great fix, all ~ gone without any issues.

Thanks Centaur!

P.S: I did realize that after the fix, any face-me 2d images turn into a crazy picasso drawing :slight_smile: But it’s not a problem, object fixes can simply be done in a new file then pasted back.

Any circle, arc, or curve is also going to be wrecked by that code because the points along the curves are rarely nice, neat numbers. Maybe the code could be enhanced to ignore curves.

@jim_foltz,
Yes, any curves will be wrecked! But even more so if the edges are joined into a curve, because then SketchUp moves every connected edge during the one operation, so it pays to explode all curves before attempting this. Ignoring curves could be done but only if they are not exploded.

Another simple enhancement could be to only work on the selected object or component. If others find this useful I can easily do that, but I doubt this will see a regular need.

Also, it leaves the model in a state of disrepair, so you must run ‘Fix Problems’ to remove the overlapping vertexes.

Test.repair_model

john

Hmm… discovered an issue… After the fix, there are some triangles on some rectangular planes, hidden, as there are no diagonal lines running between them. Deleting them and redrawing the rectangular surface just brings the hidden triangles back again.

I went to ‘windows’ > ‘model info’ > ‘statistics’ > ‘fix problems’ and it did not fix the problem.

Any ideas?

It sounds like in the process of “fixing” your bad dimensions, end points that were co-planar, aren’t co-planar anymore. The triangles are thus required so that faces form. They don’t have to be non-co-planar by very much.

From reading through this thread and seeing the problems created in the “fix”, it’s probably obvious that it’s better, faster, and easier to draw with precision from the beginning.

:grin:

I would agree with DaveR, that does sound like the problem.

You could try changing both instances of 25.4 to 2.54 in the script, which will round everything to 10mm, but it might simply make the model worse.
:rolling_eyes: