'Code created by: A.R. (9/16/2013)

'Modified by Emmett Ross (11/1/13)

' www.scripting4v5.com

' This code returns the COG position in CATIA for CATParts and CATProducts

' Description:

'               1. This code loops through every node in the product tree and identifies if it is a part or a product

'               2. If the current node is a CATPart, reads the CoG

'               3. If it is a CATProduct, it identifies the current product, reads the CoG

'

' Assumptions:  1. Active document must be a catia product

'               2. The user has added the material attributes

'

'

'************************************************************************************************

Sub CATMain()



GetNextNode CATIA.ActiveDocument.Product



End Sub



'''''''''''''''''''''''''''Loop through the nodes'''''''''''''''''''''''''''

'This section of the loops through every tree node for the current product



Sub GetNextNode(oCurrentProduct As Product)



Dim oCurrentTreeNode As Product

Dim i As Integer

Dim RwNum As Integer

For i = 1 To oCurrentProduct.Products.Count

Set oCurrentTreeNode = oCurrentProduct.Products.Item(i)

' Determine if the current node is a part or product and runs the corresponding function

If IsPart(oCurrentTreeNode, RwNum) = True Then

MsgBox "It is a part"

ElseIf IsProduct(oCurrentTreeNode, RwNum) = True Then

MsgBox "It is a product"

End If

' If sub-nodes exist below the current tree node, calls the sub recursively

If oCurrentTreeNode.Products.Count > 0 Then

GetNextNode oCurrentTreeNode

End If

Next

End Sub





Function IsPart(objCurrentProduct As Product, RwNum As Integer) As Boolean



Dim oTestPart As PartDocument

Set oTestPart = Nothing

On Error Resume Next

Set oTestPart = CATIA.Documents.Item(objCurrentProduct.PartNumber & ".CATPart")



If Not oTestPart Is Nothing Then

IsPart = True



Dim product1 As Product

Set product1 = oTestPart.Product

Set parameters1 = product1.UserRefProperties



'Extracting Volume details

Dim TheSPAWorkbench As Workbench

Dim part1 As Part

Set part1 = oTestPart.Part

Set objSPAWorkbench = part1.Parent.GetWorkbench("SPAWorkbench")

Dim TheMeasurable As Measurable

Dim Reference As Reference

Dim bodies1 As Bodies

Set bodies1 = part1.Bodies

Dim body1 As Body

Set body1 = part1.MainBody

Set Reference = part1.CreateReferenceFromObject(body1)

Set TheSPAWorkbench = CATIA.ActiveDocument.GetWorkbench("SPAWorkbench")

Set TheMeasurable = TheSPAWorkbench.GetMeasurable(Reference)

Volume = TheMeasurable.Volume * 61023.74409

'gives the volume in inches^3

If Volume = Empty Then Volume = 0

On Error Resume Next

If (Err.Number <> 0) Then

MsgBox "No selected product"

End If

Msgbox "Volume is: " & getVol = Volume



'Extracting Center of Gravity details

Dim dCoordinates(2)

Dim oInertia As AnyObject

Set oInertia = product1.GetTechnologicalObject("Inertia") 'this line works with products

oInertia.GetCOGPosition dCoordinates



MsgBox product1.Name & ", Center of gravity : X = " & CStr(dCoordinates(0)) & ", Y = " + CStr(dCoordinates(1)) & ", Z = " + CStr(dCoordinates(2))



''''''''''''''''

Else

IsPart = False

End If

End Function



''''''''''''''''''''Function for attribute addition to products'''''''''''''''



Function IsProduct(objCurrentProduct As Product, RwNum As Integer) As Boolean

Dim oTestProduct As ProductDocument

Set oTestProduct = Nothing

'On Error Resume Next

Set oTestProduct = CATIA.Documents.Item(objCurrentProduct.PartNumber &".CATProduct")

If Not oTestProduct Is Nothing Then

IsProduct = True

Dim product2 As AnyObject

Set product2 = oTestProduct.Product



'Extracting Center of Gravity details into BOM



Dim dCoordinates(2)

Dim oInertia As AnyObject

Set oInertia = objCurrentProduct.GetTechnologicalObject("Inertia")

oInertia.GetCOGPosition dCoordinates



' Display the results

MsgBox Product2.Name & ": Center of gravity : X = " &  Cstr(dCoordinates(0)) & ", Y = "+Cstr(dCoordinates(1)) & ", Z = "+Cstr(dCoordinates(2))



Else

IsProduct = False

End If

End Function