最近手头业务杂事比较多,抽空整理、清理电脑文件,顺便释放一下内存。翻文件夹的时候,翻到了几年前学 CATIA 二次开发时写的一段 VBS 脚本。
当时只是业余浅尝辄止,简单摸索了下 CATIA 的脚本开发,没有深入钻研高阶功能,就写了这么一个很基础的小工具。闲置了很久,一直存放在文件夹里没管,今天清理文件就顺手整理出来,免费分享给有需要的朋友。
做机械设计、产品建模的朋友应该都清楚,日常工作中经常需要把 CATPart 模型批量导出为 STP 格式。
如果手动单个打开、另存为,文件数量多的时候特别繁琐,重复机械操作很浪费时间。网上很多工具要么收费,要么适配性差,容易导出失败、丢参数。
而我这段原生 VBS 脚本,是适配 CATIA 官方接口写的,不用安装任何第三方插件、不用复杂配置,只要电脑装了 CATIA 就能直接运行,稳定性很高。
功能也特别纯粹:一键批量读取文件夹内所有 CATPart 文件,自动批量转换成 STP 格式,同时自动生成日志文件,记录成功、失败的文件信息,方便排查问题。
.vbs,双击即可运行; Option Explicit
Dim CATIA, fso, inputDir, outputDir, logPath, logFile
Dim folder, file, doc, stepPath
Dim converted, failed
' ===== 只需修改这里的文件夹路径 =====
inputDir = "C:\Users\jindo\Desktop\temp\catia"
outputDir = "C:\Users\jindo\Desktop\temp\catia\step_output"
Set fso = CreateObject("Scripting.FileSystemObject")
If Not fso.FolderExists(inputDir) Then Fail "Input folder does not exist: " & inputDir
EnsureFolder outputDir
logPath = fso.BuildPath(outputDir, "conversion_log.txt")
Set logFile = fso.CreateTextFile(logPath, True, True)
logFile.WriteLine "CATPart to STEP conversion"
logFile.WriteLine "Started: " & Now
logFile.WriteLine String(60, "-")
Set CATIA = GetCATIA()
If CATIA Is Nothing Then Fail "CATIA could not be started."
converted = 0
failed = 0
Set folder = fso.GetFolder(inputDir)
For Each file In folder.Files
If LCase(fso.GetExtensionName(file.Name)) = "catpart" Then
Set doc = Nothing
On Error Resume Next
Err.Clear
Set doc = CATIA.Documents.Open(file.Path)
If Err.Number <> 0 Or doc Is Nothing Then
LogFailure file.Name, "Open failed: " & Err.Description
Else
Err.Clear
stepPath = fso.BuildPath(outputDir, fso.GetBaseName(file.Name) & ".stp")
doc.ExportData stepPath, "stp"
If Err.Number <> 0 Then
LogFailure file.Name, "Export failed: " & Err.Description
Else
converted = converted + 1
logFile.WriteLine "OK " & file.Name & " -> " & stepPath
End If
Err.Clear
doc.Close
End If
Err.Clear
On Error GoTo 0
End If
Next
logFile.WriteLine String(60, "-")
logFile.WriteLine "Completed: " & Now
logFile.WriteLine "Converted: " & converted
logFile.WriteLine "Failed: " & failed
logFile.Close
MsgBox "Batch conversion completed." & vbCrLf & _
"Converted: " & converted & vbCrLf & _
"Failed: " & failed & vbCrLf & _
"Log: " & logPath, vbInformation, "CATIA VBS"
WScript.Quit 0
Sub LogFailure(fileName, reason)
failed = failed + 1
logFile.WriteLine "FAIL " & fileName & " | " & reason
End Sub
Function GetCATIA()
Dim app
On Error Resume Next
Set app = GetObject(, "CATIA.Application")
If app Is Nothing Then
Err.Clear
Set app = CreateObject("CATIA.Application")
If Not app Is Nothing Then app.Visible = True
End If
Err.Clear
On Error GoTo 0
Set GetCATIA = app
End Function
Sub EnsureFolder(folderPath)
If Not fso.FolderExists(folderPath) Then fso.CreateFolder folderPath
End Sub
Sub Fail(message)
On Error Resume Next
logFile.Close
On Error GoTo 0
MsgBox message, vbCritical, "CATIA VBS"
WScript.Quit 1
End Sub conversion_log.txt 日志文件,方便后续核对问题文件;当初学 CATIA 二次开发只是一时兴起,没有深耕,写的都是这种实用小脚本。现在回头看,虽然代码很基础,但日常工作用来解放双手、提升效率完全足够。
平时做设计、整理模型文件,批量导出格式的需求很频繁,手动操作费时费力。这个小脚本刚好能解决这个刚需,不用折腾复杂的开发程序,简单、稳定、够用。
有需要的朋友可以直接复 制自用,日常批量转STP非常方便!