assembly: replace hasattr() with hasProperty()

This commit is contained in:
Zheng, Lei 2019-10-24 14:35:46 +08:00
parent b7da4ecc41
commit f1d1547e8e

View File

@ -25,6 +25,13 @@ def getProxy(obj,tp):
checkType(obj,tp) checkType(obj,tp)
return obj.Proxy return obj.Proxy
def hasProperty(obj,prop):
try:
obj.getPropertyByName(prop)
return True
except Exception:
return False
def getLinkProperty(obj,name,default=None,writable=False): def getLinkProperty(obj,name,default=None,writable=False):
try: try:
# obj = obj.getLinkedObject(True) # obj = obj.getLinkedObject(True)
@ -85,7 +92,7 @@ def editGroup(obj,children,notouch=None):
if parent and 'Touched' in parent.State: if parent and 'Touched' in parent.State:
parent = None parent = None
if not hasattr(obj,'NoTouch'): if not hasProperty(obj,'NoTouch'):
notouch = False notouch = False
elif notouch is None: elif notouch is None:
if (isTypeOf(parent,AsmConstraintGroup) or \ if (isTypeOf(parent,AsmConstraintGroup) or \
@ -294,7 +301,7 @@ class AsmPartGroup(AsmGroup):
def linkSetup(self,obj): def linkSetup(self,obj):
super(AsmPartGroup,self).linkSetup(obj) super(AsmPartGroup,self).linkSetup(obj)
if not hasattr(obj,'DerivedFrom'): if not hasProperty(obj,'DerivedFrom'):
obj.addProperty('App::PropertyLink','DerivedFrom','Base','') obj.addProperty('App::PropertyLink','DerivedFrom','Base','')
self.derivedParts = None self.derivedParts = None
@ -475,15 +482,15 @@ class AsmElement(AsmBase):
def linkSetup(self,obj): def linkSetup(self,obj):
super(AsmElement,self).linkSetup(obj) super(AsmElement,self).linkSetup(obj)
if not hasattr(obj,'Offset'): if not hasProperty(obj,'Offset'):
obj.addProperty("App::PropertyPlacement","Offset"," Link",'') obj.addProperty("App::PropertyPlacement","Offset"," Link",'')
if not hasattr(obj,'Placement'): if not hasProperty(obj,'Placement'):
obj.addProperty("App::PropertyPlacement","Placement"," Link",'') obj.addProperty("App::PropertyPlacement","Placement"," Link",'')
obj.setPropertyStatus('Placement','Hidden') obj.setPropertyStatus('Placement','Hidden')
if not hasattr(obj,'LinkTransform'): if not hasProperty(obj,'LinkTransform'):
obj.addProperty("App::PropertyBool","LinkTransform"," Link",'') obj.addProperty("App::PropertyBool","LinkTransform"," Link",'')
obj.LinkTransform = True obj.LinkTransform = True
if not hasattr(obj,'Detach'): if not hasProperty(obj,'Detach'):
obj.addProperty('App::PropertyBool','Detach', ' Link','') obj.addProperty('App::PropertyBool','Detach', ' Link','')
obj.setPropertyStatus('LinkTransform',['Immutable','Hidden']) obj.setPropertyStatus('LinkTransform',['Immutable','Hidden'])
obj.setPropertyStatus('LinkedObject','ReadOnly') obj.setPropertyStatus('LinkedObject','ReadOnly')
@ -666,7 +673,7 @@ class AsmElement(AsmBase):
if obj.Offset.isIdentity(): if obj.Offset.isIdentity():
objPla = FreeCAD.Placement() objPla = FreeCAD.Placement()
else: else:
if hasattr(obj,'Radius'): if hasProperty(obj,'Radius'):
s = shape.SubShapes[0] s = shape.SubShapes[0]
else: else:
s = shape s = shape
@ -1051,7 +1058,7 @@ class ViewProviderAsmElement(ViewProviderAsmOnTop):
def showCS(self): def showCS(self):
vobj = getattr(self,'ViewObject',None) vobj = getattr(self,'ViewObject',None)
if not vobj or hasattr(vobj.Object,'Radius'): if not vobj or hasProperty(vobj.Object,'Radius'):
return return
if getattr(vobj,'ShowCS',False) or\ if getattr(vobj,'ShowCS',False) or\
gui.AsmCmdManager.ShowElementCS: gui.AsmCmdManager.ShowElementCS:
@ -1334,7 +1341,7 @@ def getElementInfo(parent,subname,
# object. We trim the subname reference to be relative to the part # object. We trim the subname reference to be relative to the part
# object. And obtain the shape before part's Placement by setting # object. And obtain the shape before part's Placement by setting
# 'transform' to False # 'transform' to False
if checkPlacement and not hasattr(part,'Placement'): if checkPlacement and not hasProperty(part,'Placement'):
raise RuntimeError('part has no placement') raise RuntimeError('part has no placement')
subname = '.'.join(names[1:]) subname = '.'.join(names[1:])
if not shape: if not shape:
@ -1377,17 +1384,17 @@ class AsmElementLink(AsmBase):
if parent: if parent:
self.parent = parent.Proxy self.parent = parent.Proxy
obj.setPropertyStatus('LinkedObject','ReadOnly') obj.setPropertyStatus('LinkedObject','ReadOnly')
if not hasattr(obj,'Offset'): if not hasProperty(obj,'Offset'):
obj.addProperty("App::PropertyPlacement","Offset"," Link",'') obj.addProperty("App::PropertyPlacement","Offset"," Link",'')
if not hasattr(obj,'Placement'): if not hasProperty(obj,'Placement'):
obj.addProperty("App::PropertyPlacement","Placement"," Link",'') obj.addProperty("App::PropertyPlacement","Placement"," Link",'')
obj.setPropertyStatus('Placement','Hidden') obj.setPropertyStatus('Placement','Hidden')
if not hasattr(obj,'LinkTransform'): if not hasProperty(obj,'LinkTransform'):
obj.addProperty("App::PropertyBool","LinkTransform"," Link",'') obj.addProperty("App::PropertyBool","LinkTransform"," Link",'')
obj.LinkTransform = True obj.LinkTransform = True
obj.setPropertyStatus('LinkTransform',['Immutable','Hidden']) obj.setPropertyStatus('LinkTransform',['Immutable','Hidden'])
obj.configLinkProperty('LinkedObject','Placement','LinkTransform') obj.configLinkProperty('LinkedObject','Placement','LinkTransform')
if hasattr(obj,'Count'): if hasProperty(obj,'Count'):
obj.configLinkProperty('PlacementList', obj.configLinkProperty('PlacementList',
'ShowElement',ElementCount='Count') 'ShowElement',ElementCount='Count')
self.info = None self.info = None
@ -1511,7 +1518,7 @@ class AsmElementLink(AsmBase):
obj.Label = linked.Label obj.Label = linked.Label
return return
elif prop == 'AutoCount': elif prop == 'AutoCount':
if obj.AutoCount and hasattr(obj,'ShowElement'): if obj.AutoCount and hasProperty(obj,'ShowElement'):
self.parent.checkMultiply() self.parent.checkMultiply()
if prop not in self._MyIgnoredProperties and \ if prop not in self._MyIgnoredProperties and \
not Constraint.isDisabled(self.parent.Object): not Constraint.isDisabled(self.parent.Object):
@ -1566,7 +1573,7 @@ class AsmElementLink(AsmBase):
info = getElementInfo(owner,subname) info = getElementInfo(owner,subname)
radius = utils.getElementCircular(info.Shape,True) radius = utils.getElementCircular(info.Shape,True)
if radius and not checkOnly and not hasattr(obj,'NoExpand'): if radius and not checkOnly and not hasProperty(obj,'NoExpand'):
touched = 'Touched' in obj.State touched = 'Touched' in obj.State
obj.addProperty('App::PropertyBool','NoExpand','', obj.addProperty('App::PropertyBool','NoExpand','',
'Disable auto inclusion of coplanar edges '\ 'Disable auto inclusion of coplanar edges '\
@ -1948,7 +1955,7 @@ class AsmConstraint(AsmGroup):
'constraint multiplication'.format(info.PartName)) 'constraint multiplication'.format(info.PartName))
touched = 'Touched' in firstChild.State touched = 'Touched' in firstChild.State
if not hasattr(firstChild,'Count'): if not hasProperty(firstChild,'Count'):
firstChild.addProperty("App::PropertyInteger","Count",'','') firstChild.addProperty("App::PropertyInteger","Count",'','')
firstChild.setPropertyStatus('Count','ReadOnly') firstChild.setPropertyStatus('Count','ReadOnly')
firstChild.addProperty("App::PropertyBool","AutoCount",'', firstChild.addProperty("App::PropertyBool","AutoCount",'',
@ -2552,7 +2559,7 @@ class AsmConstraintGroup(AsmGroup):
def linkSetup(self,obj): def linkSetup(self,obj):
super(AsmConstraintGroup,self).linkSetup(obj) super(AsmConstraintGroup,self).linkSetup(obj)
if not hasattr(obj,'_Version'): if not hasProperty(obj,'_Version'):
obj.addProperty("App::PropertyInteger","_Version","Base",'') obj.addProperty("App::PropertyInteger","_Version","Base",'')
obj.setPropertyStatus('_Version',['Hidden','Output']) obj.setPropertyStatus('_Version',['Hidden','Output'])
@ -3478,12 +3485,12 @@ class Assembly(AsmGroup):
self.parts = set() self.parts = set()
self.partArrays = set() self.partArrays = set()
obj.configLinkProperty('Placement') obj.configLinkProperty('Placement')
if not hasattr(obj,'ColoredElements'): if not hasProperty(obj,'ColoredElements'):
obj.addProperty("App::PropertyLinkSubHidden", obj.addProperty("App::PropertyLinkSubHidden",
"ColoredElements","Base",'') "ColoredElements","Base",'')
obj.setPropertyStatus('ColoredElements',('Hidden','Immutable')) obj.setPropertyStatus('ColoredElements',('Hidden','Immutable'))
obj.configLinkProperty('ColoredElements') obj.configLinkProperty('ColoredElements')
if not hasattr(obj,'Freeze'): if not hasProperty(obj,'Freeze'):
obj.addProperty('App::PropertyBool','Freeze','Base','') obj.addProperty('App::PropertyBool','Freeze','Base','')
obj.setPropertyStatus('Freeze','PartialTrigger') obj.setPropertyStatus('Freeze','PartialTrigger')
super(Assembly,self).linkSetup(obj) super(Assembly,self).linkSetup(obj)
@ -3854,7 +3861,7 @@ class Assembly(AsmGroup):
if mapped: if mapped:
return mapped return mapped
if hasattr(obj,'Shape'): if hasProperty(obj,'Shape'):
return obj return obj
linked = obj.getLinkedObject(False) linked = obj.getLinkedObject(False)
@ -3922,7 +3929,7 @@ class ViewProviderAssembly(ViewProviderAsmGroup):
def attach(self,vobj): def attach(self,vobj):
super(ViewProviderAssembly,self).attach(vobj) super(ViewProviderAssembly,self).attach(vobj)
if not hasattr(vobj,'ShowParts'): if not hasProperty(vobj,'ShowParts'):
vobj.addProperty("App::PropertyBool","ShowParts"," Link") vobj.addProperty("App::PropertyBool","ShowParts"," Link")
def canAddToSceneGraph(self): def canAddToSceneGraph(self):