Asked • 06/17/19

How do I automatically modify a SolidWorks 3D model by Macro?

1 Expert Answer

By:

Bob M. answered • 12/30/20

Tutor
New to Wyzant

Expert Mechanical Design Engineer/Manager

Bob M.

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
Report

12/30/20

Still looking for help? Get the right answer, fast.

Ask a question for free

Get a free answer to a quick problem.
Most questions answered within 4 hours.

OR

Find an Online Tutor Now

Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.