SolidWorks VBA跳过空数组

时间:2018-12-07 12:25:22

标签: arrays vba solidworks

我正在创建用于渲染Solidworks模型的宏。它意在渲染装配体中的所有配置。我正在处理的一段代码必须提取所有配置的质量并将其放在rendername的末尾。对于大多数配置而言,这是可行的。

只有那些在装配体中没有模型的模型(即质量属性中没有可用的数据)会给我一个错误:输入不匹配项。代码看起来像这样(错误的位置以**突出显示:

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

所以我想知道是否有可能跳过错误配置的属性提取(如果可能的话,在渲染名称中将其替换为“ 0”)。

希望您能帮助我。 预先感谢!

1 个答案:

答案 0 :(得分:0)

如果质量为零,您似乎需要跳到下一个迭代。 要跳至下一个迭代,请按以下步骤在VBA中进行操作。

For i = 1 To 3 

 If SomeConditionTrue Then 
  GoTo NextIteration
 End If 

 NextIteration:
Next i

对于SOLIDWORKS API资源,我强烈建议以下内容(您在SO中不会得到太多帮助):