Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations KootK on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

Catia Macro to export material applied on body 1

Status
Not open for further replies.

rookiezzz

Structural
Jul 27, 2022
22
Hello all:

I‘m trying to export the material applied on each body and its Catpart part number. Here is simple code I'm testing.
I believe there is something wrong on function "getmaterialonbody".Details product and error report is attached. It even make my catia crash....
Cound anyone know how to fix it? Thanks in advance![bigsmile]

Code:
 
 https://files.engineering.com/getfile.aspx?folder=fcd03e66-5200-463b-ba44-c7b762215b1d&file=details.jpg
Replies continue below

Recommended for you

Here is the code
Code:
Sub CATMain()
    GetNextNode CATIA.ActiveDocument.Product

End Sub

Sub GetNextNode(oCurrentProduct As Product)
    Dim oCurrentTreeNode As Product
    Dim StrNomenclature, StrDesignation, StrWindows As String
    Dim i As Integer
    For i = 1 To oCurrentProduct.Products.Count
        Set oCurrentTreeNode = oCurrentProduct.Products.Item(i)
        StrWindows = oCurrentTreeNode.ReferenceProduct.Parent.FullName
		'set opart = oCurrentProduct.Products.Item(i).referenceproduct.parent.part

        If Right(StrWindows, 4) = "Part" Then
					Set selection1 =catia.activedocument.selection
					selection1.clear
					selection1.add(oCurrentTreeNode)
					CATIA.StartCommand "Open in New Window"

					set opart = catia.activedocument.part
					Set Manager = opart.GetItem("CATMatManagerVBExt")
					For each body1 as body in opart.bodies
					On Error Resume Next
					Manager.GetMaterialOnBody body1, Material
					if Material is Nothing then
  						MaterialName = "No material"
					else
  						MaterialName = Material.Name 
					end if 
					msgbox MaterialName
					next
		end if
		catia.activedocument.close()

        If oCurrentTreeNode.Products.Count > 0 Then
            GetNextNode oCurrentTreeNode
        End If
   Next
End Sub
 
some minor changes... you were almost there
Code:
Option Explicit

Sub CATMain()
    GetNextNode CATIA.ActiveDocument.Product
End Sub

Sub GetNextNode(oCurrentProduct As Product)
    Dim oCurrentTreeNode As Product
    Dim oPart As Part
    Dim Manager, body1, material, MaterialName
    Dim StrNomenclature, StrDesignation, StrWindows As String
    Dim i As Integer
    For i = 1 To oCurrentProduct.Products.Count
        Set oCurrentTreeNode = oCurrentProduct.Products.item(i)
        StrWindows = oCurrentTreeNode.ReferenceProduct.Parent.FullName

        If Right(StrWindows, 4) = "Part" Then
                    Set oPart = oCurrentTreeNode.ReferenceProduct.Parent.Part
                    Set Manager = oPart.GetItem("CATMatManagerVBExt")
                    For Each body1 In oPart.Bodies

                    Manager.GetMaterialOnBody body1, material
                    If material Is Nothing Then
                        MaterialName = "No material"
                    Else
                        MaterialName = material.name
                    End If
                    MsgBox body1.name & " : " & MaterialName
                    Next
        End If

        If oCurrentTreeNode.Products.Count > 0 Then
            GetNextNode oCurrentTreeNode
        End If
   Next
End Sub

regards,
LWolf
 
Thank you so much LWolf!!! It works. XDDDD
It must be some mistakes I did before on reading it in Product level.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor