ds-blue-logo
Preview  |  SOLIDWORKS USER FORUM
Use your SOLIDWORKS ID or 3DEXPERIENCE ID to log in.
CPChris Pratt13/02/2019

My macro (see below) boss extrudes all of the regions in a sketch (since hitting ctrl + a doesn't work while selecting regions). It works for sketches with a few (~100) regions. However for sketches with >600 regions (see attached part), it causes SolidWorks to soft-lock. I believe this happens because, during a boss extrude, SolidWorks recalculates the state of everything after each selection. Thus during the macro it *selects region* -> *state recalculation* -> *selects region* -> *state recalculation* -> ... . Meaning its recalculating the state >600 times. It only needs to do this 1 time, once everything has been selected (which is a feature request in and of itself).

So, my question: How do you select all of the regions inside of a sketch AT THE SAME TIME as opposed to one at a time, so solidworks only calculates the state of the selection once.

Notes:

I know you can turn off the "use shaded preview option". This might reduce the recalculation time, but its still recalculating >600 times, and its not sufficient.

The macro must be run while currently editing a sketch (I dont know how to get what sketches the user has selected in the feature tree using VBA)

I could have the macro do separate extrudes for every 50 regions, and then combine them. That will be the workaround around the workaround around the workaround if there's no proper solution.

VBA:

Dim swApp As SldWorks.SldWorks

Dim swSelectionMgr As SldWorks.SelectionMgr

Dim swSketchMgr As SldWorks.SketchManager

Dim myModel As SldWorks.ModelDoc2

Dim mySelectData As SldWorks.SelectData

Dim mySketch As SldWorks.sketch

Dim myFeature As SldWorks.feature

Dim vSkRegions As Variant

Dim skRegion As SketchRegion

Dim regionCount As Long

Dim i As Long

Dim boolstatus As Boolean

Dim prevSetting As Boolean

Option Explicit

Sub main()

Set swApp = Application.SldWorks

Set myModel = swApp.ActiveDoc

Set swSelectionMgr = myModel.SelectionManager

Set swSketchMgr = myModel.SketchManager

Set mySelectData = swSelectionMgr.CreateSelectData

Set mySketch = swSketchMgr.ActiveSketch

Set myFeature = mySketch

' Sketch to extrude

boolstatus = myModel.Extension.SelectByID2(myFeature.Name, "SKETCH", 0, 0, 0, False, 0, Nothing, 0)

' Enable selecting contours

prevSetting = swSelectionMgr.EnableContourSelection

swSelectionMgr.EnableContourSelection = True

If Not mySketch Is Nothing Then

' Find the regions of the sketch

regionCount = mySketch.GetSketchRegionCount()

vSkRegions = mySketch.GetSketchRegions()

For i = LBound(vSkRegions) To UBound(vSkRegions)

' Loop through each region and select it.

Set skRegion = vSkRegions(i)

boolstatus = skRegion.Select2(True, mySelectData)

Next i

End If

' Create the extrusion

Set myFeature = myModel.FeatureManager.FeatureExtrusion3(True, False, True, swEndCondBlind, 0, 0.004, 0, False, False, False, False, 0, 0, False, False, False, False, True, True, True, swStartSketchPlane, 0, False)

swSelectionMgr.EnableContourSelection = prevSetting

End Sub