Wednesday, February 1, 2012

VB Scripting: CATPart Description Macro Example

How do you return a CATPart or CATProduct description with a macro? Using VB scripting you can use the following CATScript code to loop through all the parts within a product assembly and return what the description is. When you right click on a part within an assembly you will actually see two description boxes - component and product (pictured below). 


The top description name in the box is for the component level and can be called using DescriptionInst which returns or sets the product's description for a component product.  The bottom description name is for the product and is found through our script using DescriptionRef which set's the product's description for a reference product and is a property in the object product. Getting the part description may be useful in creating bills of materials or other part list documents.

Sub CATMain()


Dim productDocument1 As Document

Dim product1 As Product

Dim products1 As Products


Set productDocument1 = CATIA.ActiveDocument

Set product1 = productDocument1.Product

Set products1 = product1.Products


'count the number of CATParts within the catproduct

partcount=product1.Products.Count

msgbox"CHECK: The number of parts is "&partcount&". Please click OK to continue."


Dim i As Integer


'loop through all parts

For i=1 to partcount


'apply design mode to each part

products1.Item(i).ApplyWorkMode DESIGN_MODE


Dim partDoc1 As PartDocument

Set partDoc1=products1.Item(i)


Dim getDes, getDes2 As String

getDes = partDoc1.DescriptionInst

getDes2 = partDoc1.DescriptionRef


Msgbox "Component description is " & getDes& " and Product Description " & getDes2


Next 'i


End Sub



To rename (or to enter a new name) to your part description field use this code by adding it to the above text during the i loop, before Next and after the msgbox:


partDoc1.DescriptionRef = "Test"

partDoc1.DescriptionInst = "CompTest"&i


The first line will add "Test" to each product description field while the second line will add "CompTest" and the loop number. So for the first part in your assembly where i=1, after running the macro the component description will read "CompTest1", the next part "CompTest2" and so on.


See more examples like these in my new How To book VB Scripting for CATIA V5.  Visit my new site for even more CATIA macro programming tips here!


unpublic 

No comments:

Post a Comment

I'd love to hear from you!