Skip to content

Commit

Permalink
Draft: Fixed broken backwards compatibility with annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
yorikvanhavre committed Apr 21, 2020
1 parent 1090da6 commit dc742e8
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 39 deletions.
73 changes: 40 additions & 33 deletions src/Mod/Draft/draftviewproviders/view_dimension.py
Expand Up @@ -225,6 +225,10 @@ def __init__(self, vobj):

def attach(self, vobj):
'''Setup the scene sub-graph of the view provider'''

# backwards compatibility
self.ScaleMultiplier = 1.00

self.Object = vobj.Object
self.color = coin.SoBaseColor()
self.font = coin.SoFont()
Expand Down Expand Up @@ -341,8 +345,8 @@ def updateData(self, obj, prop):
self.p2 = self.p1
self.p3 = self.p4
if proj:
if hasattr(obj.ViewObject,"ExtLines") and hasattr(obj.ViewObject,"ScaleMultiplier"):
dmax = obj.ViewObject.ExtLines.Value * obj.ViewObject.ScaleMultiplier
if hasattr(obj.ViewObject,"ExtLines"):
dmax = obj.ViewObject.ExtLines.Value * self.ScaleMultiplier
if dmax and (proj.Length > dmax):
if (dmax > 0):
self.p1 = self.p2.add(DraftVecUtils.scaleTo(proj,dmax))
Expand Down Expand Up @@ -402,8 +406,8 @@ def updateData(self, obj, prop):
rot3 = App.Placement(DraftVecUtils.getPlaneRotation(u3,v3,norm)).Rotation.Q
self.transExtOvershoot1.rotation.setValue((rot3[0],rot3[1],rot3[2],rot3[3]))
self.transExtOvershoot2.rotation.setValue((rot3[0],rot3[1],rot3[2],rot3[3]))
if hasattr(obj.ViewObject,"TextSpacing") and hasattr(obj.ViewObject,"ScaleMultiplier"):
ts = obj.ViewObject.TextSpacing.Value * obj.ViewObject.ScaleMultiplier
if hasattr(obj.ViewObject,"TextSpacing"):
ts = obj.ViewObject.TextSpacing.Value * self.ScaleMultiplier
offset = DraftVecUtils.scaleTo(v1,ts)
else:
offset = DraftVecUtils.scaleTo(v1,0.05)
Expand Down Expand Up @@ -474,11 +478,13 @@ def updateData(self, obj, prop):
def onChanged(self, vobj, prop):
"""called when a view property has changed"""
if prop == "ScaleMultiplier" and hasattr(vobj,"ScaleMultiplier"):
if vobj.ScaleMultiplier: # don't set if zero
self.ScaleMultiplier = vobj.ScaleMultiplier
# update all dimension values
if hasattr(self,"font"):
self.font.size = vobj.FontSize.Value*vobj.ScaleMultiplier
self.font.size = vobj.FontSize.Value * self.ScaleMultiplier
if hasattr(self,"font3d"):
self.font3d.size = vobj.FontSize.Value*100*vobj.ScaleMultiplier
self.font3d.size = vobj.FontSize.Value * 100 * self.ScaleMultiplier
if hasattr(self,"node") and hasattr(self,"p2") and hasattr(vobj,"ArrowSize"):
self.remove_dim_arrows()
self.draw_dim_arrows(vobj)
Expand All @@ -492,10 +498,10 @@ def onChanged(self, vobj, prop):
vobj.Object.touch()

elif (prop == "FontSize") and hasattr(vobj,"FontSize"):
if hasattr(self,"font") and hasattr(vobj,"ScaleMultiplier"):
self.font.size = vobj.FontSize.Value*vobj.ScaleMultiplier
if hasattr(self,"font3d") and hasattr(vobj,"ScaleMultiplier"):
self.font3d.size = vobj.FontSize.Value*100*vobj.ScaleMultiplier
if hasattr(self,"font"):
self.font.size = vobj.FontSize.Value * self.ScaleMultiplier
if hasattr(self,"font3d"):
self.font3d.size = vobj.FontSize.Value * 100 * self.ScaleMultiplier
vobj.Object.touch()

elif (prop == "FontName") and hasattr(vobj,"FontName"):
Expand All @@ -514,22 +520,19 @@ def onChanged(self, vobj, prop):

elif (prop in ["ArrowSize","ArrowType"]) and hasattr(vobj,"ArrowSize"):
if hasattr(self,"node") and hasattr(self,"p2"):
if hasattr(vobj,"ScaleMultiplier"):
self.remove_dim_arrows()
self.draw_dim_arrows(vobj)
vobj.Object.touch()
self.remove_dim_arrows()
self.draw_dim_arrows(vobj)
vobj.Object.touch()

elif (prop == "DimOvershoot") and hasattr(vobj,"DimOvershoot"):
if hasattr(vobj,"ScaleMultiplier"):
self.remove_dim_overshoot()
self.draw_dim_overshoot(vobj)
vobj.Object.touch()
self.remove_dim_overshoot()
self.draw_dim_overshoot(vobj)
vobj.Object.touch()

elif (prop == "ExtOvershoot") and hasattr(vobj,"ExtOvershoot"):
if hasattr(vobj,"ScaleMultiplier"):
self.remove_ext_overshoot()
self.draw_ext_overshoot(vobj)
vobj.Object.touch()
self.remove_ext_overshoot()
self.draw_ext_overshoot(vobj)
vobj.Object.touch()

elif (prop == "ShowLine") and hasattr(vobj,"ShowLine"):
if vobj.ShowLine:
Expand Down Expand Up @@ -559,7 +562,7 @@ def draw_dim_arrows(self, vobj):

# set scale
symbol = utils.ARROW_TYPES.index(vobj.ArrowType)
s = vobj.ArrowSize.Value * vobj.ScaleMultiplier
s = vobj.ArrowSize.Value * self.ScaleMultiplier
self.trans1.scaleFactor.setValue((s,s,s))
self.trans2.scaleFactor.setValue((s,s,s))

Expand Down Expand Up @@ -593,7 +596,7 @@ def draw_dim_overshoot(self, vobj):
from pivy import coin

# set scale
s = vobj.DimOvershoot.Value * vobj.ScaleMultiplier
s = vobj.DimOvershoot.Value * self.ScaleMultiplier
self.transDimOvershoot1.scaleFactor.setValue((s,s,s))
self.transDimOvershoot2.scaleFactor.setValue((s,s,s))

Expand Down Expand Up @@ -624,7 +627,7 @@ def draw_ext_overshoot(self, vobj):
from pivy import coin

# set scale
s = vobj.ExtOvershoot.Value * vobj.ScaleMultiplier
s = vobj.ExtOvershoot.Value * self.ScaleMultiplier
self.transExtOvershoot1.scaleFactor.setValue((s,s,s))
self.transExtOvershoot2.scaleFactor.setValue((s,s,s))

Expand Down Expand Up @@ -728,6 +731,8 @@ def attach(self, vobj):
self.onChanged(vobj,"FontName")
self.onChanged(vobj,"ArrowType")
self.onChanged(vobj,"LineColor")
# backwards compatibility
self.ScaleMultiplier = 1.00

def updateData(self, obj, prop):
if hasattr(self,"arc"):
Expand Down Expand Up @@ -853,21 +858,23 @@ def updateData(self, obj, prop):

def onChanged(self, vobj, prop):
if prop == "ScaleMultiplier" and hasattr(vobj,"ScaleMultiplier"):
if vobj.ScaleMultiplier:
self.ScaleMultiplier = vobj.ScaleMultiplier
# update all dimension values
if hasattr(self,"font"):
self.font.size = vobj.FontSize.Value*vobj.ScaleMultiplier
self.font.size = vobj.FontSize.Value * self.ScaleMultiplier
if hasattr(self,"font3d"):
self.font3d.size = vobj.FontSize.Value*100*vobj.ScaleMultiplier
self.font3d.size = vobj.FontSize.Value * 100 * self.ScaleMultiplier
if hasattr(self,"node") and hasattr(self,"p2") and hasattr(vobj,"ArrowSize"):
self.remove_dim_arrows()
self.draw_dim_arrows(vobj)
self.updateData(vobj.Object,"Start")
vobj.Object.touch()
elif prop == "FontSize" and hasattr(vobj,"ScaleMultiplier"):
elif prop == "FontSize":
if hasattr(self,"font"):
self.font.size = vobj.FontSize.Value*vobj.ScaleMultiplier
self.font.size = vobj.FontSize.Value * self.ScaleMultiplier
if hasattr(self,"font3d"):
self.font3d.size = vobj.FontSize.Value*100*vobj.ScaleMultiplier
self.font3d.size = vobj.FontSize.Value * 100 * self.ScaleMultiplier
vobj.Object.touch()
elif prop == "FontName":
if hasattr(self,"font") and hasattr(self,"font3d"):
Expand All @@ -880,7 +887,7 @@ def onChanged(self, vobj, prop):
elif prop == "LineWidth":
if hasattr(self,"drawstyle"):
self.drawstyle.lineWidth = vobj.LineWidth
elif prop in ["ArrowSize","ArrowType"] and hasattr(vobj,"ScaleMultiplier"):
elif prop in ["ArrowSize","ArrowType"]:
if hasattr(self,"node") and hasattr(self,"p2"):
self.remove_dim_arrows()
self.draw_dim_arrows(vobj)
Expand All @@ -901,7 +908,7 @@ def draw_dim_arrows(self, vobj):

# set scale
symbol = utils.ARROW_TYPES.index(vobj.ArrowType)
s = vobj.ArrowSize.Value * vobj.ScaleMultiplier
s = vobj.ArrowSize.Value * self.ScaleMultiplier
self.trans1.scaleFactor.setValue((s,s,s))
self.trans2.scaleFactor.setValue((s,s,s))

Expand All @@ -926,4 +933,4 @@ def draw_dim_arrows(self, vobj):
self.node3d.insertChild(self.marks,2)

def getIcon(self):
return ":/icons/Draft_DimensionAngular.svg"
return ":/icons/Draft_DimensionAngular.svg"
18 changes: 12 additions & 6 deletions src/Mod/Draft/draftviewproviders/view_text.py
Expand Up @@ -93,6 +93,10 @@ def claimChildren(self):

def attach(self,vobj):
'''Setup the scene sub-graph of the view provider'''

# backwards compatibility
self.ScaleMultiplier = 1.00

self.mattext = coin.SoMaterial()
textdrawstyle = coin.SoDrawStyle()
textdrawstyle.style = coin.SoDrawStyle.FILLED
Expand Down Expand Up @@ -123,7 +127,6 @@ def attach(self,vobj):
self.onChanged(vobj,"Justification")
self.onChanged(vobj,"LineSpacing")


def getDisplayModes(self,vobj):
return ["2D text","3D text"]

Expand All @@ -148,8 +151,11 @@ def updateData(self,obj,prop):

def onChanged(self,vobj,prop):
if prop == "ScaleMultiplier":
if "ScaleMultiplier" in vobj.PropertiesList and "FontSize" in vobj.PropertiesList:
self.font.size = vobj.FontSize.Value * vobj.ScaleMultiplier
if "ScaleMultiplier" in vobj.PropertiesList:
if vobj.ScaleMultiplier:
self.ScaleMultiplier = vobj.ScaleMultiplier
if "FontSize" in vobj.PropertiesList:
self.font.size = vobj.FontSize.Value * self.ScaleMultiplier
elif prop == "TextColor":
if "TextColor" in vobj.PropertiesList:
l = vobj.TextColor
Expand All @@ -158,8 +164,8 @@ def onChanged(self,vobj,prop):
if "FontName" in vobj.PropertiesList:
self.font.name = vobj.FontName.encode("utf8")
elif prop == "FontSize":
if "FontSize" in vobj.PropertiesList and "ScaleMultiplier" in vobj.PropertiesList:
self.font.size = vobj.FontSize.Value * vobj.ScaleMultiplier
if "FontSize" in vobj.PropertiesList:
self.font.size = vobj.FontSize.Value * self.ScaleMultiplier
elif prop == "Justification":
try:
if getattr(vobj, "Justification", None) is not None:
Expand All @@ -177,4 +183,4 @@ def onChanged(self,vobj,prop):
elif prop == "LineSpacing":
if "LineSpacing" in vobj.PropertiesList:
self.text2d.spacing = vobj.LineSpacing
self.text3d.spacing = vobj.LineSpacing
self.text3d.spacing = vobj.LineSpacing

2 comments on commit dc742e8

@vocx-fc
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@carlopav Check what Yorik is adding. This was necessary to support older objects, because older objects don't already have the new property, so if you test for the new property it will not be found. So defining a default self.ScaleMultiplier = 1.00 makes sense. You should double check your code with older files to make sure they work as intended. This is also very very critical with all objects that you are moving from Draft.py. The GuiCommands are relatively easy to move, because they are not saved into the FreeCAD file, but the Draft objects are, so we must be extra careful.

@carlopav
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yup, I saw the changes. at the time I added the code this was still not clear to me.
Another approach perhaps would have been to check onDocumentRestored if the new property exists and add it?

Please sign in to comment.