Adding part to assembly (Solidworks API)
Adding part to assembly (Solidworks API)
(OP)
I have succeded in doing this, but I must first "open" the part, then add it, then close it.
Is there a way to OPEN ONLY into memory so I can add it?
I need to open a certain part with a certain config, then add it.
Is there a way to OPEN ONLY into memory so I can add it?
I need to open a certain part with a certain config, then add it.






RE: Adding part to assembly (Solidworks API)
Timelord
RE: Adding part to assembly (Solidworks API)
If I use the addpart function, I must first open it into memory (opendoc, etc) and I can't get it to open with out showing it for an instant.
RE: Adding part to assembly (Solidworks API)
I would think that you must have it open so the API routine knows the path (but I am no API expert).
Timelord
RE: Adding part to assembly (Solidworks API)
CODE
Dim swModel As SldWorks.ModelDoc2
Dim swAssy As SldWorks.AssemblyDoc
Dim swComp As SldWorks.Component2
Dim swDocSpec As SldWorks.DocumentSpecification
Dim sAssyName As String
Dim nErr As Long
Option Explicit
Sub main()
On Error GoTo errH 'redirects to error handler to reset part visibility on error
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
Set swAssy = swModel: Debug.Assert Not swAssy Is Nothing 'assembly should be open
sAssyName = swModel.GetTitle 'store assembly title
Set swModel = Nothing 'swModel reused for opened part
'setup document specification
Set swDocSpec = swApp.GetOpenDocSpec("C:\Users\VanHunks\Desktop\New Folder\Part1.SLDPRT")
swDocSpec.Silent = True
'open part to load into memory
swApp.DocumentVisible False, swDocPART 'sets parts invisible
Set swModel = swApp.OpenDoc7(swDocSpec): Debug.Assert Not swModel Is Nothing
'activate assembly and insert part
swApp.ActivateDoc2 sAssyName, True, nErr
Set swComp = swAssy.AddComponent4("C:\Users\VanHunks\Desktop\New Folder\Part1.SLDPRT", "Default", 0#, 0#, 0#)
'close part
swApp.CloseDoc swModel.GetTitle
'error handler - no 'exit sub' statement above, so it falls through and executes this anyway
errH:
swApp.DocumentVisible True, swDocPART
End Sub
Another method I'm more familiar with is AssemblyDoc::AddComponents and inserting a single component. This doesn't require the component(s) to be opened before insertion. There's a good example of this in the API help file.
-VanHunks
RE: Adding part to assembly (Solidworks API)
CODE
Don't forget to set this back to True when you are done. Setting sticks even after macro ends.
http://www.EsoxRepublic.com-SolidWorks API VB programming help
RE: Adding part to assembly (Solidworks API)
By the way, Tick, congratulations!