Skip to content

Commit

Permalink
Draft: Replace Part.getSortedClusters by Part.sortEdges in draftify f…
Browse files Browse the repository at this point in the history
…unction
  • Loading branch information
marioalexis84 committed Dec 30, 2020
1 parent 0f31c87 commit 60dfd25
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 26 deletions.
55 changes: 44 additions & 11 deletions src/Mod/Draft/draftfunctions/draftify.py
Expand Up @@ -27,12 +27,21 @@

## \addtogroup draftfuctions
# @{

import lazy_loader.lazy_loader as lz

import FreeCAD as App
import draftutils.gui_utils as gui_utils
import draftmake.make_block as make_block
import draftmake.make_wire as make_wire
import draftmake.make_circle as make_circle
import draftmake.make_bspline as make_bspline
import draftmake.make_bezcurve as make_bezcurve
import draftmake.make_arc_3points as make_arc_3points

# Delay import of module until first use because it is heavy
Part = lz.LazyLoader("Part", globals(), "Part")
DraftGeomUtils = lz.LazyLoader("DraftGeomUtils", globals(), "DraftGeomUtils")

def draftify(objectslist, makeblock=False, delete=True):
"""draftify(objectslist,[makeblock],[delete])
Expand All @@ -52,24 +61,18 @@ def draftify(objectslist, makeblock=False, delete=True):
delete : bool
If delete = False, old objects are not deleted
"""
import Part
import DraftGeomUtils

if not isinstance(objectslist,list):
objectslist = [objectslist]
newobjlist = []
for obj in objectslist:
if hasattr(obj,'Shape'):
for cluster in Part.getSortedClusters(obj.Shape.Edges):
for cluster in Part.sortEdges(obj.Shape.Edges):
w = Part.Wire(cluster)
if DraftGeomUtils.hasCurves(w):
if (len(w.Edges) == 1) and (DraftGeomUtils.geomType(w.Edges[0]) == "Circle"):
nobj = make_circle.make_circle(w.Edges[0])
else:
nobj = App.ActiveDocument.addObject("Part::Feature", obj.Name)
nobj.Shape = w
else:
nobj = make_wire.make_wire(w)
nobj = draftify_shape(w)
if nobj == None:
nobj = App.ActiveDocument.addObject("Part::Feature", obj.Name)
nobj.Shape = w
newobjlist.append(nobj)
gui_utils.format_object(nobj, obj)
# sketches are always in wireframe mode. In Draft we don't like that!
Expand All @@ -85,4 +88,34 @@ def draftify(objectslist, makeblock=False, delete=True):
return newobjlist[0]
return newobjlist

def draftify_shape(shape):

nobj = None
if DraftGeomUtils.hasCurves(shape):
if (len(shape.Edges) == 1):
edge = shape.Edges[0]
edge_type = DraftGeomUtils.geomType(edge)
if edge_type == "Circle":
if edge.isClosed():
nobj = make_circle.make_circle(edge)
else:
first_parameter = edge.FirstParameter
last_parameter = edge.LastParameter
points = [edge.Curve.value(first_parameter),
edge.Curve.value((first_parameter + last_parameter)/2),
edge.Curve.value(last_parameter)]
nobj = make_arc_3points.make_arc_3points(points)
# TODO: take into consideration trimmed curves and capture the specific
# type of BSpline and Bezier that can be converted to a draft object.
# elif edge_type == "BSplineCurve":
# knots = [edge.Curve.value(p) for p in edge.Curve.getKnots()]
# nobj = make_bspline.make_bspline(knots, closed=edge.isClosed())
# elif edge_type == "BezierCurve":
# nobj = make_bezcurve.make_bezcurve(edge.Curve.getPoles(),
# closed=edge.isClosed())
else:
nobj = make_wire.make_wire(shape)

return nobj

## @}
31 changes: 16 additions & 15 deletions src/Mod/Draft/draftfunctions/upgrade.py
Expand Up @@ -504,18 +504,10 @@ def makeWires(objectslist):
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:
elif len(objects) > 1 and len(edges) > 1:
result = makeWires(objects)
if result:
_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:
Expand All @@ -524,14 +516,23 @@ def makeWires(objectslist):
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
# 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"))
elif (len(objects) == 1 and len(edges) == 1
and not objects[0].isDerivedFrom("Part::Part2DObjectPython")):
e = objects[0].Shape.Edges[0]
if isinstance(e.Curve, (Part.LineSegment, Part.Line)):
result = turnToLine(objects[0])
edge_type = DraftGeomUtils.geomType(e)
# currently only support Line and Circle
if edge_type in ("Line", "Circle"):
result = ext_draftify.draftify(objects[0])
if result:
_msg(_tr("Found 1 linear object: converting to line"))
_msg(_tr("Found 1 object: draftifying it"))
# only points, no edges
elif not edges and len(objects) > 1:
result = makeCompound(objects)
Expand Down

0 comments on commit 60dfd25

Please sign in to comment.