×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Problems with Macro to Build an Assembly

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?

RE: Problems with Macro to Build an Assembly

(OP)
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?

RE: Problems with Macro to Build an Assembly

(OP)
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?

RE: Problems with Macro to Build an Assembly

Spurs,

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

Quote:

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

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

Part.ForceRebuild3 True

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

End Sub

Regards,

Regg

RE: Problems with Macro to Build an Assembly

(OP)
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?

RE: Problems with Macro to Build an Assembly

Spurs,

Read my second post.

Regg

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members!


Resources