SOLIDWORKS Secondary Development - Insert Parts in Distributes
When inserting components in the backfinder, we use the AddComponent function. If you need to select the configuration of the component, you will need to use AddComponent4.
First learn the speech:
Addcomponent4:
RetVal = askDOC.Addcomponent4 (CompName, Configname, X, Y, Z)
INPUT: (BSTR) CompName Path Name of a Loaded Part OR Assembly To Add as A Component
Input: (BST) Configname Name of the Configuration from Which To Load The Component
Input: (Double) x x coordinate of the component center
Input: (Double) Y Y Y Coordinate of The Component Center
Input: (Double) z Coordinate of the Component Center
Output: (lpcomponent2) RetVal Pointer to the component2 Object
It should be noted that the parameter 1 is the full name of the file (including path); parameter 2 is the configuration name of the file; when the function performs successful purchase, a pointer to the part is returned.
So we can write a small program as follows, used to insert the partition in the fitting:
'Filename: INSERTPART.SWP
'WRITE BY
ARDEN
2005-4-4
'This function add a part called "Part 1.sldprt" in CurrentworkingDirectory
'Precondition is the there Has a part 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 & "Parts 1.sldprt" 'get the full name of the FullPath
Model.addcomponent4 PTH, Configuration 1, 0, 0, 0 'Add Parts
End Sub
However, this program is not as good as it is imagined. why? ? Looking back at the remark of Addcomponent4, writing this:
The Specified File Must Be Loaded In Memory. A File Is Loaded Into Memory When You Load The File In YourSolidWorks Session (SLDWORKS :: OpenDoc6) or Open an assembly That Already Contains the file.
That is to say, the inserted file you want to specify must be loaded in memory before the call function.
Not used to, you can't open more simple, no way, I haven't found a good method, I can only follow people:
Take a look at the function OpenDoc6 below, it opens a document:
OpenDoc6:
Retval = SLDWORKS.OPENDOC6 (FileName, Type, Options, Configuration, & Errors, & Warnings)
INPUT: (BST) Filename Document Name or Full Path if Not in Current Directory, Including Extension
INPUT: (long) Type Document Type As Defined in SwdocumentTypes_e
Input: (long) Options Mode in which to open the document as defined in swopEndocOptions_e
Input: (BSTR) Configuration Model Configuration In Which To Open this Document:
Applies to Parts and Assemblies, Not Drawings
IF this argument is es Empty or the specified configuration is not present in the model,
The model is Opened in The last-buy configuration.
Output: (long) Errors load errors as defined in swfileloaderror_e
Output: (long) WARNINGS WARNINGS ORE Extra Information As defined in swfileloadWarning_e
Return: (LPDISPATCH) RETVAL POINTER TO A Dispatch Object, The Newly Loaded Modeldoc2, or Null IF Failed To Open
This function parameter 1 is the full name of the document, the parameter 2 is a type description to be inserted, where 0123 represents:
0 SWDOCNE: Not SW file
1 SWDOCPART: Parts
2 SWDocassembly: assembly
3 SWDOCDRAWING: Engineering
If you want to use SWDOCNE, you need to define:
Public Enum SwdocumentTypes_ESwDOCnone = 0
SWDOCPART = 1
Swdocassembly = 2 SWDOCDRAWING = 3END ENUM
The parameter 3 is the mode of opening the document. Generally, we choose SwopEndocOptions_SILENT to indicate 0, of course, there are read-only, just see the options.
Parameter 4 is open option, generally
The following is two Output, used to display the prompt when an error is opened
The function returns a pointer to the open file.
As required by the requirements, we need this step when inserting a component into the fitting ligand:
1, get the assembly
2. Use OpenDoc6 to open parts that need to be inserted
3, insert the part to the assembly using Addcomponent4
The procedure above needs this to modify it, add a function that opens the document:
'********************************************************** ****************************
'insertpart
03/21/05
BY
ARDEN
'Insert Part 1
'Prerequisites: Parts in the folder where the ligand is located "Parts"
1
"
Presented, and part 1 is configured
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 & "Parts 1.sldprt"
OpenYSB (PTH) 'Open it before adding components
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 parts 1
End Sub
This kind of practice I feel more stupid ~ I have not to find a good method for the progress of the project. If you know a good method, please let me, thank you very much.