Set drawing scale (from List) using VBA

Set drawing scale (from List) using VBA

andrew_canfield
Collaborator Collaborator
613 Views
2 Replies
Message 1 of 3

Set drawing scale (from List) using VBA

andrew_canfield
Collaborator
Collaborator

Inspired by:

https://clintbrown.co.uk/2018/10/07/automatic-drawings-with-ilogic/

Due struggling to work on multiple drawings (documents) & knowing where the focus is I've stepped out of iLogic into vba - a whole new world!

 

The question is - how can something like this (iLogic) run as vba?

'Scale the Drawing - Note your drawing views names("VIEW1")&("VIEW4") must match the template
On Error GoTo Exiter
oMyParameter = ThisDrawing.Document.Parameters.UserParameters
oParameter = oMyParameter.AddByValue("Scaler", "1:5", UnitsTypeEnum.kTextUnits)
MultiValue.SetList("Scaler","1:1", "1:2", "1:4", "1:5", "1:10", "1:20", "1:25", "1:50", "1:100")

Scaler = InputListBox("Set Drawing Scale", MultiValue.List("Scaler"), Scaler, Title:="Scale = " & ActiveSheet.View("VIEW1").ScaleString, ListName:="List")
ActiveSheet.View("VIEW1").ScaleString = Scaler
ActiveSheet.View("VIEW4").ScaleString = Scaler
Parameter.Param("Scaler").Delete

Exiter:

 

Regards

 

Andrew

0 Likes
614 Views
2 Replies
Replies (2)
Message 2 of 3

WCrihfield
Mentor
Mentor

Hi @andrew_canfield.  If I were you, I would actually be moving away from VBA, at least for right now, because it has some serious security problems, which is why Autodesk stopped including the VBA editor in any of their Inventor installs starting in 2021 (Link).  Also, it uses a much older coding system than the vb.net system that iLogic uses.  The 'InputListBox' is not available in VBA, so if that is critical to this process, you may want to stick to iLogic for this one.  The alternatives are to use a regular 'InputBox' or design a VBA UserForm that includes something like a ListBox control for use in these types of cases, and call that when you need something like that.

 

Anyways, here is the VBA version of your iLogic code, except for the InputListBox part, of course.

Sub SetDrawingScaleFromList() 'Scale the Drawing - Note your drawing views names("VIEW1")&("VIEW4") must match the template
    If ThisApplication.ActiveDocumentType <> kDrawingDocumentObject Then Exit Sub
    Dim oDDoc As DrawingDocument
    Set oDDoc = ThisApplication.ActiveDocument
    Dim oUParams As UserParameters
    Set oUParams = oDDoc.Parameters.UserParameters
    Dim oUParam As UserParameter
    On Error Resume Next 'pause normal error handling
    Set oUParam = oUParams.Item("Scaler")
    If Err <> 0 Then
        Err.Clear
        Set oUParam = oUParams.AddByValue("Scaler", "1:5", UnitsTypeEnum.kTextUnits)
        If Err <> 0 Then Exit Sub
    End If
    On Error GoTo 0 'resume normal error handling
    'On Error GoTo Exiter
    Dim oScales() As String
    oScales = Array("1:1", "1:2", "1:4", "1:5", "1:10", "1:20", "1:25", "1:50", "1:100")
    oUParam.ExpressionList.SetExpressionList (oScales)
    'there is no 'InputListBox' in VBA, so you would have to either:
    '- use a regular 'InputBox', which can not show a list to select from
    '- or create a VBA UserForm with a ListBox in it, then call that to show the list of options
    Dim Scaler As String
    'Scaler = InputListBox("Set Drawing Scale", MultiValue.List("Scaler"), Scaler, Title:="Scale = " & ActiveSheet.View("VIEW1").ScaleString, ListName:="List")
    Scaler = InputBox("Set Drawing Scale", "Set Drawing Scale", "1:10")
    Dim oViews As DrawingViews
    Set oViews = oDDoc.ActiveSheet.DrawingViews
    Dim oView As DrawingView
    For Each oView In oViews
        If oView.Name = "VIEW1" Then
            oView.ScaleString = Scaler
        ElseIf oView.Name = "VIEW4" Then
            oView.ScaleString = Scaler
        End If
    Next
    oUParam.Delete
Exiter:
End Sub

If this solved your problem, or answered your question, please click ACCEPT SOLUTION .
Or, if this helped you, please click (LIKE or KUDOS) 👍.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 3 of 3

andrew_canfield
Collaborator
Collaborator

Many thanks again, although, what I thought was a working solution needs to be moved to Visual Studio (I guess).

The upgrade to 2022 is imminent.

More reading up to do..

Regards

 

Andrew

0 Likes