Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations cowski on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

Adding part to assembly (Solidworks API) 2

Status
Not open for further replies.

JordonMusser

Mechanical
Dec 22, 2006
40
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.
 
Replies continue below

Recommended for you

There is no need to open a part to be able to add it to an assembly. If you start to insert part and it is not already open, you can browse for the part which will then loads it into the assembly without it showing up in your open windows (it is then open in memory only as a part of the assembly). After you have placed the part you can choose the configuration as follows: Right click on the part in the feature tree and choose "properties". In the section labeled referenced configuration, click the button for "use named configuration". Then chose the configuration you want from the list.

Timelord
 
Sorry, I should have been more clear. This is in reference to SW API, not just adding a part :)

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.
 
OOPS! You did have API in the title and I missed it.
I would think that you must have it open so the API routine knows the path (but I am no API expert).

Timelord
 
If you're using SW2008, the following should work. Change the file paths as needed.

Code:
Dim swApp         As SldWorks.SldWorks
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
 
The key is the swApp::DocumentVisible setting. This controls if opened parts are shown when loaded.
Code:
swApp.DocumentVisible False, swDocPART

Don't forget to set this back to True when you are done. Setting sticks even after macro ends.

[bat]Honesty may be the best policy, but insanity is a better defense.[bat]
-SolidWorks API VB programming help
 
Yes, definitely take care that it's reset to true.

By the way, Tick, congratulations!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor