Get Corresponding Note in Assembly Example (VBA)
This example shows how to determine if the selected note in an assembly
corresponds to the selected note in a part that is a component of the
assembly.
'---------------------------------------
'
' Preconditions:
' (1)
An assembly document named BlockSpherePyramid.SLDASM is open.
' (2)
Note1 exists and belongs to the part document named
' Block.SLDPRT,
which also exists and is a component of
' BlockSpherePyramid.SDLASM.
' (3)
Block.SLDPRT has a Note1 note.
'
' Postconditions: None
'
'---------------------------------------
Option Explicit
Dim swApp As SldWorks.SldWorks
Dim swAssembly As SldWorks.AssemblyDoc
Dim swPart As SldWorks.PartDoc
Dim swModel As SldWorks.ModelDoc2
Dim swCompModel As SldWorks.ModelDoc2
Dim swSelMgr As SldWorks.SelectionMgr
Dim swModelDocExt As SldWorks.ModelDocExtension
Dim swCompModelDocExt As SldWorks.ModelDocExtension
Dim swAnnotation As SldWorks.Annotation
Dim swAssemComp As SldWorks.Component2
Dim swAssemNoteCorresp As SldWorks.note
Dim swAssyNote As SldWorks.note
Dim swPartNote As SldWorks.note
Dim boolstatus As Boolean
Dim nRetVal As Long
Sub main()
Set
swApp = Application.SldWorks
Set
swModel = swApp.ActiveDoc
Set
swAssembly = swModel
Set
swSelMgr = swModel.SelectionManager
Stop
'
Interactively select the note in the assembly,
'
then programatically get the note
Set
swAssyNote = swSelMgr.GetSelectedObject6(1,
0)
swModel.ClearSelection2 True
Set
swAnnotation = swAssyNote.GetAnnotation
boolstatus
= swAnnotation.Select3(False,
Nothing)
'
Activate the part document
Set
swPart = swApp.ActivateDoc2("Block.SLDPRT",
True, nRetVal)
Set
swModelDocExt = swModel.Extension
Set
swSelMgr = swModel.SelectionManager
Debug.Assert
0 = nRetVal
swModel.ClearSelection2 True
'Select
the note in the block
Stop
'
Interactively select the note in the block
'
then programatically get the note
Set
swPartNote = swSelMgr.GetSelectedObject6(1,
0)
swModel.ClearSelection2 True
Set
swAnnotation = swPartNote.GetAnnotation
boolstatus
= swAnnotation.Select3(False,
Nothing)
'
Re-activate the assembly
Set
swAssembly = swApp.ActivateDoc2("BlockSpherePyramid.SLDASM",
True, nRetVal)
Debug.Assert
0 = nRetVal
Set
swModelDocExt = swModel.Extension
Set
swSelMgr = swModel.SelectionManager
swModel.ClearSelection2 True
'
Select the Block component in the assembly
boolstatus
= swModelDocExt.SelectByID2("Block-1@BlockSpherePyramid",
"COMPONENT", 0, 0, 0, False, 0, Nothing, 0)
Set
swAssemComp = swSelMgr.GetSelectedObject6(1,
0)
Set
swCompModel = swAssemComp.GetModelDoc
Set
swCompModelDocExt = swCompModel.Extension
swModel.ClearSelection2 True
'
Determine if the selected notes are the same note
Set
swAssemNoteCorresp = swCompModelDocExt.GetCorresponding(swPartNote)
Dim
strOutput As String
'
Test feature returned by swCompMondelDocExt::GetCorrespondingEntity
If
swAssemNoteCorresp Is Nothing Then
strOutput
= "Note not obtained."
Else
If
TypeOf swAssemNoteCorresp Is SldWorks.note Then
Set
swAnnotation = swAssemNoteCorresp.GetAnnotation
boolstatus
= swAnnotation.Select3(False,
Nothing)
Debug.Print
swAssemNoteCorresp.GetText
'If
swAssemNoteCorresp Is swAssyNote then
'the
pointers are different, but they point to the same note
If
swAssemNoteCorresp.GetText = "Note1"
Then
strOutput
= "Note obtained correctly."
Else
strOutput
= "swAssemNoteCorresp Is not swAssyNote."
End
If
End
If
End
If
strOutput
= strOutput + vbNewLine
swModel.ClearSelection2 True
strOutput
= strOutput + vbNewLine
MsgBox
strOutput
End Sub