Hide Table of Contents

Get Sketch Relations Example (VB.NET)

This example shows how to get all of the sketch relations in a sketch.

' Preconditions:
' 1. Open a part or assembly document.
' 2. Select the sketch whose relations you want to see.
' Postconditions: Inspect the Immediate window.
Imports SolidWorks.Interop.sldworks
Imports SolidWorks.Interop.swconst
Imports System.Runtime.InteropServices
Imports System
Imports System.Diagnostics

Partial Class SolidWorksMacro

Sub main()

Dim swModel As ModelDoc2
Dim swSelMgr As SelectionMgr
Dim swSelData As SelectData
Dim swFeat As Feature
Dim swSketch As Sketch
Dim swSkRelMgr As SketchRelationManager
Dim swSkRel As SketchRelation
Dim dispDim As DisplayDimension
Dim vSkRelArr As Object
        Dim vSkRel As Object
        Dim vEntTypeArr As Object
        Dim vEntType As swSketchRelationEntityTypes_e
Dim vEntArr As Object
        Dim vDefEntArr As Object
        Dim swSkSeg As SketchSegment
Dim swSkPt As SketchPoint
Dim i As Integer
        Dim j As Integer
        Dim bRet As Boolean

        swModel = swApp.ActiveDoc
        swSelMgr = swModel.SelectionManager
        swSelData = swSelMgr.CreateSelectData
        swFeat = swSelMgr.GetSelectedObject6(1, -1)
        swSketch = swFeat.GetSpecificFeature2
        swSkRelMgr = swSketch.RelationManager


"File = " & swModel.GetPathName)
"  Feat = " & swFeat.Name)

        vSkRelArr = swSkRelMgr.GetRelations(swSketchRelationFilterType_e.swAll) :
If IsNothing(vSkRelArr) Then Exit Sub
        For Each vSkRel In vSkRelArr
            swSkRel = vSkRel

"    Relation(" & i & ")")
"      Type         = " & swSkRel.GetRelationType)

            dispDim = swSkRel.GetDisplayDimension
If Not dispDim Is Nothing Then
"      Display dimension         = " & dispDim.GetNameForSelection)
End If

            vEntTypeArr = swSkRel.GetEntitiesType
            vEntArr = swSkRel.GetEntities

            vDefEntArr = swSkRel.GetDefinitionEntities2
If IsNothing(vDefEntArr) Then
                Debug.Print("    Number of definition entities in this relation: " & UBound(vDefEntArr))
End If

            If Not IsNothing(vEntTypeArr) And Not IsNothing(vEntArr) Then
                If UBound(vEntTypeArr) = UBound(vEntArr) Then

                    j = 0

For Each vEntType In vEntTypeArr
"        EntType    = " & vEntType)

Select Case vEntType
Case swSketchRelationEntityTypes_e.swSketchRelationEntityType_Unknown
"          Not known")

Case swSketchRelationEntityTypes_e.swSketchRelationEntityType_SubSketch

Case swSketchRelationEntityTypes_e.swSketchRelationEntityType_Point
                                swSkPt = vEntArr(j) : Debug.Assert(
Not swSkPt Is Nothing)

"          SkPoint ID = [" & swSkPt.GetID(0) & ", " & swSkPt.GetID(1) & "]")

                                bRet = swSkPt.Select4(
True, swSelData) '

                            Case swSketchRelationEntityTypes_e.swSketchRelationEntityType_Line, _
                                    swSketchRelationEntityTypes_e.swSketchRelationEntityType_Arc, _
                                    swSketchRelationEntityTypes_e.swSketchRelationEntityType_Ellipse, _
                                    swSketchRelationEntityTypes_e.swSketchRelationEntityType_Parabola, _

                                swSkSeg = vEntArr(j)

"          SkSeg   ID = [" & swSkSeg.GetID(0) & ", " & swSkSeg.GetID(1) & "]")

                                bRet = swSkSeg.Select4(
True, swSelData)

Case swSketchRelationEntityTypes_e.swSketchRelationEntityType_Hatch

Case swSketchRelationEntityTypes_e.swSketchRelationEntityType_Text

Case swSketchRelationEntityTypes_e.swSketchRelationEntityType_Plane

Case swSketchRelationEntityTypes_e.swSketchRelationEntityType_Cylinder

Case swSketchRelationEntityTypes_e.swSketchRelationEntityType_Sphere

Case swSketchRelationEntityTypes_e.swSketchRelationEntityType_Surface

Case swSketchRelationEntityTypes_e.swSketchRelationEntityType_Dimension

Case Else
                                Debug.Print("Something else")

End Select

                        j = j + 1

                End If
            End If

            i = i + 1


    End Sub

    Public swApp As SldWorks

End Class


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

Subject:   Feedback on Help Topics
Page:   Get Sketch Relations Example (VB.NET)
*   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:


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

Web Help Content Version: API Help (English only) 2015 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.