Hide Table of Contents

Get Corresponding Entities Between Parts and Drawing Views Example (VB.NET)

This example shows how to get corresponding entities or objects between a part and its drawing.

'----------------------------------------------------------------------------
' Preconditions:
' 1. Ensure that the specified part and drawing exist.
' 2. Open the Immediate window.
' 3. Uncomment the subroutine you want to run in Main().
' 4. At the pause, select a face, edge, vertex, feature, annotation,
'    or sketch segment.
' 5. Press F5.
'
' Postconditions:
' 1. Inspect the Immediate window.
' 2. If a corresponding face, edge, or vertex is found, it is selected in the
'    underlying part or drawing.
'
' NOTE: Because the models are used elsewhere, do not save changes.
'----------------------------------------------------------------------------
Imports SolidWorks.Interop.sldworks
Imports SolidWorks.Interop.swconst
 
 
Partial Class SolidWorksMacro
 
 
    Dim docSpec As DocumentSpecification
    Dim swModelPart As ModelDoc2
    Dim swModelDrawing As ModelDoc2
    Dim swDrawing As DrawingDoc
    Dim swView As View
    Dim lErr As Integer
    Dim selMgr As SelectionMgr
    Dim inputEntity As Entity
    Dim outputEntity As Entity
    Dim bSelected As Boolean
    Dim inputObject As Object
    Dim outputObject As Object
    Dim drComp As DrawingComponent
 
    Public Sub Main()
 
        'Uncomment the subroutine you want to run; comment the other one
        'ViewToPart()
        PartToView()
 
    End Sub
    Public Sub ViewToPart()
 
        docSpec = swApp.GetOpenDocSpec("C:\Users\Public\Documents\SOLIDWORKS\SOLIDWORKS 2018\samples\tutorial\api\clamp1.SLDPRT")
        swModelPart = swApp.OpenDoc7(docSpec)
 
        docSpec = swApp.GetOpenDocSpec("C:\Users\Public\Documents\SOLIDWORKS\SOLIDWORKS 2018\samples\tutorial\api\clamp1.SLDDRW")
        swModelDrawing = swApp.OpenDoc7(docSpec)
 
        swDrawing = swModelDrawing
        swView = swDrawing.FeatureByName("Drawing View1").GetSpecificFeature()
 
        swApp.ActivateDoc3(swModelPart.GetTitleTrueswRebuildOnActivation_e.swUserDecision, lErr)
 
        selMgr = swModelPart.SelectionManager
        swModelPart.ClearSelection2(True)
 
        Stop ' Select something in the model and press F5
 
        Select Case selMgr.GetSelectedObjectType3(1, -1)
            Case swSelectType_e.swSelFACES, swSelectType_e.swSelEDGES, swSelectType_e.swSelVERTICES
 
                inputEntity = selMgr.GetSelectedObject6(1, -1)
 
                Debug.Print("Using IView::GetCorrespondingEntity()")
 
                outputEntity = swView.GetCorrespondingEntity(inputEntity)
 
                If outputEntity Is Nothing Then
                    Debug.Print("No corresponding entity found in the drawing view")
                Else
                    Debug.Print("Corresponding entity found....selecting in drawing")
                    swApp.ActivateDoc3(swModelDrawing.GetTitleFalseswRebuildOnActivation_e.swDontRebuildActiveDoc, lErr)
                    bSelected = outputEntity.Select4(FalseNothing)
                End If
 
            Case swSelectType_e.swSelNOTHING
 
            Case Else
 
                inputObject = selMgr.GetSelectedObject6(1, -1)
 
                Debug.Print("Using IView::GetCorresponding()")
 
                outputObject = swView.GetCorresponding(inputObject)
 
                If outputObject Is Nothing Then
                    Debug.Print("No corresponding object found in the drawing view")
                Else
                    Debug.Print("Corresponding object found in the drawing view")
                End If
        End Select
 
    End Sub
 
 
    Public Sub PartToView()
 
        docSpec = swApp.GetOpenDocSpec("C:\Users\Public\Documents\SOLIDWORKS\SOLIDWORKS 2018\samples\tutorial\api\clamp1.SLDPRT")
        swModelPart = swApp.OpenDoc7(docSpec)
 
        docSpec = swApp.GetOpenDocSpec("C:\Users\Public\Documents\SOLIDWORKS\SOLIDWORKS 2018\samples\tutorial\api\clamp1.SLDDRW")
        swModelDrawing = swApp.OpenDoc7(docSpec)
 
        swDrawing = swModelDrawing
 
        swApp.ActivateDoc3(swModelDrawing.GetTitleFalseswRebuildOnActivation_e.swDontRebuildActiveDoc, lErr)
 
        selMgr = swModelDrawing.SelectionManager
        swModelDrawing.ClearSelection2(True)
 
        Stop ' Select something in the drawing and press F5
 
        swView = swDrawing.FeatureByName("Drawing View1").GetSpecificFeature()
 
        Select Case selMgr.GetSelectedObjectType3(2, -1)
            Case swSelectType_e.swSelFACES, swSelectType_e.swSelEDGES, swSelectType_e.swSelVERTICES
 
                inputEntity = selMgr.GetSelectedObject6(2, -1)
                drComp = selMgr.GetSelectedObjectsComponent4(2, -1)
 
                Debug.Print("Using IModelDocExtension::GetCorrespondingEntity2()")
 
                outputEntity = swModelPart.Extension.GetCorrespondingEntity2(inputEntity)
 
                If outputEntity Is Nothing Then
                    Debug.Print("No corresponding entity found in the part")
                Else
                    Debug.Print("Corresponding entity found...selecting in part")
                    swApp.ActivateDoc3(swModelPart.GetTitleFalseswRebuildOnActivation_e.swDontRebuildActiveDoc, lErr)
                    bSelected = outputEntity.Select4(FalseNothing)
                End If
 
            Case swSelectType_e.swSelNOTHING
 
 
            Case Else
 
                inputObject = selMgr.GetSelectedObject6(2, -1)
                drComp = selMgr.GetSelectedObjectsComponent4(2, -1)
 
                Debug.Print("Using IModelDocExtension::GetCorresponding2()")
 
                outputObject = swModelPart.Extension.GetCorresponding2(inputObject)
 
                If outputObject Is Nothing Then
                    Debug.Print("No corresponding object found in the part")
                Else
                    Debug.Print("Corresponding object found in the part")
                End If
        End Select
 
 
    End Sub
 
    ''' <summary>
    ''' The SldWorks swApp variable is pre-assigned for you.
    ''' </summary>
    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

 
*Email:  
Subject:   Feedback on Help Topics
Page:   Get Corresponding Entities Between Parts and Drawing Views Example (VB.NET)
*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) 2018 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.