ActiveX Scripting with Mathcad 8 Professional

To solve today’s complex technical problems, scientists and engineers often need to use a multitude of tools. A typical project may require combining Mathcad mathematical models with tabular data and calculations from a spreadsheet, with a CAD design, or with a specialized engineering calculation tool.

To accommodate such scenarios, which are becoming increasingly commonplace in the world of today’s engineering and science, Mathcad 8 was designed with particular emphasis on interoperability. Mathcad 8 Professional allows you to use the best tool for the task — be it a spreadsheet (such as Excel or Lotus 1-2-3), a specialized mathematical package (such as MATLAB or S-PLUS), or a CAD tool (such as AutoCAD or Imagineer). With Mathcad’s Component technology, any desktop application that supports OLE Automation can be incorporated directly into the computational flow of a Mathcad worksheet.

ActiveX Scripting is the “glue” that allows any OLE Automation-supporting object to participate in Mathcad computations. Mathcad 8 Professional ships with a Scriptable Object component that uses the Microsoft ActiveX Scripting specification to expose scripting languages which can be used to “drive” the automation object (e.g., a spreadsheet workbook or a CAD drawing). VBScript (Visual Basic Scripting Edition) and JScript (Microsoft’s implementation of JavaScript), both of which are part of the Microsoft Internet Explorer (which ships with Mathcad 8) are, perhaps, the two best-known ActiveX Scripting engines; ActiveX Scripting implementations of other popular languages, such as Perl, Python, and Scheme, are also becoming available.

Let us consider an example in which an Imagineer drawing embedded in a Mathcad worksheet and scripted using VBScript is used to solve a mechanical problem. Suppose that we have an Imagineer design of a two-pulley belt system. From the specifications of the driving motor, we know the torque and the coefficient of friction between the pulleys and the belt. The problem is to calculate the minimum force required to prevent the belt from slipping.

To insert the Imagineer drawing of the two-pulley system into Mathcad, we choose Components… from Mathcad’s Insert menu. This brings up the Component Wizard. We select Scriptable Object from the Wizard, and choose Imagineer Document from the list of installed OLE Objects in the next Wizard screen. The following Wizard screen allows us to choose the ActiveX scripting engine to use, and for this project we select VBScript Language. We name our object ImagineerDoc in the next Wizard screen, and request that it be allocated three inputs and three outputs. Once we finish the Wizard, an Imagineer object with the requested number of inputs and outputs is inserted into Mathcad. We can now double-click on it to in-place activate Imagineer and implement our two-pulley design in Imagineer (or bring in an existing design from another Imagineer session). The following screenshot shows how the Mathcad document might look once we are finished.

Note that the radii of the two pulleys and the distance between them are defined as the inputs to the Imagineer Scripted Object component (Mathcad variables RadiusA, RadiusB, and Distance, respectively). We can now use VBScript to “connect” these variables to the dimensions of the corresponding design elements of the Imagineer drawing. Dimensions of other design elements (the belt wrap on the pulleys WrapB, the length of the belt BLength, and the angle of the belt between the pulleys Beta1) are calculated automatically by Imagineer using constraints established during the design process. These results are assigned to the corresponding Mathcad variables (also using VBScript) for use in subsequent calculations to determine the minimal force to keep the belt from slipping.

To associate the VB script with this design, click on the right mouse button over the Imagineer object and choose Edit Script… from the context menu. The following simple script connects the component’s inputs and outputs to the drawing via Imagineer’s Variable Table (the Variables object).

Sub CADobjEvent_Exec(Inputs,Outputs)

   REM Assign input Mathcad variables to driving dimensions in Imagineer’s Variable Table
   Variables.Edit "RadiusA", Inputs(0).Value
   Variables.Edit "RadiusB", Inputs(1).Value
   Variables.Edit "Distance1", Inputs(2).Value

   REM Extract driven dimensions of interest from Imagineer’s Variable Table
   REM and pass the results back to Mathcad for further processing
   Set Outputs(0).Value = Variables.Translate("Wrap") 
   Set Outputs(1).Value = Variables.Translate("L1") 
   Set Outputs(2).Value = Variables.Translate("Beta")

End Sub

While this script may seem somewhat cryptic at first, it is actually quite simple. In general, in order to feel comfortable using the Scriptable Object component to integrate an OLE Automation-supporting application into the computational flow of Mathcad, you must:
•	be proficient in the scripting language that you choose (VBScript in this case; for more information of VBScript and JScript, the two most popular ActiveX Scripting engines, visit http://www.microsoft.com/vbscript/ and  http://www.microsoft.com/jscript/, respectively),
•	understand the object model of Mathcad’s Scriptable Object component (see the document entitled Scripting OLE Automation Objects in Mathcad and MathConnex; it can be found on MathSoft’s Web site at http://www.mathsoft.com/mathcad/library/mathconnex/scripted.htm,
•	understand the object model of the application that you are scripting (Imagineer in this case; usually, the object model can be found in the documentation that comes with the application – either printed or on-line).

It is easy to see how a CAD application (such as AutoCAD or Imagineer) can be used in conjunction with Mathcad to solve sophisticated mechanical systems, to verify engineering designs against specifications, or to automatically generate designs that conform to specifications. Other types of applications, such as spreadsheets, special-purpose computational environment, or data acquisition widgets, can be integrated into the computational flow of a Mathcad worksheet in a similar manner. Some additional ActiveX scripting examples can be found in the Samples folder of the Mathcad 8 root directory.

(4/9/98)