Merge pull request #4100 from marioalexis84/draft-upgrade

Draft: Improve makeWires in upgrade function
This commit is contained in:
Yorik van Havre
2020-12-14 11:59:21 +01:00
committed by GitHub
2 changed files with 80 additions and 58 deletions

View File

@@ -42,6 +42,7 @@ import draftmake.make_block as make_block
from draftutils.messages import _msg, _err
from draftutils.translate import _tr
from draftgeoutils.geometry import is_straight_line
# Delay import of module until first use because it is heavy
Part = lz.LazyLoader("Part", globals(), "Part")
@@ -160,6 +161,8 @@ def upgrade(objects, delete=False, force=None):
return None
if len(obj.Shape.Edges) == 1:
return None
if is_straight_line(obj.Shape) == True:
return None
if utils.get_type(obj) == "Wire":
obj.Closed = True
return True
@@ -333,26 +336,35 @@ def upgrade(objects, delete=False, force=None):
def makeWires(objectslist):
"""Join edges in the given objects list into wires."""
edges = []
for o in objectslist:
for e in o.Shape.Edges:
edges.append(e)
for object in objectslist:
for edge in object.Shape.Edges:
edges.append(edge)
try:
nedges = Part.__sortEdges__(edges[:])
sorted_edges = Part.sortEdges(edges)
if _DEBUG:
for e in nedges:
print("Curve: {}".format(e.Curve))
print("first: {}, last: {}".format(e.Vertexes[0].Point,
for item_sorted_edges in sorted_edges:
for e in item_sorted_edges:
print("Curve: {}".format(e.Curve))
print("first: {}, last: {}".format(e.Vertexes[0].Point,
e.Vertexes[-1].Point))
w = Part.Wire(nedges)
wires = [Part.Wire(e) for e in sorted_edges]
except Part.OCCError:
return None
else:
if len(w.Edges) == len(edges):
for wire in wires:
newobj = doc.addObject("Part::Feature", "Wire")
newobj.Shape = w
newobj.Shape = wire
add_list.append(newobj)
delete_list.extend(objectslist)
return True
# delete object only if there are no links to it
# TODO: A more refined criteria to delete object
for object in objectslist:
if object.InList:
if App.GuiUp:
object.ViewObject.Visibility = False
else:
delete_list.append(object)
return True
return None
# analyzing what we have in our selection
@@ -475,54 +487,56 @@ def upgrade(objects, delete=False, force=None):
_msg(_tr("Found 1 non-parametric objects: "
"draftifying it"))
# we have only one object that contains one edge
elif not faces and len(objects) == 1 and len(edges) == 1:
# we have a closed sketch: extract a face
if (objects[0].isDerivedFrom("Sketcher::SketchObject")
and len(edges[0].Vertexes) == 1):
result = makeSketchFace(objects[0])
# in the following cases there are no faces
elif not faces:
# we have only closed wires
if wires and not openwires and not loneedges:
# we have a sketch: extract a face
if (len(objects) == 1
and objects[0].isDerivedFrom("Sketcher::SketchObject")):
result = makeSketchFace(objects[0])
if result:
_msg(_tr("Found 1 closed sketch object: "
"creating a face from it"))
# only closed wires
else:
result = makeFaces(objects)
if result:
_msg(_tr("Found closed wires: creating faces"))
# wires or edges: we try to join them
elif len(wires) > 1 or len(loneedges) > 1:
result = makeWires(objects)
if result:
_msg(_tr("Found 1 closed sketch object: "
"creating a face from it"))
else:
_msg(_tr("Found several wires or edges: wiring them"))
# TODO: improve draftify function
# only one object: if not parametric, we "draftify" it
# elif (len(objects) == 1
# and not objects[0].isDerivedFrom("Part::Part2DObjectPython")):
# result = ext_draftify.draftify(objects[0])
# if result:
# _msg(_tr("Found 1 non-parametric objects: "
# "draftifying it"))
# special case, we have only one open wire. We close it,
# unless it has only 1 edge!
elif len(objects) == 1 and len(openwires) == 1:
result = closeWire(objects[0])
_msg(_tr("trying: closing it"))
if result:
_msg(_tr("Found 1 open wire: closing it"))
# we have only one object that contains one edge
# TODO: this case should be considered in draftify
elif len(objects) == 1 and len(edges) == 1:
# turn to Draft Line
e = objects[0].Shape.Edges[0]
if isinstance(e.Curve, (Part.LineSegment, Part.Line)):
result = turnToLine(objects[0])
if result:
_msg(_tr("Found 1 linear object: converting to line"))
# we have only closed wires, no faces
elif wires and not faces and not openwires:
# we have a sketch: extract a face
if (len(objects) == 1
and objects[0].isDerivedFrom("Sketcher::SketchObject")):
result = makeSketchFace(objects[0])
# only points, no edges
elif not edges and len(objects) > 1:
result = makeCompound(objects)
if result:
_msg(_tr("Found 1 closed sketch object: "
"creating a face from it"))
# only closed wires
else:
result = makeFaces(objects)
if result:
_msg(_tr("Found closed wires: creating faces"))
# special case, we have only one open wire. We close it,
# unless it has only 1 edge!
elif len(openwires) == 1 and not faces and not loneedges:
result = closeWire(objects[0])
if result:
_msg(_tr("Found 1 open wire: closing it"))
# only open wires and edges: we try to join their edges
elif openwires and not wires and not faces:
result = makeWires(objects)
if result:
_msg(_tr("Found several open wires: joining them"))
# only loneedges: we try to join them
elif loneedges and not facewires:
result = makeWires(objects)
if result:
_msg(_tr("Found several edges: wiring them"))
_msg(_tr("Found points: creating compound"))
# all other cases, if more than 1 object, make a compound
elif len(objects) > 1:
result = makeCompound(objects)

View File

@@ -232,7 +232,7 @@ class DraftModification(unittest.TestCase):
self.assertTrue(obj, "'{}' failed".format(operation))
def test_upgrade(self):
"""Upgrade two Draft Lines into a closed Draft Wire."""
"""Upgrade two Lines into a closed Wire, then draftify it."""
operation = "Draft Upgrade"
_msg(" Test '{}'".format(operation))
a = Vector(0, 0, 0)
@@ -242,8 +242,12 @@ class DraftModification(unittest.TestCase):
_msg(" a={0}, b={1}".format(a, b))
_msg(" Line 2")
_msg(" b={0}, c={1}".format(b, c))
line_1 = Draft.make_line(a, b)
line_2 = Draft.make_line(b, c)
shape_line_1 = Part.makeLine(a, b)
shape_line_2 = Part.makeLine(b, c)
line_1 = App.ActiveDocument.addObject("Part::Feature")
line_2 = App.ActiveDocument.addObject("Part::Feature")
line_1.Shape = shape_line_1
line_2.Shape = shape_line_2
App.ActiveDocument.recompute()
obj = Draft.upgrade([line_1, line_2], delete=True)
@@ -255,8 +259,7 @@ class DraftModification(unittest.TestCase):
obj2 = Draft.upgrade(obj[0], delete=True)
App.ActiveDocument.recompute()
s2 = obj2[0][0]
_msg(" 2: Result '{0}' ({1})".format(s2.Shape.ShapeType,
s2.TypeId))
_msg(" 2: Result '{0}' ({1})".format(s2.Shape.ShapeType, s2.TypeId))
self.assertTrue(bool(obj2[0]), "'{}' failed".format(operation))
obj3 = Draft.upgrade(obj2[0], delete=True)
@@ -265,10 +268,15 @@ class DraftModification(unittest.TestCase):
_msg(" 3: Result '{0}' ({1})".format(s3.Shape.ShapeType, s3.TypeId))
self.assertTrue(bool(obj3[0]), "'{}' failed".format(operation))
obj4 = Draft.upgrade(obj3[0], delete=True)
# when draftify, upgrade dont return a new object
Draft.upgrade(obj3[0], delete=True)
App.ActiveDocument.recompute()
wire = App.ActiveDocument.Wire
_msg(" 4: Result '{0}' ({1})".format(wire.Proxy.Type, wire.TypeId))
self.assertTrue(bool(wire), "'{}' failed".format(operation))
obj4 = Draft.upgrade(wire, delete=True)
App.ActiveDocument.recompute()
_msg(" The last object cannot be upgraded further")
self.assertFalse(bool(obj4[0]), "'{}' failed".format(operation))