0

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.

The script runs again for 478 itterations after closing and re-opening Solidworks!

My .STEP files are organized as follows:

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.

In this file bin you find a beam.step file, as well as my solidworksDummyPart.SLDPRT file that I load initially to make sure solidworks functions propperly.

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

I have tried using an IF statement to check if swPart is 'nothing' and tried to re-load the file in the hope that it only failed initially. File exists! I can guarantee that, and also, I can continue the code simply by running the for loop from where it stopped.

E.g. If I started it at 1 and it stopped at 477, then I can start the for loop at 477 and it will run flawlessly if I restart Solidworks before I rerun the code.

I am looking for a solution that doesn't require me to restart Solidworks.

I am running Solidworks 2018 SP4.0

avgJoe
  • 832
  • 7
  • 24
  • 1
    Considering the above and your comment, you should probably check why `Set swPart = swApp.LoadFile4(stepFileName, "r", swImportStepData, errors)` returns nothing at that step (i.e. `stepFileName` or `swImportStepData ` might not be what it expects... – FAB Aug 16 '19 at 17:50
  • 1
    And to build on @FAB answer, insert an `If` statement after you `Set swPart = ...` to check if it's properly assigned; something like `If swPart Is Nothing Then ...`. You can pop up an `MsgBox` or gracefully exit your program. My guess is that your file doesn't exist. – PeterT Aug 16 '19 at 18:11
  • @FAB, Thank you for your comments. I have traced the error to the extent where I could tell that swPart was 'nothing', and hence I did conclude that the swApp.LoadFile4 returned nothing. However, I have tried to take care of that by including an if statement (which successfully caught the issue before the program crashed) and then trying to reasign all necessary variables swApp and/or swPart. All my attempts didn't succeed. It is as if Solidworks stops working as expected until a restart of the software. – avgJoe Aug 17 '19 at 01:21

0 Answers0