Description
This SolidWorks macro to rebuild referenced models in all drawing sheets works by rebuilding all referenced models for each sheet in an active SOLIDWORKS drawing document. It validates the active document, iterates through every sheet, and for each one, ensures the referenced model views are refreshed. After completing the rebuild process, the macro closes the models to free up memory efficiently.
System Requirements
- SOLIDWORKS Version: SOLIDWORKS 2014 or newer
- Operating System: Windows 10 or later
Pre-Conditions
- SOLIDWORKS must be installed and running on the machine.
- An active drawing with multiple sheets and views is open.
Post-Conditions
- The referenced files will be opened, rebuilt, and closed.
- The original drawing views will update.
VBA Macro Code
Visual Basic
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
' Disclaimer: ' The code provided should be used at your own risk. ' Blue Byte Systems Inc. assumes no responsibility for any issues or damages that may arise from using or modifying this code. ' For more information, visit [Blue Byte Systems Inc.](https://bluebyte.biz). Option Explicit ' Declare variables Dim swApp As SldWorks.SldWorks Dim swModel As SldWorks.ModelDoc2 Dim swDrawModel As SldWorks.ModelDoc2 Dim swDraw As SldWorks.DrawingDoc Dim swView As SldWorks.View Dim swSheet As SldWorks.Sheet Dim vSheetNameArr As Variant Dim vSheetName As Variant Dim bRet As Boolean Dim sFileName As String Dim nErrors As Long Sub main() ' Initialize SOLIDWORKS application object Set swApp = Application.SldWorks Set swModel = swApp.ActiveDoc ' Check if a drawing document is active If swModel Is Nothing Then swApp.SendMsgToUser2 "A drawing document must be open and the active document.", swMbWarning, swMbOk Exit Sub End If ' Verify the document is a drawing If swModel.GetType <> SwConst.swDocDRAWING Then swApp.SendMsgToUser2 "A drawing document must be open and the active document.", swMbWarning, swMbOk Exit Sub End If ' Cast the active document as a drawing Set swDraw = swModel ' Get the current sheet and sheet names Set swSheet = swDraw.GetCurrentSheet vSheetNameArr = swDraw.GetSheetNames ' Loop through each sheet For Each vSheetName In vSheetNameArr ' Activate each sheet bRet = swDraw.ActivateSheet(vSheetName) Set swView = swDraw.GetFirstView Set swView = swView.GetNextView ' Skip the sheet's overall view ' Loop through all views in the sheet Do While Not swView Is Nothing ' Get the referenced model for the view Set swDrawModel = swView.ReferencedDocument sFileName = swDrawModel.GetPathName ' Open and rebuild the referenced model Set swDrawModel = swApp.ActivateDoc3(sFileName, True, swRebuildActiveDoc, nErrors) ' Rebuild the referenced model swDrawModel.EditRebuild3 ' Close the referenced model after rebuild swApp.CloseDoc swDrawModel.GetTitle ' Move to the next view Set swView = swView.GetNextView Loop Next vSheetName ' Notify the user that the rebuild is complete MsgBox "Rebuild is done." End Sub |
Macro
You can download the macro from here.
Customization
Need to modify the macro to meet specific requirements or integrate it with other processes? We provide custom macro development tailored to your needs. Contact us
