Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations 3DDave on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problems with Macro to Build an Assembly

Status
Not open for further replies.

Spurs

Mechanical
Nov 7, 2002
297
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?
 
Replies continue below

Recommended for you

My guess is that the problem is coming from the Part.AddComponent command

After the macro has run, the assembly is still blank. Nothing has been added to the tree.

Anyone have any suggestions?
 
By playing around with this, I have learned that the macro will work if the assembly file has at least one part loaded in it. If the document is blank, the macro will not add the first part.

Can anyone provide me with insite how to load the first part automatically as well?

 
Spurs,

If you read the remarks in API help for AssemblyDoc::AddComponent:

The specified file must be loaded in memory. A file is loaded into memory when you load the file in your SolidWorks session (SldWorks::OpenDoc) or open an assembly that already contains the file.

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 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
[COLOR=red] Dim InsertPart as Object[/color]
Sub main()

Set swApp = Application.SldWorks

Set Part = swApp.ActiveDoc

[COLOR=red]Set InsertPart = swApp.OpenDoc("C:\Face.SLDPRT", swDocPART)[/color]

Part.AddComponent [COLOR=red]InsertPart.GetPathName[/color], -0.001940447873138, 0.005483874424086, -0.01049999999998

[COLOR=green]'clean up - close inserted part[/color]
[COLOR=red]swApp.CloseDoc InsertPart.GetPathName[/color]

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



 
To get the code to work properly in SW2006 add the code in red. You should probably add the code for the previous versions too. Makes it more stable.

Code:
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
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

[COLOR=red]Part.ForceRebuild3 True[/color]

'clean up - close inserted part
swApp.CloseDoc InsertPart.GetPathName

End Sub

Regards,

Regg

 
Regg

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?
 
Spurs,

Read my second post.

Regg
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor