The SolidWorks API (Application Programming Interface) is an interface that allows you to develop custom applications for the SolidWorks system. The API contains many functions that can be called from Microsoft Visual Basic, Microsoft Visual C ++, Microsoft Visual Studio, or from SolidWorks macro files. These features provide the programmer with direct access to SolidWorks functionality.
Using API applications, you can solve many different tasks, such as: integrating SolidWorks with other software packages, developing specialized modules that add additional functionality to the basic SolidWorks capabilities, and various other tasks. API - applications allow you to get a lot of configurations of one part or assembly, thereby winning a huge amount of time when making design decisions.
API development can be done at the macro creation level in SolidWorks, or at the level of an individual application written in C # or VisualBasic. All dynamic libraries required for working 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, it is necessary to display the Macro toolbar in the SolidWorks software package.
Creating a new macro and its implementation
The steps to create a macro in SolidWorks.
1. Before you start recording a macro, you need to create a new part (assembly) file.
2. To start recording a macro in the Macro panel, click on Record / Pause Macro, after which each construction or change of the property will be recorded in the macro file.
3. Next, we carry out all the constructions that should be displayed in the program code of the macro.
4. After that, stop the recording by clicking "Stop macro recording". A window will appear in which you need to specify the physical path of the macro file on the hard disk and specify the format in which it will be recorded.
')
Formats in which a macro 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 program code in Visual Basic. VSTA - application functionality extension tools based on .NET.
• 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” button in the “Macro” panel, after which the “Open” window will appear.
Macro editing
In order to edit the created macro, you must click on the "Edit Macro" panel in the "Macro" panel, then in the appeared window 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 automatically starts after selecting a macro for editing. The VSTA window displays the generated code of the edited macro.
With this tool, you can make changes to the generated code and re-save the macro. It should be noted that in the Project Explorer window there are already links to all the necessary dynamic libraries.
Creating an application in Visual Studio
Reference to libraries
API - application can be developed directly in Visual Studio, using the program code generated when recording a macro.
In order to connect dynamic libraries for working with the API interface, in the Solution Explorer window, right-click on the References tab and select the “Add link” item 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 in the application itself in the classes where it is needed (that is, in those that will use the program code associated with working in SolidWorks) connect the appropriate namespaces.
Create part file
To stop recording a macro and save it, click Stop Macro Recording and specify the save path. The result is a generated C # code that we can use in our Windows Form application. However, you need to add some code to the project to create a new part file. The code snippet looks like this:
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, changing the formal parameters of which may affect the geometry of this model model
• The InsertSketch method of the SketchManager class is used to add a sketch in the active plane. As a formal parameter takes the value of type bool. Before creating a new sketch, you should 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 the starting point 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 swDoc class EditDimensionProperties method is used to edit the properties of the applied size. Accepts many formal parameters, each of which is responsible for a certain state of a size property.
• The AddDimension method of the swDoc class serves to create a new size and takes the coordinates of the size boundary points as formal parameters. For proper use, you must explicitly cast to the DisplayDimension type.