This website uses cookies to ensure you get the best experience on our website. By using our website you agree on the following Cookie Policy, Privacy Policy, and Terms Of Use
This VBA macro inserts the macro feature into the model using SOLIDWORKS API and allows to run custom code every time the model with this feature is loaded.
This can be useful when certain code needs to be run on every model load (e.g. update custom properties, log information, etc.)
Feature is visible in the tree and can be embedded into the document template if required.
Copy the code into corresponding modules of the macro
To configure the macro modify the options defined in the constants:
EMBED_MACRO_FEATURE - true to embed this macro directly into the document. With this option model can be shared and will work on each computer (macro doesn't meed to be shared). Refer the Macro Based Macro Feature section for benefits and limitation of this approach.
BASE_NAME - default naming convention for the feature
Place your code into the main sub of the HandlerModule module. The pointer to IModelDoc2 document is passed as the parameter. Use this pointer instead of ISldWorks::ActiveDoc SOLIDWORKS API property as this method is called for invisible documents (i.e. loaded as assembly or drawing components).
Sub main(model As SldWorks.ModelDoc2)
'TODO: add your routine hereEndSub
Macro Module
Main macro routine to insert feature and define event handles
Const EMBED_MACRO_FEATURE AsBoolean = FalseConst BASE_NAME AsString = "ModelLoadWatcher"Dim HandledModels As Collection
Sub main()
Dim swApp As SldWorks.SldWorks
Set swApp = Application.SldWorks
Dim swModel As SldWorks.ModelDoc2
Set swModel = swApp.ActiveDoc
IfNot swModel IsNothingThenDim curMacroPath AsString
curMacroPath = swApp.GetCurrentMacroPathName
Dim vMethods(8) AsStringDim moduleName AsString
GetMacroEntryPoint swApp, curMacroPath, moduleName, ""
vMethods(0) = curMacroPath: vMethods(1) = moduleName: vMethods(2) = "swmRebuild"
vMethods(3) = curMacroPath: vMethods(4) = moduleName: vMethods(5) = "swmEditDefinition"
vMethods(6) = curMacroPath: vMethods(7) = moduleName: vMethods(8) = "swmSecurity"Dim opts As swMacroFeatureOptions_e
If EMBED_MACRO_FEATURE Then
opts = swMacroFeatureOptions_e.swMacroFeatureEmbedMacroFile
Else
opts = swMacroFeatureOptions_e.swMacroFeatureByDefault
EndIfDim swFeat As SldWorks.Feature
Set swFeat = swModel.FeatureManager.InsertMacroFeature3(BASE_NAME, "", vMethods, _
Empty, Empty, Empty, Empty, Empty, Empty, _
Empty, opts)
If swFeat IsNothingThen
MsgBox "Failed to create model load watcher"EndIfElse
MsgBox "Please open model"EndIfEndSubSub GetMacroEntryPoint(app As SldWorks.SldWorks, macroPath AsString, ByRef moduleName AsString, ByRef procName AsString)
Dim vMethods AsVariant
vMethods = app.GetMacroMethods(macroPath, swMacroMethods_e.swMethodsWithoutArguments)
Dim i AsIntegerIfNot IsEmpty(vMethods) ThenFor i = 0 To UBound(vMethods)
Dim vData AsVariant
vData = Split(vMethods(i), ".")
If i = 0 Or LCase(vData(1)) = "main"Then
moduleName = vData(0)
procName = vData(1)
EndIfNextEndIfEndSubFunction swmRebuild(varApp AsVariant, varDoc AsVariant, varFeat AsVariant) AsVariant
swmRebuild = TrueEndFunctionFunction swmEditDefinition(varApp AsVariant, varDoc AsVariant, varFeat AsVariant) AsVariant
swmEditDefinition = TrueEndFunctionFunction swmSecurity(varApp AsVariant, varDoc AsVariant, varFeat AsVariant) AsVariantDim swFeat As SldWorks.Feature
Set swFeat = varFeat
If HandledModels IsNothingThenSet HandledModels = New Collection
EndIfDim swModel As SldWorks.ModelDoc2
Set swModel = varDoc
IfNot CollectionContains(HandledModels, swModel) Then
HandledModels.Add swModel
Dim swApp As SldWorks.SldWorks
Set swApp = varApp
ClearCache swApp
OnModelLoad swModel
EndIf
swmSecurity = SwConst.swMacroFeatureSecurityOptions_e.swMacroFeatureSecurityByDefault
EndFunctionSub ClearCache(app As SldWorks.SldWorks)
IfNot HandledModels IsNothingThenDim vDocs AsVariant
vDocs = app.GetDocuments
IfNot IsEmpty(vDocs) ThenDim i AsIntegerFor i = HandledModels.CountTo 1 Step -1
Dim swModel As SldWorks.ModelDoc2
Set swModel = HandledModels(i)
IfNot ArrayContains(vDocs, swModel) Then
HandledModels.Remove i
EndIfNextEndIfEndIfEndSubFunction CollectionContains(coll As Collection, model As SldWorks.ModelDoc2) AsBooleanDim i AsIntegerFor i = 1 To coll.Count()
If coll(i) Is model Then
CollectionContains = TrueExitFunctionEndIfNext
CollectionContains = FalseEndFunctionFunction ArrayContains(arr AsVariant, model As SldWorks.ModelDoc2) AsBooleanDim i AsIntegerIfNot IsEmpty(arr) ThenFor i = 0 To UBound(arr)
Dim thisModel As SldWorks.ModelDoc2
Set thisModel = arr(i)
If thisModel Is model Then
ArrayContains = TrueExitFunctionEndIfNextEndIf
ArrayContains = FalseEndFunctionSub OnModelLoad(model As SldWorks.ModelDoc2)
HandlerModule.main model
EndSub
HandlerModule Module
Module to insert required code to be run every time model loads. Must be named HandlerModule
Sub main(model As SldWorks.ModelDoc2)
'TODO: add your routine here
MsgBox model.GetTitle()
EndSub