From 7a338f30cb9c16bf678248ab8de3559482bdbbb3 Mon Sep 17 00:00:00 2001 From: Yorik van Havre Date: Sat, 11 Aug 2012 18:28:18 -0300 Subject: [PATCH] 0000634: Draft to Sketch conversion --- src/Mod/Draft/Draft.py | 45 ++++++++++++++++----------------- src/Mod/Draft/DraftGeomUtils.py | 29 +++++++++++---------- src/Mod/Draft/DraftTools.py | 2 +- 3 files changed, 38 insertions(+), 38 deletions(-) diff --git a/src/Mod/Draft/Draft.py b/src/Mod/Draft/Draft.py index d9e4d0a99..337d49756 100644 --- a/src/Mod/Draft/Draft.py +++ b/src/Mod/Draft/Draft.py @@ -1562,10 +1562,8 @@ def makeSketch(objectslist,autoconstraints=False,addTo=None,name="Sketch"): if tp == "BSpline": print "makeSketch: BSplines not supported" elif tp == "Circle": - if obj.FirstAngle == obj.LastAngle: - nobj.addGeometry(obj.Shape.Edges[0].Curve) - else: - nobj.addGeometry(Part.ArcOfCircle(obj.Shape.Edges[0].Curve,math.radians(obj.FirstAngle),math.radians(obj.LastAngle))) + g = (DraftGeomUtils.geom(obj.Shape.Edges[0],nobj.Placement)) + nobj.addGeometry(g) # TODO add Radius constraits ok = True elif tp == "Rectangle": @@ -1586,25 +1584,26 @@ def makeSketch(objectslist,autoconstraints=False,addTo=None,name="Sketch"): nobj.addConstraint(Constraint("Vertical",last)) ok = True elif tp in ["Wire","Polygon"]: - closed = False - if tp == "Polygon": - closed = True - elif hasattr(obj,"Closed"): - closed = obj.Closed - for edge in obj.Shape.Edges: - nobj.addGeometry(edge.Curve) - if autoconstraints: - last = nobj.GeometryCount - segs = range(last-len(obj.Shape.Edges),last-1) - for seg in segs: - nobj.addConstraint(Constraint("Coincident",seg,EndPoint,seg+1,StartPoint)) - if DraftGeomUtils.isAligned(nobj.Geometry[seg],"x"): - nobj.addConstraint(Constraint("Vertical",seg)) - elif DraftGeomUtils.isAligned(nobj.Geometry[seg],"y"): - nobj.addConstraint(Constraint("Horizontal",seg)) - if closed: - nobj.addConstraint(Constraint("Coincident",last-1,EndPoint,segs[0],StartPoint)) - ok = True + if obj.FilletRadius == 0: + closed = False + if tp == "Polygon": + closed = True + elif hasattr(obj,"Closed"): + closed = obj.Closed + for edge in obj.Shape.Edges: + nobj.addGeometry(edge.Curve) + if autoconstraints: + last = nobj.GeometryCount + segs = range(last-len(obj.Shape.Edges),last-1) + for seg in segs: + nobj.addConstraint(Constraint("Coincident",seg,EndPoint,seg+1,StartPoint)) + if DraftGeomUtils.isAligned(nobj.Geometry[seg],"x"): + nobj.addConstraint(Constraint("Vertical",seg)) + elif DraftGeomUtils.isAligned(nobj.Geometry[seg],"y"): + nobj.addConstraint(Constraint("Horizontal",seg)) + if closed: + nobj.addConstraint(Constraint("Coincident",last-1,EndPoint,segs[0],StartPoint)) + ok = True if (not ok) and obj.isDerivedFrom("Part::Feature"): if not DraftGeomUtils.isPlanar(obj.Shape): print "Error: The given object is not planar and cannot be converted into a sketch." diff --git a/src/Mod/Draft/DraftGeomUtils.py b/src/Mod/Draft/DraftGeomUtils.py index 10ee860a0..441809d22 100755 --- a/src/Mod/Draft/DraftGeomUtils.py +++ b/src/Mod/Draft/DraftGeomUtils.py @@ -39,14 +39,14 @@ precision = params.GetInt("precision") def vec(edge): - "vec(edge) or vec(line) -- returns a vector from an edge or a Part.line" - # if edge is not straight, you'll get strange results! - if isinstance(edge,Part.Shape): - return edge.Vertexes[-1].Point.sub(edge.Vertexes[0].Point) - elif isinstance(edge,Part.Line): - return edge.EndPoint.sub(edge.StartPoint) - else: - return None + "vec(edge) or vec(line) -- returns a vector from an edge or a Part.line" + # if edge is not straight, you'll get strange results! + if isinstance(edge,Part.Shape): + return edge.Vertexes[-1].Point.sub(edge.Vertexes[0].Point) + elif isinstance(edge,Part.Line): + return edge.EndPoint.sub(edge.StartPoint) + else: + return None def edg(p1,p2): "edg(Vector,Vector) -- returns an edge from 2 vectors" @@ -370,17 +370,18 @@ def geom(edge,plac=FreeCAD.Placement()): c = edge.Curve.Center cu = Part.Circle(edge.Curve.Center,normal,edge.Curve.Radius) ref = plac.Rotation.multVec(Vector(1,0,0)) - a1 = math.pi + DraftVecUtils.angle(v1.sub(c),ref,normal) - a2 = DraftVecUtils.angle(v2.sub(c),ref,normal) + a1 = DraftVecUtils.angle(v1.sub(c),ref,DraftVecUtils.neg(normal)) + a2 = DraftVecUtils.angle(v2.sub(c),ref,DraftVecUtils.neg(normal)) # direction check - if a1 > a2: + if edge.Curve.Axis.getAngle(normal) > 1: a1,a2 = a2,a1 - #print "creating sketch arc from ",cu, ", p1=",v1, " (",math.degrees(a1), "d) p2=",v2," (", math.degrees(a2),"d)" + p= Part.ArcOfCircle(cu,a1,a2) return p else: + print edge.Curve return edge.Curve def mirror (point, edge): @@ -772,8 +773,8 @@ def getNormal(shape): if (shape.ShapeType == "Face") and hasattr(shape,"normalAt"): n = shape.normalAt(0.5,0.5) elif shape.ShapeType == "Edge": - if isinstance(shape.Curve,Part.Circle): - n = shape.Curve.Axis + if isinstance(shape.Edges[0].Curve,Part.Circle): + n = shape.Edges[0].Curve.Axis else: for e in shape.Edges: if isinstance(e.Curve,Part.Circle): diff --git a/src/Mod/Draft/DraftTools.py b/src/Mod/Draft/DraftTools.py index 10d1bb382..916632716 100644 --- a/src/Mod/Draft/DraftTools.py +++ b/src/Mod/Draft/DraftTools.py @@ -3711,7 +3711,7 @@ class Draft2Sketch(): elif obj.isDerivedFrom("Part::Part2DObjectPython"): Draft.makeSketch(obj,autoconstraints=True) elif obj.isDerivedFrom("Part::Feature"): - if len(obj.Shape.Wires) == 1: + if (len(obj.Shape.Wires) == 1) or (len(obj.Shape.Edges) == 1): Draft.makeSketch(obj,autoconstraints=False) FreeCAD.ActiveDocument.commitTransaction()