Lock/Unlock drawing views in VBA


Locking and unlocking of drawing views is a common task in CATIA V5. To save a time you can automate this operation with VBA macro. Following procedure checks the lock status of the first view in a CATIA V5 drawing and then sets all views to the opposite.


Option Explicit

Sub LockUnlockViews()
    Dim drwViews As DrawingViews
    Set drwViews = CATIA.ActiveDocument.Sheets.ActiveSheet.Views
    
    ' have to be defined as Variant, Boolean is not working
    ' because of CATIA vs VBA boolean incompatibility
    Dim myLockStatus As Variant
    myLockStatus = drwViews.Item(3).LockStatus
    
    Dim i As Integer
    Dim myPrefix As String, myIdent As String, mySuffix As String

    ' loop starts from 3 because the first two views are CATIA
    ' automatically generated views Main View, Background View
    For i = 3 To drwViews.Count
        drwViews.Item(i).LockStatus = Not myLockStatus

'        Debug.Print drwViews.Item(i).Name
    Next
End Sub
    

The code is simple and self-explanatory, there are however two things to notice:

1. Variable myLockStatus is declared as Variant. Had it been a Boolean, it wouldn't have worked properly. A reason is incompatibility of the Boolean type between VBA and V5 Automation methods. This is described in V5Automation.chm help file in a chapter About VB, VBA, Debug, and Portability.

There is a known limitation concerning the usage of the Boolean type in the V5 Automation methods invoked from VBA. In V5 applications, the Boolean type is defined as an 'unsigned char' where the VBA definition is a short. When a V5 method returns True, the returned integer value is 1, though VBA is expecting -1.
...
Note that this limitation is specific to VBA and is not concerning VBScript. V5Automation.chm

2. A loop starts at 3 because the first two views are automatically generated views named Main View and Background View. Custom views therefore start at index 3.

To test it, put the code shown above into a standard module.




Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>