Macro merge duplicate materials

From FreeCAD Documentation
Jump to: navigation, search
Other languages:
English • ‎français • ‎italiano

Macro merge duplicate materials.png Macro merge duplicate materials

Description
Merges materials that have the same base name (with different numeral endings like 001, 002,...) into one. Only the first one will be kept, and all the objects that link to the duplicates will be linking to the first one instead. So before using this macro, make sure your first material (either the one without numerical ending or the lowest number found) is the right one.

Macro version : 2.0
Date last modification : 20197-07-12
FreeCAD version : All
Download : ToolBar Icon
Author
yorik
Download
ToolBar Icon
Links
Macro Version
2.0
Date last modification
20197-07-12
FreeCAD Version(s)
All
Default shortcut
None
See also
None


Description

Merges materials that have the same base name (with different numeral endings like 001, 002,...) into one. Only the first one will be kept, and all the objects that link to the duplicates will be linking to the first one instead. So before using this macro, make sure your first material (either the one without numerical ending or the lowest number found) is the right one.

Script

ToolBar Icon Macro merge duplicate materials.png

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")