SolidWorks two times development -05-assembly body insert Parts

Source: Internet
Author: User
Insert SolidWorks Two development--insert parts in assembly






When inserting parts into the assembly, we use the addcomponent function. If you need to configure the selected parts, you need to use ADDCOMPONENT4.



Learn the grammar first:







ADDCOMPONENT4:



retval = Assemblydoc.addcomponent4 (Compname, ConfigName, x, Y, z)







Input: (BSTR) compname Path name of a loaded part or assembly to add as a component



Input: (BSTR) ConfigName Name of the configuration from which to load the component



Input: (double) x x coordinate of the component center



Input: (double) y y coordinate of the component center



Input: (double) z z coordinate of the component center



Output: (LPCOMPONENT2) retval pointer to the Component2 object







Note that parameter 1 is the full name of the file (including the path), parameter 2 is the file's configuration name, and a pointer to the part is returned when the function is executed successfully.



So we can write a small program to insert parts into the assembly:



' Filename:insertPart.swp



' Write by
Arden
2005-4-4



' This function add a parts called ' Part 1. Sldprt "in Currentworkingdirectory



' Precondition is there has a parts document called ' Part 1. Sldprt "in Currentworkingdirectory



' And it has a configuration called ' Configuration 1 '







Dim Swapp as Sldworks.sldworks



Dim Model as ModelDoc2



Dim PTH as String



Dim strpath as String







Sub Insertpart ()



Set Swapp = Application.sldworks



strpath = Swapp.getcurrentworkingdirectory ' Current work path



Set Model = Swapp.activedoc



PTH = strpath & "Part 1. Sldprt "' Get the FullPath full name of the file



MODEL.ADDCOMPONENT4 PTH, "Configuration 1", 0, 0, 0 ' Add parts



End Sub







However, this program is not as useful as it is imagined. Why?? Looking back at Addcomponent4 's remark, it says:



The specified file must is loaded in memory. A file is loaded to memory when your load the file in your



SolidWorks session (SLDWORKS::OPENDOC6) or open a assembly that already contains the file.



So you want to specify that the inserted file must be loaded in memory before the function is called.



Not accustomed to, you can not directly open how simple, no way, I have not found a good way, can only be according to others:



Look at the following function Opendoc6, which opens a document:







OPENDOC6:



retval = sldworks.opendoc6 (filename, type, options, configuration, &errors, &warnings)



Input: (BSTR) Filename Document name or full path if not in current directory, including Exte Nsion



Input: (long) type Document type as defined in Swdocumenttypes_e



Input: (Long) Options Mode in which to open the document as defined in Swopendocoption S_e



Input: (BSTR) Configuration Model Configuration in which to open this document:



Applies to parts and assemblies, not drawings



If This argument is empty or the specified configuration are not present in the model,



The model is opened in the last-used configuration.



Output: (Long) Errors Load Errors as defined in Swfileloaderror_e



Output: (long) warnings warnings or extra information generated during the open operation as Defined in Swfileloadwarning_e



return: (lpdispatch) retval pointer to a Dispatch object, the newly loaded, MODELDOC2, or NULL if Faile D to open







This function argument 1 is the full name of the document, and parameter 2 is the type description to insert, where 0123 represents:



0 Swdocnone: not SW file



1 Swdocpart: Parts



2 swdocassembly: Assembly body



3 swdocdrawing: Engineering drawing

If you want to use Swdocnone, you need to define:

Public Enum Swdocumenttypes_e

}-->}-->swdocnone = 0

}-->}-->swdocpart= 1

}-->}-->swdocassembly = 2

Swdocdrawing=3

End Enum

Parameter 3 is the mode of opening the document, we usually choose swopendocoptions_silent with 0, and of course, read only, look at the options



Parameter 4 is open option, general null



followed by two output to display the prompt when the error was opened



function returns a pointer to an open file.







In accordance with the above requirements we need to insert a component into the assembly, this step is required:



1, get the assembly body



2. Use OPENDOC6 to open parts that need to be inserted



3, use ADDCOMPONENT4 to insert parts into the assembly body



The above program needs to be modified to add a function that opens the document:







' ******************************************************************************



' Insertpart 03/21/05 by
Arden




' Insert Part 1



' Prerequisites: There is part "Part 1" in the folder where the assembly is located, and Part 1 has a configuration of "Configuration 1"



' ******************************************************************************



Dim Swapp as Sldworks.sldworks



Dim Model as ModelDoc2



Dim Ysbmodel as ModelDoc2



Dim PTH as String



Dim strpath as String



Dim Nerrors as Long



Dim Nwarnings as Long











Sub Insertpart ()



Set Swapp = Application.sldworks



strpath = Swapp.getcurrentworkingdirectory



Set Model = Swapp.activedoc



PTH = strpath & "Part 1. Sldprt "



OPENYSB (PTH) ' open it before adding parts



MODEL.ADDCOMPONENT4 PTH, "Configuration 1", 0, 0, 0



End Sub







' This function opens part 1



Sub Openpart (ByVal PTH as String)



Dim Path as String



Dim Newswapp as Sldworks.sldworks



Set Newswapp = Application.sldworks







Path = PTH



Set Ysbmodel = Newswapp. OPENDOC6 (path, 1, Swopendocoptions_silent, "", Nerrors, Nwarnings)



ysbmodel.visible = False ' I don't want to see part 1



End Sub







This way I feel more stupid ~ in order to catch the project progress I did not look for a good way, if you know there is a good way to tell me, extremely grateful.




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.