Macro EdgesToArc
File:Text-x-python FaceToSketch |
Description |
---|
Replaces the selected Edges by a circular Arc if possible. Useful for restoring discretized arcs. Author: Jreinhardt |
Author |
Jreinhardt |
Download |
None |
Links |
Macros recipes How to install macros How to customize toolbars |
Macro Version |
1.0 |
Date last modified |
None |
FreeCAD Version(s) |
None |
Default shortcut |
None |
See also |
None |
Manchmal hat man mit einem Wire zu tun, bei dem ein Kreisbogen aus vielen kleinen geraden Segmenten aufgebaut ist. Dieser Fall tritt häufig auf, wenn man mit Dateien arbeitet, die mit anderen Programmen erstellt wurden. Mit diesem Macro kann man solche diskretisierten Kreisbögen in echte Kreisbögen umwandeln. Das führt zu kleineren und aufgeräumteren Dateien.
Um dieses Macro zu verwenden, muss man den Wire mit der E Draft Downgrade Funktion in seine einzelnen Segmente zerlegen. Dann markiert man die Segmente, die man durch einen Kreisbogen ersetzen will und führt das Macro aus. Man muss mindestens zwei Segmente markiert haben.
The macro will check whether the segments all lie on a common circle and will abort if this is not the case. Otherwise it will create the arc and remove the segments.
Because of small inaccuracies in the calculations, the Draft Upgrade function can sometimes fail to recombine the other edges and the arcs back into a wire. In this case the SuperWire macro provides a more robust way to do this.
import Draft
import FreeCADGui, FreeCAD
from FreeCAD import Base, Console
from math import atan2, pi, fabs
#This macro replaces a number of edges approximating a circular arc by a proper circular arc.
#It might be necessary to use the superwire macro to recombine the edges back to a wire, because of small errors in the calculations.
sel = FreeCADGui.Selection.getSelection()
if len(sel) < 2:
Console.PrintError("Too few edges are selected\n")
edges = [s.Shape for s in sel]
start_vertices = []
end_vertices = []
for edge in edges:
start_vertices.append(edge.Vertexes[0].Point)
end_vertices.append(edge.Vertexes[1].Point)
vertices = start_vertices + end_vertices
start,end,middle = None,None,None
#find start and end points
for edge in edges:
is_start = True
is_end = True
for point in end_vertices:
if edge.Vertexes[0].Point.distanceToPoint(point) < 1e-8:
is_start = False
for point in start_vertices:
if edge.Vertexes[1].Point.distanceToPoint(point) < 1e-8:
is_end = False
if is_start:
start = edge.Vertexes[0].Point
if is_end:
end = edge.Vertexes[1].Point
#find middle point, at least not too far away from the middle
for v in vertices:
ratio = v.distanceToPoint(start)/v.distanceToPoint(end)
if ratio > 0.5 and ratio < 2.:
middle = v
break
if middle is None:
Console.PrintError("Could not find suitable middle point\n")
arc = Part.ArcOfCircle(start,middle,end)
#Check circularity
circular = True
for v in vertices:
if fabs(v.distanceToPoint(arc.Center) - arc.Radius) > 1e-6:
Console.PrintError("Edges do not approximate a circular arc\n")
circular = False
break
if circular:
Part.show(arc.toShape())
for shape in sel:
FreeCAD.ActiveDocument.removeObject(shape.Name)