def write(filename, dictionary): "writes the given dictionary to the given file" # sort the data into sections contents = [] for key in getMaterialAttributeStructure( ): # get the mat file structure from material module contents.append({"keyname": key[0]}) if key[0] == "Meta": header = contents[-1] elif key[0] == "User defined": user = contents[-1] for p in key[1]: contents[-1][p] = "" for k, i in dictionary.items(): found = False for group in contents: if not found: if k in group.keys(): group[k] = i found = True if not found: user[k] = i # write header rev = FreeCAD.ConfigGet("BuildVersionMajor") + "." + FreeCAD.ConfigGet( "BuildVersionMinor") + " " + FreeCAD.ConfigGet("BuildRevision") if isinstance(filename, unicode): import sys filename = filename.encode(sys.getfilesystemencoding()) print(filename) f = pythonopen(filename, "wb") f.write("; " + header["CardName"].encode("utf8") + "\n") f.write("; " + header["AuthorAndLicense"].encode("utf8") + "\n") f.write("; file produced by FreeCAD " + rev + "\n") f.write( "; information about the content of this card can be found here:\n") f.write("; http://www.freecadweb.org/wiki/index.php?title=Material\n") f.write("\n") if header["Source"]: f.write("; source of the data provided in this card:\n") f.write("; " + header["Source"].encode("utf8") + "\n") f.write("\n") # write sections for s in contents: if s["keyname"] != "Meta": if len(s) > 1: # if the section has no contents, we don't write it f.write("[" + s["keyname"] + "]\n") for k, i in s.items(): if (k != "keyname" and i != '') or k == "Name": # use only keys which are not empty and the name even if empty f.write(k + "=" + i.encode('utf-8') + "\n") f.write("\n") f.close()
def write(filename, dictionary): "writes the given dictionary to the given file" # sort the data into sections contents = [] for key in getMaterialAttributeStructure(): # get the mat file structure from material module contents.append({"keyname": key[0]}) if key[0] == "Meta": header = contents[-1] elif key[0] == "User defined": user = contents[-1] for p in key[1]: contents[-1][p] = "" for k, i in dictionary.iteritems(): found = False for group in contents: if not found: if k in group.keys(): group[k] = i found = True if not found: user[k] = i # write header rev = FreeCAD.ConfigGet("BuildVersionMajor") + "." + FreeCAD.ConfigGet("BuildVersionMinor") + " " + FreeCAD.ConfigGet("BuildRevision") if isinstance(filename, unicode): import sys filename = filename.encode(sys.getfilesystemencoding()) print(filename) f = pythonopen(filename, "wb") f.write("; " + header["CardName"].encode("utf8") + "\n") f.write("; " + header["AuthorAndLicense"].encode("utf8") + "\n") f.write("; file produced by FreeCAD " + rev + "\n") f.write("; information about the content of this card can be found here:\n") f.write("; http://www.freecadweb.org/wiki/index.php?title=Material\n") f.write("\n") if header["Source"]: f.write("; source of the data provided in this card:\n") f.write("; " + header["Source"].encode("utf8") + "\n") f.write("\n") # write sections for s in contents: if s["keyname"] != "Meta": if len(s) > 1: # if the section has no contents, we don't write it f.write("[" + s["keyname"] + "]\n") for k, i in s.iteritems(): if (k != "keyname" and i != '') or k == "Name": # use only keys which are not empty and the name even if empty f.write(k + "=" + i.encode('utf-8') + "\n") f.write("\n") f.close()
def implementModel(self): '''implements the model with the material attribute structure.''' widget = self.widget treeView = widget.treeView model = treeView.model() model.setHorizontalHeaderLabels(["Property", "Value", "Type", "Units"]) treeView.setColumnWidth(0, 250) treeView.setColumnWidth(1, 250) treeView.setColumnHidden(2, True) treeView.setColumnHidden(3, True) tree = getMaterialAttributeStructure(True) MatPropDict = tree.getroot() for group in MatPropDict.getchildren(): gg = group.attrib['Name'] top = QtGui.QStandardItem(gg) model.appendRow([top]) self.groups.append(gg) for proper in group.getchildren(): properDict = proper.attrib pp = properDict['Name'] item = QtGui.QStandardItem(pp) self.internalprops.append(pp) it = QtGui.QStandardItem() tt = properDict['Type'] itType = QtGui.QStandardItem(tt) try: uu = properDict['Units'] itUnit = QtGui.QStandardItem(uu) except KeyError: itUnit = QtGui.QStandardItem() top.appendRow([item, it, itType, itUnit]) top.sortChildren(0) treeView.expandAll()
def __init__(self, obj=None, prop=None, material=None): """Initializes, optionally with an object name and a material property name to edit, or directly with a material dictionary.""" self.obj = obj self.prop = prop self.material = material self.customprops = [] # load the UI file from the same directory as this script self.widget = FreeCADGui.PySideUic.loadUi(os.path.dirname(__file__) + os.sep + "materials-editor.ui") # additional UI fixes and tweaks self.widget.ButtonURL.setIcon(QtGui.QIcon(":/icons/internet-web-browser.svg")) self.widget.ButtonDeleteProperty.setEnabled(False) self.widget.standardButtons.button(QtGui.QDialogButtonBox.Ok).setAutoDefault(False) self.widget.standardButtons.button(QtGui.QDialogButtonBox.Cancel).setAutoDefault(False) self.updateCards() self.widget.Editor.header().resizeSection(0, 200) self.widget.Editor.expandAll() self.widget.Editor.setFocus() # TODO allow to enter a custom property by pressing Enter in the lineedit (currently closes the dialog) self.widget.Editor.setEditTriggers(QtGui.QAbstractItemView.NoEditTriggers) QtCore.QObject.connect(self.widget.ComboMaterial, QtCore.SIGNAL("currentIndexChanged(QString)"), self.updateContents) QtCore.QObject.connect(self.widget.ButtonURL, QtCore.SIGNAL("clicked()"), self.openProductURL) QtCore.QObject.connect(self.widget.standardButtons, QtCore.SIGNAL("accepted()"), self.accept) QtCore.QObject.connect(self.widget.standardButtons, QtCore.SIGNAL("rejected()"), self.reject) QtCore.QObject.connect(self.widget.ButtonAddProperty, QtCore.SIGNAL("clicked()"), self.addCustomProperty) QtCore.QObject.connect(self.widget.EditProperty, QtCore.SIGNAL("returnPressed()"), self.addCustomProperty) QtCore.QObject.connect(self.widget.ButtonDeleteProperty, QtCore.SIGNAL("clicked()"), self.deleteCustomProperty) QtCore.QObject.connect(self.widget.Editor, QtCore.SIGNAL("itemDoubleClicked(QTreeWidgetItem*,int)"), self.itemClicked) QtCore.QObject.connect(self.widget.Editor, QtCore.SIGNAL("itemChanged(QTreeWidgetItem*,int)"), self.itemChanged) QtCore.QObject.connect(self.widget.Editor, QtCore.SIGNAL("currentItemChanged(QTreeWidgetItem*,QTreeWidgetItem*)"), self.checkDeletable) QtCore.QObject.connect(self.widget.ButtonOpen, QtCore.SIGNAL("clicked()"), self.openfile) QtCore.QObject.connect(self.widget.ButtonSave, QtCore.SIGNAL("clicked()"), self.savefile) # add material properties (the keys) to the editor for group in getMaterialAttributeStructure(True): # get the mat file structure from material module, use Spaces for better ui # print(group) self.addPropertiesToGroup(group) # update the editor with the contents of the property, if we have one d = None if self.prop and self.obj: d = FreeCAD.ActiveDocument.getObject(self.obj).getPropertyByName(self.prop) elif self.material: d = self.material if d: self.updateContents(d)
def __init__(self, obj=None, prop=None, material=None): """Initializes, optionally with an object name and a material property name to edit, or directly with a material dictionary.""" self.obj = obj self.prop = prop self.material = material self.customprops = [] # load the UI file from the same directory as this script self.widget = FreeCADGui.PySideUic.loadUi( os.path.dirname(__file__) + os.sep + "materials-editor.ui") # additional UI fixes and tweaks self.widget.ButtonURL.setIcon( QtGui.QIcon(":/icons/internet-web-browser.svg")) self.widget.ButtonDeleteProperty.setEnabled(False) self.widget.standardButtons.button( QtGui.QDialogButtonBox.Ok).setAutoDefault(False) self.widget.standardButtons.button( QtGui.QDialogButtonBox.Cancel).setAutoDefault(False) self.updateCards() self.widget.Editor.header().resizeSection(0, 200) self.widget.Editor.expandAll() self.widget.Editor.setFocus() # TODO allow to enter a custom property by pressing Enter in the lineedit (currently closes the dialog) self.widget.Editor.setEditTriggers( QtGui.QAbstractItemView.NoEditTriggers) QtCore.QObject.connect(self.widget.ComboMaterial, QtCore.SIGNAL("currentIndexChanged(QString)"), self.updateContents) QtCore.QObject.connect(self.widget.ButtonURL, QtCore.SIGNAL("clicked()"), self.openProductURL) QtCore.QObject.connect(self.widget.standardButtons, QtCore.SIGNAL("accepted()"), self.accept) QtCore.QObject.connect(self.widget.standardButtons, QtCore.SIGNAL("rejected()"), self.reject) QtCore.QObject.connect(self.widget.ButtonAddProperty, QtCore.SIGNAL("clicked()"), self.addCustomProperty) QtCore.QObject.connect(self.widget.EditProperty, QtCore.SIGNAL("returnPressed()"), self.addCustomProperty) QtCore.QObject.connect(self.widget.ButtonDeleteProperty, QtCore.SIGNAL("clicked()"), self.deleteCustomProperty) QtCore.QObject.connect( self.widget.Editor, QtCore.SIGNAL("itemDoubleClicked(QTreeWidgetItem*,int)"), self.itemClicked) QtCore.QObject.connect( self.widget.Editor, QtCore.SIGNAL("itemChanged(QTreeWidgetItem*,int)"), self.itemChanged) QtCore.QObject.connect( self.widget.Editor, QtCore.SIGNAL( "currentItemChanged(QTreeWidgetItem*,QTreeWidgetItem*)"), self.checkDeletable) QtCore.QObject.connect(self.widget.ButtonOpen, QtCore.SIGNAL("clicked()"), self.openfile) QtCore.QObject.connect(self.widget.ButtonSave, QtCore.SIGNAL("clicked()"), self.savefile) # add material properties (the keys) to the editor for group in getMaterialAttributeStructure( True ): # get the mat file structure from material module, use Spaces for better ui # print(group) self.addPropertiesToGroup(group) # update the editor with the contents of the property, if we have one d = None if self.prop and self.obj: d = FreeCAD.ActiveDocument.getObject(self.obj).getPropertyByName( self.prop) elif self.material: d = self.material if d: self.updateContents(d)