8 Replies Latest reply on Feb 20, 2017 2:28 PM by Mattew Stafford

    Macro to convert the default "View Scale" format to "Architectural Scale format" for all drawing views

    Shaun Jalbert

      We have a standard block called "title_bubble" that we pull into our drawing views to label the view (see images below).

      Within that block is an attribute called "5 Scale". This attribute is linked to the view scale and the format is driven by the system defaults in the form of 1:4.

      On our Metric Drawings this format is fine; however, when working in Imperial Drawings our Clients expect to see an Architectural format in the form of 3"=1'-0".


      The math for converting the scale is simple - taking the 1:4 scale and converting it to a fraction of 1/4 and then multiplying it by 12" (1'-0" converted to inches) and the result is 3". This Resulted Value would then need to be put into a string such as X=1'-0" where X is the resulting value (3").


      Would it be possible to create a macro that:

      1) finds a block named "title_bubble"

      2) looks up the $PRPMODEL:"SW-View Scale(View Scale)" value that the block is attached to

      3) Converts the view-scale from a ratio to a fraction (ex. 1:4 converts to 1/4)

      4) applies the math (fraction * 12 = X)

      5) strings the result into the final format (ex "SCALE X=1'-0"")

      6) overwrites the value of the attribute named "5 Scale" with the final scale format from step 5

      7) Finds the next block and repeats steps 1-6, else ends.


      I'm unable to start the code myself, so I'm hoping someone could assist me with the development of this macro or even suggest an alternative solution.


      Thanks in advance.


        • Re: Macro to convert the default "View Scale" format to "Architectural Scale format" for all drawing views
          Shaun Jalbert

          I should also mention that step 4) that any decimal value should be formatted as a fraction.

          See examples below


          1/16" = 1'-0".0625" = 1'-0"
          3/32" = 1'-0".09375" = 1'-0"
          1/8" = 1'-0".125" = 1'-0"
          3/16" = 1'-0".1875" = 1'-0"
          1/4" = 1'-0".25" = 1'-0"
          3/8" = 1'-0".375" = 1'-0"
          1/2" = 1'-0".50" = 1'-0"
          3/4" = 1'-0".75" = 1'-0"
          1" = 1'-0"1" = 1'-0"
          1 1/2" = 1'-0"1.5" = 1'-0"
          3" = 1'-0" 3" = 1'-0"
            • Re: Macro to convert the default "View Scale" format to "Architectural Scale format" for all drawing views
              Shaun Jalbert

              Am I correct to assume that this link is the property that would permit me to resolve line 4 above?

              2015 SOLIDWORKS API Help - ScaleDecimal Property (IView)

              • Re: Macro to convert the default "View Scale" format to "Architectural Scale format" for all drawing views
                Elmar Klammer

                Try this..


                Select View then run macro. You get a note centered on view showing architectural scale annotation.

                I modified code posted by talented individuals. All credits go to original owner.


                Option Explicit

                Dim swApp As SldWorks.SldWorks
                Dim swModelDoc As SldWorks.ModelDoc2
                Dim swDrawing As SldWorks.DrawingDoc
                Dim swSelMgr As SldWorks.SelectionMgr
                Dim swView As SldWorks.View
                Dim swNote As SldWorks.Note
                Dim swAnnotation As SldWorks.Annotation
                Dim swSelection() As Object

                Dim ScaleRatio As Variant
                Dim ViewCorners As Variant
                Dim SelectionIndex As Long
                Dim Viewish As String

                Dim factor As Variant
                Dim ScaleFrac As String

                Sub main()

                    'attach to SolidWorks
                    Set swApp = Application.SldWorks
                    'get active document
                    Set swModelDoc = swApp.ActiveDoc
                    'check for document
                    If swModelDoc Is Nothing Then
                        MsgBox "No active document.     ", vbOKOnly + vbCritical, "Label View Scale"
                        Exit Sub
                    End If
                    'check if document is drawing
                    If swModelDoc.GetType = swDocDRAWING Then
                        Set swDrawing = swModelDoc
                        MsgBox "Active document must be drawing.    ", vbOKOnly + vbCritical, "Label View Scale"
                        Exit Sub
                    End If
                    'get selection manager
                    Set swSelMgr = swModelDoc.SelectionManager
                    'check if anything selected
                    If swSelMgr.GetSelectedObjectCount = 0 Then
                        MsgBox "No views selected.    ", vbOKOnly + vbCritical, "Label View Scale"
                        Exit Sub
                        'save selections
                        ReDim swSelection(1 To swSelMgr.GetSelectedObjectCount) As Object
                        'check if selection is drawing view
                        For SelectionIndex = 1 To swSelMgr.GetSelectedObjectCount
                            If swSelMgr.GetSelectedObjectType2(SelectionIndex) = swSelDRAWINGVIEWS Then
                                Set swSelection(SelectionIndex) = swSelMgr.GetSelectedObject5(SelectionIndex)
                            End If

                    End If
                    'analize selections
                    For SelectionIndex = 1 To swSelMgr.GetSelectedObjectCount
                        'check if selected object is view
                        If swSelection(SelectionIndex) Is Nothing Then
                            MsgBox "Selected object " & SelectionIndex & " is not a drawing view.     ", vbOKOnly + vbExclamation, "Label View Scale"
                            'get selected view
                            Set swView = swSelection(SelectionIndex)
                            'get view scale
                            ScaleRatio = swView.ScaleRatio
                            'get view corner locations
                            ViewCorners = swView.GetOutline
                            'lock view focus so note will move if corresponding view moved
                            swView.FocusLocked = True
                            If ScaleRatio(1) >= 12 Then
                            factor = ScaleRatio(1) / 12
                            ScaleFrac = Dec2Fract(ScaleRatio(0) / factor)
                            factor = 12 / ScaleRatio(1)
                            ScaleFrac = Dec2Fract(ScaleRatio(0) * factor)
                            End If
                            Debug.Print Trim(ScaleFrac & """")
                            'create note
                            'Set swNote = swDrawing.InsertNote("SCALE: " & ScaleRatio(0) & ":" & ScaleRatio(1))
                            Set swNote = swDrawing.InsertNote("ARCHITECTUAL SCALE: " & Trim(ScaleFrac & """") & " = 1' - 0")
                            'set note angle
                            swNote.Angle = 0
                            'set note to center justification
                            swNote.SetTextJustification (swTextJustificationCenter)
                            'set note is balloon to no
                            swNote.SetBalloon 0, 0
                            'get note annotation object
                            Set swAnnotation = swNote.GetAnnotation
                            'set note leader to none
                            swAnnotation.SetLeader2 False, 0, True, False, False, False
                            'locate note center and 5mm below from bottom edge of view bounding box
                            Viewish = (ViewCorners(0) + ViewCorners(2)) / 2
                            swAnnotation.SetPosition Viewish, ViewCorners(1) - 0.005, 0
                            'unlock  view focus
                            swView.FocusLocked = False

                        End If
                    'refresh graphics screen
                End Sub

                Function Dec2Fract(X As Single) As String
                   ' Returns a string of a number rounded to a whole and
                   'fraction in 16ths, 32nds, etc.
                   ' Handles number > 1 and less than 0
                   ' Use as follows:
                   ' Label1.Caption = Dec2Fract(Val(Text1.Text))
                   ' or Label1.Caption = Dec2Fract(Val(Label1.Caption)
                   Dim F As String, Y As Single, Num As Integer, Den As Integer
                   Den = 16  'Denominator: can be set to 8, 16, 32, 64 etc
                   If X = 0 Then
                      Dec2Fract = "0"
                      Exit Function
                      Y = Abs(X)
                      If Y > 1 Then Y = Y - Int(Y)  ' get fractional part
                      Num = CInt(Den * Y)
                      If Num = Den Then
                         F = "1"
                      ElseIf Num = 0 Then
                         If Abs(X) < 1 Then F = "0" Else F = ""
                         Do Until Num Mod 2 <> 0
                            Num = Num / 2
                            Den = Den / 2
                         F = LTrim$(Str$(Num)) + "/" + LTrim$(Str$(Den))
                      End If
                      If Abs(X) > 1 Then
                         If F <> "1" Then
                            F = Trim$(Str$(Fix(X))) + " " + F
                            F = Trim$(Str$(CInt(X)))
                         End If
                      End If
                      If X < 0 And X > -1 Then F = "-" + F
                      Dec2Fract = F
                   End If
                End Function

              • Re: Macro to convert the default "View Scale" format to "Architectural Scale format" for all drawing views
                Mattew Stafford

                This code looks like what I need, but how do I use it?  I experimented by copying all of those lines, entering the "new macro" command & saving it on a network drive.  Then I did an "edit macro" and pasted the code in there. (not sure if I placed it in the right spot).  I then saved it.  Then I selected "run macro" picked the macro I saved and it didn't do anything.  I must be missing something.