Ioana Cernat is a PTC Mathcad Application Engineer (AE), living in Romania. She works closely with the quality and AE teams inside PTC. When she’s not working on her PhD in Risk Modelling, she enjoys reading, travelling, flying, extreme sports, and playing the piano.
This article provides an overview of the PTC Mathcad Prime application programming interface (API) and its software development kit (SDK).
To find the SDK,
- From PTC Mathcad Prime, open online help (F1). Alternatively, you can reach online help from a web browser by clicking here.
- Click Integrating with other products > API Guide.
What can you do with the API?
The API provides a mechanism for communicating with Mathcad from another Windows application, including custom standalone applications. The API consists of a group of automation classes that you can use to do the following (just to mention a few):
- Open Mathcad
- Calculate the results
- Assign and retrieve variables, matrices, and strings
- Control the appearance of windows
- Retrieve information about regions
- Save, print, or close the Mathcad worksheet
- Close Mathcad entirely
If you want, you can even send data or assign variable values to PTC Mathcad Prime from another application (such as Google Graphs) or send results back to the original application.
Hence, using the automation commands, you can execute numerous tasks, ranging from easier to more complex.
Intro to the SDK
The SDK contains a user guide that includes documentation about the automation commands and API examples that can help you better understand how to use and create your custom applications. Depending on your programming skills, you can use one of the following programs: C++, C#, VB, VBScript or JavaScript.
The image below shows examples that can be found in the API Guide (see image below) as well within Mathcad’s installation pack.
Location of code examples within the mathcad SDK.
Next, let’s look closer one of the examples.
Example: GraphMat
This example demonstrates in C# how to launch PTC Mathcad Prime, load a worksheet, push scalar values to the worksheet, compute the worksheet, and pull back a matrix result. It uses .NET APIs, PTC Mathcad Prime APIs, and Google Charts APIs to create a solution.
Check out the steps below:
- Download the example here: C#_GraphMat.
- To run the example, execute GraphMatrix.exe. The GraphMat dialog opens. All input fields are disabled.
- Click File > Open to open the Force Google Graph.mcdx worksheet. When this worksheet opens, the GraphMat input fields become enabled.
- In the open worksheet, note the four input regions at the top of the second page, and the two output regions at the bottom of the third page.
- In the GraphMat dialog, you can modify any of the four text boxes before clicking Send input values to send the values to the worksheet.
- In the worksheet, the four input regions now reflect the values you set in the GraphMat dialog. Mathcad Prime recalculates the worksheet and sends the values of SForce and outstring to the GraphMat dialog as a chart and a string of numbers displayed below the graph. To meet Google Charts limitations, the Force vector is scaled to generate SForce to ensure that the range of its elements is 0-100.
- For the next calculation, set new diameter or engine values, then click Send input values. The graph and the string of numbers are updated accordingly.
- In PTC Mathcad Prime, click File > Close, and then in GraphMat dialog click File > Exit.
You could override the initial Mathcad values by inserting new values in the existing fields. You will see everything getting updated in an instant in the Mathcad worksheet once you send the information to Mathcad from the interface.
Once you have Mathcad installed, you can access these examples in the installation folder and open the code to inspect it, learn from it, create a new one based on an the already existing ones, and bring your own customized inputs and information. Then, from the moment you compile the code, the system provides you with the appropriate interface.
Example: JavaScript
For an alternative example with a JavaScript demo, watch the video below:
This JavaScript example is also downloadable from the Mathcad support site.