Search code examples
arraysvbasolidworks

Solidworks VBA skip empty array


I'm creating a macro for rendering solidworks models. It is meant to render all configurations in an assembly. A piece of code I'm working on has to extract the mass of all configurations and place it at the end of the rendername. For most configurations this works.

Only the ones where there are no models in the assembly, meaning there is no data available in Mass Properties, would give me an error: Type missmatch. The code looks like this (with the location of the error highlighted with **:

Sub RenderAllConfig()

    Dim swApp As SldWorks.SldWorks
    Dim swModel As SldWorks.ModelDoc2
    Dim swModelExt As SldWorks.ModelDocExtension
    Dim swModelView As SldWorks.ModelView
    Dim vConfNameArr As Variant
    Dim vMassProp As Variant
    Dim nStatus As Long
    Dim status As Boolean
    Dim weight As Long
    Dim sConfigName As String
    Dim Scene As SldWorks.swScene
    Dim i As Long
    Dim bShowConfig As Boolean
    Dim swRayTraceRenderer As SldWorks.RayTraceRenderer
    Dim swRayTraceRenderOptions As SldWorks.RayTraceRendererOptions
    Dim Part As Object

    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc  
    Set swModelExt = swModel.Extension
    Set swModelView = swModel.ActiveView
    vConfNameArr = swModel.GetConfigurationNames

    For i = 0 To UBound(vConfNameArr)

        sConfigName = vConfNameArr(i)
        bShowConfig = swModel.ShowConfiguration2(sConfigName)
        swModelExt.SelectAll
        vMassProp = swModelExt.GetMassProperties2(0, nStatus, True)

        Debug.Print "" & nStatus

        **weight = vMassProp(5)**

        swModel.ClearSelection2 True
        Set Part = swApp.ActiveDoc
        Dim myModelView As Object
        Set myModelView = Part.ActiveView
        myModelView.AddPerspective

        Part.ViewZoomtofit2
        Part.ViewZoomtofit2
        Part.ViewZoomtofit2
        Part.ViewZoomtofit2
        Part.ViewZoomtofit2
        Part.ShowNamedView2 "*Isometric", 7
        Part.ViewZoomtofit2

        Part.ViewDisplayShaded
        Dim activeModelView As Object
        Set activeModelView = Part.ActiveView
        activeModelView.DisplayMode = swViewDisplayMode_e.swViewDisplayMode_ShadedWithEdges
        Part.ClearSelection2 True
        boolstatus = Part.Extension.SketchBoxSelect("0.000000", "0.000000", "0.000000", "0.000000", "0.000000", "0.000000")
        Part.ViewDisplayShaded

        ' Access PhotoView 360
        Set swRayTraceRenderer = swApp.GetRayTraceRenderer(swPhotoView)
        ' Get and set rendering options
        Set swRayTraceRenderOptions = swRayTraceRenderer.RayTraceRendererOptions '<-- Geeft hier foutcode als PhotoView 360 NIET is ingeschakeld
        ' Display render window
        Debug.Print "Current rendering values"
        Debug.Print "  ImageHeight          = " & swRayTraceRenderOptions.ImageHeight
        Debug.Print "  ImageWidth           = " & swRayTraceRenderOptions.ImageWidth
        Debug.Print "  ImageFormat          = " & swRayTraceRenderOptions.ImageFormat
        Debug.Print "  PreviewRenderQuality = " & swRayTraceRenderOptions.PreviewRenderQuality
        Debug.Print "  FinalRenderQuality   = " & swRayTraceRenderOptions.FinalRenderQuality
        Debug.Print "  BloomEnabled         = " & swRayTraceRenderOptions.BloomEnabled
        Debug.Print "  BloomThreshold       = " & swRayTraceRenderOptions.BloomThreshold
        Debug.Print "  BloomRadius          = " & swRayTraceRenderOptions.BloomRadius
        Debug.Print "  ContourEnabled       = " & swRayTraceRenderOptions.ContourEnabled
        Debug.Print "  ShadedContour        = " & swRayTraceRenderOptions.ShadedContour
        Debug.Print "  ContourLineThickness = " & swRayTraceRenderOptions.ContourLineThickness
        Debug.Print "  ContourLineColor     = " & swRayTraceRenderOptions.ContourLineColor
        Debug.Print " "

        'Change rendering values
        Debug.Print "New rendering values"
        swRayTraceRenderOptions.ImageHeight = 405
        Debug.Print "  ImageHeight          = " & swRayTraceRenderOptions.ImageHeight
        swRayTraceRenderOptions.ImageWidth = 720
        Debug.Print "  ImageWidth           = " & swRayTraceRenderOptions.ImageWidth
        swRayTraceRenderOptions.ImageFormat = swImageFormat_PNG
        Debug.Print "  ImageFormat          = " & swRayTraceRenderOptions.ImageFormat
        swRayTraceRenderOptions.PreviewRenderQuality = swRenderQuality_Better
        Debug.Print "  PreviewRenderQuality = " & swRayTraceRenderOptions.PreviewRenderQuality
        swRayTraceRenderOptions.FinalRenderQuality = swRenderQuality_Best
        Debug.Print "  FinalRenderQuality   = " & swRayTraceRenderOptions.FinalRenderQuality
        swRayTraceRenderOptions.BloomEnabled = False
        Debug.Print "  BloomEnabled         = " & swRayTraceRenderOptions.BloomEnabled
        swRayTraceRenderOptions.BloomThreshold = 0
        Debug.Print "  BloomThreshold       = " & swRayTraceRenderOptions.BloomThreshold
        swRayTraceRenderOptions.BloomRadius = 0
        Debug.Print "  BloomRadius          = " & swRayTraceRenderOptions.BloomRadius
        swRayTraceRenderOptions.ContourEnabled = False
        Debug.Print "  ContourEnabled       = " & swRayTraceRenderOptions.ContourEnabled
        swRayTraceRenderOptions.ShadedContour = False
        Debug.Print "  ShadedContour        = " & swRayTraceRenderOptions.ShadedContour
        swRayTraceRenderOptions.ContourLineThickness = 0
        Debug.Print "  ContourLineThickness = " & swRayTraceRenderOptions.ContourLineThickness
        swRayTraceRenderOptions.ContourLineColor = 255
        Debug.Print "  ContourLineColor     = " & swRayTraceRenderOptions.ContourLineColor

        Set swConfig = swModel.GetActiveConfiguration
        Set Scene = swConfig.GetScene
        status = Scene.DeleteFloorAppearance()
        Scene.BackgroundType = 0
        Scene.FloorShadows = False
        Scene.FloorReflections = False
        Scene.FloorOffset = 5

        status = swModel.ForceRebuild3(True)
        Part.ViewZoomtofit2

        ' Display the preview window
        status = swRayTraceRenderer.DisplayPreviewWindow
        ' Close render
        status = swRayTraceRenderer.CloseRayTraceRender
        ' Invoke final render window
        status = swRayTraceRenderer.InvokeFinalRender
        ' Abort final render window
        status = swRayTraceRenderer.AbortFinalRender
        ' Render to Windows Bitmap format
        status = swRayTraceRenderer.RenderToFile("renderlocation\" & "150cm\" & "150" & vConfNameArr(i) & "_" & weight & ".png", 0, 0)
        swRayTraceRenderOptions.FinalRenderQuality = swRenderQuality_Good
        ' Render to HDR format (format extension omitted)
        status = swRayTraceRenderer.RenderToFile("renderlocation\" & "150cm\" & "150" & vConfNameArr(i) & "_" & weight, 0, 0)

        Set swRayTraceRenderOptions = Nothing
        ' Close render
        status = swRayTraceRenderer.CloseRayTraceRender

    Next i

End Sub

So I'm wondering if there is a possibility to skip the extraction of properties for the configurations with errors (and if possible replace it with "0" in the rendername).

Hope you can help me out. Thanks in advance!


Solution

  • Looks like you need to skip to the next iteration if you have a mass of zero. To skip to the next iteration, here's how you do it in VBA.

    For i = 1 To 3 
    
     If SomeConditionTrue Then 
      GoTo NextIteration
     End If 
    
     NextIteration:
    Next i
    

    For SOLIDWORKS API resources, I highly suggest the following (You won't get much here in SO):