-
1
- #1
PDMAdmin
Mechanical
- Apr 21, 2004
- 488
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
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