Hi,
I tried to improve a macro given in this forum by James Sharp and add another “layer” to the macro as follows:
Go over the all the configuration and display states and for each, renders each camera view.
I have a test model with a single configuration, 3 display states and 3 camera views – 9 renders in total.
The macro, in this case, should create 3 sets of 3 renders, 3 for each display state – 9 renders in total
Last time I run the it, it created the 1st 3 renders for the current display state, the 2nd 3 sets for the next display state but for the last display state it generated blank renders.
I added the command:
status = swRayTraceRenderer.CloseRayTraceRender
right before the display should change but it did not help.
Should I reset VP360 after I change the display state? If yes than how?
If you test this macro change the TASK_WORKING_DIR to the directory where you want the renders to be saved.
Dim swApp As Object
Dim Part As Object
Dim swConfMgr As ConfigurationManager
'Dim swDoc As ModelDoc2
Dim swConf As Configuration
Dim vConfigNameArr As Variant
Dim vConfigName As Variant
Dim vDisplayStatesNamesArr() As String
Dim vDPName As Variant
Dim countDP As Integer
'------------------------------------------
'
' Preconditions: SolidWorks model is open and contains at least one camera.
'
' Postconditions: The cameras are turned on and off.
'
'------------------------------------------
Option Explicit
'
' Utility struct to combine the name and ID of a camera
'
Type Camera_t
Name As String
Id As Long
End Type
'Dim swApp As SldWorks.SldWorks
Sub main()
Dim swModel As SldWorks.ModelDoc2
Dim swModelView As SldWorks.ModelView
Dim swCamera As SldWorks.Camera
Dim lNumCameras As Long
Dim lCameraId As Long
Dim swFeature As SldWorks.Feature
Dim aCameras() As Camera_t
Dim bValue As Boolean
Dim sFeatureName As String
Dim lTypeOfCamera As Long
Dim lTypeOfCameraPosition As Long
Dim TASK_WORKING_DIR As String
Dim FILE_NAME As String
Set swApp = Application.SldWorks ' Connect to SolidWorks
Set swModel = swApp.ActiveDoc ' Get active document
Set swModelView = swModel.ActiveView ' Get the active model view for the active document
Set swConfMgr = swModel.ConfigurationManager
Set swConf = swConfMgr.ActiveConfiguration
vConfigNameArr = swModel.GetConfigurationNames
' Get current directory (destination of render)
TASK_WORKING_DIR = <YOUR_RENDER_DIRECTORY>
FILE_NAME = InputBox("Enter Render Name")
' Check if a camera view is active
Set swCamera = swModelView.Camera
Debug.Print
If (swCamera Is Nothing) Then
Debug.Print "No active camera."
Else
Debug.Print "Camera is active."
Debug.Print " Name = " & swCamera.Id
End If
swModelView.Camera = Nothing ' Turn off camera view
Dim lOutFile As String
Dim status As Boolean
Dim swRayTraceRenderer, swRayTraceRenderOptions As Object
Set swRayTraceRenderer = swApp.GetRayTraceRenderer(swPhotoView) ' Access PhotoView 360
Set swRayTraceRenderOptions = swRayTraceRenderer.RayTraceRendererOptions ' Get and set rendering options
' Get the number of cameras
lNumCameras = swModel.Extension.GetCameraCount
Debug.Print
Debug.Print "Number of cameras = " & lNumCameras
ReDim aCameras(lNumCameras - 1)
For lCameraId = 0 To (lNumCameras - 1)
' Valid ID:
'
' 0 <= ID <= (ModelDocExtension::GetCameraCount - 1)
'
' IDs are reassigned if a camera is deleted
aCameras(lCameraId).Id = lCameraId
Set swCamera = swModel.Extension.GetCameraById(lCameraId) ' Get the camera
Set swFeature = swCamera
sFeatureName = swFeature.Name ' Get the names of the feature and camera
aCameras(lCameraId).Name = sFeatureName
lTypeOfCamera = swCamera.Type ' Get the type of camera
lTypeOfCameraPosition = swCamera.PositionType ' Get the type of camera position
Next lCameraId
'Loop over configurations
For Each vConfigName In vConfigNameArr
Debug.Print "Configuration = " & vConfigName
Set swConf = swModel.GetConfigurationByName(vConfigName)
countDP = swConf.GetDisplayStatesCount()
Debug.Print " Count Display states: " & countDP
vDisplayStatesNamesArr = swConf.GetDisplayStates()
'Loop over display states
For Each vDPName In vDisplayStatesNamesArr
Debug.Print " Display state = " & vDPName
'activate display state
'InputBox ("Press to switch Display")
'MsgBox "Switch to Display " & vDPName, vbOKOnly
swConf.ApplyDisplayState (vDPName)
' Now switch the view to each camera
For lCameraId = 0 To (lNumCameras - 1)
bValue = swModelView.SetCameraByName(aCameras(lCameraId).Name)
If (bValue = False) Then
Debug.Print "Failed to set model view to use camera " & aCameras(lCameraId).Name
Else
Debug.Print "Model view set to use camera " & aCameras(lCameraId).Name
lOutFile = TASK_WORKING_DIR & "render_" & FILE_NAME & "_" & vDPName & "_" & lCameraId
status = swRayTraceRenderer.RenderToFile(lOutFile, 0, 0) ' Render
End If
swModelView.Camera = Nothing
Next lCameraId
status = swRayTraceRenderer.CloseRayTraceRender
Next
Next
MsgBox "Done"
End Sub