AnsweredAssumed Answered

STEP to STL script stops after 478 itterations with runtime error 91!?

Question asked by Philippe Wyder on Aug 16, 2019
Latest reply on Aug 17, 2019 by Philippe Wyder

I have encountered a very odd error. When I run my STEP to STL conversion VBA  script, it runs flawlessly for 478 itterations and then stops with the following message:

Run-time error '91': Object variable or With block variable not set

the debugger shows the error on the line "Set swModelDocExt = swModel.Extension", but it happens because the variable 'swPart' is 'Nothing' even though the line "Set swPart = swApp.LoadFile4(stepFileName, "r", swImportStepData, errors)" was run.


I have run this script many times for different data, and it throws the above error after exactly 478 itterations every time.



' 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 300
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