From b38ba6e11d1884ebc9a3140760ae7763f08e1ca3 Mon Sep 17 00:00:00 2001 From: vocx-fc Date: Fri, 31 Jan 2020 13:09:43 -0600 Subject: [PATCH] Draft: lowercase use_link option for arrays Inside the class `_DraftLink` the `onDocumentRestored` function tests for the existence of the old attribute `useLink`. If it is present, it uses it to define the new variable name `use_link`. This is done use Python naming conventions. The old `useLink` is deleted so it is not longer saved together with the object when the document is saved. --- src/Mod/Draft/Draft.py | 52 +++++++++++++------ src/Mod/Draft/DraftTools.py | 12 ++--- src/Mod/Draft/draftobjects/circulararray.py | 2 +- src/Mod/Draft/draftobjects/orthoarray.py | 4 +- src/Mod/Draft/draftobjects/polararray.py | 2 +- .../drafttaskpanels/task_circulararray.py | 3 +- .../Draft/drafttaskpanels/task_orthoarray.py | 2 +- .../Draft/drafttaskpanels/task_polararray.py | 3 +- 8 files changed, 50 insertions(+), 30 deletions(-) diff --git a/src/Mod/Draft/Draft.py b/src/Mod/Draft/Draft.py index 47d2cb5345..5d0a1d9816 100644 --- a/src/Mod/Draft/Draft.py +++ b/src/Mod/Draft/Draft.py @@ -688,7 +688,7 @@ def makeBlock(objectslist): select(obj) return obj -def makeArray(baseobject,arg1,arg2,arg3,arg4=None,arg5=None,arg6=None,name="Array",useLink=False): +def makeArray(baseobject,arg1,arg2,arg3,arg4=None,arg5=None,arg6=None,name="Array",use_link=False): """makeArray(object,xvector,yvector,xnum,ynum,[name]) for rectangular array, or makeArray(object,xvector,yvector,zvector,xnum,ynum,znum,[name]) for rectangular array, or makeArray(object,center,totalangle,totalnum,[name]) for polar array, or @@ -707,7 +707,7 @@ def makeArray(baseobject,arg1,arg2,arg3,arg4=None,arg5=None,arg6=None,name="Arra if not FreeCAD.ActiveDocument: FreeCAD.Console.PrintError("No active document. Aborting\n") return - if useLink: + if use_link: obj = FreeCAD.ActiveDocument.addObject("Part::FeaturePython",name,_Array(None),None,True) else: obj = FreeCAD.ActiveDocument.addObject("Part::FeaturePython",name) @@ -742,7 +742,7 @@ def makeArray(baseobject,arg1,arg2,arg3,arg4=None,arg5=None,arg6=None,name="Arra obj.Angle = arg2 obj.NumberPolar = arg3 if gui: - if useLink: + if use_link: _ViewProviderDraftLink(obj.ViewObject) else: _ViewProviderDraftArray(obj.ViewObject) @@ -753,8 +753,8 @@ def makeArray(baseobject,arg1,arg2,arg3,arg4=None,arg5=None,arg6=None,name="Arra select(obj) return obj -def makePathArray(baseobject,pathobject,count,xlate=None,align=False,pathobjsubs=[],useLink=False): - """makePathArray(docobj,path,count,xlate,align,pathobjsubs,useLink): distribute +def makePathArray(baseobject,pathobject,count,xlate=None,align=False,pathobjsubs=[],use_link=False): + """makePathArray(docobj,path,count,xlate,align,pathobjsubs,use_link): distribute count copies of a document baseobject along a pathobject or subobjects of a pathobject. Optionally translates each copy by FreeCAD.Vector xlate direction and distance to adjust for difference in shape centre vs shape reference point. @@ -762,7 +762,7 @@ def makePathArray(baseobject,pathobject,count,xlate=None,align=False,pathobjsubs if not FreeCAD.ActiveDocument: FreeCAD.Console.PrintError("No active document. Aborting\n") return - if useLink: + if use_link: obj = FreeCAD.ActiveDocument.addObject("Part::FeaturePython","PathArray",_PathArray(None),None,True) else: obj = FreeCAD.ActiveDocument.addObject("Part::FeaturePython","PathArray") @@ -780,7 +780,7 @@ def makePathArray(baseobject,pathobject,count,xlate=None,align=False,pathobjsubs obj.Xlate = xlate obj.Align = align if gui: - if useLink: + if use_link: _ViewProviderDraftLink(obj.ViewObject) else: _ViewProviderDraftArray(obj.ViewObject) @@ -5146,7 +5146,7 @@ class _Shape2DView(_DraftObject): class _DraftLink(_DraftObject): def __init__(self,obj,tp): - self.useLink = False if obj else True + self.use_link = False if obj else True _DraftObject.__init__(self,obj,tp) if obj: self.attach(obj) @@ -5158,11 +5158,11 @@ class _DraftLink(_DraftObject): if isinstance(state,dict): self.__dict__ = state else: - self.useLink = False + self.use_link = False _DraftObject.__setstate__(self,state) def attach(self,obj): - if self.useLink: + if self.use_link: obj.addExtension('App::LinkExtensionPython', None) self.linkSetup(obj) @@ -5195,12 +5195,30 @@ class _DraftLink(_DraftObject): obj.configLinkProperty('LinkTransform','ColoredElements') def getViewProviderName(self,_obj): - if self.useLink: + if self.use_link: return 'Gui::ViewProviderLinkPython' return '' + def migrate_attributes(self, obj): + """Migrate old attribute names to new names if they exist. + + This is done to comply with Python guidelines or fix small issues + in older code. + """ + if hasattr(self, "useLink"): + # This is only needed for some models created in 0.19 + # while it was in development. Afterwards, + # all models should use 'use_link' by default + # and this won't be run. + self.use_link = bool(self.useLink) + FreeCAD.Console.PrintWarning("Migrating 'useLink' to 'use_link', " + "{} ({})\n".format(obj.Label, + obj.TypeId)) + del self.useLink + def onDocumentRestored(self, obj): - if self.useLink: + self.migrate_attributes(obj) + if self.use_link: self.linkSetup(obj) else: obj.setPropertyStatus('Shape','-Transient') @@ -5214,7 +5232,7 @@ class _DraftLink(_DraftObject): import Part import DraftGeomUtils - if self.useLink: + if self.use_link: if not getattr(obj,'ExpandArray',True) or obj.Count != len(pls): obj.setPropertyStatus('PlacementList','-Immutable') obj.PlacementList = pls @@ -5244,11 +5262,11 @@ class _DraftLink(_DraftObject): if not DraftGeomUtils.isNull(pl): obj.Placement = pl - if self.useLink: + if self.use_link: return False # return False to call LinkExtension::execute() def onChanged(self, obj, prop): - if not getattr(self,'useLink',False): + if not getattr(self,'use_link',False): return if prop == 'Fuse': if obj.Fuse: @@ -5287,7 +5305,7 @@ class _Array(_DraftLink): obj.addProperty("App::PropertyInteger","Symmetry","Draft",QT_TRANSLATE_NOOP("App::Property","number of circles")) obj.addProperty("App::PropertyBool","Fuse","Draft",QT_TRANSLATE_NOOP("App::Property","Specifies if copies must be fused (slower)")) obj.Fuse = False - if self.useLink: + if self.use_link: obj.addProperty("App::PropertyInteger","Count","Draft",'') obj.addProperty("App::PropertyBool","ExpandArray","Draft", QT_TRANSLATE_NOOP("App::Property","Show array element as children object")) @@ -5421,7 +5439,7 @@ class _PathArray(_DraftLink): obj.Xlate = FreeCAD.Vector(0,0,0) obj.Align = False - if self.useLink: + if self.use_link: obj.addProperty("App::PropertyBool","ExpandArray","Draft", QT_TRANSLATE_NOOP("App::Property","Show array element as children object")) obj.ExpandArray = False diff --git a/src/Mod/Draft/DraftTools.py b/src/Mod/Draft/DraftTools.py index 76726b5353..a5ce4bec8c 100644 --- a/src/Mod/Draft/DraftTools.py +++ b/src/Mod/Draft/DraftTools.py @@ -4635,9 +4635,9 @@ class Draft2Sketch(Modifier): class Array(Modifier): """The Shape2DView FreeCAD command definition""" - def __init__(self,useLink=False): + def __init__(self,use_link=False): Modifier.__init__(self) - self.useLink = useLink + self.use_link = use_link def GetResources(self): return {'Pixmap' : 'Draft_Array', @@ -4661,7 +4661,7 @@ class Array(Modifier): obj = FreeCADGui.Selection.getSelection()[0] FreeCADGui.addModule("Draft") self.commit(translate("draft","Array"), - ['obj = Draft.makeArray(FreeCAD.ActiveDocument.{},FreeCAD.Vector(1,0,0),FreeCAD.Vector(0,1,0),2,2,useLink={})'.format(obj.Name,self.useLink), + ['obj = Draft.makeArray(FreeCAD.ActiveDocument.{},FreeCAD.Vector(1,0,0),FreeCAD.Vector(0,1,0),2,2,use_link={})'.format(obj.Name,self.use_link), 'Draft.autogroup(obj)', 'FreeCAD.ActiveDocument.recompute()']) self.finish() @@ -4680,9 +4680,9 @@ class LinkArray(Array): class PathArray(Modifier): """The PathArray FreeCAD command definition""" - def __init__(self,useLink=False): + def __init__(self,use_link=False): Modifier.__init__(self) - self.useLink = useLink + self.use_link = use_link def GetResources(self): return {'Pixmap' : 'Draft_PathArray', @@ -4712,7 +4712,7 @@ class PathArray(Modifier): defCount = 4 defAlign = False FreeCAD.ActiveDocument.openTransaction("PathArray") - Draft.makePathArray(base,path,defCount,defXlate,defAlign,pathsubs,useLink=self.useLink) + Draft.makePathArray(base,path,defCount,defXlate,defAlign,pathsubs,use_link=self.use_link) FreeCAD.ActiveDocument.commitTransaction() FreeCAD.ActiveDocument.recompute() # feature won't appear until recompute. self.finish() diff --git a/src/Mod/Draft/draftobjects/circulararray.py b/src/Mod/Draft/draftobjects/circulararray.py index 07982d16e4..803b923b47 100644 --- a/src/Mod/Draft/draftobjects/circulararray.py +++ b/src/Mod/Draft/draftobjects/circulararray.py @@ -41,5 +41,5 @@ def make_circular_array(obj, obj = Draft.makeArray(obj, arg1=r_distance, arg2=tan_distance, arg3=axis, arg4=center, arg5=number, arg6=symmetry, - useLink=use_link) + use_link=use_link) return obj diff --git a/src/Mod/Draft/draftobjects/orthoarray.py b/src/Mod/Draft/draftobjects/orthoarray.py index f8d415f614..e1193fda8d 100644 --- a/src/Mod/Draft/draftobjects/orthoarray.py +++ b/src/Mod/Draft/draftobjects/orthoarray.py @@ -42,7 +42,7 @@ def make_ortho_array(obj, obj = Draft.makeArray(obj, arg1=v_x, arg2=v_y, arg3=v_z, arg4=n_x, arg5=n_y, arg6=n_z, - useLink=use_link) + use_link=use_link) return obj @@ -56,5 +56,5 @@ def make_ortho_array2(obj, obj = Draft.makeArray(obj, arg1=v_x, arg2=v_y, arg3=n_x, arg4=n_y, - useLink=use_link) + use_link=use_link) return obj diff --git a/src/Mod/Draft/draftobjects/polararray.py b/src/Mod/Draft/draftobjects/polararray.py index ccf155ae44..52d1573f73 100644 --- a/src/Mod/Draft/draftobjects/polararray.py +++ b/src/Mod/Draft/draftobjects/polararray.py @@ -38,5 +38,5 @@ def make_polar_array(obj, """ obj = Draft.makeArray(obj, arg1=center, arg2=angle, arg3=number, - useLink=use_link) + use_link=use_link) return obj diff --git a/src/Mod/Draft/drafttaskpanels/task_circulararray.py b/src/Mod/Draft/drafttaskpanels/task_circulararray.py index a0405bbf52..d1e7d4327d 100644 --- a/src/Mod/Draft/drafttaskpanels/task_circulararray.py +++ b/src/Mod/Draft/drafttaskpanels/task_circulararray.py @@ -218,7 +218,8 @@ class TaskPanelCircularArray: _cmd += "arg4=" + DraftVecUtils.toString(self.center) + ", " _cmd += "arg5=" + str(self.number) + ", " _cmd += "arg6=" + str(self.symmetry) + ", " - _cmd += "useLink=" + str(self.use_link) + ")" + _cmd += "use_link=" + str(self.use_link) + _cmd += ")" _cmd_list = ["FreeCADGui.addModule('Draft')", _cmd, diff --git a/src/Mod/Draft/drafttaskpanels/task_orthoarray.py b/src/Mod/Draft/drafttaskpanels/task_orthoarray.py index 170be5e810..6a709602a0 100644 --- a/src/Mod/Draft/drafttaskpanels/task_orthoarray.py +++ b/src/Mod/Draft/drafttaskpanels/task_orthoarray.py @@ -209,7 +209,7 @@ class TaskPanelOrthoArray: _cmd += "arg4=" + str(self.n_X) + ", " _cmd += "arg5=" + str(self.n_Y) + ", " _cmd += "arg6=" + str(self.n_Z) + ", " - _cmd += "useLink=" + str(self.use_link) + _cmd += "use_link=" + str(self.use_link) _cmd += ")" _cmd_list = ["FreeCADGui.addModule('Draft')", diff --git a/src/Mod/Draft/drafttaskpanels/task_polararray.py b/src/Mod/Draft/drafttaskpanels/task_polararray.py index 64292973f2..042a18a8a3 100644 --- a/src/Mod/Draft/drafttaskpanels/task_polararray.py +++ b/src/Mod/Draft/drafttaskpanels/task_polararray.py @@ -195,7 +195,8 @@ class TaskPanelPolarArray: _cmd += "arg1=" + DraftVecUtils.toString(self.center) + ", " _cmd += "arg2=" + str(self.angle) + ", " _cmd += "arg3=" + str(self.number) + ", " - _cmd += "useLink=" + str(self.use_link) + ")" + _cmd += "use_link=" + str(self.use_link) + _cmd += ")" _cmd_list = ["FreeCADGui.addModule('Draft')", _cmd,