Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

Batch script to export surface area from solids in CATParts and CATProducts???

Status
Not open for further replies.

djole988

Structural
Dec 7, 2016
1
0
0
RS
I need CATScript to export surface area of solids from CATParts and CATProducts in one whole folder and it's subfolders. I need to export them to .TXT or .XLS file. Also, I need headers (File names, Product names, etc.) - exact what "Inertia tool" does when exports .TXT file when is checked only Area, but in this case I want whole folder to be processed with hundreds of files.

I was searching many threads on the internet and I didn't find anything that will help me about this problem. I found something about exporting those parameters when selected, but I need to run it in Catia BATCH mode (because i have many files to process).

Please help me with something. Something to start with.

I think this is a good start point, but I am not that familiar with CATScript programming syntax. This script process whole folder, but .CATDrawings files and exports something (because I dont know how to use PARAMNAMES) to .TXT file. And it can be runned in Batch mode. How to change it to process .CATParts and extract surface Areas of solids in .CATParts and .CATProducts (it may process only CATParts).

I appreciate any kind of help.

'--------------------------------------------------------------------------------
' Author:
' Mike Berry
' Published on CATIA V5 Automation blog (v5vb.wordpress.com)
' Send comments and feedback to blogv5vb@gmail.com
'
' Purpose:
' This program will open every CATDrawing in the specified folder one by one
' and retrieve the requested parameter values and write them to a text file
' This program should be run as a batch if many drawings are to be processed.
'
' History:
' Version Date Comment
' 1.0 09/26/10 First version

' Notes:
' 1. You must set the two input values below before running the script
' where it says, "Define input values for this batch run"
'
' FOLDER This is the full path of the folder to be processed
' PARAMNAMES This is a comma separated list of parameter names
' whose value should be retrieved
'--------------------------------------------------------------------------------

Option Explicit

Sub CATMain()

Dim objFolder As Object
Dim intIndex As Integer
Dim intIndex2 As Integer
Dim objFile As File
Dim objDwgDoc As Document
Dim objParams As Parameters
Dim strParamName As String
Dim varParamNames As Variant
Dim strOutputValues As String
Dim intArraySize As Integer
Dim strOutputFilePath As String
Dim objTextStream As TextStream
Dim strOutput As String
Dim objFileSystem As Object
Dim strTimeStamp As String
Dim lngNbDwgs As Long

'Define input values for this batch run
Const FOLDER As String = "C:\Temp\BatchTest"
Const PARAMNAMES As String = "TestString,TestLength,TestMass"

'Make sure the requested folder exists
If CATIA.FileSystem.FolderExists(FOLDER) = False Then Exit Sub

'Create a header in the output string
strOutput = "Folder processed: " & FOLDER & Chr(13)
strOutput = strOutput & "Parameter names: " & PARAMNAMES & Chr(13) & Chr(13)
strOutput = strOutput & "File #" & Chr(9) & "Drawing name" & Chr(9) & Replace(PARAMNAMES, ",", Chr(9)) & Chr(13)

'Create an array from the list of parameter names
'If there is only one name, manually create the array
'otherwise split the string into an array based on the commas
If InStr(1, PARAMNAMES, ",") > 0 Then
varParamNames = Split(PARAMNAMES, ",")
Else
ReDim varParamNames(0)
varParamNames(0) = PARAMNAMES
End If
intArraySize = UBound(varParamNames)

'Process each CATDrawing in the specified folder
lngNbDwgs = 0
Set objFolder = CATIA.FileSystem.GetFolder(FOLDER)
If objFolder.Files.Count > 0 Then
For intIndex = 1 To objFolder.Files.Count
Set objFile = objFolder.Files.Item(intIndex)
If UCase(Right(objFile.Name, 11)) = ".CATDRAWING" Then

'Count the number of drawing processed
lngNbDwgs = lngNbDwgs + 1

'Open the drawing and get at the root parameters
Set objDwgDoc = CATIA.Documents.Open(objFile.Path)
Set objParams = objDwgDoc.Parameters.RootParameterSet.AllParameters

'Append the drawing name to the output string
strOutput = strOutput & lngNbDwgs & Chr(9)
strOutput = strOutput & objDwgDoc.Name & Chr(9)

'Get the value of each requested parameter and
'append them to the output string
For intIndex2 = 0 To intArraySize
strParamName = Trim(varParamNames(intIndex2))
strOutput = strOutput & Chr(9) & GetParameterValue(objParams, strParamName)
Next
strOutput = strOutput & Chr(13)

'Close the drawing
objDwgDoc.Close

End If
Next
End If

'If no drawings were processed, make a note in the output string
If lngNbDwgs = 0 Then strOutput = strOutput & "No CATDrawings were found!"

'Create a timestamp for the output text file by removing invalid chars from
'the current date and time string that is returned by the Now() function
'This is an easy way to guarantee a new file each time the batch is executed
strTimeStamp = Replace(Now, "/", "-")
strTimeStamp = Replace(strTimeStamp, ":", "-")
strTimeStamp = Replace(strTimeStamp, " ", "_")

'Create a new output text file and write the output string
strOutputFilePath = objFolder.Path & "\" & "DwgParamBatchResult_" & strTimeStamp & ".txt"
Set objFile = CATIA.FileSystem.CreateFile(strOutputFilePath, True)
Set objTextStream = objFile.OpenAsTextStream("ForWriting")
objTextStream.Write strOutput
objTextStream.Close

End Sub

'--------------------------------------------------------------------------------
Function GetParameterValue(ByRef iParams As Parameters, ByVal iParamName As String) As String

Dim objParam As Parameter

'Try to find the parameter and trap error in case it doesn't exist
On Error Resume Next
Set objParam = iParams.Item(iParamName)
If Err.Number = 0 Then
GetParameterValue = objParam.ValueAsString
Else
GetParameterValue = "Not Found"
End If

End Function
 
Replies continue below

Recommended for you

Code:
If UCase(Right(objFile.Name, 11)) = ".CATDRAWING" Then

well if you change the .CATDrawing to .CATPart it should actually process only CATPart.

now for your surface area info, I suggest you do a bit of learning and check the v5automation.chm file. This should help you understand CATIA API and Objects. You might be closer to the result than you think... I'll help if you post your progress.

You can also look into this thread560-251808


Eric N.
indocti discant et ament meminisse periti
 
Status
Not open for further replies.
Back
Top