×
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

Macro for SolidWorks properties

Macro for SolidWorks properties

Macro for SolidWorks properties

(OP)
This is a macro I wrote recently to define a custom property in a SolidWorks part called "Volume" and set it to the system-defined volume calculation.  This can easily be modified for any of the other system-defined properties like mass, surface area, etc.

The part has to be open in SolidWorks first.  Then when you run the macro, it will create the custom property and assign the appropriate value.  (It's the same one from "Tools / Mass Properties.")  If the property already exists, its value will be overwritten.  Once it's done, it saves the file.  I have an "If - Then" statement to make sure the file is a part; otherwise it will ignore it.  If you need to write the custom property to an assembly, you should remove the "If - Then" constraint, but most likely you'll still want to make sure the file isn't a drawing.

Enjoy...

**********************

Option Explicit

Sub main()

Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swFileName As String
Dim swType As String
Dim Success As Boolean

Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc

'Determine whether the model is a part
swType = swModel.GetType

'If the model is a part, set the Volume property accordingly.
If swType = swDocPART Then

swFileName = swModel.GetPathName
Success = swModel.AddCustomInfo3("", "Volume", swCustomInfoText, "")
swModel.CustomInfo2("", "Volume") = Chr$(34) & "SW-Volume@" & swFileName & Chr$(34)
Success = swModel.Save3(swSaveAsOptions_Silent, swGenericSaveError, swFileSaveWarning_NeedsRebuild)

End If

'If the model is not a part, nothing will happen.

End Sub

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