This website uses cookies to ensure you get the best experience on our website. By using our website you agree on the following Cookie Policy, Privacy Policy, and Terms Of Use
This VBA macro merges the selected sketches (3D and 3D) into a single 3D sketch using SOLIDWORKS API. This macro is using convert entities API to copy the entities from the source sketches to a target sketch.
Options
Macro can be configured by changing the values of the constant at the beginning of the macro
DELETE_SOURCE_SKETCHES - True to delete original source sketches, False to not delete
NEW_SKETCH_NAME - Name for the newly generated merged sketch, Empty string to use default auto generated name
Const DELETE_SOURCE_SKETCHES AsBoolean = True'delete all source sketchesConst NEW_SKETCH_NAME AsString = "MergedSketch"'new merged sketch to be named 'MergedSketch'
Notes
Sketches in the assembly or drawings components are also supported
Relations and dimensions from the source sketch are not copied to a target sketch
Sketches are merged to an active 3D sketch, or new 3D sketch is created automatically
Use this macro in conjunction with Select Features By Type to select all sketches to be merged.
Const DELETE_SOURCE_SKETCHES AsBoolean = FalseConst NEW_SKETCH_NAME AsString = ""Dim swApp As SldWorks.SldWorks
Sub main()
Set swApp = Application.SldWorks
Dim swModel As SldWorks.ModelDoc2
try:
OnErrorGoTocatchSet swModel = swApp.ActiveDoc
IfNot swModel IsNothingThen
MergeSelectedSketches swModel
Else
Err.Raise vbError, "", "Please open model"EndIfGoTofinallycatch:
Debug.Print Err.Number
swApp.SendMsgToUser2 Err.Description, swMessageBoxIcon_e.swMbStop, swMessageBoxBtn_e.swMbOk
finally:
EndSubSub MergeSelectedSketches(model As SldWorks.ModelDoc2)
Dim swSketch As SldWorks.Sketch
IfNot model.SketchManager.ActiveSketch IsNothingThenIfFalse = model.SketchManager.ActiveSketch.Is3D() Then
Err.Raise vbError, "", "Only 3D sketch is supported as a target sketch"EndIfEndIfDim vSketchSegs AsVariantDim vSketches AsVariant
vSketchSegs = GetSelectedSketchSegments(model, vSketches)
If model.SketchManager.ActiveSketch IsNothingThen
model.ClearSelection2 True
model.SketchManager.Insert3DSketch TrueEndIfDim swTargetSketch As SldWorks.Feature
If model.Extension.MultiSelect2(vSketchSegs, False, Nothing) = UBound(vSketchSegs) + 1 Then
model.SketchManager.SketchUseEdge3 False, FalseSet swTargetSketch = model.SketchManager.ActiveSketch
model.SketchManager.ActiveSketch.RelationManager.DeleteAllRelations
model.SketchManager.Insert3DSketch TrueElse
Err.Raise vbError, "", "Failed to select sketches"EndIfIf DELETE_SOURCE_SKETCHES ThenIf model.Extension.MultiSelect2(vSketches, False, Nothing) = UBound(vSketches) + 1 Then
model.Extension.DeleteSelection2 swDeleteSelectionOptions_e.swDelete_Absorbed
Else
Err.Raise vbError, "", "Failed to selected sketches for deletion"EndIfEndIfIf NEW_SKETCH_NAME <> ""Then
swTargetSketch.Name = NEW_SKETCH_NAME
EndIfEndSubFunction GetSelectedSketchSegments(model As SldWorks.ModelDoc2, ByRef vSketches AsVariant) AsVariantDim swSketches() As SldWorks.Feature
Dim isSkArrInit AsBoolean
isSkArrInit = FalseDim swSketchSegs() As SldWorks.SketchSegment
Dim isInit AsBoolean
isInit = FalseDim swSelMgr As SldWorks.SelectionMgr
Set swSelMgr = model.SelectionManager
Dim i AsIntegerFor i = 1 To swSelMgr.GetSelectedObjectCount2(-1)
If swSelMgr.GetSelectedObjectType3(i, -1) = swSelectType_e.swSelSKETCHES ThenDim swFeat As SldWorks.Feature
Set swFeat = swSelMgr.GetSelectedObject6(i, -1)
IfNot isSkArrInit Then
isSkArrInit = TrueReDim swSketches(0)
ElseReDimPreserve swSketches(UBound(swSketches) + 1)
EndIfSet swSketches(UBound(swSketches)) = swFeat
Dim swSketch As SldWorks.Sketch
Set swSketch = swFeat.GetSpecificFeature2
Dim vSegs AsVariant
vSegs = swSketch.GetSketchSegments
Dim j AsIntegerIfNot IsEmpty(vSegs) ThenFor j = 0 To UBound(vSegs)
IfNot isInit ThenReDim swSketchSegs(0)
isInit = TrueElseReDimPreserve swSketchSegs(UBound(swSketchSegs) + 1)
EndIfSet swSketchSegs(UBound(swSketchSegs)) = vSegs(j)
NextEndIfEndIfNext
GetSelectedSketchSegments = swSketchSegs
If isSkArrInit Then
vSketches = swSketches
EndIfEndFunction
Notifications
Join session by SOLIDWORKS and PDM API expret Artem Taturevych at 3DEXPERIENCE World 2025 on Feb 26 at 08:30 AM CST to explore 10 essential macros for automating drawings, assemblies, custom properties, and more