Skip to content

Commit

Permalink
Draft: Discretize projected arcs and ellipses - fixes #1612
Browse files Browse the repository at this point in the history
  • Loading branch information
yorikvanhavre committed Nov 8, 2016
1 parent f432c42 commit 00b5dbf
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 9 deletions.
39 changes: 30 additions & 9 deletions src/Mod/Draft/Draft.py
Expand Up @@ -1811,6 +1811,18 @@ def getProj(vec):
if techdraw:
ly = -ly
return Vector(lx,ly,0)

def getDiscretized(edge):
ml = FreeCAD.ParamGet("User parameter:BaseApp/Preferences/Mod/Draft").GetFloat("svgDiscretization",10.0)
d = int(edge.Length/ml)
edata = ""
for i in range(d+1):
v = getProj(edge.valueAt(edge.FirstParameter+(float(i)/d*(edge.LastParameter-edge.FirstParameter))))
if not edata:
edata += 'M ' + str(v.x) +' '+ str(v.y) + ' '
else:
edata += 'L ' + str(v.x) +' '+ str(v.y) + ' '
return edata

def getPattern(pat):
if pat in svgpatterns():
Expand Down Expand Up @@ -1859,11 +1871,7 @@ def getPath(edges=[],wires=[],pathname=None):
drawing_plane_normal = FreeCAD.Vector(0,0,1)
if plane: drawing_plane_normal = plane.axis
c = e.Curve
if round(c.Axis.getAngle(drawing_plane_normal),2) == 1.57:
# arc is perpendicular to view direction: represent as a line
v = getProj(vs[-1].Point)
edata += 'L '+ str(v.x) +' '+ str(v.y) + ' '
else:
if round(c.Axis.getAngle(drawing_plane_normal),2) == 0:
if len(e.Vertexes) == 1 and iscircle: #complete curve
svg = getCircle(e)
return svg
Expand All @@ -1875,7 +1883,6 @@ def getPath(edges=[],wires=[],pathname=None):
getProj(vs[-1].Point))
else:
endpoints = (getProj(vs[-1].Point),)

# arc
if iscircle:
rx = ry = c.Radius
Expand Down Expand Up @@ -1904,6 +1911,8 @@ def getPath(edges=[],wires=[],pathname=None):
(str(rx),str(ry),str(rot),\
str(int(flag_large_arc)),\
str(int(flag_sweep)),str(v.x),str(v.y))
else:
edata += getDiscretized(e)
elif DraftGeomUtils.geomType(e) == "Line":
v = getProj(vs[-1].Point)
edata += 'L '+ str(v.x) +' '+ str(v.y) + ' '
Expand Down Expand Up @@ -1955,9 +1964,21 @@ def getPath(edges=[],wires=[],pathname=None):
def getCircle(edge):
cen = getProj(edge.Curve.Center)
rad = edge.Curve.Radius
svg = '<circle cx="' + str(cen.x)
svg += '" cy="' + str(cen.y)
svg += '" r="' + str(rad)+'" '
if hasattr(FreeCAD,"DraftWorkingPlane"):
drawing_plane_normal = FreeCAD.DraftWorkingPlane.axis
else:
drawing_plane_normal = FreeCAD.Vector(0,0,1)
if plane: drawing_plane_normal = plane.axis
if round(edge.Curve.Axis.getAngle(drawing_plane_normal),2) == 0:
# perpendicular projection: circle
svg = '<circle cx="' + str(cen.x)
svg += '" cy="' + str(cen.y)
svg += '" r="' + str(rad)+'" '
else:
# any other projection: ellipse
svg = '<path d="'
svg += getDiscretized(edge)
svg += '" '
svg += 'stroke="' + stroke + '" '
svg += 'stroke-width="' + str(linewidth) + ' px" '
svg += 'style="stroke-width:'+ str(linewidth)
Expand Down
51 changes: 51 additions & 0 deletions src/Mod/Draft/Resources/ui/preferences-svg.ui
Expand Up @@ -172,6 +172,52 @@
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_2">
<item>
<widget class="QLabel" name="label">
<property name="text">
<string>Max segment length for discretized arcs</string>
</property>
<property name="buddy">
<cstring></cstring>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="Gui::PrefDoubleSpinBox" name="doubleSpinBox">
<property name="toolTip">
<string>When arcs are projected, if your version of OpenCasCade doesn't support arc projection, these arcs will be discretized into small line segments. This value is the maximum segment length.</string>
</property>
<property name="suffix">
<string>mm</string>
</property>
<property name="maximum">
<double>9999.989999999999782</double>
</property>
<property name="prefEntry" stdset="0">
<cstring>svgDiscretization</cstring>
</property>
<property name="prefPath" stdset="0">
<cstring>Mod/Draft</cstring>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
</item>
Expand Down Expand Up @@ -203,6 +249,11 @@
<extends>QComboBox</extends>
<header>Gui/PrefWidgets.h</header>
</customwidget>
<customwidget>
<class>Gui::PrefDoubleSpinBox</class>
<extends>QDoubleSpinBox</extends>
<header>Gui/PrefWidgets.h</header>
</customwidget>
</customwidgets>
<resources/>
<connections/>
Expand Down

0 comments on commit 00b5dbf

Please sign in to comment.