Sub MaxwellZoomAnimation()
On Error GoTo handle_error
'dialog title
Dim dialogTitle As String
dialogTitle = "Maxwell Turntable Animation"
'get SW
Set swApp = Application.SldWorks
If swApp Is Nothing Then
MsgBox "Unable to connect to SolidWorks", vbOKOnly, dialogTitle
Exit Sub
End If
'get active doc
Dim doc As ModelDoc2
Set doc = swApp.ActiveDoc
If doc Is Nothing Then
MsgBox "There is no active document.", vbOKOnly, dialogTitle
Exit Sub
End If
'get active view
Dim view As ModelView
Set view = doc.ActiveView
If view Is Nothing Then
MsgBox "There is no active view.", vbOKOnly, dialogTitle
Exit Sub
End If
'get view camera
Dim cam As Camera
Set cam = view.Camera
If cam Is Nothing Then
MsgBox "The active view has no SolidWorks camera - " & _
"please add one, or choose a viewport which does.", vbOKOnly, dialogTitle
Exit Sub
End If
'Maxwell.Script ScriptObject
Dim maxwell As Object
Set maxwell = CreateObject("Maxwell.Script.ScriptObject")
'ScriptObject is created?
If maxwell Is Nothing Then
MsgBox "Unable to create Maxwell.Script.ScriptObject.", vbOKOnly, dialogTitle
Exit Sub
End If
'ScriptObject is connected?
If Not maxwell.IsConnected Then
MsgBox "There is no current Maxwell Scene.", vbOKOnly, dialogTitle
Exit Sub
End If
'declare a bunch of variables
Dim viewportRect(0 To 3) As Long
Dim originalPosition As MathPoint
Dim cameraTarget As MathPoint
Dim distance As MathVector
Dim framePosition As MathPoint
Dim strFrameCount As String
Dim frameCount As Integer
Dim meshesWereCached As Boolean
Dim i As Integer
'get viewport for refreshing
viewportRect(0) = view.FrameLeft
viewportRect(1) = view.FrameTop
viewportRect(2) = view.FrameWidth
viewportRect(3) = view.FrameHeight
'initialize previous frameCount
If previousFrameCount < 2 Then
previousFrameCount = 24
End If
'get number of frames from user
get_frameCount:
strFrameCount = InputBox("Specify the number of frames to render.", _
dialogTitle, previousFrameCount)
'cancelled by user?
If strFrameCount = "" Then Exit Sub
'validate to numeric input
If Not IsNumeric(strFrameCount) Then
MsgBox "Please enter a number.", vbOKOnly, dialogTitle
'try again
GoTo get_frameCount
End If
'validate specified number of frames
If CInt(strFrameCount) < 2 Then
MsgBox "Please enter a number greater than one.", vbOKOnly, dialogTitle
'try again
GoTo get_frameCount
End If
'set framecount
frameCount = CDbl(strFrameCount)
'check for crazy number of frames
If frameCount > 360 Then
If Not MsgBox("That sure is alot of frames (" & _
frameCount & "). Do you really want " & _
"continue?", vbYesNo, dialogTitle) = vbYes Then
Exit Sub
End If
End If
'store as previous count
previousFrameCount = frameCount
'current position/target
Set originalPosition = cam.GetPosition
Set cameraTarget = cam.TargetPointPosition
'save current value of Cache MXS Meshes option
meshesWereCached = maxwell.PlugInOptions.CacheMXSMeshes
'enable Cache MXS Meshes
maxwell.PlugInOptions.CacheMXSMeshes = True
'start plugin animation loop
maxwell.Rendering.BeginAnimation
For i = 1 To frameCount
'write MXS
maxwell.Rendering.RenderToMxs
'get distance vector again
Set distance = originalPosition.Subtract(cameraTarget)
'scale by frameCount
Set distance = distance.Scale(1# - (i / frameCount))
'add scaled distance to original target
Set framePosition = cameraTarget.AddVector(distance)
'set new position
cam.SetPositionCartesian framePosition.ArrayData(0), _
framePosition.ArrayData(1), _
framePosition.ArrayData(2)
'refresh viewport
view.GraphicsRedraw (viewportRect)
Next
'end plugin animation loop
maxwell.Rendering.EndAnimation
'reset camera exactly where it was before
cam.SetPositionCartesian originalPosition.ArrayData(0), _
originalPosition.ArrayData(1), _
originalPosition.ArrayData(2)
'refresh viewport
view.GraphicsRedraw (viewportRect)
'restore Cache MXS Meshes option to previous value
maxwell.PlugInOptions.CacheMXSMeshes = meshesWereCached
'done
Exit Sub
'something went wrong
handle_error: MsgBox "An unexpected error occurred (" & Err.Description & ")"
End Sub
Sadly, this lack of a response demonstrates a mori[…]