Solidworks API support for Slot creation

Hi, 

I could be able to create a slot feature in 3D model & is there any API to orient (Horizontal / Vertical) thru. API call.
Manually I could select the slot direction but thru code facing challenges.

 

Thanks & Regards,

Prem

Answers 2
JB
JB 2026-11-20

If you just need the geometry generated, and you don’t necessarily need a Hole Wizard Slot feature in the final feature tree, you might want to consider just generating the geometry in a sketch and doing a cut-extrude.

“Offset” is a super easy way to generate sketch geometry for a slot.  All you have to do is sketch a line for the centerline of the slot, and then Offset in both directions, cap ends round, and make the original sketch geometry for construction.

 

comment imagecomment image
JS
JS 2026-11-21

Go with ISketchManager.CreateSketchSlot and create an angled slot.  Then IModelDoc2.SketchAddConstraints "sgHORIZONTAL" or IModelDoc2.SketchAddConstraints "sgVERTICAL".  See video and code below.



'by John R. Sitek
'XX-XXX-20XX
'This macro is for testing
Option Explicit

Sub Main()

Dim swApplication As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swSelectData As SldWorks.SelectData

Set swApplication = Application.SldWorks
Set swModel = swApplication.ActiveDoc

'If nothing is open
If swApplication.GetDocumentCount = 0 Then
   MsgBox "Please start/open a part file first, then select a plane/surface to Sketch on.", vbOKOnly, "Nothing Selected"
   Exit Sub
End If

'If the file is not a part
If swModel.GetType <> swDocumentTypes_e.swDocPART Then
   MsgBox "This macro is for parts only.", vbOKOnly, "Nothing Selected"
   Exit Sub
End If

'If nothing is selected
If swModel.SelectionManager.GetSelectedObject6(1, -1) Is Nothing Then
   MsgBox "Please select a face or plane first, then rerun this macro.", vbOKOnly, "Nothing Selected"
   Exit Sub
End If

'If a plane/face has not been selected
If swModel.SelectionManager.GetSelectedObjectType3(1, -1) <> swSelectType_e.swSelDATUMPLANES Then ' Or swModel.SelectionManager.GetSelectedObjectType3(1, -1) <> swSelectType_e.swSelFACES Then
   MsgBox "Please select a face or plane first, then rerun this macro.", vbOKOnly, "Nothing Selected"
   Exit Sub
End If

'Create a Sketch with a Slot
swModel.SketchManager.InsertSketch True
swModel.Extension.SelectByID2 "", "EXTSKETCHPOINT", 0, 0, 0, True, 0, Nothing, swSelectOption_e.swSelectOptionDefault
swModel.SketchManager.CreateSketchSlot swSketchSlotCreationType_e.swSketchSlotCreationType_center_line, swSketchSlotLengthType_e.swSketchSlotLengthType_CenterCenter, 0.0025, 0, 0, 0, 0.00254, 0.00254, 0, 0, 0, 0, 1, True
swModel.ClearSelection2 True
swModel.SketchManager.ActiveSketch.GetSketchSegments(2).Select4 True, swSelectData
swModel.SketchAddConstraints "sgHORIZONTAL"
'swModel.SketchAddConstraints "sgVERTICAL"
swModel.SketchManager.InsertSketch True

End Sub