Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

CatVBA Macro Mass Extraction to Excel

Status
Not open for further replies.

nicolevicius123

Aerospace
Jan 21, 2023
2
0
0
SG
Hello guys,

Here I have some problem with the lack of my knowledge in VBA or something else. This is my first time when I took this assignment and I don't have any background in software engineering. I got script from my colleague to count volume of CatPart. Now I need to tweak it with adding extracted mass of the CatPart. Bassically I got the insight from here:

Link

However I saw that the result of mass is "0". Here is the code:

Code:
Sub CATMain()

'Created by: Irfan M Syahdan
'Revision 1 completed at 30 Sep 2021
'This macro extract part volume by using the GetMeasurable() method which applies on a Part object. It opens all catparts in the selected folder and extract the volume one by one _
in a loop.
'Assumption and limitations:
'Ectract Volume only works with .CATPart
'Recommended all files in folder are all .CATPart, it will speed up the macro a bit
'Ensure all .CATPART are named according to their respective PN
'Ensure you have all the license for the part creation (typically MPA or MPA+SPA+TUB). Sheet metal will need the sheet metal license
'if you have large numbers of .CATPart, it will take a few minutes as it needs to open and close files (about 1 minute for 60 .CATPart in the folder)
'The macro will close any .CATPART that has the same Title as ones in folder (recommended to close all CATIA windows firts)

'Changes from rev - to 1 _
Macro now only opens .CATParts, it will not try to open files with other type of extension. Only-.CATPart-allowed-in-folder limitation is now lifted

On Error Resume Next                                'continue if error occurs

'Set/Finding the parent folder to identifiy where the catparts are located
Set FileSys = CATIA.FileSystem                      'get root object for files/folder

FPath = CATIA.FileSelectionBox("Select a Catalog Part File.", "*.CATPart", CatFileSelectionModeOpen) 'Open input window to set destination folder, only show .catpart

If FPath = vbCancel Then                            'kill the subroutine if user choose cancel
    Exit Sub
End If

Dim oFolder As Folder                               'Declare variable for selected folder
Set oFile = FileSys.GetFile(FPath)                  'get file path

Set oFold = oFile.ParentFolder                      'find parent folder (the selected folder above)

Dim files1 As Files                                 'Declare object to enable looping through files
Set files1 = oFold.Files                            'Set or identify files the folder are the ones to open (to extract volume)

'excel preparation
Set oExcel = CreateObject("Excel.Application")      'call Excel
oExcel.Visible = True                               'show opened excel
Set oWorkbook = oExcel.workbooks.Add()              'add workbook
Set osheet1 = oWorkbook.Sheets.Item(1)              'set working on firts sheet/tab
osheet1.cells(1, 1).Value = "No"                    'set cell location for No
osheet1.cells(1, 2).Value = "PN"                    'set cell location for extracted volume Part Number
osheet1.cells(1, 3).Value = "Volume (inch^3)"       'set cell location for volume
osheet1.cells(1, 4).Value = "Mass (Kg)"

Dim i As Integer                                    'set iteration variable for looping files in folder
Dim n As Integer                                    'set counter for .CATPart
n = 1

'Iteration to open and extract volume and mass one by one start
For i = 1 To files1.Count                                                   'Looping through all the catpartfiles in the folder
    If Right(files1.Item(i).Name, 8) = ".CATPart" Then                      'If condition to only open .CATPart
        Set Doc = CATIA.Documents.Open(files1.Item(i).Path)                 'open catpart file for i th sequence

        Dim partDocument1 As Document
        Dim oPart As Part

        Set oPart = CATIA.ActiveDocument.Part                               'set the current opened document as Part object to enable Part object methods
        Set oRef = oPart.CreateReferenceFromObject(oPart.MainBody)          'Create a reference from catpart partbody as the thing that will be measured
        Set oSPAWkb = CATIA.ActiveDocument.GetWorkbench("SPAWorkbench")     'activate the workbench mode to enable GetMeasurable method
        Set oMeasurable = oSPAWkb.GetMeasurable(oRef)                       'set the GetMesurable method to measure referenced object/body
        
        ' Compute the inertia
        Dim oInertia
        Set oInertia = oSPAWkb.GetMeasurable(oRef)

        ' Read the inertia data
        Dim dMass As Double
        dMass = oInertia.Mass
               
        
        osheet1.cells(n + 1, 1).Value = n                                                                   'n function as No value in the excel table
        osheet1.cells(n + 1, 2).Value = Left(CATIA.ActiveDocument.Name, Len(CATIA.ActiveDocument.Name) - 8) 'Extract PN and Remove .CATPart string in the PN column
        osheet1.cells(n + 1, 3).Value = Round(oMeasurable.Volume * 61023.7441, 3)                           'Extract Volume to excel cell. Value converted from m3 to inch3 and rounded to 3 decimal
        osheet1.cells(n + 1, 4).Value = dMass
        
        n = n + 1
        CATIA.ActiveDocument.Close                                          'close opened .CATPART file
    End If

Next i

'Tidying up the excels (autofit column size)
osheet1.Columns("A:A").EntireColumn.Autofit
osheet1.Columns("B:B").EntireColumn.Autofit
osheet1.Columns("C:C").EntireColumn.Autofit

End Sub


I hope somebody can give me a hand about this. :)

Thank you
 
Replies continue below

Recommended for you

Status
Not open for further replies.
Back
Top