Example 2 - A Sun Animation
This example demonstrates a macro that will generate a 100 frame animation showing the effect of the passage of the Sun between 8:00 and 20:00 hrs.
(Click on the image to view the animation)
The part consists of a simple sphere with a cylindrical cut through the center. Each frame image was rendered at 600x600 pixels and reduced to produce the animation shown here. Average time taken per frame was 5 minutes to reach Sampling Level 11, total time approx 8.5 hours for 101 frames.
Essentially, the process is to generate a frame, reset the time of day and then generate the next frame.
Each frame is generated in its own mxs file which will be later processed by mxcl to create each rendered image in the animation.
A DOS batch file, batchRender.bat is generated during the process which can be run separately to create the actual images.
The images are post-processed by a separate application into the final animation.
The program is written as 3 subroutines:
- main
- GenerateMXSFrame
- AddCommandToBatchRenderFile
main
Sub main()
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swObject As Object
Dim nltAddin As swMaxwell2006.CswMaxwell2006
Dim strCLSID As String
Dim NumberOfFrames As Integer
Dim index As Long
Set swApp = CreateObject(“SldWorks.Application”)
Set swModel = swApp.ActiveDoc
Set swAssy = swModel
Dim dirName As String
Dim batchFileName As String
dirName = “c:\maxwell\Caustics\frames\”
Dim fsBatch, aFile
Set fsBatch = CreateObject(“Scripting.FileSystemObject”)
batchFileName = dirName + “batchRender.bat”
Set aFile = fsBatch.CreateTextFile(batchFileName, True)
strCLSID = “swMaxwell2006.swMaxwell2006”
Set swObject = swApp.GetAddInObject(strCLSID)
If (Not (swObject Is Nothing)) Then
Set nltAddin = swObject
If (nltAddin Is Nothing) Then
Exit Sub
End If
End If
NumberOfFrames = 100
Dim res As Long
Dim Day As Long
Dim DayTime As Double
Dim Sm As Double
Dim Longitude As Double
Dim Latitude As Double
Dim Turbidity As Double
Dim Ozone As Double
Dim Water As Double
Day = 199
Longitude = -3.683
Latitude = 40.4
Turbidity = 3#
Ozone = 0.35
Water = 2#
Sm = 1# ‘GMT offset?
Dim startTime, endTime, incrTime As Double
startTime = 8#
endTime = 20#
incrTime = (endTime - startTime) / NumberOfFrames
DayTime = startTime
For index = 0 To NumberOfFrames
res = nltAddin.setPhysicalSky(True, Day, DayTime, Sm, Longitude, Latitude, Turbidity, Ozone, Water)
Call GenerateMXSFrame(nltAddin, dirName, index, aFile)
DayTime = DayTime + incrTime
Next index
aFile.Close
End Sub
This is the Main routine - where most of the work takes place.
Hopefully, the code is self-explanatory.
It is worth noting:
- The default time and location is day 199 in Madrid
- Time sequence runs from 8:00am to 8:00pm
- Most parameters are specified as variables
- The API function setPhysicalSky is used to modify the time of day
- The total number of frames required can be easily modified if more frames are needed in the animation
The macro assumes that
- The Maxwell Addin has been loaded into SolidWorks
To generate the animation:
- Open a model in SolidWorks
- Load the Maxwell Addin
- Check the Directory locations used by the macro exist
- Run the macro
- Run the generated batchRender.bat DOS batch file
- Combine the generated images into the required animation
GenerateMXSFrame
Sub GenerateMXSFrame(
nltAddin As swMaxwell2006.CswMaxwell2006,
dirName As String,
index As Long,
outputFile
)
Dim MyName As String
Dim myMXS As String
Dim myIMG As String
Dim res As Long
MyName = CStr(index)
myMXS = dirName + MyName + “.mxs”
myIMG = dirName + MyName + “.jpg”
res = nltAddin.ExportToMxs(myMXS, myIMG, False)
Call AddCommandToBatchRenderFile(dirName + MyName, outputFile)
End Sub
GenerateMXSFrame is called to generate an mxs frame in the animation sequence where
- nltAddin is the Maxwell Addin object
- dirName is the pathname of the directory where the mxs files are created
- index is the frame number in the sequence
- outputFile is the name of the DOS batch file
The name of the frame mxs file is constructed from the given index number in the form 0.mxs, 1.mxs etc. in the specified directory dirname.
Then the API function ExportToMxs is called to create the mxs file
Data for this frame is used to create a relevant line in the specified DOS batch file outputFile by calling AddCommandToBatchRenderFile
AddCommandToBatchRenderFile
mxsfile = name + “.mxs”
imgfile = name + “.jpg”
CommandLine = “mxcl -mxs:” + mxsfile
CommandLine = CommandLine & “ -output:” & imgfile
CommandLine = CommandLine & “ -sampling:11”
CommandLine = CommandLine & “ -threads:4”
CommandLine = CommandLine & “ -nowait”
outputFile.WriteLine (CommandLine)
End Sub
AddCommandToBatchRenderFile is called to write a line to the DOS batch file which will be used to generate the rendered image for each frame, where
- name is the name of the frame file (without the suffix)
- outputFile is the name of the DOS batch file
This routine is used to add details of the frame to the batch file.
A DOS command line is constructed to execute mxcl for the frame and added as a new line in the batch file.
Command line parameters for mxcl are added in this routine. These could be modified depending on the model being used for the animation sequence. For the example I used, a sampling level of 11 was a good trade off between quality (noise level) and the time taken to reach that level.
Other parameters worth noting are:
- -threads:4 (for a dual core dual cpu machine)
- -nowait (don't wait for user interaction after completing each render)