Code example: Create a window showing TopSolid Version
Updated : 13 November 2024
Add button to application form
Tip
You can download this basic example on Github here
Before you start, create a new .NET application project. The project automatically includes a form, which you can modify.
Now, add TopSolid Automation references to this new project.
Important
Configuring TopSolid
When TopSolid is launched, it will automatically create an entry point for the use of automation. It is not necessary to configure TopSolid to use the automation locally. However, if TopSolid is to be used on a remote computer, a remote connection must be configured. This method will be shown later in this course.
Note
When multiple instances of TopSolid are open, Automation will default to interacting with the first instance that was opened. However, it is possible to specify which instance the program should use for Automation by explicitly defining the target instance in your code.
- 1 - Open the file Program.cs.
To connect the program to TopSolid, the following lines must be added to the "Main" method of the Program.cs class:
static void Main()
{
//connect automating
TopSolidHost.Connect();
TopSolidDesignHost.Connect();
TopSolidDraftingHost.Connect();
*
*
*
TopSolidHost.Disconnect();
TopSolidDesignHost.Disconnect();
TopSolidDraftingHost.Disconnect();
}
Once the program has been launched, these lines are used to create connections with TopSolid, and also to close them at the end.
Note
Access to this article if you want to know more about remote connection to TopSolid
- 2 - Open the design of the form Form1.cs.
You can do it by double clicking on it, or with right-click on the file Form1.cs from project treeview > "View Designer"
3 - Acces to the menu View > Toolbox to display the toolbox.
4 - From the Toolbox tab, access to the "All Windows Forms" group, then double-click on "Button" inside the list. This action will create a button on the form.
5 - Double-click the button to open the code for its Click event.
6 - Copy these lines at the top of this Form1.cs file to add references:
using TopSolid.Kernel.Automating; using TopSolid.Cad.Design.Automating; using TopSolid.Cad.Drafting.Automating;
7 - Finally, add this code to the click event:
TopSolidHost.Documents.Open(ref docId); TopSolidHost.Documents.Save(docId); TopSolidHost.Documents.Close(docId,false,false);
Note
The version of TopSolid can be obtained using the TopSolidHost.Application.Version
method, which returns an integer.
Run your application from Visual Studio
1 - You can manually launch TopSolid beforehand. However, if you don’t, TopSolid will automatically launch when your application connects to Automation, opening the most recent version installed on the system.
2 - When Automation is connected, the window of your application will be shown.
3 - Click on the button. A message box appears, showing current TopSolid version in use.