Skip to content

Instantly share code, notes, and snippets.

@phnk
Created May 26, 2021 11:58
Show Gist options
  • Save phnk/43b43659448de7b6194d501c3f0dead4 to your computer and use it in GitHub Desktop.
Save phnk/43b43659448de7b6194d501c3f0dead4 to your computer and use it in GitHub Desktop.
calculate distance between two selected parts in freecad for urdf
import math
import numpy as np
# https://www.freecadweb.org/wiki/Code_snippets
sel = FreeCADGui.Selection.getSelection() # select object with getSelection()
boundBox_= sel[0].Shape.BoundBox # BoundBox of the object
boundBox_2 = sel[1].Shape.BoundBox
# https://www.freecadweb.org/wiki/Vector_API
v1 = FreeCAD.Vector(boundBox_.Center)
v2 = FreeCAD.Vector(boundBox_2.Center)
# Select parent -> child (urdf)
print("Distance between " + sel[0].FullName + " and " + sel[1].FullName + ": " + str(-0.001*(v1.x-v2.x)) + " " + str(-0.001*(v1.y-v2.y)) + " " + str(-0.001*(v1.z-v2.z)))
print("-"*100)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment