Tutorials to .com

Tutorials to .com » Mechine » Autocad » Brief description of the application of SolidWorks API

Brief description of the application of SolidWorks API

Print View , by: iSee ,Total views: 60 ,Word Count: 2259 ,Date: Sun, 16 Aug 2009 Time: 4:03 PM

SolidWorks API is the SolidWorks application development of the OLE interface, users can use it and support the development of OLE programming tools such as VB, VBA (Excel, Access), C, vc + +, etc. to the secondary development of SolidWorks, the establishment of a suitable user need SolidWorks-specific function modules.

First, the problem

The majority of engineering drawings of the title bar should include the design of some basic information, such as the name of spare parts, spare parts, code-named, materials, as well as designers and design date, in which parts of the name, code, material, quantity, weight and after a number of important responding to requests for information should also appear in the schedule of assembly materials (BOM) in the. If these 11 projects carried out in the preparation of plans and changes to the workload is huge, and this approach is not conducive to design information to ensure the consistency of the model file (parts or assembly files) documents and drawings in terms of the management is out of touch. SolidWorks is a whole three-dimensional mechanical design related software, document design model data, such as size, annotation, file attributes, such as content and related engineering drawings, so we deal with the engineering drawings in the title bar and the material breakdown in the design data When these elements should be kept in the model document and automatically display the corresponding chart in the engineering position. When engineering drawings need to modify the content directly modify the model of the corresponding data files can be realized.

This approach not only guarantees the consistency of design information, and can greatly improve design efficiency. According to this vision, the author of such design information is summarized, and model them as custom properties of documents stored in the model specified configuration file attributes (in SolidWorks, select the menu "File → Properties", click "specified configuration" label, as shown in Figure 1), the specified configuration file for the model of these attributes can be automatically brought to the title bar of engineering drawings and material schedules.

The BOM template to modify or create a new template on the schedule of the assembly map in the schedule of the materials needed to display the design information (engineering graphics format definition and the definition of BOM template, see "SolidWorks2001 Chinese version of the essence of practical technology, "a book). Therefore, no matter when it is necessary to modify the design of information, just in the SolidWorks model to open the corresponding document, modify configuration attributes can be specified. However, even if such a treatment method to modify the file in the current configuration properties still seemed to be somewhat cumbersome and can work for the establishment of a SolidWorks interface, you can more easily add and modify the information has become key to the problem.

Second, custom attributes Cpbom

To a greater degree of efficiency, we use VB6.0 established through the SolidWorks API setup program custom attributes - Cpbom, this process of dialogue through an interface directly on the current model of the current configuration file to add or modify, its basic functions as follows:

☆ model for the configuration file to add custom attributes;

☆ settings include: code-named parts, materials, name, post-processing, the designer name, design date;

☆ custom attributes to determine automatically save the file; if not save the current file, prompted save the file path and file name;

☆ document for the assembly, you can choose whether or not to delete the "material" and "post-processing" two attributes. This procedure for the use of vb compiler to generate executable file. Custom Attributes Interface "custom attributes" procedure is through the SolidWorks API documentation on the current model of the current configuration of the custom attribute name and value of "time" and "write" operation, the procedure is very simple, tell us about the program here in SolidWorks API to use some of the objects and methods.

1.SolidWorks object here refers to the SolidWorks API is the object of the highest level. Object can be controlled through the implementation of SolidWorks software, or withdraw from, as well as the establishment of SolidWorks document, open, shut down operations and so on. The use of the CreateObject method in VB to get SolidWorks objects, using the methods of the object can be activedoc current SolidWorks document object, document object for the space-time that did not open any SolidWorks file.

Dim swApp As ObjectDim

activeDocument As ObjectSet swApp = CreateObject ( "SldWorks.Application")

Set activeDocument = swApp.activedocIf (activeDocument Is Nothing) Then Dim Msg, Style, title Style = vbExclamation

title = "Model of the custom attribute"

Msg = "Please open a Solidworks model of the first document"

Call MsgBox (Msg, Style, title) "Display error message

End

End If

2. To obtain information from the definition of the current file attributes on the model program is set up a custom document properties, has nothing to do with the engineering drawing files, so need to use the GetType method to obtain the current file type, and use Gettitle () methods and Getpathname () method to obtain the current file Save the name and path.

Dim docType As LongDim

docName As StringDim

filename As StringDim

FilePath as StringdocType = activeDocument.

GetTypeIf (docType = swDocPART)

Then docName = "parts" With CPbomfrm. Deldescription.Enabled = False. Delmaterial.Enabled = FalseEnd With Picture5.Picture = Picture3.Picture

ElseIf (docType = swDocASSEMBLY) Then docName = "assembly" Picture5.Picture = Picture2.Picture

ElseIf (docType = swDocDRAWING) Then Style = vbExclamation Msg = "the current document is a document of engineering drawings, parts or assembly open file"

Call MsgBox (Msg, Style, title) "Display error message

End

End

If filename = activeDocument.GETTITLE ()

FilePath = activeDocument.getpathname ()

3. To obtain the name of SolidWorks API configuration file provides the name of the visit in accordance with the allocation method of custom attributes - CustomInfo2 (to read the value of custom properties), deletecustominfo2 (delete the specified custom attribute), ADDcustominfo3 (Add a custom attribute name and parameter values), these methods require the configuration of the specified file name. Therefore, in order to document the current model and the current configuration set custom attributes, we must first of all to obtain the name of the current configuration, use GetActiveConfiguration () method available to document the current configuration of the object model and obtained the use of Name in the name of the current configuration.

Dim ConfName As StringSet

CurCFG = activeDocument.GetActiveConfiguration ()

ConfName = CurCFG.Name

4. In accordance with the allocation of access to custom attributes in the process, the author attributes to the specified configuration settings in accordance with the following steps:

(1) in accordance with the configuration file name to read the custom attribute value, and assign the appropriate text in the box;

(2) to determine the current file and current configuration has changed, such as a change in one of them, it prompted an error message;

(3) in accordance with the specified configuration to delete the names of custom attributes;

(4) the contents of the corresponding text box in accordance with the model configuration file name is added to a custom attribute name and parameter value. The above-mentioned control of the following custom properties to configure the three methods to be introduced.

☆ CustomInfo2: to obtain or set the current configuration of the document specified the value of custom properties.

value = ModelDoc.CustomInfo2 (configuration, fieldName)

Which, configuration name for the configuration;

fieldName is the name of custom attributes.

☆ deletecustominfo2: delete the current configuration of the document specified the value of custom properties

retval = ModelDoc.DeleteCustomInfo2 (configuration, FieldName)

☆ ADDcustominfo3: the specified document to the current configuration, as well as add a custom attribute value

retval = ModelDoc.AddCustomInfo3 (configuration, FieldName, FieldType, FieldValue)

Which, FieldType for the type of custom attributes; FieldValue for the value of custom properties.

The following code is click "OK" to the subroutine, in order to simply give them a custom attribute process.

Private Sub cmd_OK_Click () On Error GoTo ERR_91

Dim swApp2 As ObjectDim

activeDocument2 As ObjectDim

curcfg2 As ObjectDim

cfgname2 As StringDim

retval As BooleanSet

swApp2 = CreateObject ( "SldWorks.Application")

Set activeDocument2 = swApp2.activedoc

Set curcfg2 = activeDocument2.GetActiveConfiguration ()

cfgname2 = curcfg2.Name "to determine whether a file or configuration changes, such as change, then an error prompt.

If cfgname2 = ConfName And FilePath = activeDocument2.getpathname () Then "delete property

retval = activeDocument2.deletecustominfo2 (ConfName, "PartID") "to add properties

retval = activeDocument2.ADDcustominfo3 (ConfName, "PartID", swCustomInfoText, frmPartID) "Save the file

retval = activeDocument2.Save ()

Else Style = vbExclamation

title = "Model of the custom attribute"

Msg = "Error: open file or configuration has changed!" & Chr (10) & "Please re-implementation of this procedure"

Call MsgBox (Msg, Style, title)

End

End

IfERR_91: Style = vbExclamation title = "Model of the custom attribute"

Msg = "Error running program, the document may have been closed" & Chr (10) & "can not add custom attributes, click OK to exit!"

Call MsgBox (Msg, Style, title)

End

End Sub

Third, the implementation of SolidWorks application interface

VB compiled for the use of EXE, DLL files, SolidWorks is not currently directly support the use of these documents as a plug-in, if you want to call SolidWorks directly through VB compiled executable program, you can use SolidWorks "macro" to carry out the operation. The use of "macro" in the SolidWorks interface to add the designated application process, just like SolidWorks function call as their own, using a similar method, you can add any executable in the SolidWorks documents. Following the use of "custom attributes" to explain these procedures. 1. To establish a "macro" file in SolidWorks, we use to record and amend the "macro" approach to build up the necessary "macro" file. The main steps are as follows:

(1) Select menu "Tools → Macro → Record operation."

(2) to stop recording, and save the macro file. Given file name, such as: Cpbom.swp.

(3) Select the menu "Tools → Macro → Edit operation" and Cpbom.swp "macro" file to delete all the contents of record, and enter the following content.

Sub main () MyAppID = Shell ( "C: Program FilesCPbomCPbom.exe", 1) AppActivate MyAppID

End Sub

(4) Save and exit the macro editor and return to the SolidWorks environment.

2. Designated "macro" operation in the SolidWorks in order to specify the file location macro and custom toolbar, by the implementation of the macro buttons added to the designated position.

(1) Select menu "Tools → Macro → custom" in the "Custom" dialog box, click "Macro" tab.

(2) refer to Figure 4 the add macro, click the "Apply" to complete.

(3) In the "Customize" dialog box, click "order" labels, from the "Category" list box, select "Macro", the macro will be defined by (No. 1) drag the icon button corresponding toolbar in, as shown in Figure 5.

(4) Click "OK" button to complete the macro definition and the definition of the toolbar.

3. The definition of the implementation of the application "Macro" then, click the corresponding icon will be able to direct the implementation of SolidWorks applications. In the implementation of the SolidWorks application

Fourth, to sum up

I use the "custom attributes" procedures, improved work efficiency. It should be noted that, SolidWorks API objects and methods very rich, covering all the SolidWorks data model, introduced here only the tip of the iceberg. In addition, the procedures mentioned in the definition of properties and businesses from the requirements relating to the need to develop according to their need for custom setup program attributes.


AutoCAD Tutorial Articles


Can't Find What You're Looking For?


Rating: Not yet rated

Comments

No comments posted.