Solidworks Macro - Add Sketch Relations (Constraints)
In this post, I tell you about how to Add Sketch Relations (Constraints) using Solidworks VBA Macros in a Sketch.
In this post, I explain about SketchAddConstraints method from Solidworks’s ModelDoc2 object.
This method is most updated method, I found in Solidworks API Help.
This post will utilize the methods explained in earlier posts, hence knowledge to those is required but it is not mandatory.
An absolute beginner can follow what is written here.
Add Sketch Relations (Constraints) method
For adding relations to a sketch segment, we use SketchAddConstraints method from Solidworks’s ModelDoc2 object.
This SketchAddConstraints method takes following parameters as explained:
- Constraint : ID of constraint as given on this page of Solidworks API Help.
Return Value: There are no return value for this method.
In following sections we add different sketch constraints to sketch segments.
Add ‘Fix’ Sketch Relation to a sketch segment
Here we learn how to add Fixed sketch relation to a sketch segment through VBA.
We need an unconstraint sketch segment.
In this post, I use a circle as shown in below image:
Before Add Fix Sketch Relation to Circle

Code to add Fix sketch relation
Option Explicit
' Create variable for Solidworks application
Dim swApp As SldWorks.SldWorks
' Create variable for Solidworks document
Dim swDoc As SldWorks.ModelDoc2
' Boolean Variable
Dim BoolStatus As Boolean
Sub main()
' Set Solidworks variable to Solidworks application
Set swApp = Application.SldWorks
' Set Solidworks document to active part document
Set swDoc = swApp.ActiveDoc
' Select Circle
BoolStatus = swDoc.Extension.SelectByID2("Arc1", "SKETCHSEGMENT", 0, 0, 0, True, 0, Nothing, swSelectOption_e.swSelectOptionDefault)
' Add Fix sketch relation
swDoc.SketchAddConstraints ("sgFIXED")
' Clear selection after adding relation
swDoc.ClearSelection2 True
End Sub
After Add Fix Sketch Relation to Circle

I have added comments to each line code sample, hence it is easy to understand.
Add ‘Coincident’ Sketch Relation to a sketch segment
Here we learn how to add Coincident sketch relation to a sketch segment through VBA.
We need an unconstraint sketch segment.
In this post, I use a circle as shown in below image:
Before Add Coincident Sketch Relation to Circle

Code to add Coincident sketch relation
Option Explicit
' Create variable for Solidworks application
Dim swApp As SldWorks.SldWorks
' Create variable for Solidworks document
Dim swDoc As SldWorks.ModelDoc2
' Boolean Variable
Dim BoolStatus As Boolean
Sub main()
' Set Solidworks variable to Solidworks application
Set swApp = Application.SldWorks
' Set Solidworks document to new part document
Set swDoc = swApp.ActiveDoc
' Select Circle center point
BoolStatus = swDoc.Extension.SelectByID2("Point2", "SKETCHSEGMENT", 0, 0, 0, True, 0, Nothing, swSelectOption_e.swSelectOptionDefault)
' Select Origin
BoolStatus = swDoc.Extension.SelectByID2("Point1@Origin", "EXTSKETCHPOINT", 0, 0, 0, True, 0, Nothing, swSelectOption_e.swSelectOptionDefault)
' Add Coincident sketch relation
swDoc.SketchAddConstraints ("sgCOINCIDENT")
' Clear selection after adding relation
swDoc.ClearSelection2 True
End Sub
After Add Coincident Sketch Relation to Circle

I have added comments to each line code sample, hence it is easy to understand.
Add ‘Horizontal’ Sketch Relation to a sketch segment
Here we learn how to add Horizontal sketch relation to a sketch segment through VBA.
We need an unconstraint sketch segment.
In this post, I use a Line as shown in below image:
Before Add Horizontal Sketch Relation to Line

Code to add Horizontal sketch relation
Option Explicit
' Create variable for Solidworks application
Dim swApp As SldWorks.SldWorks
' Create variable for Solidworks document
Dim swDoc As SldWorks.ModelDoc2
' Boolean Variable
Dim BoolStatus As Boolean
Sub main()
' Set Solidworks variable to Solidworks application
Set swApp = Application.SldWorks
' Set Solidworks document to new part document
Set swDoc = swApp.ActiveDoc
' Select Line
BoolStatus = swDoc.Extension.SelectByID2("Line1", "SKETCHSEGMENT", 0, 0, 0, True, 0, Nothing, swSelectOption_e.swSelectOptionDefault)
' Add Horizontal sketch relation
swDoc.SketchAddConstraints ("sgHORIZONTAL2D")
' Clear selection after adding relation
swDoc.ClearSelection2 True
End Sub
After Add Horizontal Sketch Relation to Line

I have added comments to each line code sample, hence it is easy to understand.
Add ‘Vertical’ Sketch Relation to a sketch segment
Here we learn how to add Vertical sketch relation to a sketch segment through VBA.
We need an unconstraint sketch segment.
In this post, I use a Line as shown in below image:
Before Add Vertical Sketch Relation to Line

Code to add Vertical sketch relation
Option Explicit
' Create variable for Solidworks application
Dim swApp As SldWorks.SldWorks
' Create variable for Solidworks document
Dim swDoc As SldWorks.ModelDoc2
' Boolean Variable
Dim BoolStatus As Boolean
Sub main()
' Set Solidworks variable to Solidworks application
Set swApp = Application.SldWorks
' Set Solidworks document to new part document
Set swDoc = swApp.ActiveDoc
' Select Line
BoolStatus = swDoc.Extension.SelectByID2("Line1", "SKETCHSEGMENT", 0, 0, 0, True, 0, Nothing, swSelectOption_e.swSelectOptionDefault)
' Add Vertical sketch relation
swDoc.SketchAddConstraints ("sgVERTICAL2D")
' Clear selection after adding relation
swDoc.ClearSelection2 True
End Sub
After Add Vertical Sketch Relation to Line

I have added comments to each line code sample, hence it is easy to understand.
Add ‘Midpoint’ Sketch Relation to a sketch segment
Here we learn how to add Midpoint sketch relation to a sketch segment through VBA.
We need an unconstraint sketch segment.
In this post, I use a Line as shown in below image:
Before Add Midpoint Sketch Relation to Line

Code to add Midpoint sketch relation
Option Explicit
' Create variable for Solidworks application
Dim swApp As SldWorks.SldWorks
' Create variable for Solidworks document
Dim swDoc As SldWorks.ModelDoc2
' Boolean Variable
Dim BoolStatus As Boolean
Sub main()
' Set Solidworks variable to Solidworks application
Set swApp = Application.SldWorks
' Set Solidworks document to new part document
Set swDoc = swApp.ActiveDoc
' Select Line
BoolStatus = swDoc.Extension.SelectByID2("Line1", "SKETCHSEGMENT", 0, 0, 0, True, 0, Nothing, swSelectOption_e.swSelectOptionDefault)
' Select Origin
BoolStatus = swDoc.Extension.SelectByID2("Point1@Origin", "EXTSKETCHPOINT", 0, 0, 0, True, 0, Nothing, swSelectOption_e.swSelectOptionDefault)
' Add Midpoint sketch relation
swDoc.SketchAddConstraints ("sgATMIDDLE")
' Clear selection after adding relation
swDoc.ClearSelection2 True
End Sub
After Add Midpoint Sketch Relation to Line

I have added comments to each line code sample, hence it is easy to understand.
Add ‘Co-Linear’ Sketch Relation to a sketch segment
Here we learn how to add Co-Linear sketch relation to a sketch segment through VBA.
We need an unconstraint sketch segment.
In this post, I use two Lines as shown in below image:
Before Add Co-Linear Sketch Relation to Line

Code to add Co-Linear sketch relation
Option Explicit
' Create variable for Solidworks application
Dim swApp As SldWorks.SldWorks
' Create variable for Solidworks document
Dim swDoc As SldWorks.ModelDoc2
' Boolean Variable
Dim BoolStatus As Boolean
Sub main()
' Set Solidworks variable to Solidworks application
Set swApp = Application.SldWorks
' Set Solidworks document to new part document
Set swDoc = swApp.ActiveDoc
' Select Line 1
BoolStatus = swDoc.Extension.SelectByID2("Line1", "SKETCHSEGMENT", 0, 0, 0, True, 0, Nothing, swSelectOption_e.swSelectOptionDefault)
' Select Line 2
BoolStatus = swDoc.Extension.SelectByID2("Line2", "SKETCHSEGMENT", 0, 0, 0, True, 0, Nothing, swSelectOption_e.swSelectOptionDefault)
' Add Co-Linear sketch relation
swDoc.SketchAddConstraints ("sgCOLINEAR")
' Clear selection after adding relation
swDoc.ClearSelection2 True
End Sub
After Add Co-Linear Sketch Relation to Line

I have added comments to each line code sample, hence it is easy to understand.
Add ‘Perpendicular’ Sketch Relation to a sketch segment
Here we learn how to add Perpendicular sketch relation to a sketch segment through VBA.
We need an unconstraint sketch segment.
In this post, I use two Lines as shown in below image:
Before Add Perpendicular Sketch Relation to Line

Code to add Perpendicular sketch relation
Option Explicit
' Create variable for Solidworks application
Dim swApp As SldWorks.SldWorks
' Create variable for Solidworks document
Dim swDoc As SldWorks.ModelDoc2
' Boolean Variable
Dim BoolStatus As Boolean
Sub main()
' Set Solidworks variable to Solidworks application
Set swApp = Application.SldWorks
' Set Solidworks document to new part document
Set swDoc = swApp.ActiveDoc
' Select Line 1
BoolStatus = swDoc.Extension.SelectByID2("Line1", "SKETCHSEGMENT", 0, 0, 0, True, 0, Nothing, swSelectOption_e.swSelectOptionDefault)
' Select Line 2
BoolStatus = swDoc.Extension.SelectByID2("Line2", "SKETCHSEGMENT", 0, 0, 0, True, 0, Nothing, swSelectOption_e.swSelectOptionDefault)
' Add Perpendicular sketch relation
swDoc.SketchAddConstraints ("sgPERPENDICULAR")
' Clear selection after adding relation
swDoc.ClearSelection2 True
End Sub
After Add Perpendicular Sketch Relation to Line

I have added comments to each line code sample, hence it is easy to understand.
Add ‘Parallel’ Sketch Relation to a sketch segment
Here we learn how to add Parallel sketch relation to a sketch segment through VBA.
We need an unconstraint sketch segment.
In this post, I use two Lines as shown in below image:
Before Add Parallel Sketch Relation to Line

Code to add Parallel sketch relation
Option Explicit
' Create variable for Solidworks application
Dim swApp As SldWorks.SldWorks
' Create variable for Solidworks document
Dim swDoc As SldWorks.ModelDoc2
' Boolean Variable
Dim BoolStatus As Boolean
Sub main()
' Set Solidworks variable to Solidworks application
Set swApp = Application.SldWorks
' Set Solidworks document to new part document
Set swDoc = swApp.ActiveDoc
' Select Line 1
BoolStatus = swDoc.Extension.SelectByID2("Line1", "SKETCHSEGMENT", 0, 0, 0, True, 0, Nothing, swSelectOption_e.swSelectOptionDefault)
' Select Line 2
BoolStatus = swDoc.Extension.SelectByID2("Line2", "SKETCHSEGMENT", 0, 0, 0, True, 0, Nothing, swSelectOption_e.swSelectOptionDefault)
' Add Parallel sketch relation
swDoc.SketchAddConstraints ("sgPARALLEL")
' Clear selection after adding relation
swDoc.ClearSelection2 True
End Sub
After Add Parallel Sketch Relation to Line

I have added comments to each line code sample, hence it is easy to understand.
Add ‘Equal Length’ Sketch Relation to a sketch segment
Here we learn how to add Equal Length sketch relation to a sketch segment through VBA.
We need an unconstraint sketch segment.
In this post, I use two Lines as shown in below image:
Before Add Equal Length Sketch Relation to Line

Code to add Equal Length sketch relation
Option Explicit
' Create variable for Solidworks application
Dim swApp As SldWorks.SldWorks
' Create variable for Solidworks document
Dim swDoc As SldWorks.ModelDoc2
' Boolean Variable
Dim BoolStatus As Boolean
Sub main()
' Set Solidworks variable to Solidworks application
Set swApp = Application.SldWorks
' Set Solidworks document to new part document
Set swDoc = swApp.ActiveDoc
' Select Line 1
BoolStatus = swDoc.Extension.SelectByID2("Line1", "SKETCHSEGMENT", 0, 0, 0, True, 0, Nothing, swSelectOption_e.swSelectOptionDefault)
' Select Line 2
BoolStatus = swDoc.Extension.SelectByID2("Line2", "SKETCHSEGMENT", 0, 0, 0, True, 0, Nothing, swSelectOption_e.swSelectOptionDefault)
' Add Equal Length sketch relation
swDoc.SketchAddConstraints ("sgSAMELENGTH")
' Clear selection after adding relation
swDoc.ClearSelection2 True
End Sub
After Add Equal Length Sketch Relation to Line

I have added comments to each line code sample, hence it is easy to understand.
Add ‘Tangent’ Sketch Relation to a sketch segment
Here we learn how to add Tangent sketch relation to a sketch segment through VBA.
We need an unconstraint sketch segment.
In this post, I use a Line and a Circle as shown in below image:
Before Add Tangent Sketch Relation to Line

Code to add Tangent sketch relation
Option Explicit
' Create variable for Solidworks application
Dim swApp As SldWorks.SldWorks
' Create variable for Solidworks document
Dim swDoc As SldWorks.ModelDoc2
' Boolean Variable
Dim BoolStatus As Boolean
Sub main()
' Set Solidworks variable to Solidworks application
Set swApp = Application.SldWorks
' Set Solidworks document to new part document
Set swDoc = swApp.ActiveDoc
' Select Line 1
BoolStatus = swDoc.Extension.SelectByID2("Line1", "SKETCHSEGMENT", 0, 0, 0, True, 0, Nothing, swSelectOption_e.swSelectOptionDefault)
' Select Line 2
BoolStatus = swDoc.Extension.SelectByID2("Arc1", "SKETCHSEGMENT", 0, 0, 0, True, 0, Nothing, swSelectOption_e.swSelectOptionDefault)
' Add Tangent sketch relation
swDoc.SketchAddConstraints ("sgTANGENT")
' Clear selection after adding relation
swDoc.ClearSelection2 True
End Sub
After Add Tangent Sketch Relation to Line

I have added comments to each line code sample, hence it is easy to understand.
Add ‘Same Curve Length’ Sketch Relation to a sketch segment
Here we learn how to add Same Curve Length sketch relation to a sketch segment through VBA.
We need an unconstraint sketch segment.
In this post, I use a Line and a Circle as shown in below image:
Before Add Same Curve Length Sketch Relation to Line

Code to add Same Curve Length sketch relation
Option Explicit
' Create variable for Solidworks application
Dim swApp As SldWorks.SldWorks
' Create variable for Solidworks document
Dim swDoc As SldWorks.ModelDoc2
' Boolean Variable
Dim BoolStatus As Boolean
Sub main()
' Set Solidworks variable to Solidworks application
Set swApp = Application.SldWorks
' Set Solidworks document to new part document
Set swDoc = swApp.ActiveDoc
' Select Line 1
BoolStatus = swDoc.Extension.SelectByID2("Line1", "SKETCHSEGMENT", 0, 0, 0, True, 0, Nothing, swSelectOption_e.swSelectOptionDefault)
' Select Line 2
BoolStatus = swDoc.Extension.SelectByID2("Arc1", "SKETCHSEGMENT", 0, 0, 0, True, 0, Nothing, swSelectOption_e.swSelectOptionDefault)
' Add Same Curve Length sketch relation
swDoc.SketchAddConstraints ("sgSAMECURVELENGTH")
' Clear selection after adding relation
swDoc.ClearSelection2 True
End Sub
After Add Same Curve Length Sketch Relation to Line

I have added comments to each line code sample, hence it is easy to understand.
Add ‘Coincident’ Sketch Relation to a sketch segment
Here we learn how to add Coincident sketch relation to a sketch segment through VBA.
We need an unconstraint sketch segment.
In this post, I use two Circles as shown in below image:
Before Add Coincident Sketch Relation to Line

Code to add Coincident sketch relation
Option Explicit
' Create variable for Solidworks application
Dim swApp As SldWorks.SldWorks
' Create variable for Solidworks document
Dim swDoc As SldWorks.ModelDoc2
' Boolean Variable
Dim BoolStatus As Boolean
Sub main()
' Set Solidworks variable to Solidworks application
Set swApp = Application.SldWorks
' Set Solidworks document to new part document
Set swDoc = swApp.ActiveDoc
' Select Line 1
BoolStatus = swDoc.Extension.SelectByID2("Arc1", "SKETCHSEGMENT", 0, 0, 0, True, 0, Nothing, swSelectOption_e.swSelectOptionDefault)
' Select Line 2
BoolStatus = swDoc.Extension.SelectByID2("Arc2", "SKETCHSEGMENT", 0, 0, 0, True, 0, Nothing, swSelectOption_e.swSelectOptionDefault)
' Add Coincident sketch relation
swDoc.SketchAddConstraints ("sgCONCENTRIC")
' Clear selection after adding relation
swDoc.ClearSelection2 True
End Sub
After Add Coincident Sketch Relation to Line

I have added comments to each line code sample, hence it is easy to understand.
Add ‘Coradial’ Sketch Relation to a sketch segment
Here we learn how to add Coradial sketch relation to a sketch segment through VBA.
We need an unconstraint sketch segment.
In this post, I use two Circles as shown in below image:
Before Add Coradial Sketch Relation to Line

Code to add Coradial sketch relation
Option Explicit
' Create variable for Solidworks application
Dim swApp As SldWorks.SldWorks
' Create variable for Solidworks document
Dim swDoc As SldWorks.ModelDoc2
' Boolean Variable
Dim BoolStatus As Boolean
Sub main()
' Set Solidworks variable to Solidworks application
Set swApp = Application.SldWorks
' Set Solidworks document to new part document
Set swDoc = swApp.ActiveDoc
' Select Line 1
BoolStatus = swDoc.Extension.SelectByID2("Arc1", "SKETCHSEGMENT", 0, 0, 0, True, 0, Nothing, swSelectOption_e.swSelectOptionDefault)
' Select Line 2
BoolStatus = swDoc.Extension.SelectByID2("Arc2", "SKETCHSEGMENT", 0, 0, 0, True, 0, Nothing, swSelectOption_e.swSelectOptionDefault)
' Add Coradial sketch relation
swDoc.SketchAddConstraints ("sgCORADIAL")
' Clear selection after adding relation
swDoc.ClearSelection2 True
End Sub
After Add Coradial Sketch Relation to Line

I have added comments to each line code sample, hence it is easy to understand.
This is it !!!
I hope my efforts will helpful to someone!
If you found anything to add or update, please let me know on my e-mail.
Hope this post helps you to Add Sketch Relations (Constraints) with Solidworks VBA Macros.
For more such tutorials on Solidworks VBA Macro, do come to this blog after sometime.
If you like the post then please share it with your friends also.
Do let me know by you like this post or not!
Till then, Happy learning!!!