PowerPoint Slide Show with 3D Animation
In this exercise, you will go through the process of creating a PowerPoint slide with an embedded ActiveX component.
The complete source code for this example is given below.
After opening PowerPoint, you will see an error that Active content has been disabled, with a button to Enable Content. Click the Enable Content button to use this tutorial.
You may need to change the macro security level for the controls to work properly. To do this, open up PowerPoint and select Developer -> Macro Security in the menu. Choose the Enable all macros button. PowerPoint is now set to allow the Controls to be accessed.
Setting up the Slide
Open up SimpleExample.pptm which resides in:
<STK install folder>\Data\Resources\stkX\gettingStarted
Now do a SaveAs and change the name to SimpleExampleFINAL.ppt, and use the Save as type PowerPoint Macro-Enabled Presentation (*.pptm).
There are four items that will be added to the slide; three buttons and the AGI Globe Control. Start by adding the buttons.
You can add a new button to the slide by using the Controls section of the Developer tab on the ribbon bar.
If this tab is not present, right-click the ribbon, and select "Customize the Ribbon....". In the Customize dialog that pops up, confirm that the Developer tab is selected in the list of Main Tabs.
In versions of PowerPoint prior to PowerPoint 2007, you can add a new button to the slide by using the Control Toolbox, shown below.
If this toolbar is not found in your PowerPoint toolbar by default, right-click the gray space and select Control Toolbox.
To add a command button, click the rectangular icon, then click and drag a box on the slide. Add two more, for three buttons total. Right-click on a new button that has been added to the slide, and choose to view the Properties of the control. A Properties dialog will now be visible somewhere on your screen (pictured below).
Make all the necessary changes to the command buttons, with the exception of the code itself, inside these property panels. Change the properties of this button, as well as all the other needed buttons, using the settings in the table below. The properties listed below will change the name, caption, position, and size.
(Name) | Caption | Left | Top | Width | Height | Enabled |
---|---|---|---|---|---|---|
Facility | Add Facility | 552 | 204 | 155 | 30 | False |
Satellite | Add Satellite | 552 | 174 | 155 | 30 | False |
Scenario | New Scenario | 552 | 144 | 155 | 30 | True |
The only remaining object that needs to be added to the slide is the 3D window. To add this object, click the More Controls button on the Controls section of the Developer tab, and choose AGI Globe Control from the list of available objects.
Right-click on the new Control and select Properties Sheet. Enter the following:
Name: AxVO
Height: 354
Left: 12
Top: 102
Visible: true
Width: 540
Save the presentation to view the slide show. Although none of the functionality is there, you can view your slide and it should look similar to the image below.
Writing the Code
Right-click any object on the slide and choose the View Code option. This will cause the Microsoft Visual Basic window to appear, containing the code for the slide. This is where you will add functionality to the buttons that you just added to the slide.
Start by creating a SendCommand
function, through which all the commands in the slide will be directed. At the very bottom of the code type in the following:
Function SendCommand(stkCommand As String) On Error Resume Next Set rVal = AxVO.Application.ExecuteCommand(stkCommand) If rVal.Count > 0 Then SendCommand = rVal(0) Else SendCommand = 0 End If End Function
This function will receive a Connect Command in the form of a string, and then send it to the STK Application object associated with the Globe control that sits on the slide. It is important to include the On Error Resume Next
line to ensure that in the event of a NACK
(or Not Acknowledge) statement from STK, the code will continue to run. The If-Then-Else statement is in place to catch and return any data that STK sends back to the code.
Now, edit the code for each of the buttons. From the pull-down menu on top left of the editor, choose AnimFor
, and from the pull-down menu on the top right choose Click
. A subroutine will appear in your code now. Inside this subroutine, add the following line:
SendCommand("Animate * Start Forward")
When you click the AnimFor button, it will now send the above Connect Command to the SendCommand
function and tell STK to start animating the scenario forward.
Now edit the Scenario button's subroutine. Add the following code:
Private Sub Scenario_Click() If Scenario.Caption = "New Scenario" Then SendCommand ("New / Scenario Scenario1") AnimFor.Enabled = True Reset.Enabled = True Facility.Enabled = True Satellite.Enabled = True Scenario.Caption = "Unload Scenario" Else SendCommand ("Unload / *") AnimFor.Enabled = False Reset.Enabled = False Facility.Enabled = False Satellite.Enabled = False Scenario.Caption = "New Scenario" End If End Sub
There is a lot going on in this section of code. When the Scenario button is clicked, it first checks the button's caption. If the caption is set to New Scenario, it sends the command to create the new scenario and then enables all the other buttons on the slide. It then changes the caption on the button to Unload Scenario so that the next time the button is clicked, it performs the Else portion of the code.
If the Scenario button's caption reads Unload Scenario, then it first unloads the scenario (and any objects that have been loaded), then disables all of the other buttons, and then sets the button's caption back to New Scenario. After this is done, the slide will return to its original condition.
Now edit the Facility button. Add the following two lines to the button's subroutine:
SendCommand("New / */Facility Exton") Facility.Enabled = False
When you click this button, it will create a new facility named Exton and then disable the button to make sure that no one can attempt to create this same facility twice in the same scenario.
Finally, edit the Satellite button. Much like the Facility button, this button should create a satellite and then disable the button. We will also add a SetState
command to propagate the satellite.
SendCommand ("New / */Satellite Sat1") SendCommand ("SetState */Satellite/Sat1 Classical J2Perturbation UseScenarioInterval 60 J2000 ""1 Jul 2009 12:00:00.00"" 7163000.137079 0.0 98.5 0.0 139.7299 360.0") Satellite.Enabled = False
Save the code and then close the Microsoft Visual Basic editor. Then back in PowerPoint, save the presentation once again. Finally, view the presentation. This time, all functionality exists behind your buttons, and a working version of STK now exists inside your presentation.
VB Code for PowerPoint Example
Function SendCommand(stkCommand As String) On Error Resume Next Set rVal = AxVO.Application.ExecuteCommand(stkCommand) If rVal.Count > 0 Then SendCommand = rVal(0) Else SendCommand = 0 End If End Function Private Sub AnimFor_Click() SendCommand ("Animate * Start Forward") End Sub Private Sub Reset_Click() SendCommand ("Animate * Reset") End Sub Private Sub Scenario_Click() If Scenario.Caption = "New Scenario" Then SendCommand ("New / Scenario Scenario1") AnimFor.Enabled = True Reset.Enabled = True Facility.Enabled = True Satellite.Enabled = True Scenario.Caption = "Unload Scenario" Else SendCommand ("Unload / *") AnimFor.Enabled = False Reset.Enabled = False Facility.Enabled = False Satellite.Enabled = False Scenario.Caption = "New Scenario" End If End Sub Private Sub Facility_Click() SendCommand ("New / */Facility Exton") Facility.Enabled = False End Sub Private Sub Satellite_Click() SendCommand ("New / */Satellite Sat1") SendCommand ("SetState */Satellite/Sat1 Classical J2Perturbation UseScenarioInterval 60 J2000 ""1 Jul 2009 12:00:00.00"" 7163000.137079 0.0 98.5 0.0 139.7299 360.0") Satellite.Enabled = False End Sub