×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Adding part to assembly (Solidworks API)
2

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.

RE: Adding part to assembly (Solidworks API)

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

RE: Adding part to assembly (Solidworks API)

(OP)
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.

RE: Adding part to assembly (Solidworks API)

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

RE: Adding part to assembly (Solidworks API)

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

RE: Adding part to assembly (Solidworks API)

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.

batHonesty may be the best policy, but insanity is a better defense.bat
http://www.EsoxRepublic.com-SolidWorks API VB programming help

RE: Adding part to assembly (Solidworks API)

Yes, definitely take care that it's reset to true.

By the way, Tick, congratulations!
 

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members!


Resources