How to apply material to a CATPart using a macro
|Typically, material is applied to a CATPart by click on the “Apply Material” icon then browsing the material catalog until you find the material you’re looking for. But what if you need to apply the material to hundreds of parts? Apply individually by hand will take forever. This is an example where an automated process like a macro comes in handy. The key to applying material to a CATPart via a macro is to use the ApplyMaterialOnBody command.
The code to apply the material is shown below. This is only for a single CATPart. You could add to this code to cycle through a product tree and apply material to a part then loop through to the next part, etc.
Sub CATMain() Dim MaterialDocPath As String Dim MaterialName As String, MaterialFamilyName As String ' Identify which Material you want: '********************************************* MaterialName = "Aluminium" '* MaterialFamilyName = "Metal" '* '*********************************************. ' Identify the Material Doc Path: '***************************************************************** MaterialDocPath = "c:\Catalogs for CATIA\materials\Catalog.CATMaterial" '* '***************************************************************** ' Modified From the AutomationV5.chm File: Dim oMaterial_document As Document Set oMaterial_document = CATIA.Documents.Read(MaterialDocPath) Dim oMaterial As Material Set oMaterial = oMaterial_document.Families.Item(MaterialFamilyName).Materials.Item(MaterialName) Dim oPartDocument As Document Set oPartDocument = CATIA.ActiveDocument Dim oRootPart As Part Set oRootPart = oPartDocument.Part Set oManager = oRootPart.GetItem("CATMatManagerVBExt") Dim oMainBody As Body Set oMainBody = oRootPart.MainBody linkMode = 1 oManager.ApplyMaterialOnBody oMainBody, oMaterial, linkMode End Sub
6 Comments
When I am using the above code, it is throwing an error ” User defined type not defined” at Dim oMaterial_document As Document. Could you correct me.
Hi,and I think your macro was request that the CATIA have to located in partion C,witch will make a problem when somebody’s Catia was NOT.
Nice program. But in CATIA V5R28 it doesn’t work.
Emmett, can you help?
Hello..
I am working on a project where I am trying to print the different attributes like mass, cog, material applied on a part into the spreadsheet.
If I want to open a product file that has a bunch of parts with materials assigned, can I print all of them into a spreadsheet? like parts in column 1 and their applied materials in column 2.
Hello Emmett
Instead of the material stay inside the partbody, its possible to put it just inside the part, at the first level?