Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

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

How to get mass of subassembly with VBA

Status
Not open for further replies.

jproberge

Mechanical
May 2, 2014
10
CA
Dear CATIA users,

I think this is an easy one, but I've been struggling with this for a very long time now. I have a product consisting of a (1) subproduct and a (2) 3dpart. I want to get mass and inertia of (1) and (2), separately, using GetInertiaElement(). I'm able to get the mass of the whole Product and the 3dpart, but not for the subproduct... This is what I have:



' 1- Gets the current Editor
Dim myEditor As Editor
Set myEditor = CATIA.ActiveEditor

' 2- Gets the Context from the Editor
Dim MyContext As PLMAppContext
Set MyContext = myEditor.GetService("PLMProductContext")
Dim myRootOccurrence As VPMRootOccurrence
Set myRootOccurrence = MyContext.RootOccurrence
Dim myMCXParent As VPMReference
Set myMCXParent = myRootOccurrence.ReferenceRootOccurrenceOf

'3- Retrieving the PLMProductContext object as a service associated to the editor
Dim oPLMProductContext As PLMProductContext
Set oPLMProductContext = myEditor.GetService("PLMProductContext")

'4- Retrieves the list of instances within the input reference
Dim TheInstances As VPMInstances
Set TheInstances = myMCXParent.Instances
Dim oVPMInst As VPMInstance
Set oVPMInst = TheInstances.Item(1) ' Index=1 is the subassembly index #

'Getting the dynamical properties of each part (Mass, Inertia tensor, center of gravity)
Set theInertiaService = CATIA.ActiveEditor.GetService("InertiaService")
Dim theInertiaElement As Inertia
Dim theInertiaElement2 As Object
Set theInertiaElement = theInertiaService.GetInertiaElement(oVPMInst)
Set theInertiaElement2 = theInertiaElement
Dim theMass As Double
Dim theMoments(2)
Dim theMatrix(8)
theInertiaElement2.GetInertiaMatrix theMatrix ' Here is the problematic line
theInertiaElement2.GetPrincipalMoments theMoments
theMass = theInertiaElement2.GetMass



Running this will result in the error "The method GetInertiaMatrix failed". Does anyone know how I can fix this and get the mass / moments of the subassembly?

 
Replies continue below

Recommended for you

Search for "Computing a Mass and a Center of Gravity's Position" in the V5Automation.chm. It may help you.
 
Thanks lardmand363 for your reply:)

Actually, the code above belongs to CATIA V6, so documentation in V5Automation.chm (CATIA V5 API) might not have helped.

In fact, I solved this problem earlier this week.

To compute a Product mass you have to go at higher level in the product model. You can achieve this by modifying the code I've already posted with this version:





Sub ExtractKinematicsMain2()


' 1- Gets the current Editor
Dim myEditor As Editor
Set myEditor = CATIA.ActiveEditor

' 2- Gets the Context from the Editor
Dim MyContext As PLMAppContext
Set MyContext = myEditor.GetService("PLMProductContext")
Dim myRootOccurrence As VPMRootOccurrence
Set myRootOccurrence = MyContext.RootOccurrence


'Getting the dynamical properties of each part (Mass, Inertia tensor, center of gravity)
Set theInertiaService = CATIA.ActiveEditor.GetService("InertiaService")
Dim theInertiaElement As Inertia
Dim theInertiaElement2 As Object
Set theInertiaElement = theInertiaService.GetInertiaElement(myRootOccurrence.Occurrences.Item(2))
Set theInertiaElement2 = theInertiaElement
Dim theMass As Double
Dim theMoments(2)
Dim theMatrix(8)
theInertiaElement2.GetInertiaMatrix theMatrix
theInertiaElement2.GetPrincipalMoments theMoments
theMass = theInertiaElement2.GetMass
MsgBox "The Mass: " & theMass

End Sub




Thanks for your reply,

Regards,

Jean-Philippe
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Top