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 cataloghow to apply material to a catpart macro 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

Add a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.