Macro to position model and render with PV360

Hi,

I'm trying to make a macro for rendering models (assembly files, SolidWorks 2018, VBA macro).

The render itself is working fine, using RayTraceRenderer.

Positioning using the ShowNamedView2 function also works as expected.

To get a nice position of the model though, I want to move it a bit more. Untill now we've been doing it like this:

Select front view (ctrl+1), hit the left arrowkey 3 times and hit the down arrow key.

I'm able to get the right position using the VBA macro, as long as the render isn't invoked. As soon as I add the line to initiate the render the keystrokes (arrow keys) are executed after the render is done. Looks like I've built a time machine

Here's my code:

Option Explicit

Dim swApp As SldWorks.SldWorks

Dim swModel As SldWorks.ModelDoc2

Dim status As Boolean

Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As LongPtr)

Declare PtrSafe Sub keybd_event Lib "user32.dll" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)

Dim swRayTraceRenderer As SldWorks.RayTraceRenderer

Dim swRayTraceRenderOptions As SldWorks.RayTraceRendererOptions

Sub main()

    Const VK_UP = &H26

    Const VK_LEFT = &H25

    Const VK_DOWN = &H28

    Set swApp = Application.SldWorks

    Set swModel = swApp.ActiveDoc

    swApp.RunCommand swCommands_e.swCommands_Hideshow_Brwser_Tree, ""

    swModel.ShowNamedView2 "*Front", -1

 

    keybd_event VK_LEFT, 0, 0, 0

    keybd_event VK_LEFT, 0, 0, 0

    keybd_event VK_LEFT, 0, 0, 0

    keybd_event VK_DOWN, 0, 0, 0

    swModel.ActiveView.AddPerspective

    swModel.ViewZoomtofit2

    Set swRayTraceRenderer = swApp.GetRayTraceRenderer(swPhotoView)

    Set swRayTraceRenderOptions = swRayTraceRenderer.RayTraceRendererOptions

  

    status = swRayTraceRenderer.DisplayPreviewWindow

    status = swRayTraceRenderer.RenderToFile("c:\temp\Filter_2", 0, 0)

    swApp.RunCommand swCommands_e.swCommands_Hideshow_Brwser_Tree, ""

    swModel.ActiveView.RemovePerspective

    status = swRayTraceRenderer.CloseRayTraceRender

End Sub

I've tried adding Sleep to give the script some time to execute the keypresses, but that makes no difference.

First I tried it with SendKeys, but as it's kinda iffy I prefer this keybd_event-function.

When I don't run the RenderToFile function it's all fine. But as soon as the Render-function is called the keystrokes are executed at the end of the script.

I'm at a lost here. Any ideas?

SolidworksApi/macros