MACRO LIBRARY

Get polylines information

Free public access. No account or subscription required.

Files

Get polylines information

Code

'Heavily simplified version of "Get Polylines Information Example (VBA)" from API _
    Help. This macro is an exploration of how the array returned by _
    IView::GetPolyLines7 works so that the most useful data can be pulled from it.
    
'Modified by Keith Rice
'CADSharp LLC
'www.cadsharp.com
    
'Preconditions: Drawing document is open and contains drawing views.
'Postconditions: See immediate window.
    
'The array described in IView::GetPolyLines7 describes one set for EACH element found _
    in the view. Hence a side view of a cylinder contains 2 poly lines and 2 arcs, so _
    there are four sets of those array values. All of these values are bunched up _
    against one another, however, so the location of the index must constatntly be _
    monitored. This is done below using iBufferIdx.
    
Option Explicit
    
Sub main()
    Dim swApp                 As Object
    Dim swModel               As SldWorks.ModelDoc2
    Dim swDrawing             As SldWorks.DrawingDoc
    Dim swView                As SldWorks.View
    Dim swSheetView           As SldWorks.View
    
    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    Set swDrawing = swModel
    
    'First view in a sheet is the sheet itself
    Set swSheetView = swDrawing.GetFirstView
    'Cycle through actual drawing views
    Set swView = swSheetView.GetNextView
    Do While Not swView Is Nothing
        GetPolyLineData swView
        Set swView = swView.GetNextView
    Loop
End Sub
    
Function GetPolyLineData(swView As SldWorks.View) As Boolean
    Dim vPolyLinesBuffer       As Variant
    Dim vLines                 As Variant
    Dim lNumGeomData           As Long
    Dim iGeomIndex             As Integer
    Dim lNumLines              As Long
    Dim lItemType              As Long
    Dim lBufferSize            As Long
    Dim lBufferIdx             As Long
    Dim lGeomDataSize          As Long
    Dim dGeomData(11)          As Double
    Dim lLineData(3)           As Long
    Dim lLayerData(1)          As Long
    Dim lNumPoints             As Long
    Dim dPoint(2)              As Double
    Dim lGeomDataIdx           As Long
    Dim lLineDataIdx           As Long
    Dim lLayerDataIdx          As Long
    Dim lStartIdx              As Long
    Dim lEndIdx                As Long
    Dim lLineDataSize          As Long
    Dim lLayerDataSize         As Long
    Dim lNumProjectedElements  As Long
    
    ' Entries for line data and layer data have a fixed size
    lLineDataSize = 4
    lLayerDataSize = 2
    ' Initialize the number of projected elements for data is contained in the buffer
    lNumProjectedElements = 0
    
    Debug.Print "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
    Debug.Print "View name = " + swView.GetName2
    
    'Get polyline data without cross-hatches
    swView.GetPolylines7 1, vPolyLinesBuffer
    
    If Not IsEmpty(vPolyLinesBuffer) Then
        lBufferSize = UBound(vPolyLinesBuffer) + 1
        lBufferIdx = 0
    
        While lBufferIdx < lBufferSize
            'count number of entities
            lNumProjectedElements = lNumProjectedElements + 1
        
            '1. Type [size = 1]
            lItemType = vPolyLinesBuffer(lBufferIdx)
            lBufferIdx = lBufferIdx + 1
        
            '2. GeomDataSize [size = 1 for poly-lines, 12 for others]
            If lItemType = 0 Then
                Debug.Print "    polyline"
                'GeomDataSize should be zero, consume it by bumping counter up one
                lGeomDataSize = vPolyLinesBuffer(lBufferIdx)
                lBufferIdx = lBufferIdx + 1
            Else
                Debug.Print "    arc"
                ' - next to the piecewise linear approximation of the projected model geometry,
                '   the buffer also contains the arc defining the model geometry projection
    
                lGeomDataSize = vPolyLinesBuffer(lBufferIdx)
                lBufferIdx = lBufferIdx + 1
                
                '3. GeomData; create new array (dGeomData) of size 12
                lGeomDataIdx = 0
                lStartIdx = lBufferIdx  'the size 12 index starts at the current location _
                    in vPolyLinesBuffer; hence no sub-array
                lEndIdx = lStartIdx + (lGeomDataSize - 1)
                For lBufferIdx = lStartIdx To lEndIdx
                    dGeomData(lGeomDataIdx) = vPolyLinesBuffer(lBufferIdx)
                    lGeomDataIdx = lGeomDataIdx + 1
                Next lBufferIdx
                
                Debug.Print "      center pt = (" & dGeomData(0) * 1000# & ", " & dGeomData(1) * 1000# & ", " & dGeomData(2) * 1000# & ") mm"
                Debug.Print "      start  pt = (" & dGeomData(3) * 1000# & ", " & dGeomData(4) * 1000# & ", " & dGeomData(5) * 1000# & ") mm"
                Debug.Print "      end    pt = (" & dGeomData(6) * 1000# & ", " & dGeomData(7) * 1000# & ", " & dGeomData(8) * 1000# & ") mm"
                Debug.Print "      normal    = (" & dGeomData(9) & ", " & dGeomData(10) & ", " & dGeomData(11) & ")"
            End If
            
            '4. Get line data (size = 4; LineColor, LineStyle, LineFont, LineWeight)
            lLineDataIdx = 0
            lStartIdx = lBufferIdx
            lEndIdx = lStartIdx + (lLineDataSize - 1)
                
            For lBufferIdx = lStartIdx To lEndIdx
                lLineData(lLineDataIdx) = vPolyLinesBuffer(lBufferIdx)
                lLineDataIdx = lLineDataIdx + 1
            Next lBufferIdx
                
            '5. Get layer data (size = 2; LayerID, LayerOverride)
            lLayerDataIdx = 0
            lStartIdx = lBufferIdx
            lEndIdx = lStartIdx + (lLayerDataSize - 1)
    
            For lBufferIdx = lStartIdx To lEndIdx
                lLayerData(lLayerDataIdx) = vPolyLinesBuffer(lBufferIdx)
                lLayerDataIdx = lLayerDataIdx + 1
            Next lBufferIdx
        
            '6. Get number of poly points (size = 1)
            lNumPoints = vPolyLinesBuffer(lBufferIdx)
            Debug.Print "        points = " & CStr(lNumPoints)
            lBufferIdx = lBufferIdx + 1
            
            '7. Get point data (size = 3 * number of poly points)
            lStartIdx = lBufferIdx
            lEndIdx = lStartIdx + lNumPoints * 3 - 1
            
            For lBufferIdx = lStartIdx To lEndIdx Step 3
                dPoint(0) = vPolyLinesBuffer(lBufferIdx)
                dPoint(1) = vPolyLinesBuffer(lBufferIdx + 1)
                dPoint(2) = vPolyLinesBuffer(lBufferIdx + 2)
            Next lBufferIdx
        Wend
    End If
    Debug.Print "    Number of projected elements: " & lNumProjectedElements
End Function