gpt4 book ai didi

arrays - Solidworks VBA 跳过空数组

转载 作者:行者123 更新时间:2023-12-02 00:56:54 28 4
gpt4 key购买 nike

我正在创建一个用于渲染 solidworks 模型的宏。它旨在呈现装配中的所有配置。我正在处理的一段代码必须提取所有配置的质量并将其放在渲染名称的末尾。对于大多数配置,这都有效。

只有装配中没有模型的模型,即质量属性中没有可用数据,才会给我一个错误:类型不匹配。代码如下所示(错误位置用 ** 突出显示:

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”)。

希望你能帮帮我。提前致谢!

最佳答案

如果您的质量为零,您似乎需要跳到下一次迭代。要跳到下一次迭代,请按照以下方法在 VBA 中执行此操作。

For i = 1 To 3 

If SomeConditionTrue Then
GoTo NextIteration
End If

NextIteration:
Next i

对于 SOLIDWORKS API 资源,我强烈建议如下(在 SO 中你不会得到太多):

关于arrays - Solidworks VBA 跳过空数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53669516/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com