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!

VBA macro to get inertia on body won't work 2

Status
Not open for further replies.

ArnaudG

Mechanical
Dec 5, 2019
25
CA
Hi, so here's the thing : I wrote a code that is supposed to give me the inertia (and it does get the inertia object) of a body and then retrieve the center of gravity and inertia matrix values. I followed the help and as I said I can get the inertia object. However in my code, I'm unable to use GetInertiMatrix and GetCOGPosition nor use GetCOG on a measurable (which I'm able to get as well).
When I use these methods, I don't see the arguments proposed in the visual basic editor and I have an error when launching the macro : Function marked as restricted or uses a type not supported in Visual Basic

The code seems to works as VBscript or CATscript but what is more annoying is that I found on this forum someone doing the same thing (here : apparently the same way and it works for him and not me (and I tried his code and it works fine).

So here's my code which gives an error :

Code:
Option Explicit

Sub GetBodyInertia()

Dim objdoc As Document

On Error Resume Next
Set objdoc = CATIA.ActiveDocument

Dim oPart As Part
Dim oBody As Body
Dim refBody As Reference
Dim oSPAWorkbench
Dim oInertias As Inertias
Dim oInertia As Inertia
Dim oMeasure As Measurable

Set oPart = objdoc.MainBody
Set refBody = oPart.CreateReferenceFromObject(oBody)
Set oSPAWorkbench = objdoc.GetWorkbench("SPAWorkbench")
Set oInertias = oSPAWorkbench.Inertias
Set oMeasure = oSPAWorkbench.GetMeasurable(refBody)

If oInertias.Count = 0 Then
    Set oInertia = oInertias.Add(oBody)
Else
    Set oInertia = oInertias.Item(1)
End If

Dim cogArr(2)
Dim Matrix(8)

oInertia.GetInertiaMatrix Matrix
oMeasure.GetCOG cogArr

End Sub


And now here's his code simplifid to the minimum I require and which works fine :
Code:
' written by evereux
Option Explicit
Sub CATMain()

On Error Resume Next


Dim partDoc As Object
Set partDoc = CATIA.ActiveDocument.Part
Dim docname As String
docname = partDoc.Name

Dim objSPAWkb
Set objSPAWkb = CATIA.ActiveDocument.GetWorkbench("SPAWorkbench")
 
Dim i As Integer
Dim bodyNumber As Integer
bodyNumber = partDoc.Bodies.Count

Dim body1 As Body
Set body1 = partDoc.Bodies.Item(1)

Dim namebody As String
namebody = body1.Name

Dim objRef As Object
Dim objMeasurable

Set objRef = partDoc.CreateReferenceFromObject(body1)
Set objMeasurable = objSPAWkb.GetMeasurable(objRef)

Dim TheInertiasList As Inertias
Set TheInertiasList = objSPAWkb.Inertias

Dim NewInertia
Set NewInertia = TheInertiasList.Add(body1)

Dim Matrix(8)
NewInertia.GetInertiaMatrix Matrix

Dim bodyArea
bodyArea = objMeasurable.Area

bodyArea = bodyArea * 1550

Dim bodyArea2 As Integer
bodyArea2 = bodyArea / 2

Dim objCOG(2)
objMeasurable.GetCOG objCOG
 
End Sub

I read again and again and compared and I can't find my error(s) so a little help to point the maybe obvious would be appreciated. I may directly use his code but god that's so frustrating to not understand where I'm wrong !
 
Replies continue below

Recommended for you

"Function marked as restricted" error is a well-known VBA limitation that's even described in Automation help.
Basically to pass an array as an argument in VBA you must declare object owning method as Variant (equal to no type specification).

 
in your code

Code:
Dim oInertia As Inertia
Dim oMeasure As Measurable

the code that works

Code:
Dim oInertia 
Dim oMeasure

Eric N.
indocti discant et ament meminisse periti
 
That's crazy, I never thought it could be this. Thanks a lot once again.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top