我遇到了一个非常奇怪的错误。当我运行我的 STEP 到 STL 转换 VBA 脚本时,它完美地运行了 478 次迭代,然后停止并显示以下消息:
Run-time error '91': Object variable or With block variable not set
调试器在该行显示错误,但它发生是Set swModelDocExt = swModel.Extension
因为该变量即使该行已运行。swPart
Nothing
Set swPart = swApp.LoadFile4(stepFileName, "r", swImportStepData, errors)
我已经针对不同的数据多次运行此脚本,并且每次恰好在 478 次迭代后都会引发上述错误。
关闭并重新打开 Solidworks 后,脚本再次运行 478 次迭代!
我的 .STEP 文件组织如下:
C:\mydata\beams\0\beam.step
C:\mydata\beams\1\beam.step
C:\mydata\beams\2\beam.step
...
C:\mydata\beams\500\beam.step
etc.
在此文件箱中,您可以找到一个 beam.step 文件,以及我最初加载的 solidworksDummyPart.SLDPRT 文件,以确保solidworks 正常运行。
'-------------------------------------------------------------------------------
' Preconditions: Verify that the specified SOLIDWORKS part document to open exists.
'
' Postconditions:
' 1. Opens Dummy Solidworks File
' 2. Closes Dummy File
' 3. For i (0:15000)
' 3.1 Imports the STEP file
' 3.2 Runs import diagnostics on the STEP file and repairs
' the bad faces.
' 3.3 Examine the FeatureManager design tree and graphics area.
' 3.4 Exports STL file
'-------------------------------------------------------------------------------
Option Explicit
Dim swApp As SldWorks.SldWorks
Dim swPart As SldWorks.PartDoc
Dim swModel As SldWorks.ModelDoc2
Dim swModelDocExt As SldWorks.ModelDocExtension
Dim swImportStepData As SldWorks.ImportStepData
Dim status As Boolean
Dim errors As Long
Dim warnings As Long
Dim fileName As String
Dim stepFileName As String
Dim datasetPath As String
Dim datapointPath As String
Dim longstatus As Long
Dim i As Integer
Dim counter As Integer
Sub main()
Set swApp = Application.SldWorks
'Open the SOLIDWORKS part document to export to a STEP file
fileName = "C:\mydata\solidworksDummyPart.SLDPRT"
Set swPart = swApp.OpenDoc6(fileName, swDocumentTypes_e.swDocPART, swOpenDocOptions_e.swOpenDocOptions_Silent, "", errors, warnings)
Set swModel = swPart
Set swModelDocExt = swModel.Extension
'Close Dummy Part
Set swPart = Nothing
swApp.CloseDoc "solidworksDummyPart.SLDPRT"
datasetPath = "C:\mydata\beams\"
'Export the SOLIDWORKS part document to a STEP file
counter = 0
For i = 0 To 500
datapointPath = datasetPath & CStr(i)
stepFileName = datapointPath & "\body.STEP"
'Get import information
Set swImportStepData = swApp.GetImportFileData(stepFileName)
'If ImportStepData::MapConfigurationData is not set, then default to
'the environment setting swImportStepConfigData; otherwise, override
'swImportStepConfigData with ImportStepData::MapConfigurationData
swImportStepData.MapConfigurationData = True
'Import the STEP file
Set swPart = swApp.LoadFile4(stepFileName, "r", swImportStepData, errors)
Set swModel = swPart
Set swModelDocExt = swModel.Extension
'Run diagnostics on the STEP file and repair the bad faces
status = swModelDocExt.SelectByID2("Imported1", "BODYFEATURE", 0, 0, 0, False, 0, Nothing, 0)
swModel.ClearSelection2 True
errors = swPart.ImportDiagnosis(True, False, True, 0)
swModel.ClearSelection2 True
' Save As .STL file
longstatus = swPart.SaveAs3(datapointPath & "\body.STL", 0, 0)
'record last saved STL
Debug.Print "Counter: " & CStr(counter) & Chr(9) & "Datapoint: " & CStr(i)
'Reset & Close
Set swPart = Nothing
swApp.CloseDoc "body.SLDPRT"
counter = counter + 1
Next i
End Sub
我尝试使用 IF 语句检查 swPart 是否为“无”,并尝试重新加载文件,希望它最初只是失败。文件已存在!我可以保证,而且,我可以通过从它停止的地方运行 for 循环来继续代码。
例如,如果我在 1 开始它并在 477 停止,那么我可以在 477 开始 for 循环,如果我在重新运行代码之前重新启动 Solidworks,它将完美运行。
我正在寻找不需要我重新启动 Solidworks 的解决方案。
我正在运行 Solidworks 2018 SP4.0