Files
Lesson 7.5 - Update Custom Properties With Macro Feature
Code
'This demonstrates how macro features can be used to update custom properties. In this _
example, when the macro feature rebuilds, the custom property called "SW Version" is _
updated with the current SolidWorks version. That custom property is added if it is _
not already present. The Options argument in IFeatureManager::InsertMacroFeature3 is _
set so that the macro feature always remains at the end of the FeatureManager tree. _
The security options are set so that the feature cannot be deleted, replaced, or _
suppressed.
'Written by Keith Rice
'CADSharp LLC
'www.cadsharp.com
'Preconditions: model is open
'Results: see FeatureManager tree and custom properties
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swCustPropMgr As SldWorks.CustomPropertyManager
'Rebuild function
Function swmRebuild(app As Variant, model As Variant, feat As Variant) As Variant
Set swApp = app
Set swModel = model
Set swCustPropMgr = swModel.Extension.CustomPropertyManager(Empty)
'assume that if ICustomPropertyManager::Set returns 0, it is because that custom _
property doesn't exist, in which case we will use ICustomPropertyManager::Add2
If swCustPropMgr.Set("SW Version", swApp.RevisionNumber) = 0 Then _
swCustPropMgr.Add2 "SW Version", swCustomInfoText, swApp.RevisionNumber
End Function
'Edit definition function
Function swmEditDefinition(app As Variant, model As Variant, feat As Variant) As Variant
End Function
'Security function
Function swmSecurity(app As Variant, model As Variant, feat As Variant) As Variant
swmSecurity = swMacroFeatureSecurityOptions_e.swMacroFeatureSecurityCannotBeDeleted _
+ swMacroFeatureSecurityOptions_e.swMacroFeatureSecurityCannotBeRolledBack _
+ swMacroFeatureSecurityOptions_e.swMacroFeatureSecurityCannotBeSuppressed
End Function
'Insert macro feature
Sub main()
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
STATE = INSERT
'MACRO METHODS
Dim strMacroMethods(8) As String
'Rebuild function
strMacroMethods(0) = swApp.GetCurrentMacroPathName
strMacroMethods(1) = "mMacroFeature"
strMacroMethods(2) = "swmRebuild"
'Edit definition function
strMacroMethods(3) = swApp.GetCurrentMacroPathName
strMacroMethods(4) = "mMacroFeature"
strMacroMethods(5) = "swmEditDefinition"
'Security function (optional)
strMacroMethods(6) = swApp.GetCurrentMacroPathName
strMacroMethods(7) = "mMacroFeature"
strMacroMethods(8) = "swmSecurity"
'Create the macro feature
swModel.FeatureManager.InsertMacroFeature3 "RecordVersion", Empty, strMacroMethods, _
Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, 1
End Sub