0000634: Draft to Sketch conversion

This commit is contained in:
Yorik van Havre 2012-08-11 18:28:18 -03:00
parent 1859ec2559
commit 7a338f30cb
3 changed files with 38 additions and 38 deletions

View File

@ -1562,10 +1562,8 @@ def makeSketch(objectslist,autoconstraints=False,addTo=None,name="Sketch"):
if tp == "BSpline": if tp == "BSpline":
print "makeSketch: BSplines not supported" print "makeSketch: BSplines not supported"
elif tp == "Circle": elif tp == "Circle":
if obj.FirstAngle == obj.LastAngle: g = (DraftGeomUtils.geom(obj.Shape.Edges[0],nobj.Placement))
nobj.addGeometry(obj.Shape.Edges[0].Curve) nobj.addGeometry(g)
else:
nobj.addGeometry(Part.ArcOfCircle(obj.Shape.Edges[0].Curve,math.radians(obj.FirstAngle),math.radians(obj.LastAngle)))
# TODO add Radius constraits # TODO add Radius constraits
ok = True ok = True
elif tp == "Rectangle": elif tp == "Rectangle":
@ -1586,25 +1584,26 @@ def makeSketch(objectslist,autoconstraints=False,addTo=None,name="Sketch"):
nobj.addConstraint(Constraint("Vertical",last)) nobj.addConstraint(Constraint("Vertical",last))
ok = True ok = True
elif tp in ["Wire","Polygon"]: elif tp in ["Wire","Polygon"]:
closed = False if obj.FilletRadius == 0:
if tp == "Polygon": closed = False
closed = True if tp == "Polygon":
elif hasattr(obj,"Closed"): closed = True
closed = obj.Closed elif hasattr(obj,"Closed"):
for edge in obj.Shape.Edges: closed = obj.Closed
nobj.addGeometry(edge.Curve) for edge in obj.Shape.Edges:
if autoconstraints: nobj.addGeometry(edge.Curve)
last = nobj.GeometryCount if autoconstraints:
segs = range(last-len(obj.Shape.Edges),last-1) last = nobj.GeometryCount
for seg in segs: segs = range(last-len(obj.Shape.Edges),last-1)
nobj.addConstraint(Constraint("Coincident",seg,EndPoint,seg+1,StartPoint)) for seg in segs:
if DraftGeomUtils.isAligned(nobj.Geometry[seg],"x"): nobj.addConstraint(Constraint("Coincident",seg,EndPoint,seg+1,StartPoint))
nobj.addConstraint(Constraint("Vertical",seg)) if DraftGeomUtils.isAligned(nobj.Geometry[seg],"x"):
elif DraftGeomUtils.isAligned(nobj.Geometry[seg],"y"): nobj.addConstraint(Constraint("Vertical",seg))
nobj.addConstraint(Constraint("Horizontal",seg)) elif DraftGeomUtils.isAligned(nobj.Geometry[seg],"y"):
if closed: nobj.addConstraint(Constraint("Horizontal",seg))
nobj.addConstraint(Constraint("Coincident",last-1,EndPoint,segs[0],StartPoint)) if closed:
ok = True nobj.addConstraint(Constraint("Coincident",last-1,EndPoint,segs[0],StartPoint))
ok = True
if (not ok) and obj.isDerivedFrom("Part::Feature"): if (not ok) and obj.isDerivedFrom("Part::Feature"):
if not DraftGeomUtils.isPlanar(obj.Shape): if not DraftGeomUtils.isPlanar(obj.Shape):
print "Error: The given object is not planar and cannot be converted into a sketch." print "Error: The given object is not planar and cannot be converted into a sketch."

View File

@ -39,14 +39,14 @@ precision = params.GetInt("precision")
def vec(edge): def vec(edge):
"vec(edge) or vec(line) -- returns a vector from an edge or a Part.line" "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 edge is not straight, you'll get strange results!
if isinstance(edge,Part.Shape): if isinstance(edge,Part.Shape):
return edge.Vertexes[-1].Point.sub(edge.Vertexes[0].Point) return edge.Vertexes[-1].Point.sub(edge.Vertexes[0].Point)
elif isinstance(edge,Part.Line): elif isinstance(edge,Part.Line):
return edge.EndPoint.sub(edge.StartPoint) return edge.EndPoint.sub(edge.StartPoint)
else: else:
return None return None
def edg(p1,p2): def edg(p1,p2):
"edg(Vector,Vector) -- returns an edge from 2 vectors" "edg(Vector,Vector) -- returns an edge from 2 vectors"
@ -370,17 +370,18 @@ def geom(edge,plac=FreeCAD.Placement()):
c = edge.Curve.Center c = edge.Curve.Center
cu = Part.Circle(edge.Curve.Center,normal,edge.Curve.Radius) cu = Part.Circle(edge.Curve.Center,normal,edge.Curve.Radius)
ref = plac.Rotation.multVec(Vector(1,0,0)) ref = plac.Rotation.multVec(Vector(1,0,0))
a1 = math.pi + DraftVecUtils.angle(v1.sub(c),ref,normal) a1 = DraftVecUtils.angle(v1.sub(c),ref,DraftVecUtils.neg(normal))
a2 = DraftVecUtils.angle(v2.sub(c),ref,normal) a2 = DraftVecUtils.angle(v2.sub(c),ref,DraftVecUtils.neg(normal))
# direction check # direction check
if a1 > a2: if edge.Curve.Axis.getAngle(normal) > 1:
a1,a2 = a2,a1 a1,a2 = a2,a1
#print "creating sketch arc from ",cu, ", p1=",v1, " (",math.degrees(a1), "d) p2=",v2," (", math.degrees(a2),"d)" #print "creating sketch arc from ",cu, ", p1=",v1, " (",math.degrees(a1), "d) p2=",v2," (", math.degrees(a2),"d)"
p= Part.ArcOfCircle(cu,a1,a2) p= Part.ArcOfCircle(cu,a1,a2)
return p return p
else: else:
print edge.Curve
return edge.Curve return edge.Curve
def mirror (point, edge): def mirror (point, edge):
@ -772,8 +773,8 @@ def getNormal(shape):
if (shape.ShapeType == "Face") and hasattr(shape,"normalAt"): if (shape.ShapeType == "Face") and hasattr(shape,"normalAt"):
n = shape.normalAt(0.5,0.5) n = shape.normalAt(0.5,0.5)
elif shape.ShapeType == "Edge": elif shape.ShapeType == "Edge":
if isinstance(shape.Curve,Part.Circle): if isinstance(shape.Edges[0].Curve,Part.Circle):
n = shape.Curve.Axis n = shape.Edges[0].Curve.Axis
else: else:
for e in shape.Edges: for e in shape.Edges:
if isinstance(e.Curve,Part.Circle): if isinstance(e.Curve,Part.Circle):

View File

@ -3711,7 +3711,7 @@ class Draft2Sketch():
elif obj.isDerivedFrom("Part::Part2DObjectPython"): elif obj.isDerivedFrom("Part::Part2DObjectPython"):
Draft.makeSketch(obj,autoconstraints=True) Draft.makeSketch(obj,autoconstraints=True)
elif obj.isDerivedFrom("Part::Feature"): 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) Draft.makeSketch(obj,autoconstraints=False)
FreeCAD.ActiveDocument.commitTransaction() FreeCAD.ActiveDocument.commitTransaction()