Macro merge duplicate materials

From FreeCAD Documentation
Revision as of 11:28, 23 May 2020 by FuzzyBot (talk | contribs) (Updating to match new version of source page)
Other languages:

Macro merge duplicate materials

Description
Fusionne les matériaux ayant le même nom

Version macro : 2.0
Date dernière modification : 20197-07-12
Version FreeCAD : All
Téléchargement : ToolBar Icon
Auteur: yorik
Auteur
yorik
Téléchargement
ToolBar Icon
Liens
Version Macro
2.0
Dernière modification
20197-07-12
Version(s) FreeCAD
All
Raccourci clavier
None
Voir aussi
None

Description

Fusionne les matériaux qui ont le même nom de base (avec différentes terminaisons numériques comme 001, 002, ...) dans un seul. Seul le premier sera conservé, et tous les objets qui se lient aux doublons seront en lien avec le premier. Donc, avant d'utiliser cette macro, assurez-vous que votre premier matériau (soit celui sans sans chiffre, est le nombre le plus bas dans la liste trouvée) est le bon.

Script

ToolBar Icon

Merge duplicate materials.FCMacro

import FreeCAD,FreeCADGui
mats = [o for o in FreeCAD.ActiveDocument.Objects if o.isDerivedFrom("App::MaterialObject")]
todelete = []
for mat in mats:
    if mat.Label[-1].isdigit() and mat.Label[-2].isdigit() and mat.Label[-3].isdigit():
        orig = None
        for om in mats:
            if om.Label == mat.Label[:-3].strip():
                orig = om
                break
        if orig:
            for par in mat.InList:
                for prop in par.PropertiesList:
                    if getattr(par,prop) == mat:
                        print( "Changed property '"+prop+"' of object "+par.Label+" from "+mat.Label+" to "+orig.Label)
                        setattr(par,prop,orig)
            todelete.append(mat)
for tod in todelete:
    if not tod.InList:
        print( "Deleting material "+tod.Label)
        FreeCAD.ActiveDocument.removeObject(tod.Name)
    elif (len(tod.InList) == 1) and (tod.InList[0].isDerivedFrom("App::DocumentObjectGroup")):
        print( "Deleting material "+tod.Label)
        FreeCAD.ActiveDocument.removeObject(tod.Name)
    else:
        print( "Unable to delete material "+tod.Label+": InList not empty")