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 measure inertia (mass) only for shown elements

Status
Not open for further replies.

pieswt

Mechanical
Nov 16, 2023
9
Hello all,

I have created a macro to measure mass of each part in an Assembly(CatProduct).
The macro is exporting BOM of complete assembly as excel file and then using that BOM to get mass of each part present in the assembly.
The macro is working fine but I noticed that it is measuring mass of hidden bodies/elements too.

Can someone help to modify the macro so that it measures only shown elements?

here's my code:
Code:
Sub CATMain()
    Dim Template As String
    Template = "C:\Users\xyz\Desktop\BOM.xls"  'modify this path
    Dim productDocument1 As ProductDocument
    Set productDocument1 = CATIA.ActiveDocument

    Dim product1 As Product
    Set product1 = productDocument1.Product

    Dim assemblyConvertor1 As AssemblyConvertor
    Set assemblyConvertor1 = product1.GetItem("BillOfMaterial")

    Dim arrayOfVariantOfBSTR1(4)
    arrayOfVariantOfBSTR1(0) = "Quantity"
    arrayOfVariantOfBSTR1(1) = "Part Number"
    arrayOfVariantOfBSTR1(2) = "Type"
    arrayOfVariantOfBSTR1(3) = "Nomenclature"
    arrayOfVariantOfBSTR1(4) = "Revision"
    Set assemblyConvertor1Variant = assemblyConvertor1
    assemblyConvertor1Variant.SetCurrentFormat arrayOfVariantOfBSTR1

    Dim arrayOfVariantOfBSTR2(1)
    arrayOfVariantOfBSTR2(0) = "Quantity"
    arrayOfVariantOfBSTR2(1) = "Part Number"
    Set assemblyConvertor1Variant = assemblyConvertor1
    assemblyConvertor1Variant.SetSecondaryFormat arrayOfVariantOfBSTR2
    assemblyConvertor1.[Print] "XLS", Template, product1
    
    MsgBox "Bom Exported Successfully"
    
    Dim FileSys
    Set FileSys = CATIA.FileSystem
    Dim xlApp
    Set xlApp = CreateObject("Excel.Application")
    Dim mydoc
    Set mydoc = xlApp.workbooks.Open(Template)
    
    Call Getweight
    
End Sub


Sub Getweight()
    Dim BOMExcel As Object
    
    On Error Resume Next
    
    'if Excel is already running, then get the Excel object
    Set BOMExcel = GetObject(, "Excel.Application")
    
    
    If Err.Number <> 0 Then
        'If Excel is not already running, then create a new session of Excel
        Set BOMExcel = CreateObject("Excel.Application")
        BOMExcel.Visible = True
    End If
    
    'add a new workbook
    BOMExcel.workbooks.Open ("C:\Users\Z0002829\Desktop\Steering Team Macro Support\WIP\BOM.xls")
    BOMExcel.Visible = True
    Set ws = BOMExcel.ActiveWorkbook.Sheets(1)
    
    ws.cells(4, 6).Value = "CAD Weight"
    With ws
        Set findrow = .Range("A:A").Find(What:="Recapitulation", LookIn:=xlValues) 
    End With
    
    
    FindRowNumber = findrow.Row
    
    For i = 1 To FindRowNumber - 1
            If ws.cells(i, 3).Value = "Part" Then
            
            PNtoSearch = ws.cells(i, 2).Value
            'measure weight
            Set productDocument1 = CATIA.ActiveDocument
            
            Set product1 = productDocument1.Product
            
            Dim oSelection As Selection
            
            Set oSelection = CATIA.ActiveDocument.Selection
            
            oSelection.Search "CATAsmSearch.Part.PartNumber=" & PNtoSearch & ",all"
            
             Dim objProd As Product
             
              Set objProd = CATIA.ActiveDocument.Selection.Item2(1).Value
              Set objInertia = GetProductInertiaa(objProd)
              If Not (objInertia Is Nothing) Then
                'Retrieve the mass just to show it worked
                'MsgBox objInertia.Mass
                ws.cells(i, 6).Value = objInertia.Mass * 1000
                
              Else
                MsgBox "The Inertia could not be retrieved!"
              End If
            End If
    Next i
    
    MsgBox "Weight Exported!"
    
    ws.SaveAs Filename:="BOM with Weight exproted.xlsx", FileFormat:=xlWorkbookDefault
    

End Sub

Function GetProductInertia(ByRef iProd As Product) As Inertia
 
  'If successful, this function will return an inertia object
  'Otherwise, Nothing is returned (you should check the return value)
 
  Dim objInertia As Inertia
 

  On Error Resume Next
 
  Set objInertia = iProd.ReferenceProduct.GetTechnologicalObject("Inertia")
  'Set ObjDensity = iProd.ReferenceProduct.GetTechnologicalObject("Density")
  If Err.Number = 0 Then
    Set GetProductInertia = objInertia
  Else
    Set GetProductInertia = Nothing
End If
 
End Function


thread560-340604
 
Replies continue below

Recommended for you

In short, I am searching for how to select "Measure only shown elements" using catvba? as shown in this image:

MicrosoftTeams-image_plqzoy.png
 
Thank you for your reply Little Cthulhu.

Selection.VisProperties doesn't solve my issue as there are visible parts having some bodies hiddden.

I want to measure the mass of that part as a whole (but it should not measure the hidden body mass).
 
there is a setting in tools-options that controls that tick-mark:
General-Parameters and Measure: Measuring Criteria: Measure only shown elements
Once you export settings of this tab, you will be able to deduct how you can toggle it on via script (GetAttr/PutAttr-methods):
Code:
Set settingControllers1 = CATIA.SettingControllers

Set settingRepository1 = settingControllers1.Item("MeasureSettings")
long2 = settingRepository1.GetAttr("MeasureOnlyShownElementsStatus")
'--------------------------------------------------
' Parameter 1 : (String) "MeasureOnlyShownElementsStatus"
' Returned value : (Variant) (Long) 0
'--------------------------------------------------


regards,
LWolf
 
Great, I was looking for this only...
I will modify the macro and test it....

Thank you LWolf!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor