move getProj outside of getSVG, make 'plane' explicit.

This commit is contained in:
George Shuklin
2018-12-13 16:25:21 +02:00
committed by Yorik van Havre
parent 615d81fda3
commit e1eae5e114

View File

@@ -30,6 +30,19 @@ def getLineStyle(linestyle):
return "none"
def getProj(vec, plane):
if not plane: return vec
nx = DraftVecUtils.project(vec,plane.u)
lx = nx.Length
if abs(nx.getAngle(plane.u)) > 0.1: lx = -lx
ny = DraftVecUtils.project(vec,plane.v)
ly = ny.Length
if abs(ny.getAngle(plane.v)) > 0.1: ly = -ly
#if techdraw: buggy - we now simply do it at the end
# ly = -ly
return Vector(lx,ly,0)
def getSVG(obj,scale=1,linewidth=0.35,fontsize=12,fillstyle="shape color",direction=None,linestyle=None,color=None,linespacing=None,techdraw=False,rotation=0):
'''getSVG(object,[scale], [linewidth],[fontsize],[fillstyle],[direction],[linestyle],[color],[linespacing]):
returns a string containing a SVG representation of the given object,
@@ -75,18 +88,6 @@ def getSVG(obj,scale=1,linewidth=0.35,fontsize=12,fillstyle="shape color",direct
stroke = getrgb(obj.ViewObject.LineColor)
def getProj(vec):
if not plane: return vec
nx = DraftVecUtils.project(vec,plane.u)
lx = nx.Length
if abs(nx.getAngle(plane.u)) > 0.1: lx = -lx
ny = DraftVecUtils.project(vec,plane.v)
ly = ny.Length
if abs(ny.getAngle(plane.v)) > 0.1: ly = -ly
#if techdraw: buggy - we now simply do it at the end
# ly = -ly
return Vector(lx,ly,0)
def getDiscretized(edge):
ml = FreeCAD.ParamGet("User parameter:BaseApp/Preferences/Mod/Draft").GetFloat("svgDiscretization",10.0)
if ml == 0:
@@ -96,7 +97,7 @@ def getSVG(obj,scale=1,linewidth=0.35,fontsize=12,fillstyle="shape color",direct
d = 1
edata = ""
for i in range(d+1):
v = getProj(edge.valueAt(edge.FirstParameter+((float(i)/d)*(edge.LastParameter-edge.FirstParameter))))
v = getProj(edge.valueAt(edge.FirstParameter+((float(i)/d)*(edge.LastParameter-edge.FirstParameter))), plane)
if not edata:
edata += 'M ' + str(v.x) +' '+ str(v.y) + ' '
else:
@@ -135,7 +136,7 @@ def getSVG(obj,scale=1,linewidth=0.35,fontsize=12,fillstyle="shape color",direct
if (vs[0].Point-previousvs[-1].Point).Length > 1e-6:
vs.reverse()
if edgeindex == 0:
v = getProj(vs[0].Point)
v = getProj(vs[0].Point, plane)
edata += 'M '+ str(v.x) +' '+ str(v.y) + ' '
else:
if (vs[0].Point-previousvs[-1].Point).Length > 1e-6:
@@ -174,9 +175,9 @@ def getSVG(obj,scale=1,linewidth=0.35,fontsize=12,fillstyle="shape color",direct
#return svg
endpoints = (getProj(c.value((c.LastParameter-\
c.FirstParameter)/2.0)), \
getProj(vs[-1].Point))
getProj(vs[-1].Point), plane)
else:
endpoints = (getProj(vs[-1].Point),)
endpoints = (getProj(vs[-1].Point), plane)
# arc
if iscircle:
rx = ry = c.Radius
@@ -208,7 +209,7 @@ def getSVG(obj,scale=1,linewidth=0.35,fontsize=12,fillstyle="shape color",direct
else:
edata += getDiscretized(e)
elif DraftGeomUtils.geomType(e) == "Line":
v = getProj(vs[-1].Point)
v = getProj(vs[-1].Point, plane)
edata += 'L '+ str(v.x) +' '+ str(v.y) + ' '
else:
bspline=e.Curve.toBSpline(e.FirstParameter,e.LastParameter)
@@ -228,13 +229,13 @@ def getSVG(obj,scale=1,linewidth=0.35,fontsize=12,fillstyle="shape color",direct
elif bezierseg.Degree==3:
edata +='C '
for pole in bezierseg.getPoles()[1:]:
v = getProj(pole)
v = getProj(pole, plane)
edata += str(v.x) +' '+ str(v.y) + ' '
else:
print("Debug: one edge (hash ",e.hashCode(),\
") has been discretized with parameter 0.1")
for linepoint in bspline.discretize(0.1)[1:]:
v = getProj(linepoint)
v = getProj(linepoint, plane)
edata += 'L '+ str(v.x) +' '+ str(v.y) + ' '
if fill != 'none':
edata += 'Z '
@@ -260,7 +261,7 @@ def getSVG(obj,scale=1,linewidth=0.35,fontsize=12,fillstyle="shape color",direct
return svg
def getCircle(edge):
cen = getProj(edge.Curve.Center)
cen = getProj(edge.Curve.Center, plane)
rad = edge.Curve.Radius
if hasattr(FreeCAD,"DraftWorkingPlane"):
drawing_plane_normal = FreeCAD.DraftWorkingPlane.axis
@@ -287,7 +288,7 @@ def getSVG(obj,scale=1,linewidth=0.35,fontsize=12,fillstyle="shape color",direct
return svg
def getEllipse(edge):
cen = getProj(edge.Curve.Center)
cen = getProj(edge.Curve.Center, plane)
mir = edge.Curve.MinorRadius
mar = edge.Curve.MajorRadius
svg = '<ellipse cx="' + str(cen.x)
@@ -463,16 +464,16 @@ def getSVG(obj,scale=1,linewidth=0.35,fontsize=12,fillstyle="shape color",direct
prx = obj.ViewObject.Proxy
ts = (len(prx.string)*obj.ViewObject.FontSize.Value)/4.0
rm = ((prx.p3.sub(prx.p2)).Length/2.0)-ts
p2a = getProj(prx.p2.add(DraftVecUtils.scaleTo(prx.p3.sub(prx.p2),rm)))
p2b = getProj(prx.p3.add(DraftVecUtils.scaleTo(prx.p2.sub(prx.p3),rm)))
p1 = getProj(prx.p1)
p2 = getProj(prx.p2)
p3 = getProj(prx.p3)
p4 = getProj(prx.p4)
tbase = getProj(prx.tbase)
p2a = getProj(prx.p2.add(DraftVecUtils.scaleTo(prx.p3.sub(prx.p2),rm)), plane)
p2b = getProj(prx.p3.add(DraftVecUtils.scaleTo(prx.p2.sub(prx.p3),rm)), plane)
p1 = getProj(prx.p1, plane)
p2 = getProj(prx.p2, plane)
p3 = getProj(prx.p3, plane)
p4 = getProj(prx.p4, plane)
tbase = getProj(prx.tbase, plane)
r = prx.textpos.rotation.getValue().getValue()
rv = FreeCAD.Rotation(r[0],r[1],r[2],r[3]).multVec(FreeCAD.Vector(1,0,0))
angle = -DraftVecUtils.angle(getProj(rv))
angle = -DraftVecUtils.angle(getProj(rv, plane))
#angle = -DraftVecUtils.angle(p3.sub(p2))
# drawing lines
@@ -560,12 +561,12 @@ def getSVG(obj,scale=1,linewidth=0.35,fontsize=12,fillstyle="shape color",direct
# drawing arrows
if hasattr(obj.ViewObject,"ArrowType"):
p2 = getProj(prx.p2)
p3 = getProj(prx.p3)
p2 = getProj(prx.p2, plane)
p3 = getProj(prx.p3, plane)
arrowsize = obj.ViewObject.ArrowSize.Value/pointratio
arrowlength = 4*obj.ViewObject.ArrowSize.Value
u1 = getProj((prx.circle.valueAt(prx.circle.FirstParameter+arrowlength)).sub(prx.circle.valueAt(prx.circle.FirstParameter)))
u2 = getProj((prx.circle.valueAt(prx.circle.LastParameter-arrowlength)).sub(prx.circle.valueAt(prx.circle.LastParameter)))
u1 = getProj((prx.circle.valueAt(prx.circle.FirstParameter+arrowlength)).sub(prx.circle.valueAt(prx.circle.FirstParameter)), plane)
u2 = getProj((prx.circle.valueAt(prx.circle.LastParameter-arrowlength)).sub(prx.circle.valueAt(prx.circle.LastParameter)), plane)
angle1 = -DraftVecUtils.angle(u1)
angle2 = -DraftVecUtils.angle(u2)
if hasattr(obj.ViewObject,"FlipArrows"):
@@ -578,16 +579,16 @@ def getSVG(obj,scale=1,linewidth=0.35,fontsize=12,fillstyle="shape color",direct
# drawing text
if obj.ViewObject.DisplayMode == "2D":
t = prx.circle.tangentAt(prx.circle.FirstParameter+(prx.circle.LastParameter-prx.circle.FirstParameter)/2.0)
t = getProj(t)
t = getProj(t, plane)
tangle = DraftVecUtils.angle(t)
if (tangle <= -math.pi/2) or (tangle > math.pi/2):
tangle = tangle + math.pi
tbase = getProj(prx.circle.valueAt(prx.circle.FirstParameter+(prx.circle.LastParameter-prx.circle.FirstParameter)/2.0))
tbase = getProj(prx.circle.valueAt(prx.circle.FirstParameter+(prx.circle.LastParameter-prx.circle.FirstParameter)/2.0), plane)
tbase = tbase.add(DraftVecUtils.rotate(Vector(0,2.0/scale,0),tangle))
#print(tbase)
else:
tangle = 0
tbase = getProj(prx.tbase)
tbase = getProj(prx.tbase, plane)
svg += getText(stroke,fontsize,obj.ViewObject.FontName,tangle,tbase,prx.string)
elif getType(obj) == "Label":
@@ -598,7 +599,7 @@ def getSVG(obj,scale=1,linewidth=0.35,fontsize=12,fillstyle="shape color",direct
)
# Draw multisegment line
proj_points = list(map(getProj, obj.Points))
proj_points = list(map(getProj, obj.Points), plane)
path_dir_list = [format_point(proj_points[0], action='M')]
path_dir_list += map(format_point, proj_points[1:])
path_dir_str = " ".join(path_dir_list)
@@ -614,7 +615,7 @@ def getSVG(obj,scale=1,linewidth=0.35,fontsize=12,fillstyle="shape color",direct
# if Line is set to 'off', no arrow is drawn
if hasattr(obj.ViewObject, "ArrowType") and len(obj.Points) >= 2:
last_segment = FreeCAD.Vector(obj.Points[-1] - obj.Points[-2])
angle = -DraftVecUtils.angle(getProj(last_segment)) + math.pi
angle = -DraftVecUtils.angle(getProj(last_segment, plane)) + math.pi
svg += getArrow(
arrowtype=obj.ViewObject.ArrowType,
point=proj_points[-1],
@@ -630,7 +631,7 @@ def getSVG(obj,scale=1,linewidth=0.35,fontsize=12,fillstyle="shape color",direct
print("export of texts to SVG is only available in GUI mode")
else:
fontname = obj.ViewObject.TextFont
position = getProj(obj.Placement.Base)
position = getProj(obj.Placement.Base, plane)
rotation = obj.Placement.Rotation
justification = obj.ViewObject.TextAlignment
text = obj.Text
@@ -645,11 +646,11 @@ def getSVG(obj,scale=1,linewidth=0.35,fontsize=12,fillstyle="shape color",direct
else:
n = obj.ViewObject.FontName
if getType(obj) == "Annotation":
p = getProj(obj.Position)
p = getProj(obj.Position, plane)
r = obj.ViewObject.Rotation.getValueAs("rad")
t = obj.LabelText
else: # DraftText
p = getProj(obj.Placement.Base)
p = getProj(obj.Placement.Base, plane)
r = obj.Placement.Rotation
t = obj.Text
j = obj.ViewObject.Justification
@@ -741,12 +742,12 @@ def getSVG(obj,scale=1,linewidth=0.35,fontsize=12,fillstyle="shape color",direct
lspc = FreeCAD.Vector(obj.ViewObject.Proxy.header.translation.getValue().getValue())
p1 = p2.add(lspc)
j = obj.ViewObject.TextAlign
svg += getText(c,f1,n,a,getProj(p1),t1,linespacing,j,flip=True)
svg += getText(c,f1,n,a,getProj(p1, plane),t1,linespacing,j,flip=True)
if t2:
ofs = FreeCAD.Vector(0,lspc.Length,0)
if a:
ofs = FreeCAD.Rotation(FreeCAD.Vector(0,0,1),-rotation).multVec(ofs)
svg += getText(c,fontsize,n,a,getProj(p1).add(ofs),t2,linespacing,j,flip=True)
svg += getText(c,fontsize,n,a,getProj(p1, plane).add(ofs),t2,linespacing,j,flip=True)
elif obj.isDerivedFrom('Part::Feature'):
if obj.Shape.isNull():
@@ -800,8 +801,8 @@ def getSVG(obj,scale=1,linewidth=0.35,fontsize=12,fillstyle="shape color",direct
if FreeCAD.GuiUp:
if hasattr(obj.ViewObject,"EndArrow") and hasattr(obj.ViewObject,"ArrowType") and (len(obj.Shape.Vertexes) > 1):
if obj.ViewObject.EndArrow:
p1 = getProj(obj.Shape.Vertexes[-2].Point)
p2 = getProj(obj.Shape.Vertexes[-1].Point)
p1 = getProj(obj.Shape.Vertexes[-2].Point, plane)
p2 = getProj(obj.Shape.Vertexes[-1].Point, plane)
angle = -DraftVecUtils.angle(p2.sub(p1))
arrowsize = obj.ViewObject.ArrowSize.Value/pointratio
svg += getArrow(obj.ViewObject.ArrowType,p2,arrowsize,stroke,linewidth,angle)