Back to Home

Developing an API application for SolidWorks 2012 software package. Part 1

SolidWorks · api · c #

Developing an API application for SolidWorks 2012 software package. Part 1

SolidWorks API (Application Programming Interface) is an interface that allows you to develop custom applications for the SolidWorks system. API - the interface contains many functions that can be called from Microsoft Visual Basic, Microsoft Visual C ++, Microsoft Visual Studio, or from SolidWorks macro files. These features give the programmer direct access to SolidWorks functionality.

Using API - applications, you can solve many different tasks, for example, such as: integrating SolidWorks with other software packages, developing specialized modules that add additional functionality and various other tasks to the basic capabilities of SolidWorks. API applications allow you to get many configurations of one part or assembly, thereby gaining a huge amount of time when making design decisions.

Development of an API application can be carried out at the level of creating a macro in SolidWorks, or at the level of an individual application written in C # or VisualBasic. All the dynamic libraries required to work with API applications are automatically installed with SolidWorks. As a rule, if you need to develop a full-fledged application, for geometric constructions it is more convenient to use the program code recorded in the SolidWorks macro. To start working with macros, you need to display the Macro toolbar in the SolidWorks software package.

Creating a new macro and executing it


The steps to create a macro in SolidWorks.

1. Before you start recording a macro, you must create a new part (assembly) file.
2. To start recording a macro in the “Macro” panel, click “Record \ Pause Macro”, after which each construction or change of a property will be written to a macro file.
3. Next, we carry out all the constructions that should be displayed in the macro program code.
4. After that, stop recording by clicking on “Stop Macro Recording”. A window will appear in which you must specify the physical path of the macro file on the hard disk and indicate the format in which it will be written.

Formats in which macros can be saved:

• VBA - a simplified implementation of the Visual Basic programming language, built into the Microsoft Office product line.
• VSTA VB - the macro will be saved in the program code in Visual Basic. VSTA is a .NET-based application extension toolkit.
• VSTA C # - the macro will be saved in the program code in C #.
In order to execute the recorded macro, click on the "Run Macro" panel in the "Macro" panel, and then the "Open" window will appear.

Macro editing


In order to edit the created macro, click on the “Edit Macro” panel in the “Macro” panel, and then in the window that appears, specify the location of the macro file on the hard disk and its format. To edit the macro (VB, C #), use the Visual Studio for Applications (VSTA) toolkit. VSTA starts automatically after selecting a macro for editing. The VSTA window displays the generated code for the macro being edited.

Using this tool, you can make changes to the generated code and re-save the macro. It is necessary to pay attention to the fact that in the Project Explorer window links to all necessary dynamic libraries are already indicated.

Creating an Application in Visual Studio


Linking libraries

API - the application can be developed directly in Visual Studio, using the program code generated when recording the macro.

In order to connect dynamic libraries for working with the API, in the Solution Explorer window, right-click on the References tab and select "Add Link" in the pop-up menu. After that, the “Add Link” menu will appear, in which we select the COM tab and add the following libraries: SolidWorks2010 Type Library, SolidWorks2010 Constant Type Library, SolidWorks.Interop.swocommands.dll. Next, you need to connect the corresponding namespaces in the classes in the application where it is necessary (i.e., in those in which the program code associated with working in SolidWorks will be used)

Create Part File

To stop recording a macro and save it, click "Stop Macro Recording" and specify the save path. As a result, we get the generated code in C #, which we can use in our Windows Form application. However, some code must be added to the project to create a new part file. The code snippet is as follows:

Document = "C:\\ProgramData\\SolidWorks\\SolidWorks 2010\\templates\\ Сборка.asmdot";
 swModelDoc = (ModelDoc2)swApp.OpenDoc6(document, (int)swDocumentTypes_e.swDocASSEMBLY, (int)swOpenDocOptions_e.swOpenDocOptions_Silent, "", ref errors, ref warnings);


The main methods, the change in the formal parameters of which can affect the geometry of this model model

• The InsertSketch method of the SketchManager class is used to add a sketch in the active plane. It takes a value of type bool as a formal parameter. Before creating a new sketch, you must select the plane on which it will be placed.

boolstatus = swDoc.Extension.SelectByID2("Спереди", "PLANE", 0, 0, 0, false, 0, null, 0);
           swDoc.SketchManager.InsertSketch(true);

• The CreateLine method of the SketchManager class is used to create a new sketch line in the sketch editing mode. It takes 6 formal parameters - the coordinates of the end and start points of the line. Similar to the CreateLine method, there are methods for creating other sketch elements. An example of using the CreateLine method:

skSegment= ((SketchSegment) (swDoc.SketchManager.CreateLine (0.116812, 0.026916, 0.000000, 0.149183, 0.026916, 0.000000)));

• The EditDimensionProperties method of the swDoc class is used to edit the properties of the applied size. It takes many formal parameters, each of which is responsible for a certain state of some size property.
• The AddDimension method of the swDoc class is used to create a new size and accepts the coordinates of the boundary points of the size as formal parameters. Correct use requires explicit casting to the DisplayDimension type.

Read Next