Hide Table of Contents

Constrain Sketch Example (VBA)

This example shows how to fully constrain a sketch.

 

Before constraining the sketch

After constraining the sketch

 

 

 

'----------------------------------------------------------------------------

' Preconditions: Open a document with a sketch similar to "Before constraining

'                the sketch".

'

' Postconditions: Sketch is fully constrained and looks

'                 similar to "After constraining the sketch".

'----------------------------------------------------------------------------

Option Explicit

 

Dim swApp As SldWorks.SldWorks

Dim swModel As SldWorks.ModelDoc2

Dim swSketchMgr As SldWorks.SketchManager

Dim swSketch As SldWorks.Sketch

Dim swSelMgr As SldWorks.SelectionMgr

Dim swFeat As SldWorks.Feature

Dim nSketchStatus As Long

Dim boolstatus As Boolean

 

Sub main()

 

Set swApp = Application.SldWorks

Set swModel = swApp.ActiveDoc

 

' Is a model document active?

If swModel Is Nothing Then

    swApp.SendMsgToUser2 "A part document must be open and the active document.", swMbWarning, swMbOk

    Exit Sub

End If

 

' Is it a part document?

Dim modelType As Long

modelType = swModel.GetType

If modelType <> SwConst.swDocPART Then

    swApp.SendMsgToUser2 "A part document must be open and the active document.", swMbWarning, swMbOk

    Exit Sub

End If

 

Set swSketchMgr = swModel.SketchManager

Set swSketch = swSketchMgr.ActiveSketch

    If swSketch Is Nothing Then

    swApp.SendMsgToUser2 "No active sketch; thus, a sketch could not be selected.", swMbWarning, swMbOk

    Exit Sub

    End If

 

' Select the lines and make them colinear and vertical

boolstatus = swModel.Extension.SelectByID2("Line2", "SKETCHSEGMENT", 0.02116924482339, 0.04904427527406, 0, False, 0, Nothing, 0)

boolstatus = swModel.Extension.SelectByID2("Line3", "SKETCHSEGMENT", 0.06508556638246, 0.02563976857491, 0, True, 0, Nothing, 0)

swModel.SketchAddConstraints "sgCOLINEAR"

swModel.SketchAddConstraints "sgVERTICAL2D"

    MsgBox "The lines have been selected, made colinear, and vertically constrained."

swModel.ClearSelection2 True

 

'Select the center of the circles and constrain them to the origin

boolstatus = swModel.Extension.SelectByID2("Point7", "SKETCHPOINT", 0.1074240560292, 0.006179841656516, 0, False, 0, Nothing, 0)

boolstatus = swModel.Extension.SelectByID2("Point1@Origin", "EXTSKETCHPOINT", 0, 0, 0, True, 0, Nothing, 0)

swModel.SketchAddConstraints "sgCOINCIDENT"

    MsgBox "The center of the circles and the origin were selected and made coincident"

swModel.ClearSelection2 True

 

' Select a line and the circle and make them tangent

boolstatus = swModel.Extension.SelectByID2("Line2", "SKETCHSEGMENT", 0.005390925700365, 0.009861449451888, 0, False, 0, Nothing, 0)

boolstatus = swModel.Extension.SelectByID2("Arc1", "SKETCHSEGMENT", -0.01222819732034, 0.04720347137637, 0, True, 0, Nothing, 0)

swModel.SketchAddConstraints "sgTANGENT"

    MsgBox "One line and a cirle were selected; both lines are now tangent with the circle."

swModel.ClearSelection2 True

 

'Select the circles and make them concentric

boolstatus = swModel.Extension.SelectByID2("Arc2", "SKETCHSEGMENT", -0.0290584043849, 0.03116218026797, 0, False, 0, Nothing, 0)

boolstatus = swModel.Extension.SelectByID2("Arc1", "SKETCHSEGMENT", -0.01222819732034, 0.04720347137637, 0, True, 0, Nothing, 0)

swModel.SketchAddConstraints "sgCONCENTRIC"

    MsgBox "The circles have been selected and made concentric."

swModel.ClearSelection2 True

 

'Select all the sketch entities and fix their positions

MsgBox "All  sketch entities will be selected and made fixed to fully constrain the sketch."

boolstatus = swModel.Extension.SelectByID2("Line2", "SKETCHSEGMENT", 0.02116924482339, 0.04904427527406, 0, False, 0, Nothing, 0)

boolstatus = swModel.Extension.SelectByID2("Line3", "SKETCHSEGMENT", 0.06508556638246, 0.02563976857491, 0, True, 0, Nothing, 0)

boolstatus = swModel.Extension.SelectByID2("Arc2", "SKETCHSEGMENT", -0.0290584043849, 0.03116218026797, 0, False, 0, Nothing, 0)

boolstatus = swModel.Extension.SelectByID2("Arc1", "SKETCHSEGMENT", -0.01222819732034, 0.04720347137637, 0, True, 0, Nothing, 0)

swModel.SketchAddConstraints "sgFIXED"

swModel.ClearSelection2 True

 

End Sub



Provide feedback on this topic

SOLIDWORKS welcomes your feedback concerning the presentation, accuracy, and thoroughness of the documentation. Use the form below to send your comments and suggestions about this topic directly to our documentation team. The documentation team cannot answer technical support questions. Click here for information about technical support.

* Required

 
*Email:  
Subject:   Feedback on Help Topics
Page:   Constrain Sketch Example (VBA)
*Comment:  
*   I acknowledge I have read and I hereby accept the privacy policy under which my Personal Data will be used by Dassault Systèmes

Print Topic

Select the scope of content to print:

x

We have detected you are using a browser version older than Internet Explorer 7. For optimized display, we suggest upgrading your browser to Internet Explorer 7 or newer.

 Never show this message again
x

Web Help Content Version: API Help (English only) 2013 SP05

To disable Web help from within SOLIDWORKS and use local help instead, click Help > Use SOLIDWORKS Web Help.

To report problems encountered with the Web help interface and search, contact your local support representative. To provide feedback on individual help topics, use the “Feedback on this topic” link on the individual topic page.