Site icon Embarcadero RAD Studio, Delphi, & C++Builder Blogs

How To Automate The Mathcad Prime Software With Delphi Using COM

How To Automate The Mathcad Prime Software With Delphi
How To Automate The Mathcad Prime Software With Delphi the Softacom logo

Among the most notable features of Delphi, it’s worth mentioning the ability to enhance your Windows app development by interacting with other installed software products on your computer, using the COM technology for functionality embedding or automating.

In this article, we will show you how to automate the Mathcad Prime software in your Delphi VCL apps, by controlling the automation server and the fundamental methods and properties of its API, and how to implement an event sink in your app to handle events triggered by the Mathcad Prime software.

Mathcad Prime is an engineering calculation software developed by PTC. It is used by engineers to solve, document, and share calculations for product design.

It provides an Automation API as a type library (TLB), that you can import in Delphi IDE and use in your apps to launch the software, send data to be treated and analyzed in the opened worksheet, and get the result back to be shown in your app.

This can be done using Microsoft’s COM technology, which is a language-independent software component model that enables interaction between software components (the COM server) and developed applications (the COM client) running on the  Windows platform. The COM client can be either on the same computer as the server or on another computer on the network (connected through the Distributed COM (DCOM) mechanism).

The key aspect of COM is that it enables the communication between clients and servers through interfaces clearly defined by the server developer. These interfaces provide a way for clients to ask a COM component which features it supports at run time. And to develop a COM client, you must understand the interface (properties and methods) exposed by the server to clients.

In order to access this interface, its type information is typically published as a type library that can be imported and used in Delphi VCL projects.

To get specific information about the published interfaces of PTC Mathcad Prime, check the developer documentation here.

Importing Mathcad Prime API to Delphi

For communication with Mathcad Prime, you have to access its API through the “Ptc.MathcadPrime.Automation.dll” installed at the software top level of the installation directory.

Now, Delphi will generate the unit “Ptc_MathcadPrime_Automation_TLB” that will be specific to the installed version of Mathcad Prime. You can save it for later use.

COM Client implementation

To implement your COM Client app, start a new (or open an existing) VCL project.

In the uses clauses, add your generated TLB unit and the following unit:

[crayon-676de745d5e0d050872568/]

As you can see in the generated unit, the Mathcad Prime automation server has a dual interface, which will make the implementation tasks much easier (You can find the basics of using COM interfaces in Delphi documentation).

We need the following global variables to be used in the project:

Variable forPurposes and possible tasksVariable type and its constructor and destructor
Mathcad Prime main applicationStarting, hiding and closing the application.

 

Creating a new (or opening an existing) worksheet.

Closing the active worksheet.

IMathcadPrimeApplication3

 

Constructor : “CoApplication.Create”

Destructor : assign a nil value

Mathcad Prime active worksheetSaving the worksheet.

 

Exchanging data between the server and client.

IMathcadPrimeWorksheet3

 

Constructor : “IMathcadPrimeApplication3.Open() as IMathcadPrimeWorksheet3”

Destructor : “IMathcadPrimeApplication3.Close()”

Mathcad Prime eventsHandling events raised by the user’s actions on the server app.IMathcadPrimeEvents2

 

Constructor : (TMathcadPrimeEvents.Create)

Destructor : assign a nil value

So, we will declare variables as follows:

[crayon-676de745d5e13867172235/]

These variables are used in the code snippets as mentioned above.

Basic tasks

Now we will illustrate how you can use the Mathcad Prime API through some basic tasks. And :

Start the application

For starting the app, implement the “try … except … end;” instruction to create an instance of the “IMathcadPrimeApplication3” interface (through the constructor of the “CoApplication” class) and assign it to the Application variable, and set the Application “Visibility” property to “True”.

Or, in the case of an exception, show a failure message with the exception message.

[crayon-676de745d5e15878414044/]

Close the application

For closing the app, implement a condition to check if the Application variable is assigned (the variable does not have a null value), then release it by calling the Application “Quit()” method with the parameter “SaveOption_spSaveChanges”. Or show a failure message if an exception is detected.

[crayon-676de745d5e17287511108/]

Show the application

To activate the app and bring it to the front, implement a condition to check if the Application variable is assigned, then set the Application “Visibility” property to “True” and call the “Activate()” method. Or show a failure message for the exception.

[crayon-676de745d5e18373113119/]

Hide the application

To hide the app, implement a condition to check if the Application variable is assigned, then set the Application “Visibility” property to “False”. Or show a failure message in the case of an exception.

[crayon-676de745d5e19614410035/]

Get the active worksheet in the application

To get an active worksheet in the app (to exchange data between the worksheet and the client app), implement a condition to check if the Application variable is assigned, then assign the Application “ActiveWorksheet” property value to the Worksheet variable.

(Use type casting, because the Application “ActiveWorksheet” type is not “IMathcadPrimeWorksheet3”).

[crayon-676de745d5e1a871097193/]

Open an existing worksheet in the application

[crayon-676de745d5e1b034863893/]

Create a new worksheet in the application

To create a new worksheet in the app, implement a condition to check if the Application variable is assigned, then call the Application “Open” function with an empty string as a parameter and assign the resulting value to the Worksheet variable (Use type casting).

[crayon-676de745d5e1c266803768/]

Save modifications in the opened worksheet in the application

To save modifications in the active worksheet (existing as a local file) in the app, implement a condition to check if both Application and Worksheet variables are assigned, then call the Worksheet “Save” method. Or show a failure message in the case of an exception.

[crayon-676de745d5e1d158403079/]

Save the worksheet in the application as Mathcad worksheet (MCDX)

[crayon-676de745d5e1e119374815/]

Save the worksheet in the application as a Mathcad template (MCTX)

It will be the same code as the previous one, but you have to change the filter and the default extension to “MCTX”.

[crayon-676de745d5e1f073734626/]

Close the active worksheet in the application

To close the active worksheet in the app, implement a condition to check if both Application and Worksheet variables are assigned, then:

Now, to destroy the Worksheet object, add a condition to check if the Worksheet variable is assigned, then set its value to null.

[crayon-676de745d5e20813074360/]

The event sink

Your client app can also respond to the events raised by the COM server. And to do this, you can simply use the events added to the generated component in the Object inspector. But if you are not using a Component wrapper or if the server uses COM+ events, you have to implement an event sink.

As it may sound difficult, in this section, we will show you how to properly implement an event sink for the Mathcad Prime COM server.

Interface descending class

Mathcad Prime events are declared in the interface “IMathcadPrimeEvents2 “, you can find it in the “Ptc_MathcadPrime_Automation_TLB” unit:

[crayon-676de745d5e21274668802/]

Now, either on a new separate unit or in your existing project’s unit, declare the class “TMathcadPrimeEvents” with descendance from both “TInterfacedObject” and “IMathcadPrimeEvents2”, like the following:

[crayon-676de745d5e22909503098/]

Now, the followimg positions should be added to this class :

[crayon-676de745d5e23425396599/]

Methods implementation

Implement the event’s class methods as the following way:

All these functions have to return the “S_OK” value when their executions have successed. They are used by the COM server to get interfaces and call the appropriate event’s handler.

For the “IUnknown” interface methods “QueryInterface”, “_AddRef” and “_Release”, their implementations are inherited from the ascending class “TInterfacedObject”, so there is no need to re-implement them.

For now, your “TMathcadPrimeEvents” class implementation will look like the code below:

[crayon-676de745d5e24951113672/]

Save the event sink unit to be used later.

Connecting the event sink

[crayon-676de745d5e25624117966/]

Go to the method for starting Mathcad Prime app, and :

[crayon-676de745d5e27505357479/]

In the FormClose event handler, by using the “try … except … end” instruction :

[crayon-676de745d5e28261858957/]

Enable/Disable the events

It would be nice if we implemented something like radio buttons to enable/disable the events handling.

Drop a TRadioGroup control, with two items (Enable, and Disable). Then open its “OnClick” event and implement a switch case condition:

[crayon-676de745d5e29304746061/]

Run the project

The automation works in Delphi without the need to deploy the “Ptc.MathcadPrime.Automation.dll” library. So just run your project.

Check the features of your COM Client app:


This article was written by Embarcadero Tech Partner Softacom. Softacom specialize in all sorts of software development focused on Delphi. Read more about their services on the Softacom website.

Exit mobile version