Skip to content

Instantly share code, notes, and snippets.

@thehans
Created November 30, 2011 05:34
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thehans/1408180 to your computer and use it in GitHub Desktop.
Save thehans/1408180 to your computer and use it in GitHub Desktop.
A FreeCAD script for the purpose of inspecting the radius and center of circles in 3D models
from FreeCAD import Base
class MeasureCircle:
"""This class will report the computed radius and center of a circle or arc given 3 vertices.
A line is drawn from the first vertex to the center of the circle, which may be used for future measurements.
Just select the vertices and the result will be shown in Report View
Edges may also be selected and count as two vertices.
If two edges are selected one of the vertices in the second edge is not used in the calculation"""
def __init__(self):
self.points = []
Gui.Selection.addObserver(self)
FreeCAD.Console.PrintMessage("Select any 3 vertices on a circle\n")
# don't allow duplicate points
def addPoint(self, v):
for p in self.points:
if p.Point == v.Point:
return
self.points.append(v)
def addSelection(self, doc, obj, sub, pos):
o = App.getDocument(doc).getObject(obj)
if sub != '' and type(o) == Part.Feature:
e = getattr(o.Shape, sub)
if e.ShapeType == 'Edge':
self.addPoint(e.Vertexes[0])
self.addPoint(e.Vertexes[1])
elif e.ShapeType == 'Vertex':
self.addPoint(e)
if len(self.points) >= 3:
Gui.Selection.removeObserver(self)
v1 = self.points[0].Point
v2 = self.points[1].Point
v3 = self.points[2].Point
curve = Part.Arc(v1, v2, v3).toShape().Curve
line = Part.makeLine(v1, curve.Center)
obj = FreeCAD.ActiveDocument.addObject("Part::Feature","Radius")
obj.Shape = line
FreeCAD.Console.PrintMessage("Radius: %f\n" % curve.Radius)
FreeCAD.Console.PrintMessage("Center: X=%f, Y=%f, Z=%f\n" % (curve.Center.x, curve.Center.y, curve.Center.z))
if __name__ == "__main__":
MeasureCircle()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment