ds-blue-logo
Preview  |  SOLIDWORKS USER FORUM
Use your SOLIDWORKS ID or 3DEXPERIENCE ID to log in.
BSBrian Smith04/02/2017

I am trying to write a macro that does two things.

1.) A Control-Q rebuild

2.) Traverse assemblies and all sub-assemblies for nonread-only parts and rebuild any macro feature in them

I am doing this because calling a "full-full" rebuild (   swModel.ForceRebuild3(false)  ) takes a very long time for our assemblies because it is literally rebuilding every feature in every part and assembly. This "full-full" rebuild comes from: 2016 SOLIDWORKS API Help - Force Rebuild Example (VBA) and was the start of my macro.

This is currently the only way I know how to force a rebuild on lower level macro features. The macro I am trying to write now is meant to replace the control-Q rebuild and search out my macro features and will hopefully be faster than the "full-full" rebuild

I believe I am close and I think I am stuck on the last step, which is is running the SwComFeature regenerate method. 2015 SOLIDWORKS API Help - Regenerate Method (ISwComFeature)

I've already added the swPublished interface library, but when I try to call the regenerate rebuild I getting "Run-time error '91' : Object variable or with block variable not set"

I believe the problem is that I am not setting my ISWcomFeature object properly. I am still pretty green at writing macros so any help is appreciated. The line that is erroring out is bolded below.

Option Explicit

Public Enum swUserPreferenceIntegerValue_e

    swAutoSaveInterval = 3

End Enum

Sub RegenerateMacroFeature(Doc As SldWorks.ModelDoc2)

    Dim swApp As SldWorks.SldWorks

    Dim swFeat As SldWorks.Feature

    Dim instance As ISwComFeature

    Dim temp As Object

   

    Set swFeat = Doc.FirstFeature

    Set swApp = Application.SldWorks

   

    While Not swFeat Is Nothing

        If (swFeat.GetTypeName2 = "MacroFeature") Then

            Set temp = instance.Regenerate(swApp, Doc, swFeat)

        End If

        Set swFeat = swFeat.GetNextFeature

    Wend  

End Sub

Sub TraverseComponent(swComp As SldWorks.Component2, nLevel As Long)

    Dim swApp As SldWorks.SldWorks

    Dim vChildComp As Variant

    Dim swChildComp As SldWorks.Component2

    Dim swCompConfig As SldWorks.Configuration

    Dim sPadStr As String

    Dim Doc As ModelDoc2

   

    Dim i As Long

    For i = 0 To nLevel - 1

        sPadStr = sPadStr + "  "

    Next i

    vChildComp = swComp.GetChildren

    For i = 0 To UBound(vChildComp)

        Set swChildComp = vChildComp(i)

       

        Set Doc = swChildComp.GetModelDoc2

        If Doc.GetType() = 1 Then

            If Doc.IsOpenedReadOnly() = False Then

                RegenerateMacroFeature Doc

            End If

        End If

        TraverseComponent swChildComp, nLevel + 1

        Debug.Print sPadStr & swChildComp.Name2 & " <" & swChildComp.ReferencedConfiguration & ">"

    Next i

End Sub

Sub main()

    Dim swApp As SldWorks.SldWorks

    Dim swModel As SldWorks.ModelDoc2

    Dim swConf As SldWorks.Configuration

    Dim swRootComp As SldWorks.Component2

    Dim nStart As Single

    Dim i As Long

    Dim bRet As Boolean

   

    Set swApp = Application.SldWorks

    Set swModel = swApp.ActiveDoc

    Set swConf = swModel.GetActiveConfiguration

    Set swRootComp = swConf.GetRootComponent3(True)

   'Turn off automatic save

    bRet = swApp.SetUserPreferenceIntegerValue(swAutoSaveInterval, 0)

    nStart = Timer

    bRet = swModel.ForceRebuild3(True)

    TraverseComponent swRootComp, 1

End Sub