Problems with Macro to Build an Assembly
Problems with Macro to Build an Assembly
(OP)
I have recorded the following macro in which I added a part called "Face.SLDPRT" at the origin in an open blank assembly file.
Dim swApp As Object
Dim Part As Object
Dim boolstatus As Boolean
Dim longstatus As Long, longwarnings As Long
Dim FeatureData As Object
Dim Feature As Object
Dim Component As Object
Sub main()
Set swApp = Application.SldWorks
Set Part = swApp.ActiveDoc
Part.AddComponent "C:\Face.SLDPRT", -0.001940447873138, 0.005483874424086, -0.01049999999998
End Sub
When I open the blank file a 2nd time and run the macro, nothing happens.
The assembly file and part file are in the same directory.
Any suggestions as to what is wrong?
Dim swApp As Object
Dim Part As Object
Dim boolstatus As Boolean
Dim longstatus As Long, longwarnings As Long
Dim FeatureData As Object
Dim Feature As Object
Dim Component As Object
Sub main()
Set swApp = Application.SldWorks
Set Part = swApp.ActiveDoc
Part.AddComponent "C:\Face.SLDPRT", -0.001940447873138, 0.005483874424086, -0.01049999999998
End Sub
When I open the blank file a 2nd time and run the macro, nothing happens.
The assembly file and part file are in the same directory.
Any suggestions as to what is wrong?






RE: Problems with Macro to Build an Assembly
After the macro has run, the assembly is still blank. Nothing has been added to the tree.
Anyone have any suggestions?
RE: Problems with Macro to Build an Assembly
Can anyone provide me with insite how to load the first part automatically as well?
RE: Problems with Macro to Build an Assembly
If you read the remarks in API help for AssemblyDoc::AddComponent:
Thus the macro will not work correctly because the file is not loaded into memory. Try the following by adding the code shown in red to your macro.
CODE
Dim Part As Object
Dim boolstatus As Boolean
Dim longstatus As Long, longwarnings As Long
Dim FeatureData As Object
Dim Feature As Object
Dim Component As Object
Dim InsertPart as Object
Sub main()
Set swApp = Application.SldWorks
Set Part = swApp.ActiveDoc
Set InsertPart = swApp.OpenDoc("C:\Face.SLDPRT", swDocPART)
Part.AddComponent InsertPart.GetPathName, -0.001940447873138, 0.005483874424086, -0.01049999999998
'clean up - close inserted part
swApp.CloseDoc InsertPart.GetPathName
End Sub
Since you did not indicate which version of SolidWorks you have. I tested the macro in SW2004, SW2005 and SW2006. It works in SW2004 & SW2005 but in SW2006, the assembly does not rebuild correctly and would need a few more lines of code.
Regards,
Regg
RE: Problems with Macro to Build an Assembly
CODE
Dim Part As Object
Dim boolstatus As Boolean
Dim longstatus As Long, longwarnings As Long
Dim FeatureData As Object
Dim Feature As Object
Dim Component As Object
Dim InsertPart as Object
Sub main()
Set swApp = Application.SldWorks
Set Part = swApp.ActiveDoc
Set InsertPart = swApp.OpenDoc("C:\Face.SLDPRT", swDocPART)
Part.AddComponent InsertPart.GetPathName, -0.001940447873138, 0.005483874424086, -0.01049999999998
Part.ForceRebuild3 True
'clean up - close inserted part
swApp.CloseDoc InsertPart.GetPathName
End Sub
Regards,
Regg
RE: Problems with Macro to Build an Assembly
Thank you for the help. Your suggested changes worked perfectly.
I am using SW2005. Any idea what the changes are to 2006 for when I upgrade?
RE: Problems with Macro to Build an Assembly
Read my second post.
Regg