Sebmarshall
Automotive
Hello
I am trying to create a VBA macro to have an analysis of assembly mass and center of gravity, by checking each part body from each part in the assembly.
The part analysis is made by a sub called from the main one which is working from the assembly.
The macro only make calculation for part body that are at the root ; not in the no show and not empty.
After getting COG coordinates, it calculates the COG coordinates in the assembly reference.
The mass and COG are put in an excel file according to the part category, this is for a race engine, and I need to know the mass and the COG analyse according a given list of part. This is managed from the assembly instance description (MassFIA and CODFIA)
To get the part body COG I apply the SPA Workbench (objMeasurable.GetCOG (Coords) here under), but I get an empty result instead of the expected x,y,z coordinates. Can you help me understand why ?
Here under my code ; sorry if it looks quite messy, this my first one :
I am trying to create a VBA macro to have an analysis of assembly mass and center of gravity, by checking each part body from each part in the assembly.
The part analysis is made by a sub called from the main one which is working from the assembly.
The macro only make calculation for part body that are at the root ; not in the no show and not empty.
After getting COG coordinates, it calculates the COG coordinates in the assembly reference.
The mass and COG are put in an excel file according to the part category, this is for a race engine, and I need to know the mass and the COG analyse according a given list of part. This is managed from the assembly instance description (MassFIA and CODFIA)
To get the part body COG I apply the SPA Workbench (objMeasurable.GetCOG (Coords) here under), but I get an empty result instead of the expected x,y,z coordinates. Can you help me understand why ?
Here under my code ; sorry if it looks quite messy, this my first one :
Code:
Sub TraitementPart(InstanceCourante, g, WBK, AxisSystemProd, Niveau1, Niveau2, Niveau3, Niveau4, ComptaResultats)
Set PieceInst = InstanceCourante.ReferenceProduct
Set Piece = PieceInst.Parent.Part
NbreCorpsDePiece = Piece.Bodies.Count
For h = 1 To NbreCorpsDePiece
Dim CorpsH As Body
Set CorpsH = Piece.Bodies.Item(h)
'Verifier si le corps de pièce n'est pas vide
Y = CorpsH.Shapes.Count + CorpsH.HybridBodies.Count + CorpsH.Sketches.Count
Dim showstate As CatVisPropertyShow
'Verifier si le corps de pièce est en Show
Set CorpsHSelection = CATIA.ActiveDocument.Selection
CorpsHSelection.Clear
CorpsHSelection.Add CorpsH
Set CorpsHSelection = CATIA.ActiveDocument.Selection.VisProperties
CorpsHSelection.GetShow showstate
'Verifier si le corps de pièce est à la racine de la part
InBoolOpp = CorpsH.InBooleanOperation
'Si le corps de pièce ne réponds à l'un des 3 critères précédents alors on passe au suivant
If (Y <> 0 And showstate = 0 And InBoolOpp = False) Then
'Calcul masse
Set objSPAWorkbench = PieceInst.Parent.GetWorkbench("SPAWorkbench")
Set objInertia = objSPAWorkbench.Inertias.Add(CorpsH)
Set objRef = Piece.CreateReferenceFromObject(CorpsH)
Set objMeasurable = objSPAWorkbench.GetMeasurable(objRef)
Dim Coords(2) As Variant
objMeasurable.GetCOG (Coords)
' Recuperer le systeme d'axe de la pièce exprimé dans le repère de l'assemblage
Dim AxisSystemRef(11)
InstanceCourante.Position.GetComponents AxisSystemRef
'Changement de repere du repere pièce vers le repere de son assemblage (de rang n)
XcdgN = AxisSystemRef(9) + Coords(0) * AxisSystemRef(0) + Coords(1) * AxisSystemRef(3) + Coords(2) * AxisSystemRef(6)
YcdgN = AxisSystemRef(10) + Coords(0) * AxisSystemRef(1) + Coords(1) * AxisSystemRef(4) + Coords(2) * AxisSystemRef(7)
ZcdgN = AxisSystemRef(11) + Coords(0) * AxisSystemRef(2) + Coords(1) * AxisSystemRef(5) + Coords(2) * AxisSystemRef(8)
'Changement de repere de l'assemblage (de rang n) vers le repere du Porduct rang 0
Xcdg = AxisSystemProd(9) + XcdgN * AxisSystemProd(0) + YcdgN * AxisSystemProd(3) + ZcdgN * AxisSystemProd(6)
Ycdg = AxisSystemProd(10) + XcdgN * AxisSystemProd(1) + YcdgN * AxisSystemProd(4) + ZcdgN * AxisSystemProd(7)
Zcdg = AxisSystemProd(11) + XcdgN * AxisSystemProd(2) + YcdgN * AxisSystemProd(5) + ZcdgN * AxisSystemProd(8)
MasseCorpsH = objInertia.Mass
XiMi = Xcdg * MasseCorpsH
YiMi = Ycdg * MasseCorpsH
ZiMi = Zcdg * MasseCorpsH
'Verifier les attributs masse/CDG
Dim PropDescriptionMCDG As String
PropDescriptionMCDG = InstanceCourante.DescriptionInst
If PropDescriptionMCDG Like "*MasseFIA*" Then
MasseFIA = "Oui"
InertiaFIA = MasseCorpsH
Else
MasseFIA = "Non"
InertiaFIA = 0
End If
If PropDescriptionMCDG Like "*CDGFIA*" Then
CDGFIA = "Oui"
InertiaCDGFIA = MasseCorpsH
XiMiFIA = Xcdg * InertiaCDGFIA
YiMiFIA = Ycdg * InertiaCDGFIA
ZiMiFIA = Zcdg * InertiaCDGFIA
Else
CDGFIA = "Non"
InertiaCDGFIA = 0
End If
'Inscription des données dans Excel
NomPiece = Piece.Name
NomCorps = CorpsH.Name
WBK.Cells(g, 1) = Niveau1
WBK.Cells(g, 2) = Niveau2
WBK.Cells(g, 3) = Niveau3
WBK.Cells(g, 4) = Niveau4
WBK.Cells(g, 5) = NomPiece
WBK.Cells(g, 6) = NomCorps
WBK.Cells(g, 7) = objInertia.Density
WBK.Cells(g, 8) = Round(MasseCorpsH, 3)
WBK.Cells(g, 9) = Round(Xcdg, 3)
WBK.Cells(g, 10) = Round(Ycdg, 3)
WBK.Cells(g, 11) = Round(Zcdg, 3)
WBK.Cells(g, 12) = MasseFIA
WBK.Cells(g, 13) = CDGFIA
WBK.Cells(g, 14) = Round(InertiaFIA, 3)
WBK.Cells(g, 15) = Round(InertiaCDGFIA, 3)
WBK.Cells(g, 16) = Round(XiMiFIA, 3)
WBK.Cells(g, 17) = Round(YiMiFIA, 3)
WBK.Cells(g, 18) = Round(ZiMiFIA, 3)
'Comptabilisation des sommes
ComptaResultats = Array(ComptaResultats(0) + MasseCorpsH, ComptaResultats(1) + InertiaFIA, ComptaResultats(2) + InertiaCDGFIA, ComptaResultats(3) + XiMiFIA, ComptaResultats(4) + YiMiFIA, ComptaResultats(5) + ZiMiFIA, ComptaResultats(6) + XiMi, ComptaResultats(7) + YiMi, ComptaResultats(8) + ZiMi)
g = g + 1
End If
Next
End Sub