I am writing a macro that will eventually (hopefully!) read part numbers from an excel or text file, then search through the config's of my parts library and insert the corresponding components into an assembly, then make the corresponding config'n active.
I have a problem when it comes to inserting the parts and or assemblies. I started off by recording a macro of inserting a part. Pretty simple. It uses the AddComponent command, which needs a filepath and x-y-z coordinates. This seemed to work ok but It kept glitching up. From what I've been able to figure out this command cannot insert a part or assembly UNLESS that part or assembly has already been used during the current session of solidworks.
Option Base 1
Dim swApp As Object
Dim Part As Object
Dim SelMgr As Object
Dim boolstatus As Boolean
Dim longstatus As Long, longwarnings As Long
Dim Feature As Object
Dim filepath As String
Dim partnum(8) As String
Dim posx As Integer
Dim posy As Integer
Dim posz As Integer
Dim x As Integer
Sub main()
Set swApp = Application.SldWorks
Set Part = swApp.ActiveDoc
Set SelMgr = Part.SelectionManager
posx = 0
posy = 0
posz = 0
partnum(1) = "07010304"
partnum(2) = "07010318"
partnum(3) = "07010321"
partnum(4) = "07010331"
For x = 1 To 8
filepath = "C:\Documents and Settings\user\My Documents\Solid Works Testing\Parts\"+ partnum(x) + ".SLDPRT"
Part.AddComponent filepath, posx, posy, posz
filepath = "C:\Documents and Settings\user\My Documents\Solid Works Testing\Assemblies\" + partnum(x) + ".SLDASM"
Part.AddComponent filepath, posx, posy, posz
posx = posx + 1.5
Next
End Sub
This macro grabs three parts 07010304.SLDPRT, 07010318.SLDPRT, etc. an assembly (made up of those parts) and inserts them into the active assembly, spacing them out as it does so.
So In conclusion, how do I fix this? Is there another command to insert parts that doesn't rely on the "loaded into SW memory" thing that seems to be happening? Or a command to do just that and load the parts into SW memory?
First of all, you're missing the SolidWorks version you are using. So it is important that you know which API method variant you should use (i.e. in SolidWorks 2013 it would be AddComponent5 instead of AddComponent).
A part of this important detail, generally spoken, SolidWorks handles adding components to an assembly exactly as you have already noticed.
According to the SolidWorks API documentation, to add a component to an assembly, you first need to load that file into memory. A file is loaded into memory using the OpenDoc method (please note that this depends on the version of SolidWorks you have, in 2013 you should use ISldWorks::OpenDoc6 or ISldWorks::OpenDoc7).
You should also take into consideration that calling ISldWorks::OpenDoc6 doesn't activate & display the ModelDoc2 (document) if it is already open (in memory) in an assembly or drawing document.
ISldWorks::OpenDoc6 returns a reference to ModelDoc2 (IModelDoc2). Use the reference gained and pass it to ISldWorks::ActivateDoc2 or ISldWorks::IActivateDoc3 to activate and display.
You should also take a look at the related OpenDoc events, like FileOpenNotify2, ActiveDocChangeNotify and ActiveModelDocChangeNotify to fine tune and control the loading and activation process of a document.
Here are some links to the related API documentation pages (API 2013):
AddComponent5
OpenDoc6
OpenDoc7
AddComponent5 Example: Add component and mate
http://help.solidworks.com/2013/English/api/sldworksapi/add_component_and_mate_example_vb.htm