135 lines
6.3 KiB
HTML
135 lines
6.3 KiB
HTML
<html><head><title>Macro EdgesToArc</title><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><link type='text/css' href='wiki.css' rel='stylesheet'></head><body><h1>Macro EdgesToArc</h1></div>
|
|
|
|
<div id="mw-content-text" lang="en" dir="ltr" class="mw-content-ltr"><div class="mw-parser-output"><table class="fcinfobox wikitable ct" width="100%" style="float: right; width: 230px; margin-left: 10px;">
|
|
<tr>
|
|
<td class="ctTitle">
|
|
<h3><span class="mw-headline" id="EdgesToArc"><a href="https://www.freecadweb.org/wiki/index.php?title=File:Text-x-python.png" class="image"><img alt="Text-x-python.png" src="32px-Text-x-python.png" width="32" height="32" srcset="/wiki/images/2/2c/Text-x-python.png 1.5x" /></a> EdgesToArc</span></h3>
|
|
</td></tr>
|
|
<tr>
|
|
<th class="ctOdd">Description
|
|
</th></tr>
|
|
<tr>
|
|
<td class="ctEven left macro-description">Replaces the selected Edges by a circular Arc if possible. Useful for restoring discretized arcs.
|
|
</td></tr>
|
|
<tr>
|
|
<th class="ctOdd">Author
|
|
</th></tr>
|
|
<tr>
|
|
<td class="ctEven macro-author"><a href="https://www.freecadweb.org/wiki/index.php?title=User:Jreinhardt&action=edit&redlink=1" class="new" title="User:Jreinhardt (page does not exist)">Jreinhardt</a>
|
|
</td></tr>
|
|
<tr>
|
|
<th class="ctOdd">Links
|
|
</th></tr>
|
|
<tr>
|
|
<td class="ctEven"><a href="Macros_recipes.html" title="Macros recipes">Macros recipes</a><br /><a href="How_to_install_macros.html" title="How to install macros">How to install macros</a><br /><a href="Customize_Toolbars.html" title="Customize Toolbars">How to customize toolbars</a>
|
|
</td></tr>
|
|
<tr>
|
|
<th class="ctOdd">Version
|
|
</th></tr>
|
|
<tr>
|
|
<td class="ctEven macro-version">1.0
|
|
</td></tr>
|
|
<tr>
|
|
<th class="ctOdd">Date last modification
|
|
</th></tr>
|
|
<tr>
|
|
<td class="ctEven macro-date">2014-01-02
|
|
</td></tr>
|
|
<tr>
|
|
<th class="ctOdd">
|
|
</th></tr>
|
|
<tr>
|
|
<td class="ctToc"><br /><div id="toc" class="toc"><div class="toctitle"><h2>Contents</h2></div>
|
|
<ul>
|
|
<li class="toclevel-1"><a href="#EdgesToArc"><span class="tocnumber">1</span> <span class="toctext">EdgesToArc</span></a></li>
|
|
</ul>
|
|
</div>
|
|
|
|
</td></tr>
|
|
</table>
|
|
<p><br />
|
|
</p><p>Sometimes one encounters wires that contain arcs that are made up of small straight segments. This often happens when working with files from other programs. This macro makes it relatively easy to convert these discretized arcs back to circular arcs. This reduces the file size and makes the file more manageable.
|
|
</p><p>To use this macro, you have to break down the wire into individual edges using the <span style="background: #DDDDDD; border: 1px solid #888888; padding: 0px 5px 1px 5px;"><a href="https://www.freecadweb.org/wiki/index.php?title=File:Draft_Downgrade.png" class="image"><img alt="Draft Downgrade.png" src="16px-Draft_Downgrade.png" width="16" height="16" srcset="/wiki/images/thumb/8/86/Draft_Downgrade.png/24px-Draft_Downgrade.png 1.5x, /wiki/images/thumb/8/86/Draft_Downgrade.png/32px-Draft_Downgrade.png 2x" /></a> <a href="Draft_Downgrade.html" title="Draft Downgrade">Draft Downgrade</a></span> function. Then just select the segments that you want to replace by an circular arc and execute the macro. You need at least two segments.
|
|
</p><p>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.
|
|
</p><p>Because of small inaccuracies in the calculations, the <span style="background: #DDDDDD; border: 1px solid #888888; padding: 0px 5px 1px 5px;"><a href="https://www.freecadweb.org/wiki/index.php?title=File:Draft_Upgrade.png" class="image"><img alt="Draft Upgrade.png" src="16px-Draft_Upgrade.png" width="16" height="16" srcset="/wiki/images/thumb/b/be/Draft_Upgrade.png/24px-Draft_Upgrade.png 1.5x, /wiki/images/thumb/b/be/Draft_Upgrade.png/32px-Draft_Upgrade.png 2x" /></a> <a href="Draft_Upgrade.html" title="Draft Upgrade">Draft Upgrade</a></span> function can sometimes fail to recombine the other edges and the arcs back into a wire. In this case the <a href="Macro_SuperWire.html" title="Macro SuperWire">Macro_SuperWire</a> provides a more robust way to do this.
|
|
</p><p><br />
|
|
</p>
|
|
<pre>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) </pre>
|
|
<div style="clear:both"></div>
|
|
</div>
|
|
|
|
|
|
|
|
</div>
|
|
|
|
</div><div class="printfooter">
|
|
Online version: "<a dir="ltr" href="https://www.freecadweb.org/wiki/index.php?title=Macro_EdgesToArc&oldid=240409">http://www.freecadweb.org/wiki/index.php?title=Macro_EdgesToArc&oldid=240409</a>"</div>
|
|
<div id="catlinks" class="catlinks" data-mw="interface"></div><div class="visualClear"></div>
|
|
</div>
|
|
</div>
|
|
<div id="mw-navigation">
|
|
<h2>Navigation menu</h2>
|
|
|
|
</body></html> |