Summary
API Access from C#
To start off, lets look at a simple C# console application which controls Microwave Office. For this example we'll use Visual Studio as the development environment.
Project Setup
-
Inside Visual Studio create a new solution, and from the C# options we select C# Console Application.
-
Next to access the object model for the API we need to add a reference to the API type library.
-
The type library provides information about all the objects exposed by the API to the development environment.
-
In the Solution Explorer window right click on the References tree item and select "Add Reference…"
-
In the Add Reference Dialog select the "COM" tab.
-
In the list box find an entry that starts with "AWR Design Environment <year>" highlight that entry and click 'Select'.
-
The entry will appear in the lower list box labeled "Selected Components", see Figure 6 below.
-
Now Click OK and the reference will be added to the list of references in the solution explorer tree, see Figure 7 below .
-
The Add Reference dialog allows you to set references to a number of components by selecting each adding the reference and then approving all the components at the end. So first locate the 'AWR Design Environment <year>' entry under the Component Name in the upper list box. Select the line and press the Select key. The highlighted component will then be moved to the lower list of selected components.
Once the OK button is pressed the MWOffice reference should appear in the list of references in the solution explorer.
Code Snippets
Now open the main C# code file for the project and locate the Main function in the console class, it should look something like this:
using System; namespace CSharpConsoleMWO { /// <summary> /// Summary for class ConsoleMWO /// </summary> class ConsoleMWO { /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main(string[] args) { } } }
To fill out the Main() method add the following code:
static void Main(string[] args) { MWOffice.Application objMWO = null; objMWO = new MWOffice.Application(); if (objMWO == null) { Console.WriteLine("Error: Cannot Create MWOffice Application object"); return; } objMWO.New(""); MWOffice.Schematic schem; schem = objMWO.Project.Schematics.Add("MySchematic"); MWOffice.Element elem1, elem2; elem1 = schem.Elements.Add("MLIN", 0, 0, 0, false, ""); elem2 = schem.Elements.Add("MLIN", 0, 0, 180, false, ""); Console.WriteLine("Element Count = {0}", schem.Elements.Count); }
First we declare a reference to a MWOffice.Application object with:
MWOffice.Application objMWO = null;
We can do this because the type library reference added above describes all the AWR Design Environment objects to the development environment and the C# compiler. Next we create an instance of the application object.
objMWO = new MWOffice.Application(); if (objMWO == null) { Console.WriteLine("Error: Cannot Create MWOffice Application object"); return; }
If this succeeds then the MWOffice application will be started and we will have a reference to a running instance inside the variable objMWO. Next we can use this reference to load a new untitled project with:
objMWO.New("");
Then to the new project we add a schematic named "MySchematic" with the code:
MWOffice.Schematic schem; schem = objMWO.Project.Schematics.Add("MySchematic");
The assignment to the variable "schem" above gives us a reference to the newly created schematic which we can use to add elements. Finally we add two elements with the code:
MWOffice.Element elem1, elem2; elem1 = schem.Elements.Add("MLIN", 0, 0, 0, false, ""); elem2 = schem.Elements.Add("MLIN", 0, 0, 180, false, "");
These are added with their lowest number node at the coordinate x=0, y=0. The second one is rotated 180 degrees from the first so they will not be on top of each other, but because they are added at the same location they will be connected. Finally to verify that we succeeded we write the element count to the console:
Console.WriteLine("Element Count = {0}", schem.Elements.Count);
Now compile and run the program. The AWR Design Environment should appear, a new schematic should be added to the project and then two connected elements should appear in the middle of the open schematic window. In the console the program should produce the following output:
Element Count = 2