Question

I've been trying to resurrect an existing drawing check macro, and want to find the co-ordinates of any note blocks on each sheet. I've been modifying code found here using the GetAttachPos method from this page, but for some reason any co-ordinates returned come back around (8.80942311664557E-03,2.24429295226372E-03).

I'm thinking that the problem is that I've missed a reference somewhere, but I'm not sure where. Although it's definitely finding the notes since it passes back their text. Anyway, here's the method I'm testing at the moment:

Sub Main()

Dim swApp As SldWorks.SldWorks
Set swApp = CreateObject("SldWorks.Application")

Dim NoteNumbersText As String
Dim NoteText As String


Dim NumberofSheets As Integer                   ' The number of sheets in this drawing
Dim NamesOfSheets As Variant                    ' Names of all of the sheets
Dim sheet As SldWorks.sheet                     ' The Sheet that we are working on
Dim LocalView As SldWorks.View                  ' Current View that we are looking at
Dim LocalNote As SldWorks.Note                  ' Current Note that we are looking at

Dim TextFormat As SldWorks.TextFormat           ' Current text format object of a note
Dim Xpos As Double                              ' X, Y Z position on the drawing in Metres
Dim Ypos As Double
Dim SizeOfSheet As Double

Dim x As Integer                                ' general Loop Variables
Dim j As Integer
Dim k As Integer
Dim l As Integer

Dim vPosition As Variant

Dim vNote As Variant                            ' Single note
Dim swNote As SldWorks.Note                     ' Single Solidworks Note Object

Dim ThisAnnotation As SldWorks.Annotation
Dim swBlockInst As SldWorks.SketchBlockInstance
Dim swBlockDef As SldWorks.SketchBlockDefinition

Dim NumofNotes As Integer
Dim ArrayOfNotes() As NoteInfo

Dim LocalDrawingDoc As SldWorks.DrawingDoc        ' Declared as an Object so that non Drawings can be detected!
Dim LocalPart As SldWorks.ModelDoc2 ' Declared as an Object so that non Drawings can be detected!

Dim strShtProp As Variant

Set LocalDrawingDoc = swApp.ActiveDoc
Set LocalPart = swApp.ActiveDoc
ReDim ArrayOfNotes(0)
' Get the sheet names and the number of them
NamesOfSheets = LocalDrawingDoc.GetSheetNames()
NumberofSheets = LocalDrawingDoc.GetSheetCount

' store this sheet name
Set sheet = LocalDrawingDoc.GetCurrentSheet()
strShtProp = sheet.GetProperties() ' get the sheet properties use much later for converting position into ref
SizeOfSheet = strShtProp(5)

Dim SwSketchMgr As SldWorks.SketchManager
Set SwSketchMgr = LocalDrawingDoc.SketchManager

Dim i As Integer
Dim vBlockDef As Variant
Dim vBlockInst As Variant

Dim strReturn As String

'    Dim bret As Boolean

vBlockDef = SwSketchMgr.GetSketchBlockDefinitions

For x = NumberofSheets - 1 To 0 Step -1

    If LocalDrawingDoc.GetCurrentSheet.GetName <> NamesOfSheets(x) Then LocalDrawingDoc.ActivateSheet NamesOfSheets(x)

        Set LocalView = LocalDrawingDoc.GetFirstView
        While Not LocalView Is Nothing

            If Not IsEmpty(vBlockDef) Then
                For i = 0 To UBound(vBlockDef)
                    Set swBlockDef = vBlockDef(i)

                    vBlockInst = swBlockDef.GetInstances
                    vNote = swBlockDef.GetNotes

                    If Not IsEmpty(vNote) Then

                        For j = 0 To UBound(vNote)
                            Set swNote = vNote(j)

                            NoteNumbersText = Trim(swNote.GetText)

                            If Left(NoteNumbersText, 1) = "n" And Len(NoteNumbersText) > 1 And Len(NoteNumbersText) < 4 Then
                                Set ThisAnnotation = swNote.GetAnnotation
                                'vPosition = swNote.GetAttachPos
                                vPosition = ThisAnnotation.GetPosition
                                Xpos = vPosition(0)
                                Ypos = vPosition(1)

                                Debug.Print ("Note " & NoteNumbersText & ": " & Xpos & "," & Ypos)


                            End If

                        Next j
                    End If
                Next i
           End If

        Set LocalView = LocalView.GetNextView
        Wend



Next x

End Sub
Was it helpful?

Solution

Turns out that SolidWorks is set up to return positions of blocks relative to the drawing view on which they're placed. Calling GetXForm for the view which they are placed on then provides a way of calculating the absolute position of each note.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top