def _setupBitShape(self, obj, path=None): activeDoc = FreeCAD.ActiveDocument (doc, docOpened) = self._loadBitBody(obj, path) obj.Label = doc.RootObjects[0].Label self._deleteBitSetup(obj) obj.BitBody = obj.Document.copyObject(doc.RootObjects[0], True) if docOpened: FreeCAD.setActiveDocument(activeDoc.Name) FreeCAD.closeDocument(doc.Name) if obj.BitBody.ViewObject: obj.BitBody.ViewObject.Visibility = False self._copyBitShape(obj) for sketch in [ o for o in obj.BitBody.Group if o.TypeId == 'Sketcher::SketchObject' ]: for constraint in [c for c in sketch.Constraints if c.Name != '']: typ = ParameterTypeConstraint.get(constraint.Type) PathLog.track(constraint, typ) if typ is not None: parts = [p.strip() for p in constraint.Name.split(';')] prop = parts[0] desc = '' if len(parts) > 1: desc = parts[1] obj.addProperty(typ, prop, PropertyGroupBit, desc) obj.setEditorMode(prop, 1) value = constraint.Value if constraint.Type == 'Angle': value = value * 180 / math.pi PathUtil.setProperty(obj, prop, value)
def CreateFromAttrs(self, attrs, name='ToolBit'): # pylint: disable=protected-access obj = Factory.Create(name, attrs['shape']) obj.Label = attrs['name'] params = attrs['parameter'] for prop in params: PathUtil.setProperty(obj, prop, params[prop]) obj.Proxy._updateBitShape(obj) obj.Proxy.unloadBitBody(obj) params = attrs['attribute'] proto = AttributePrototype() uservals = {} for pname in params: #print(f"pname: {pname}") try: prop = proto.getProperty(pname) val = prop.valueFromString(params[pname]) prop.setupProperty(obj, pname, PropertyGroupAttribute, prop.valueFromString(params[pname])) except: # prop = obj.addProperty('App::PropertyString', pname, "Attribute", translate('PathTooolBit', 'User Defined Value')) # setattr(obj, pname, params[pname]) prop = proto.getProperty("UserAttributes") uservals.update({pname: params[pname]}) #prop.setupProperty(obj, pname, "UserAttributes", prop.valueFromString(params[pname])) if len(uservals.items()) > 0: prop.setupProperty(obj, "UserAttributes", PropertyGroupAttribute, uservals) # print("prop[%s] = %s (%s)" % (pname, params[pname], type(val))) #prop.setupProperty(obj, pname, PropertyGroupAttribute, prop.valueFromString(params[pname])) return obj
def CreateFromAttrs(self, attrs, name='ToolBit'): PathLog.debug(attrs) obj = Factory.Create(name, attrs['shape']) obj.Label = attrs['name'] params = attrs['parameter'] for prop in params: PathUtil.setProperty(obj, prop, params[prop]) obj.Proxy._updateBitShape(obj) obj.Proxy.unloadBitBody(obj) params = attrs['attribute'] proto = AttributePrototype() uservals = {} for pname in params: try: prop = proto.getProperty(pname) prop.setupProperty(obj, pname, PropertyGroupAttribute, prop.valueFromString(params[pname])) except Exception: prop = proto.getProperty("UserAttributes") uservals.update({pname: params[pname]}) if len(uservals.items()) > 0: prop.setupProperty(obj, "UserAttributes", PropertyGroupAttribute, uservals) return obj
def CreateFromAttrs(self, attrs, name='ToolBit', path=None): PathLog.track(attrs, path) obj = Factory.Create(name, attrs['shape'], path) obj.Label = attrs['name'] params = attrs['parameter'] for prop in params: PathUtil.setProperty(obj, prop, params[prop]) obj.Proxy._updateBitShape(obj) obj.Proxy.unloadBitBody(obj) return obj
def _setupProperty(self, obj, prop, orig): # extract property parameters and values so it can be copied val = orig.getPropertyByName(prop) typ = orig.getTypeIdOfProperty(prop) grp = orig.getGroupOfProperty(prop) dsc = orig.getDocumentationOfProperty(prop) obj.addProperty(typ, prop, grp, dsc) if 'App::PropertyEnumeration' == typ: setattr(obj, prop, orig.getEnumerationsOfProperty(prop)) obj.setEditorMode(prop, 1) PathUtil.setProperty(obj, prop, val)
def updateInputField(obj, prop, widget, onBeforeChange=None): """updateInputField(obj, prop, widget) ... update obj's property prop with the value of widget. The property's value is only assigned if the new value differs from the current value. This prevents onChanged notifications where the value didn't actually change. Gui::InputField and Gui::QuantitySpinBox widgets are supported - and the property can be of type Quantity or Float. If onBeforeChange is specified it is called before a new value is assigned to the property. Returns True if a new value was assigned, False otherwise (new value is the same as the current). """ PathLog.track() value = widget.property("rawValue") PathLog.track("value: {}".format(value)) attr = PathUtil.getProperty(obj, prop) attrValue = attr.Value if hasattr(attr, "Value") else attr isDiff = False if not PathGeom.isRoughly(attrValue, value): isDiff = True else: if hasattr(obj, "ExpressionEngine"): noExpr = True for (prp, expr) in obj.ExpressionEngine: if prp == prop: noExpr = False PathLog.debug('prop = "expression": {} = "{}"'.format( prp, expr)) value = FreeCAD.Units.Quantity( obj.evalExpression(expr)).Value if not PathGeom.isRoughly(attrValue, value): isDiff = True break if noExpr: widget.setReadOnly(False) widget.setStyleSheet("color: black") else: widget.setReadOnly(True) widget.setStyleSheet("color: gray") widget.update() if isDiff: PathLog.debug("updateInputField(%s, %s): %.2f -> %.2f" % (obj.Label, prop, attr, value)) if onBeforeChange: onBeforeChange(obj) PathUtil.setProperty(obj, prop, value) return True return False
def CreateFromAttrs(self, attrs, name='ToolBit'): # pylint: disable=protected-access obj = Factory.Create(name, attrs['shape']) obj.Label = attrs['name'] params = attrs['parameter'] for prop in params: PathUtil.setProperty(obj, prop, params[prop]) obj.Proxy._updateBitShape(obj) obj.Proxy.unloadBitBody(obj) params = attrs['attribute'] proto = AttributePrototype() for pname in params: prop = proto.getProperty(pname) val = prop.valueFromString(params[pname]) print("prop[%s] = %s (%s)" % (pname, params[pname], type(val))) prop.setupProperty(obj, pname, PropertyGroupAttribute, prop.valueFromString(params[pname])) return obj
def updateInputField(obj, prop, widget, onBeforeChange=None): '''updateInputField(obj, prop, widget) ... update obj's property prop with the value of widget. The property's value is only assigned if the new value differs from the current value. This prevents onChanged notifications where the value didn't actually change. Gui::InputField and Gui::QuantitySpinBox widgets are supported - and the property can be of type Quantity or Float. If onBeforeChange is specified it is called before a new value is assigned to the property. Returns True if a new value was assigned, False otherwise (new value is the same as the current). ''' value = FreeCAD.Units.Quantity(widget.text()).Value attr = PathUtil.getProperty(obj, prop) attrValue = attr.Value if hasattr(attr, 'Value') else attr if not PathGeom.isRoughly(attrValue, value): PathLog.debug("updateInputField(%s, %s): %.2f -> %.2f" % (obj.Label, prop, attr, value)) if onBeforeChange: onBeforeChange(obj) PathUtil.setProperty(obj, prop, value) return True return False
def onDocumentRestored(self, obj): # when files are shared it is essential to be able to change/set the shape file, # otherwise the file is hard to use # obj.setEditorMode('BitShape', 1) obj.setEditorMode("BitBody", 2) obj.setEditorMode("File", 1) obj.setEditorMode("Shape", 2) if not hasattr(obj, "BitPropertyNames"): obj.addProperty( "App::PropertyStringList", "BitPropertyNames", "Base", QT_TRANSLATE_NOOP( "App::Property", "List of all properties inherited from the bit"), ) propNames = [] for prop in obj.PropertiesList: if obj.getGroupOfProperty(prop) == "Bit": val = obj.getPropertyByName(prop) typ = obj.getTypeIdOfProperty(prop) dsc = obj.getDocumentationOfProperty(prop) obj.removeProperty(prop) obj.addProperty(typ, prop, PropertyGroupShape, dsc) PathUtil.setProperty(obj, prop, val) propNames.append(prop) elif obj.getGroupOfProperty(prop) == "Attribute": propNames.append(prop) obj.BitPropertyNames = propNames obj.setEditorMode("BitPropertyNames", 2) for prop in obj.BitPropertyNames: if obj.getGroupOfProperty(prop) == PropertyGroupShape: # properties in the Shape group can only be modified while the actual # shape is loaded, so we have to disable direct property editing obj.setEditorMode(prop, 1) else: # all other custom properties can and should be edited directly in the # property editor widget, not much value in re-implementing that obj.setEditorMode(prop, 0)
def updateExpression(name, widget): value = str(widget.text()) val = PathUtil.getProperty(self.obj, name) if val != value: PathUtil.setProperty(self.obj, name, value)
val = PathUtil.getProperty(o, p) dsc = o.getDocumentationOfProperty(p) enm = '' enum = [] if ttp == 'Enumeration': enum = o.getEnumerationsOfProperty(p) enm = "{}".format(','.join(enum)) if GroupMap.get(grp): group = GroupMap.get(grp) print("move: {}.{} -> {}".format(grp, p, group)) o.removeProperty(p) o.Proxy.addCustomProperty(typ, p, group, dsc) if enum: print("enum {}.{}: {}".format(group, p, enum)) setattr(o, p, enum) PathUtil.setProperty(o, p, val) if p == set_var: print("set {}.{} = {}".format(grp, p, set_val)) if ttp == 'Enumeration' and set_val[0] == '[': enum = set_val[1:-1].split(',') setattr(o, p, enum) else: PathUtil.setProperty(o, p, set_val) if p == args.delete: print("delete {}.{}".format(grp, p)) o.removeProperty(p) if not args.print_all and grp == 'Shape': continue if args.print or args.print_all: print(" {:10} {:20} {:20} {:10} {}".format(grp, p, ttp, str(val), enm)) o.Proxy.refreshCustomPropertyGroups()