This document describes the use of Embarcadero Delphi (formerly Borland Delphi) with the Microwave Office programming interface (COM API). Delphi is a great for rapid development of applications with a nice graphical user interface.

In this example, Delphi XE2 was used to create a 32bit application for use with the MWO v11 32 bit version.
64bit applications can be created by simply switching the build configuration to 64 bit.

Delphi library to access the MWO programming interface

Before we can access MWO from delphi code, we need to create a Delphi type library with declarations of the MWO programming interface. This is mostly automatic, with only few manual changes needed. The procedure for creating this type library is described at the end of this application note.

Example project

Once you have created the type library, it is very easy to remote control MWO. In Delphi create a new VCL Forms project and then add the unit with the MWO type library.

(click to view full size)

It is assumed that you are familiar with Delphi, so the basics of creating the main window for with a menu isn’t discussed here. On the main form, place a TMemo component where some text output can be shown.

In the main form class, a variable of type IMWOffice is defined. Once initialized, this is the interface to MWO.

Starting and terminating the link to MWO

The FormShow procedure of the main window is used to initialize that variable (default=nil) and then try to connect to MWO.

The minimum call to connect to MWO would be

 MWOffice := CoMWOApplication.Create;

which connects to an existing instance of MWO if the program is already running, or otherwise starts MWO.

In the example code below, we have added some code around it to make things more robust, and display the connection status in the status bar. Before trying to connect, we use the ExistClassID function to check if MWO is registered on this machine. If yes, we call CoMWOApplication.Create and then wait for the Windows process to start up. If it doesn’t start, we run into a timeout. So after this procedure is finished, we have a pointer to the MWO interface if everything worked as expected, or we have MWOffice=nil if something went wrong.

The corresponding cleanup work is done in the FormHide procedure, where we dispose the interface to MWO when our program is terminated.

That’s all we need to do to establish and close the connection to MWO. As a little bonus, we have added a TTimer component that checks every second if the connection to MWO is still alive. Of course, this is optional.

Utility functions processExists and ExistClassID are not part of the Delphi libraries. For your convenience, they are included in the attached example project.

Create schematic, place components, assign parameter values

When the connection to MWO is established, we can access the current project and remote control MWO. The code below adds a new schematic and places elements on that schematic. The first element (MLIN) is placed at location (x=0, y=0) with 0° rotation. After it is placed, new values are assigned to parameters W and L. Note how these parameters can be accessed by passing the variable name as a string in the array brackets.

Then, more elements are placed. The insertion point for these elements is calculated from the location of pin 2 of the previous element. With this query of pin 2 location, elements are cascaded nicely, regardless of the symbol size.

Query schematic and components

The second code snippet shows how to query the schematics in the projects, and the elements in the schematics, and the parameters of these elements.

Nothing fancy here, but this might be the basis for creating your own parts list etc. In this little example program, you can save the list of schematics, elements and parameters to a text file. If you like, you could also add a link to additional programs like Microsoft Office and auto-generate some documentation. Maybe that’s covered in a future application note …

That’s all you need to get started with MWO automation in Delphi. The example project is available for download below, but does not include the MWO type library. Read below how you can create the type library yourself.

Create the Delphi type library

The MWO type libray is not available for download, so you will need to create it yourself as described here. This requires Delphi and MWO installed on the same computer.

In Delphi, click on Component > Import Component …

A dialog will open showing all the different programming interfaces of programs installed on your machine. You need to select the AWR Design Environment. Click Next to continue.

Next, you can select what to import and where to store the output file. Leave the Class Name(s) field and the Palette Page field empty. In the Unit Dir field, enter the location where the unit with the type libary will be stored. In the example below, this is the directory where we will create the example code. The search path field will automatically include that path. Click Next to continue.

Select Create Unit and click Finish to create the unit with the type library.

Before we can use the unit with the type library, we need to make three small modifications to the code.

  • Application is a reserved word in Delphi, rename to MWOApplication
  • Rename CoApplication to CoMWOApplication
  • Remove property Frequencies in section IModel = interface(IDispatch) because it creates a variable type issue.

To rename Application to MWOApplication, load the library unit to the Delphi editor and use the search & replace function with option “Whole words only”.

Same for renaming CoApplication to CoMWOApplication.

Finally, remove the property Frequencies in section IModel = interface(IDispatch) by making it a comment line. This is required because there is an issue with variable types in that line. Removing the property is the easiest fix.

That’s it! You now have type library unit that you can use to access MWO from Delphi.
In the example project, the library unit name is “MWOffice_TLB.pas” expected. Please use that name and copy the file to the example project directory where the other Delphi files reside.

Object names and class names

There are some subtle differences between some MWO object names and the corresponding class names(e.g. LayoutProcessDefinitions object is accessed as Project.ProcessDefinitions) but that is documented. The MWO class library documentation is available from the MWO documentation download location, and Delphi will do auto-completion once you have added the unit with the MWO type library.