SOLIDWORKS Macro to create Drawings for All Configurations with Updated BOM
Article by Samony Riyaz updated November 21, 2019
Article
Have you ever had a part with tens or hundreds or thousands of configurations? And you’re responsible for creating a drawing for EACH of those configurations with a correct BOM?
Well fear no more! We’ve got a SOLIDWORKS Macro for you in which all you have to do is…Change two lines, Click a button, Sit back and Relax while SOLIDWORKS generates the files automatically.
Let’s begin.
- Create a New Macro
- Use the New Macro button in the Macro Toolbar (or select Tools > Macro > New)
- Ensure that the references in SOLIDWORKS Visual Basic Editor (VBA) are correct with your SOLIDWORKS Version
- Copy the code below and change the Highlighted Orange text to match your environment:
- Save the code
- Run
Preconditions: Ensure you are running the code below on a DRAWING containing a MODEL, which contains multiple configurations.
Results: In the OUTPUT_FOLDER of your choice, you should see the Drawing files for EACH configuration with an updated BOM
Const OUTPUT_FOLDER = "C:\Out\" Dim swApp As SldWorks.SldWorks Dim swModel As SldWorks.ModelDoc2 Dim swDraw As SldWorks.DrawingDoc Dim swRefModel As SldWorks.ModelDoc2 Sub main() Dim i As Integer Dim vConfs As Variant Dim swView As SldWorks.View Dim confName As String Set swApp = Application.SldWorks Set swModel = swApp.ActiveDoc Set swDraw = swModel Set swView = swDraw.GetFirstView().GetNextView Set swRefModel = swView.ReferencedDocument vConfs = swRefModel.GetConfigurationNames For i = 0 To UBound(vConfs) confName = vConfs(i) ProcessViews confName swModel.ForceRebuild3 False boolstatus = swModel.Extension.SaveAs(OUTPUT_FOLDER + confName + ".slddrw", swSaveAsVersion_e.swSaveAsCurrentVersion, swSaveAsOptions_e.swSaveAsOptions_Copy, Nothing, 0, 0) Next i MsgBox "Completed" Set swApp = Nothing Set swModel = Nothing Set swDraw = Nothing Set swRefModel = Nothing End Sub Sub ProcessViews(confName As String) Dim i As Integer Dim vSheets As Variant Dim j As Integer Dim vViews As Variant Dim swFeat As SldWorks.Feature Dim swView As SldWorks.View vSheets = swDraw.GetViews For i = 0 To UBound(vSheets) vViews = vSheets(i) For j = 0 To UBound(vViews) Set swView = vViews(j) swView.ReferencedConfiguration = confName Next j 'delete BOM Set swFeat = swDraw.FirstFeature Debug.Print swFeat.Name + " - " + swFeat.GetTypeName While Not swFeat Is Nothing Debug.Print swFeat.Name + " - " + swFeat.GetTypeName If "BomFeat" = swFeat.GetTypeName Then swFeat.Select2 False, -1 swDraw.Extension.DeleteSelection2 swDeleteSelectionOptions_e.swDelete_Absorbed End If Set swFeat = swFeat.GetNextFeature Wend boolstatus = swModel.Extension.SelectByID2(swView.Name, "DRAWINGVIEW", 0, 0, 0, False, 0, Nothing, 0) AnchorType = swBOMConfigurationAnchorType_e.swBOMConfigurationAnchor_TopLeft BomType = swBomType_e.swBomType_PartsOnly Configuration = confName TableTemplate = "" ' Insert BOM table Set swBomAnn = swView.InsertBomTable2(True, 0.4, 0.3, AnchorType, BomType, Configuration, TableTemplate) swModel.ClearSelection2 True ' Update FeatureManager design tree swDraw.ForceRebuild Next i Set swFeat = Nothing Set swView = Nothing End Sub
NOTE: The API examples are provided as is and should be used as reference only. You may use it in your environment, change it, or distribute it, but Javelin is not responsible for any types of damages whatsoever (including without limitation, damages from the loss of use, data, profits, or business) arising out of the uses of this information, applications or services.
Learn more about SOLIDWORKS Macros/API
Javelin provides SOLIDWORKS VB for Applications and SOLIDWORKS API training courses, which you can take live online, or in one of our Canadian classrooms. Check our schedule to learn more.
Related Links
Get Certified SOLIDWORKS Services from Javelin
Javelin Experts can help you to: