Commit 914520be authored by Roy-043's avatar Roy-043 Committed by Uwe
Browse files

Draft: Fix flatten wire

parent ecce035d
......@@ -32,7 +32,6 @@ import lazy_loader.lazy_loader as lz
import FreeCAD as App
import DraftVecUtils
import WorkingPlane
import FreeCAD as App
from draftgeoutils.general import geomType, vec, precision
from draftgeoutils.geometry import get_normal
......@@ -157,26 +156,27 @@ def findWiresOld(edges):
return result[1]
def flattenWire(wire):
"""Force a wire to get completely flat along its normal."""
n = get_normal(wire)
# for backward compatibility with previous getNormal implementation
if n is None:
n = App.Vector(0, 0, 1)
o = wire.Vertexes[0].Point
plane = WorkingPlane.plane()
plane.alignToPointAndAxis(o, n, 0)
verts = [o]
def flattenWire(wire, origin=None, normal=None):
"""Force a wire to be flat on a plane defined by an origin and a normal.
for v in wire.Vertexes[1:]:
verts.append(plane.projectPoint(v.Point))
If origin or normal are None they are derived from the wire.
"""
if normal is None:
normal = get_normal(wire)
# for backward compatibility with previous getNormal implementation
if normal is None:
normal = App.Vector(0, 0, 1)
if origin is None:
origin = wire.Vertexes[0].Point
plane = WorkingPlane.plane()
plane.alignToPointAndAxis(origin, normal, 0)
points = [plane.projectPoint(vert.Point) for vert in wire.Vertexes]
if wire.isClosed():
verts.append(o)
w = Part.makePolygon(verts)
points.append(points[0])
new_wire = Part.makePolygon(points)
return w
return new_wire
def superWire(edgeslist, closed=False):
......@@ -432,8 +432,8 @@ def get_placement_perpendicular_to_wire(wire):
def get_extended_wire(wire, offset_start, offset_end):
"""Return a wire trimmed (negative offset) or extended (positive offset) at its first vertex, last vertex or both ends.
"""Return a wire trimmed (negative offset) or extended (positive offset) at its first vertex, last vertex or both ends.
get_extended_wire(wire, -100.0, 0.0) -> returns a copy of the wire with its first 100 mm removed
get_extended_wire(wire, 0.0, 100.0) -> returns a copy of the wire extended by 100 mm after it's last vertex
"""
......
......@@ -39,6 +39,9 @@ class ViewProviderBezCurve(ViewProviderWire):
def __init__(self, vobj):
super(ViewProviderBezCurve, self).__init__(vobj)
def setupContextMenu(self, vobj, menu):
return
# Alias for compatibility with v0.18 and earlier
_ViewProviderBezCurve = ViewProviderBezCurve
......
......@@ -39,6 +39,9 @@ class ViewProviderBSpline(ViewProviderWire):
def __init__(self, vobj):
super(ViewProviderBSpline, self).__init__(vobj)
def setupContextMenu(self, vobj, menu):
return
# Alias for compatibility with v0.18 and earlier
_ViewProviderBSpline = ViewProviderBSpline
......
......@@ -39,4 +39,8 @@ class ViewProviderFillet(ViewProviderWire):
def __init__(self, vobj):
super(ViewProviderFillet, self).__init__(vobj)
def setupContextMenu(self, vobj, menu):
return
## @}
......@@ -39,11 +39,13 @@ from PySide.QtCore import QT_TRANSLATE_NOOP
import FreeCAD as App
import FreeCADGui as Gui
import DraftVecUtils
import DraftGeomUtils
import draftgeoutils.wires as wires
import draftutils.utils as utils
import draftutils.gui_utils as gui_utils
from draftutils.messages import _msg
from draftutils.translate import translate
from draftviewproviders.view_base import ViewProviderDraft
......@@ -146,32 +148,43 @@ class ViewProviderWire(ViewProviderDraft):
return [self.Object.Base,self.Object.Tool]
return []
def setupContextMenu(self,vobj,menu):
def setupContextMenu(self, vobj, menu):
action1 = QtGui.QAction(QtGui.QIcon(":/icons/Draft_Edit.svg"),
"Flatten this wire",
translate("draft", "Flatten"),
menu)
QtCore.QObject.connect(action1,
QtCore.SIGNAL("triggered()"),
self.flatten)
menu.addAction(action1)
def flatten(self):
if hasattr(self,"Object"):
if len(self.Object.Shape.Wires) == 1:
fw = DraftGeomUtils.flattenWire(self.Object.Shape.Wires[0])
points = [v.Point for v in fw.Vertexes]
if len(points) == len(self.Object.Points):
if points != self.Object.Points:
App.ActiveDocument.openTransaction("Flatten wire")
Gui.doCommand("FreeCAD.ActiveDocument." +
self.Object.Name +
".Points=" +
str(points).replace("Vector", "FreeCAD.Vector").replace(" " , ""))
App.ActiveDocument.commitTransaction()
else:
_flat = "This Wire is already flat"
_msg(QT_TRANSLATE_NOOP("Draft", _flat))
def flatten(self): # Only to be used for Draft_Wires.
if not hasattr(self, "Object"):
return
if hasattr(App, "DraftWorkingPlane"):
App.DraftWorkingPlane.setup()
origin = App.DraftWorkingPlane.position
normal = App.DraftWorkingPlane.axis
# Align the grid for visual feedback:
if hasattr(Gui, "Snapper"):
Gui.Snapper.setTrackers()
else:
origin = App.Vector(0, 0, 0)
normal = App.Vector(0, 0, 1)
flat_wire = wires.flattenWire(self.Object.Shape.Wires[0],
origin=origin,
normal=normal)
doc = App.ActiveDocument
doc.openTransaction(translate("draft", "Flatten"))
self.Object.Placement = App.Placement()
self.Object.Points = [vert.Point for vert in flat_wire.Vertexes]
self.Object.Closed = flat_wire.isClosed()
doc.recompute()
doc.commitTransaction()
# Alias for compatibility with v0.18 and earlier
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment