Use solidworks macro to insert parts into assembly based on excel file

cheapkid1 picture cheapkid1 · Oct 28, 2013 · Viewed 8.5k times · Source

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.

  1. If I open an assembly and run the macro, nothing happens.
  2. If I open an assembly, insert the cube, delete it, then run the macro, it inserts the cube.
  3. If I open an assembly, insert all the parts, delete them, then run the macro, it inserts all the parts (but not the assembly made up of two of them).
  4. If I open an assembly, insert the cube-sphere assembly, delete it, and run the macro, it will insert the cube, the sphere, and the cube-sphere assembly, but not the cylinder.
  5. If the parts have been used in one assembly, and another is opened or made active, then the macro works fine in the new window.

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?

Answer

alphabit picture alphabit · Jan 19, 2014

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

http://help.solidworks.com/2013/English/api/sldworksapi/SolidWorks.Interop.sldworks~SolidWorks.Interop.sldworks.IAssemblyDoc~AddComponent5.html

OpenDoc6

http://help.solidworks.com/2013/English/api/sldworksapi/solidworks.interop.sldworks~solidworks.interop.sldworks.isldworks~opendoc6.html

OpenDoc7

http://help.solidworks.com/2013/English/api/sldworksapi/solidworks.interop.sldworks~solidworks.interop.sldworks.isldworks~opendoc7.html

AddComponent5 Example: Add component and mate

http://help.solidworks.com/2013/English/api/sldworksapi/add_component_and_mate_example_vb.htm