Draft: Fixed parametric dimensions - fixes #1804, #2614

This commit is contained in:
Yorik van Havre 2016-10-09 14:20:05 -03:00
parent 3cac7be520
commit 0704ad0b71
2 changed files with 106 additions and 42 deletions

View File

@ -715,11 +715,13 @@ def makeDimension(p1,p2,p3=None,p4=None):
l = [] l = []
l.append((p1,"Edge"+str(p2+1))) l.append((p1,"Edge"+str(p2+1)))
if p3 == "radius": if p3 == "radius":
l.append((p1,"Center")) #l.append((p1,"Center"))
obj.ViewObject.Override = "R $dim" obj.ViewObject.Override = "R $dim"
obj.Diameter = False
elif p3 == "diameter": elif p3 == "diameter":
l.append((p1,"Diameter")) #l.append((p1,"Diameter"))
obj.ViewObject.Override = "Ø $dim" obj.ViewObject.Override = "Ø $dim"
obj.Diameter = True
obj.LinkedGeometry = l obj.LinkedGeometry = l
obj.Support = p1 obj.Support = p1
p3 = p4 p3 = p4
@ -3488,6 +3490,7 @@ class _Dimension(_DraftObject):
obj.addProperty("App::PropertyLink","Support","Draft",QT_TRANSLATE_NOOP("App::Property","The object measured by this dimension")) obj.addProperty("App::PropertyLink","Support","Draft",QT_TRANSLATE_NOOP("App::Property","The object measured by this dimension"))
obj.addProperty("App::PropertyLinkSubList","LinkedGeometry","Draft",QT_TRANSLATE_NOOP("App::Property","The geometry this dimension is linked to")) obj.addProperty("App::PropertyLinkSubList","LinkedGeometry","Draft",QT_TRANSLATE_NOOP("App::Property","The geometry this dimension is linked to"))
obj.addProperty("App::PropertyLength","Distance","Draft",QT_TRANSLATE_NOOP("App::Property","The measurement of this dimension")) obj.addProperty("App::PropertyLength","Distance","Draft",QT_TRANSLATE_NOOP("App::Property","The measurement of this dimension"))
obj.addProperty("App::PropertyBool","Diameter","Draft",QT_TRANSLATE_NOOP("App::Property","For arc/circle measurements, false = radius, true = diameter"))
obj.Start = FreeCAD.Vector(0,0,0) obj.Start = FreeCAD.Vector(0,0,0)
obj.End = FreeCAD.Vector(1,0,0) obj.End = FreeCAD.Vector(1,0,0)
obj.Dimline = FreeCAD.Vector(0,1,0) obj.Dimline = FreeCAD.Vector(0,1,0)
@ -3501,29 +3504,47 @@ class _Dimension(_DraftObject):
obj.setEditorMode('Support',2) obj.setEditorMode('Support',2)
def execute(self, obj): def execute(self, obj):
import DraftGeomUtils
# set start point and end point according to the linked geometry
if obj.LinkedGeometry: if obj.LinkedGeometry:
if "Edge" in obj.LinkedGeometry[0][1]: if len(obj.LinkedGeometry) == 1:
n = int(obj.LinkedGeometry[0][1][4:])-1 lobj = obj.LinkedGeometry[0][0]
if len(obj.LinkedGeometry) > 1: lsub = obj.LinkedGeometry[0][1]
c = obj.LinkedGeometry[0][0].Shape.Edges[n].Curve.Center if len(lsub) == 1:
r = obj.LinkedGeometry[0][0].Shape.Edges[n].Curve.Radius if "Edge" in lsub[0]:
ray = DraftVecUtils.scaleTo(obj.Dimline.sub(c),r) n = int(lsub[0][4:])-1
if "Center" in obj.LinkedGeometry[1][1]: edge = lobj.Shape.Edges[n]
obj.Start = c if DraftGeomUtils.geomType(edge) == "Line":
obj.End = c.add(ray) obj.Start = edge.Vertexes[0].Point
elif "Diameter" in obj.LinkedGeometry[1][1]: obj.End = edge.Vertexes[-1].Point
obj.Start = c.add(ray.negative()) elif DraftGeomUtils.geomType(edge) == "Circle":
obj.End = c.add(ray) c = edge.Curve.Center
else: r = edge.Curve.Radius
obj.Start = obj.LinkedGeometry[0][0].Shape.Edges[n].Vertexes[0].Point ray = DraftVecUtils.scaleTo(obj.Dimline.sub(c),r)
obj.End = obj.LinkedGeometry[0][0].Shape.Edges[n].Vertexes[-1].Point if hasattr(obj,"Diameter"):
elif "Vertex" in obj.LinkedGeometry[0][1]: if obj.Diameter:
n = int(obj.LinkedGeometry[0][1][6:])-1 obj.Start = c.add(ray.negative())
obj.Start = obj.LinkedGeometry[0][0].Shape.Vertexes[n].Point obj.End = c.add(ray)
if len(obj.LinkedGeometry) > 1: else:
if "Vertex" in obj.LinkedGeometry[1][1]: obj.Start = c
n = int(obj.LinkedGeometry[1][1][6:])-1 obj.End = c.add(ray)
obj.End = obj.LinkedGeometry[1][0].Shape.Vertexes[n].Point elif len(lsub) == 2:
if ("Vertex" in lsub[0]) and ("Vertex" in lsub[1]):
n1 = int(lsub[0][6:])-1
n2 = int(lsub[1][6:])-1
obj.Start = lobj.Shape.Vertexes[n1].Point
obj.End = lobj.Shape.Vertexes[n2].Point
elif len(obj.LinkedGeometry) == 2:
lobj1 = obj.LinkedGeometry[0][0]
lobj2 = obj.LinkedGeometry[1][0]
lsub1 = obj.LinkedGeometry[0][1]
lsub2 = obj.LinkedGeometry[1][1]
if (len(lsub1) == 1) and (len(lsub2) == 1):
if ("Vertex" in lsub1[0]) and ("Vertex" in lsub2[1]):
n1 = int(lsub1[0][6:])-1
n2 = int(lsub2[0][6:])-1
obj.Start = lobj1.Shape.Vertexes[n1].Point
obj.End = lobj2.Shape.Vertexes[n2].Point
if obj.ViewObject: if obj.ViewObject:
obj.ViewObject.update() obj.ViewObject.update()
@ -3541,6 +3562,7 @@ class _ViewProviderDimension(_ViewProviderDraft):
obj.addProperty("App::PropertyColor","LineColor","Draft",QT_TRANSLATE_NOOP("App::Property","Line color")) obj.addProperty("App::PropertyColor","LineColor","Draft",QT_TRANSLATE_NOOP("App::Property","Line color"))
obj.addProperty("App::PropertyDistance","ExtLines","Draft",QT_TRANSLATE_NOOP("App::Property","Length of the extension lines")) obj.addProperty("App::PropertyDistance","ExtLines","Draft",QT_TRANSLATE_NOOP("App::Property","Length of the extension lines"))
obj.addProperty("App::PropertyBool","FlipArrows","Draft",QT_TRANSLATE_NOOP("App::Property","Rotate the dimension arrows 180 degrees")) obj.addProperty("App::PropertyBool","FlipArrows","Draft",QT_TRANSLATE_NOOP("App::Property","Rotate the dimension arrows 180 degrees"))
obj.addProperty("App::PropertyBool","FlipText","Draft",QT_TRANSLATE_NOOP("App::Property","Rotate the dimension text 180 degrees"))
obj.addProperty("App::PropertyBool","ShowUnit","Draft",QT_TRANSLATE_NOOP("App::Property","Show the unit suffix")) obj.addProperty("App::PropertyBool","ShowUnit","Draft",QT_TRANSLATE_NOOP("App::Property","Show the unit suffix"))
obj.addProperty("App::PropertyVectorDistance","TextPosition","Draft",QT_TRANSLATE_NOOP("App::Property","The position of the text. Leave (0,0,0) for automatic position")) obj.addProperty("App::PropertyVectorDistance","TextPosition","Draft",QT_TRANSLATE_NOOP("App::Property","The position of the text. Leave (0,0,0) for automatic position"))
obj.addProperty("App::PropertyString","Override","Draft",QT_TRANSLATE_NOOP("App::Property","Text override. Use $dim to insert the dimension length")) obj.addProperty("App::PropertyString","Override","Draft",QT_TRANSLATE_NOOP("App::Property","Text override. Use $dim to insert the dimension length"))
@ -3639,7 +3661,7 @@ class _ViewProviderDimension(_ViewProviderDraft):
proj = None proj = None
else: else:
base = Part.Line(self.p2,self.p3).toShape() base = Part.Line(self.p2,self.p3).toShape()
proj = DraftGeomUtils.findDistance(self.p1,base) proj = DraftGeomUtils.findDistance(self.p1,base).negative()
if not base: if not base:
if DraftVecUtils.equals(self.p1,self.p4): if DraftVecUtils.equals(self.p1,self.p4):
base = None base = None
@ -3650,20 +3672,22 @@ class _ViewProviderDimension(_ViewProviderDraft):
if proj: if proj:
self.p2 = self.p1.add(proj.negative()) self.p2 = self.p1.add(proj.negative())
self.p3 = self.p4.add(proj.negative()) self.p3 = self.p4.add(proj.negative())
if hasattr(obj.ViewObject,"ExtLines"):
dmax = obj.ViewObject.ExtLines.Value
if dmax and (proj.Length > dmax):
if (dmax > 0):
self.p1 = self.p2.add(DraftVecUtils.scaleTo(proj,dmax))
self.p4 = self.p3.add(DraftVecUtils.scaleTo(proj,dmax))
else:
rest = proj.Length + dmax
self.p1 = self.p2.add(DraftVecUtils.scaleTo(proj,rest))
self.p4 = self.p3.add(DraftVecUtils.scaleTo(proj,rest))
else: else:
self.p2 = self.p1 self.p2 = self.p1
self.p3 = self.p4 self.p3 = self.p4
proj = (self.p3.sub(self.p2)).cross(Vector(0,0,1)) if proj:
if hasattr(obj.ViewObject,"ExtLines"):
dmax = obj.ViewObject.ExtLines.Value
if dmax and (proj.Length > dmax):
if (dmax > 0):
self.p1 = self.p2.add(DraftVecUtils.scaleTo(proj,dmax))
self.p4 = self.p3.add(DraftVecUtils.scaleTo(proj,dmax))
else:
rest = proj.Length + dmax
self.p1 = self.p2.add(DraftVecUtils.scaleTo(proj,rest))
self.p4 = self.p3.add(DraftVecUtils.scaleTo(proj,rest))
else:
proj = (self.p3.sub(self.p2)).cross(Vector(0,0,1))
# calculate the arrows positions # calculate the arrows positions
self.trans1.translation.setValue((self.p2.x,self.p2.y,self.p2.z)) self.trans1.translation.setValue((self.p2.x,self.p2.y,self.p2.z))
@ -3679,7 +3703,7 @@ class _ViewProviderDimension(_ViewProviderDraft):
else: else:
norm = Vector(0,0,1) norm = Vector(0,0,1)
else: else:
norm = obj.Normal norm = FreeCAD.Vector(obj.Normal)
else: else:
if proj: if proj:
norm = (self.p3.sub(self.p2).cross(proj)).negative() norm = (self.p3.sub(self.p2).cross(proj)).negative()
@ -3702,20 +3726,24 @@ class _ViewProviderDimension(_ViewProviderDraft):
offset = DraftVecUtils.scaleTo(v1,obj.ViewObject.TextSpacing.Value) offset = DraftVecUtils.scaleTo(v1,obj.ViewObject.TextSpacing.Value)
else: else:
offset = DraftVecUtils.scaleTo(v1,0.05) offset = DraftVecUtils.scaleTo(v1,0.05)
rott = rot1
if hasattr(obj.ViewObject,"FlipText"):
if obj.ViewObject.FlipText:
rott = FreeCAD.Rotation(*rott).multiply(FreeCAD.Rotation(norm,180)).Q
offset = offset.negative()
# setting text # setting text
try: try:
m = obj.ViewObject.DisplayMode m = obj.ViewObject.DisplayMode
except: # swallow all exceptions here since it always fails on first run (Displaymode enum no set yet) except: # swallow all exceptions here since it always fails on first run (Displaymode enum no set yet)
m = ["2D","3D"][getParam("dimstyle",0)] m = ["2D","3D"][getParam("dimstyle",0)]
if m== "3D": if m == "3D":
offset = offset.negative() offset = offset.negative()
self.tbase = (self.p2.add((self.p3.sub(self.p2).multiply(0.5)))).add(offset) self.tbase = (self.p2.add((self.p3.sub(self.p2).multiply(0.5)))).add(offset)
if hasattr(obj.ViewObject,"TextPosition"): if hasattr(obj.ViewObject,"TextPosition"):
if not DraftVecUtils.isNull(obj.ViewObject.TextPosition): if not DraftVecUtils.isNull(obj.ViewObject.TextPosition):
self.tbase = obj.ViewObject.TextPosition self.tbase = obj.ViewObject.TextPosition
self.textpos.translation.setValue([self.tbase.x,self.tbase.y,self.tbase.z]) self.textpos.translation.setValue([self.tbase.x,self.tbase.y,self.tbase.z])
self.textpos.rotation = coin.SbRotation(rot1[0],rot1[1],rot1[2],rot1[3]) self.textpos.rotation = coin.SbRotation(rott[0],rott[1],rott[2],rott[3])
su = True su = True
if hasattr(obj.ViewObject,"ShowUnit"): if hasattr(obj.ViewObject,"ShowUnit"):
su = obj.ViewObject.ShowUnit su = obj.ViewObject.ShowUnit

View File

@ -1713,8 +1713,36 @@ class Dimension(Creator):
self.force = None self.force = None
self.info = None self.info = None
self.selectmode = False self.selectmode = False
self.setFromSelection()
msg(translate("draft", "Pick first point:\n")) msg(translate("draft", "Pick first point:\n"))
FreeCADGui.draftToolBar.show() FreeCADGui.draftToolBar.show()
def setFromSelection(self):
"If we already have selected geometry, fill the nodes accordingly"
sel = FreeCADGui.Selection.getSelectionEx()
import DraftGeomUtils
if len(sel) == 1:
if len(sel[0].SubElementNames) == 1:
if "Edge" in sel[0].SubElementNames[0]:
edge = sel[0].SubObjects[0]
n = int(sel[0].SubElementNames[0].lstrip("Edge"))-1
self.indices.append(n)
if DraftGeomUtils.geomType(edge) == "Line":
self.node.extend([edge.Vertexes[0].Point,edge.Vertexes[1].Point])
v1 = None
v2 =None
for i,v in enumerate(sel[0].Object.Shape.Vertexes):
if v.Point == edge.Vertexes[0].Point:
v1 = i
if v.Point == edge.Vertexes[1].Point:
v2 = i
if (v1 != None) and (v2 != None):
self.link = [sel[0].Object,v1,v2]
elif DraftGeomUtils.geomType(edge) == "Circle":
self.node.extend([edge.Curve.Center,edge.Vertexes[0].Point])
self.edges = [edge]
self.arcmode = "diameter"
self.link = [sel[0].Object,n]
def hasMeasures(self): def hasMeasures(self):
"checks if only measurements objects are selected" "checks if only measurements objects are selected"
@ -1759,7 +1787,15 @@ class Dimension(Creator):
self.commit(translate("draft","Create Dimension"), self.commit(translate("draft","Create Dimension"),
['Draft.makeAngularDimension(center='+DraftVecUtils.toString(self.center)+',angles=['+str(self.angledata[0])+','+str(self.angledata[1])+'],p3='+DraftVecUtils.toString(self.node[-1])+',normal='+normal+')']) ['Draft.makeAngularDimension(center='+DraftVecUtils.toString(self.center)+',angles=['+str(self.angledata[0])+','+str(self.angledata[1])+'],p3='+DraftVecUtils.toString(self.node[-1])+',normal='+normal+')'])
elif self.link and (not self.arcmode): elif self.link and (not self.arcmode):
self.commit(translate("draft","Create Dimension"), ops = []
if self.force == 1:
self.commit(translate("draft","Create Dimension"),
['dim = Draft.makeDimension(FreeCAD.ActiveDocument.'+self.link[0].Name+','+str(self.link[1])+','+str(self.link[2])+','+DraftVecUtils.toString(self.node[2])+')','dim.Direction=FreeCAD.Vector(0,1,0)'])
elif self.force == 2:
self.commit(translate("draft","Create Dimension"),
['dim = Draft.makeDimension(FreeCAD.ActiveDocument.'+self.link[0].Name+','+str(self.link[1])+','+str(self.link[2])+','+DraftVecUtils.toString(self.node[2])+')','dim.Direction=FreeCAD.Vector(1,0,0)'])
else:
self.commit(translate("draft","Create Dimension"),
['Draft.makeDimension(FreeCAD.ActiveDocument.'+self.link[0].Name+','+str(self.link[1])+','+str(self.link[2])+','+DraftVecUtils.toString(self.node[2])+')']) ['Draft.makeDimension(FreeCAD.ActiveDocument.'+self.link[0].Name+','+str(self.link[1])+','+str(self.link[2])+','+DraftVecUtils.toString(self.node[2])+')'])
elif self.arcmode: elif self.arcmode:
self.commit(translate("draft","Create Dimension"), self.commit(translate("draft","Create Dimension"),
@ -1790,9 +1826,9 @@ class Dimension(Creator):
elif arg["Type"] == "SoLocation2Event": #mouse movement detection elif arg["Type"] == "SoLocation2Event": #mouse movement detection
import DraftGeomUtils import DraftGeomUtils
shift = hasMod(arg,MODCONSTRAIN) shift = hasMod(arg,MODCONSTRAIN)
self.point,ctrlPoint,self.info = getPoint(self,arg,noTracker=(len(self.node)>0))
if self.arcmode or self.point2: if self.arcmode or self.point2:
setMod(arg,MODCONSTRAIN,False) setMod(arg,MODCONSTRAIN,False)
self.point,ctrlPoint,self.info = getPoint(self,arg,noTracker=(len(self.node)>0))
if (hasMod(arg,MODALT) or self.selectmode) and (len(self.node)<3): if (hasMod(arg,MODALT) or self.selectmode) and (len(self.node)<3):
self.dimtrack.off() self.dimtrack.off()
if not self.altdown: if not self.altdown: