Preview | SOLIDWORKS USER FORUM
Use your SOLIDWORKS ID or 3DEXPERIENCE ID to log in.
CKCraig Kennedy08/05/2008
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 broke this problem down into its basic components and have been working to solve each one. I have hit a perplexing 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.
Apparently, from what I've been able to figure out by fiddling around, this command cannot insert a part or assembly UNLESS that part or assembly has already been used during the current session of solidworks.
For example:
----------------------------------------------------------------------------------------------
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(4) 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) = "001"
partnum(2) = "002"
partnum(3) = "003"
partnum(4) = "004"
For x = 1 To 4
filepath = "C:\Documents and Settings\ckennedy\My Documents\Solid Works Testing\Parts\" + partnum(x) + ".SLDPRT"
Part.AddComponent filepath, posx, posy, posz
filepath = "C:\Documents and Settings\ckennedy\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 001.SLDPRT, 002.SLDPRT, etc. (a cube, a sphere, and a cylinder) and an assembly (made up of the cube and sphere parts) and inserts them into the active assembly, spacing them out as it does so.
If I open an assembly and run the macro, nothing happens.
If I open an assembly, insert the cube, delete it, then run the macro, it inserts the cube.
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).
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.
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? Any help or insight would be appreciated.
~Craig
I broke this problem down into its basic components and have been working to solve each one. I have hit a perplexing 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.
Apparently, from what I've been able to figure out by fiddling around, this command cannot insert a part or assembly UNLESS that part or assembly has already been used during the current session of solidworks.
For example:
----------------------------------------------------------------------------------------------
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(4) 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) = "001"
partnum(2) = "002"
partnum(3) = "003"
partnum(4) = "004"
For x = 1 To 4
filepath = "C:\Documents and Settings\ckennedy\My Documents\Solid Works Testing\Parts\" + partnum(x) + ".SLDPRT"
Part.AddComponent filepath, posx, posy, posz
filepath = "C:\Documents and Settings\ckennedy\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 001.SLDPRT, 002.SLDPRT, etc. (a cube, a sphere, and a cylinder) and an assembly (made up of the cube and sphere parts) and inserts them into the active assembly, spacing them out as it does so.
If I open an assembly and run the macro, nothing happens.
If I open an assembly, insert the cube, delete it, then run the macro, it inserts the cube.
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).
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.
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? Any help or insight would be appreciated.
~Craig