Skip to content

Instantly share code, notes, and snippets.

@5263
Last active August 29, 2015 13:56
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save 5263/8847217 to your computer and use it in GitHub Desktop.
FreeCAD Macro to convert a wire into a piecevise Bezier curve
import FreeCAD
def segments2points(segments,degree):
pts=segments[0].getPoles()[0:1]
for seg in segments:
if seg.Degree < degree:
seg.Increase(degree)
pts.extend(seg.getPoles()[1:])
return pts
def partwire2draftbezcurve(wire,degree=3,label=None):
points=[wire.Vertexes[0].Point]
for edge in wire.Edges:
curve=edge.Curve
if not isinstance(curve,Part.BezierCurve) or curve.Degree>degree:
print 'approimate'
if not isinstance(curve,Part.BSplineCurve):
curve=curve.toBSpline()
if curve.Degree<degree:
curve.increaseDegree(degree)
if curve.Degree==degree:
seglst = curve.toBezier()
else:
seglst = curve.approximateBSpline(0.05,20, 3,'C0').toBezier()
points.extend(segments2points( seglst,degree)[1:])
else:
print 'isbezier',curve.Degree
if curve.Degree<degree:
curve.Increase(degree)
if curve.Degree==degree:
points.extend(curve.getPoles()[1:])
else:
raise ValueError
if (len(points) > 3) and (points[0]==points[-1]):
points.pop()
closed=True
else:
closed=False
import Draft
do=Draft.makeBezCurve(points,closed=closed,support=None,Degree=degree)
if label:
do.Label='Bez%s'%label
if __name__ == '__main__':
import FreeCADGui
for obj in FreeCADGui.Selection.getSelection():
wires = obj.Shape.Wires if obj.Shape.ShapeType != 'Edge' else [PartEdge(obj.Shape)]
for wire in wires:
partwire2draftbezcurve(wire,label=obj.Label)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment