示例#1
0
    def implementModel(self):
        '''implements the model with the material attribute structure.'''

        p = FreeCAD.ParamGet("User parameter:BaseApp/Preferences/Mod/Material")
        widget = self.widget
        treeView = widget.treeView
        model = treeView.model()
        model.setHorizontalHeaderLabels(["Property", "Value", "Type"])

        treeView.setColumnWidth(0, 250)
        treeView.setColumnWidth(1, 250)
        treeView.setColumnHidden(2, True)

        from materialtools.cardutils import get_material_template
        template_data = get_material_template(True)

        for group in template_data:
            gg = list(group.keys())[0]  # group dict has only one key
            top = QtGui.QStandardItem(gg)
            model.appendRow([top])
            self.groups.append(gg)

            for properName in group[gg]:
                pp = properName  # property name
                item = QtGui.QStandardItem(pp)
                item.setToolTip(group[gg][properName]['Description'])
                self.internalprops.append(pp)

                it = QtGui.QStandardItem()
                it.setToolTip(group[gg][properName]['Description'])

                tt = group[gg][properName]['Type']
                itType = QtGui.QStandardItem(tt)

                top.appendRow([item, it, itType])
                treeView.setExpanded(top.index(),
                                     p.GetBool("TreeExpand" + gg, True))
示例#2
0
    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"])

        treeView.setColumnWidth(0, 250)
        treeView.setColumnWidth(1, 250)
        treeView.setColumnHidden(2, True)

        from materialtools.cardutils import get_material_template
        template_data = get_material_template(True)

        for group in template_data:
            gg = list(group.keys())[0]  # group dict has only one key
            top = QtGui.QStandardItem(gg)
            model.appendRow([top])
            self.groups.append(gg)

            for properName in group[gg]:
                pp = properName  # property name
                item = QtGui.QStandardItem(pp)
                self.internalprops.append(pp)

                it = QtGui.QStandardItem()

                tt = group[gg][properName]['Type']
                itType = QtGui.QStandardItem(tt)

                top.appendRow([item, it, itType])

            top.sortChildren(0)

        treeView.expandAll()
示例#3
0
def write(filename, dictionary, write_group_section=True):
    "writes the given dictionary to the given file"

    # sort the data into sections
    contents = []
    user = {}
    template_data = get_material_template()
    for group in template_data:
        groupName = list(group.keys())[0]  # group dict has only one key
        contents.append({"keyname": groupName})
        if groupName == "Meta":
            header = contents[-1]
        elif groupName == 'UserDefined':
            user = contents[-1]
        for properName in group[groupName]:
            contents[-1][properName] = ''
    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
    # delete empty properties
    for group in contents:
        # iterating over a dict and changing it is not allowed
        # thus it is iterated over a list of the keys
        for k in list(group.keys()):
            if group[k] == '':
                del group[k]

    # card writer
    rev = "{}.{}.{}".format(FreeCAD.ConfigGet("BuildVersionMajor"),
                            FreeCAD.ConfigGet("BuildVersionMinor"),
                            FreeCAD.ConfigGet("BuildRevision"))
    if isinstance(filename, unicode):
        if sys.version_info.major < 3:
            filename = filename.encode(sys.getfilesystemencoding())
    # print(filename)
    card_name_file = os.path.splitext(os.path.basename(filename))[0]
    # print(card_name_file)
    f = pythonopen(filename, "w")
    # write header
    # first five lines are the same in any card file, see comment above read def
    if header["CardName"] != card_name_file:
        # CardName is the MatCard file name
        FreeCAD.Console.PrintMessage(
            "File CardName is used: {}\n".format(card_name_file))
    if sys.version_info.major >= 3:
        f.write("; " + card_name_file + "\n")
        #f.write("; " + header["AuthorAndLicense"] + "\n")
        f.write("; " + header.get("AuthorAndLicense", "no author") + "\n")
    else:
        f.write("; " + header["CardName"].encode("utf8") + "\n")
        f.write("; " + header["AuthorAndLicense"].encode("utf8") + "\n")
    f.write(
        "; information about the content of such cards can be found on the wiki:\n"
    )
    f.write("; https://www.freecadweb.org/wiki/Material\n")
    f.write("; file created by FreeCAD " + rev + "\n")
    # write sections
    # write standard FCMat section if write group section parameter is set to False
    if write_group_section is False:
        f.write("\n[FCMat]\n")
    for s in contents:
        if s["keyname"] != "Meta":
            # if the section has no contents, we don't write it
            if len(s) > 1:
                # only write group section if write group section parameter is set to True
                if write_group_section is True:
                    f.write("\n[" + 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
                        if sys.version_info.major >= 3:
                            f.write(k + " = " + i + "\n")
                        else:
                            f.write(k + " = " + i.encode('utf-8') + "\n")
    f.close()