1 Reply Latest reply on Oct 21, 2014 1:54 AM by Andreas Killer

    Next view label in api

    Joe Reneker

      I'm writing a macro to create a drawing document with several detail views and section views. I'd like the macro to automatically assign the next view label to any created view, much like the normal user interface does. The information I need is available in the "Sheet Properties" (on sheet RMB -> Properties) window shown below. Does anyone know how to retrieve the "Next view label"?


      I've gotten CreateSectionViewAt5 to automatically assign the next view label by passing "Nothing" to the SectionLabel argument. In VB.NET, my code looks like:

      sectionView = swDrawing.CreateSectionViewAt5(0, 0, 0, Nothing, SwConst.swCreateSectionViewAtOptions_e.swCreateSectionView_DisplaySurfaceCut, Nothing, 0)

      If the "Next view label" in the Sheet Properties window was V, then this code creates a view named "Section View V-V".


      CreateDetailViewAt3 doesn't have the same behavior. Passing "Nothing" or an empty string simply creates a detail view named "Detail View ".

      detailView = swDrawing.CreateDetailViewAt3(0, 0, 0, SwConst.swDetViewStyle_e.swDetViewSTANDARD, 4, 1, Nothing, SwConst.swDetCircleShowType_e.swDetCircleCIRCLE, True)


      I could simply add a user input for each detail view, but that's fairly clunky. Any insight on the problem would be helpful.



        • Re: Next view label in api
          Andreas Killer

          I can not find a property in the view object that returns the label name, but IMHO it is possible to parse it from the view name.


          A label is just a string, usually from A to Z, but also "qwe asd" is possible. Furthermore it is possible that "B" and "C" is used and "A" is not used in the drawing.


          So I suggest to retrieve all label names and search for an unused name. See the code below




          Option Explicit


          Sub Main()
            Dim swApp  As SldWorks.SldWorks
            Dim Temp, i As Long
            Dim AllLabels As String


            'Get all labels
            Set swApp = Application.SldWorks
            Temp = GetLabels(swApp.ActiveDoc)
            'Show them
            For i = LBound(Temp) To UBound(Temp)
              Debug.Print "Label: " & Temp(i)
            'All labels in one string with delimiters
            AllLabels = "," & Join(Temp, ",") & ","
            'Search all from A to Z
            For i = Asc("A") To Asc("Z")
              If InStr(1, AllLabels, "," & Chr(i) & ",", vbTextCompare) = 0 Then
                Debug.Print "Next view label: " & Chr(i)
                Exit For
              End If
          End Sub


          Function GetLabels(ByVal swDraw As SldWorks.DrawingDoc, Optional ByRef Count As Long) As Variant
            'Return a sorted array with all view labels in this drawing
            Dim swView As SldWorks.View
            Dim i As Long, j As Long
            Dim Temp, Labels
            'Assume failure
            Labels = Array()
            Count = 0
            If swDraw Is Nothing Then GoTo ExitPoint
            Set swView = swDraw.GetFirstView
            If swView Is Nothing Then GoTo ExitPoint
            ReDim Labels(1 To swDraw.GetViewCount)
            Do While Not swView Is Nothing
            'Section view?
              If swView.Type = swDrawingViewTypes_e.swDrawingSectionView Then
                'Remove the first word
                Temp = Mid(swView.GetName2, InStr(swView.GetName2, " ") + 1)
                'Store the view label
                Count = Count + 1
                Labels(Count) = Split(Temp, "-")(0)
              End If
              Set swView = swView.GetNextView
            If Count = 0 Then GoTo ExitPoint
            'Remove empty slots
            ReDim Preserve Labels(1 To Count)
            If Count = 1 Then GoTo ExitPoint
            'Sort the array
            For i = LBound(Labels) + 1 To UBound(Labels)
              Temp = Labels(i)
              For j = i - 1 To LBound(Labels) Step -1
                If Labels(j) <= Temp Then Exit For
                Labels(j + 1) = Labels(j)
              Labels(j + 1) = Temp
            GetLabels = Labels
          End Function