This is a step-by-step tutorial of how to use Visual Studio to create your own dynamic link library (DLL) written in C# to add APPS and/or Parametric shapes to IGEMS.
In this tutorial we will create a DLL that creates one simple APP and one simple Parametric shape.
The tutorial covers these tasks.
- Create a DLL in visual studio
- Add required references
- How to attach IGEMS.EXE for debugging
- Required function to register APPS and Parametric shapes
- One simple APP
- One simple Parametric shape
Distribution
To distribution of the DLL you just send it to the user and they can save it on their computer and load in in IGEMS. NOTE! When sending a DLL with for example email Windows will place a block on the file and it can not be loaded by IGEMS until you UNBLOCK the file. So after copying the file you should right click on it and select properties. In the file properties window there will be a security message that the file is blocked. Just check the UNBLOCK checkbox to unblock it. After you have done this the DLL will be possible to load.
Prerequisites
- A copy of Visual Studio
- An installation of the latest IGEMS version
- A basic knowledge of the Visual Studio IDE
- A basic knowledge of C#
Create the DLL project
1. Start Visual Studio and select Create New Project.
2. In the search bar type in Class Library and select Class Library (.NET Framework) and press Next. Note! It's important to select the correct project type.
3. Name the project IGEMSTutorial and make sure Framework is set to .NET Framework 4.8
4. Press Create to create the project
Add references
Next step is to add the required references.
1. Right-Click on References and select Add reference...
2. Start by adding System.Drawing
3. Press the Browse button and browse to the IGEMS installation bin folder. Here you select Genamo.dll and IGEMSAPI.dll and press Add
4. Now press OK and it should look like this.
5. Now select Genamo and IGEMSAPI in the References list and change the Copy local property to False. This is important, if you don't do this you will get a lot of unnecessary DLLs in your build folder.
Attaching IGEMS.EXE for easy debugging
To be able to debug your project you need to attach IGEMS.EXE to the DLL. If you do this you can press F5 to build and run your DLL and automatically start IGEMS.
1. Right click on the IGEMSTutorial project in the Solution Explorer and select Properties.
2. Select the Debug tab and check the Start external program radio button. Now press Browse... and select the IGEMS.EXE file.
This concludes the project creation steps. You can save all files and press F5 to check if everything compiles and runs. Note! at this point we have not added any APPS or Parametric shapes this we will do in the next section.
Required functions to register APPS and Parametric shapes
Start by adding
using IGEMSAPI;
using IGS.Genamo;
to the Class1.cs file
To register APPS in the DLL that can be used by IGEMS you need to create this method in your class.
public static void DLLMain()
In this method you will call API.RegisterCommand to register an APP.
To register Parametric shapes that can be used in IGEMS you need to create this method in your class.
public static void DLLParamParts()
In this method you will call PAR.RegisterParametricPart to register a parametric shape.
The IGEMSAPI.DLL has two main classes API and PAR that you will use when writing APPS and Parametric shapes. The API class is used for APPS and the PAR class is used for parametric shapes.
Create a simple APP
This example will create a simple APP.
Add public static void DLLMain() to your Class1.cs
using IGEMSAPI;
using IGS.Genamo;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace IGEMSTutorial
{
public class Class1
{
public static void DLLMain()
{
}
}
}
In the DLLMain method we now register the APP like this.
API.RegisterCommand("TUTORIAL_1", "SimpleApp", null, SimpleApp);
The API.RegisterCommand takes the following arguments.
- Unique name, this is a unique identifier of the APP
- Name of the APP displayed in IGEMS
- Button image or null
- Callback function
Now we add the API.RegisterCommand and implement the SimpleApp callback method. The whole file should look like this.
using IGEMSAPI;
using IGS.Genamo;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace IGEMSTutorial
{
public class Class1
{
public static void DLLMain()
{
API.RegisterCommand("TUTORIAL_1", "SimpleApp", null, SimpleApp);
}
private static void SimpleApp()
{
API.DrawRectangle(0, 0, 100, 100, 1);
MessageBox.Show("Hello World!");
}
}
}
When typing in the MessageBox row you will need to add
using System.Windows.Forms;
This APP will draw a red rectangle in the current drawing and popup a "Hello World!" message.
Now press F5 to build and run the DLL. When IGEMS starts you go to the APPS tab.
Press the Settings button to open the APP settings dialog.
Press the + button and browse to select you DLL file.
Press Open and the DLL will be added to the list of APPS.
Now press OK in the APP Settings dialog and the APP will be added to the APPS toolbar.
To test we press the SimpleApp button.
Create a simple Parametric Part
This example will create a simple Parametric Part.
We write this example in the same project as the APP example. So, create a new class Class2 in the same file.
Add public static void DLLParamParts() to the Class2 class. (You don't need to add a new class but we do that in this example)
using IGEMSAPI;
using IGS.Genamo;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace IGEMSTutorial
{
public class Class1
{
public static void DLLMain()
{
API.RegisterCommand("TUTORIAL_1", "SimpleApp", null, SimpleApp);
}
private static void SimpleApp()
{
API.DrawRectangle(0, 0, 100, 100, 1);
MessageBox.Show("Hello World!");
}
}
public class Class2
{
public static void DLLParamParts()
{
}
}
}
In the DLLParamParts method we add the PAR.RegisterParametricPart call.
PAR.RegisterParametricPart("TUTORIAL", "SimplePart", null, SimplePart,
PARVal.ParDist("X", 200),
PARVal.ParDist("Y", 100));
- Group
- Name
- Image
- Callback
- params...Parameters to the Parametric part
We set the Group name to "TUTORIAL" and the name of the Parametric Part to "SimplePart".
We set the Image to null for now. (If you like to add an Image the preferred size is 400x400.)
We set the Callback function to SimplePart and add two parameters. "X" and "Y", both are Distance parameters. The PARVal class has other parameter types as well if you need.
Ok, now let's implement the SimplePart method used to generate the Parametric Part.
Tip! You can set the cursor on the SimplePart text and press Ctrl+. and select Generate method.
Complete code
using IGEMSAPI;
using IGS.Genamo;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace IGEMSTutorial
{
public class Class1
{
public static void DLLMain()
{
API.RegisterCommand("TUTORIAL_1", "SimpleApp", null, SimpleApp);
}
private static void SimpleApp()
{
API.DrawRectangle(0, 0, 100, 100, 1);
MessageBox.Show("Hello World!");
}
}
public class Class2
{
public static void DLLParamParts()
{
PAR.RegisterParametricPart("TUTORIAL", "SimplePart", null, SimplePart,
PARVal.ParDist("X", 200),
PARVal.ParDist("Y", 100));
}
private static List<object> SimplePart(Dictionary<string, double> dictionary)
{
List<object> list = new List<object>();
double x = dictionary["X"];
double y = dictionary["Y"];
list.Add(PAR.Rectangle(0, 0, x, y));
return list;
}
}
}
The callback method that generates the parametric part should return a list of object and takes a dictionary of parameters as input.
This simple parametric part creates a rectangle from 0,0 to x,y.
The x and y parameters we get from the dictionary. Note! The dictionary is case sensitive so make sure you use the same string as when you registered the part.
Now press F5 to build and start IGEMS.
In the CAD tab you press the Parametric button to open the Parametric shapes dialog.
Here you press the DLL Settings button to add your DLL.
Press the + button and select the DLL file.
Press OK and your Parametric shape will be added to the parametric shapes under the group TUTORIAL.
Double click on the SimplePart and fill in the X and Y parameters and press Single to add the parametric shape to the drawing.