Valued Contributor
52 Posts
1 Kudo
Registered: ‎11-25-2008
Post 11 of 13

Re: Alternative Attribute command

05-31-2012 06:35 AM in reply to: scarta

I try this but don't work...


CommandMethod("AddDocColEvent")> _




Dim doc As Autodesk.AutoCAD.ApplicationServices.Document

doc = Autodesk.AutoCAD.ApplicationServices.



AddHandler doc.DocumentLockModeChanged, NewObjectEventHandler(AddressOfdocColDocAct)


EndSubPublicSub docColDocAct(ByVal sender AsObject, docColDocActEvtArgs AsDocumentLockModeChangedEventArgs)


Application.ShowAlertDialog(docColDocActEvtArgs.Document.Name & _


" was activated.")





*Expert Elite*
1,088 Posts
217 Kudos
Registered: ‎04-27-2009
Post 12 of 13

Re: Alternative Attribute command

05-31-2012 07:07 AM in reply to: scarta

Did you try to compile your code (or did get intellisense prompt when writing the code in VB editor)? Document class DOES NOT have event DocumentLockModeChanged event. So, I am wondering how you write your code without intellisense prompt and how you compile your code without error. It is DocumentCollection class that has event DocumentLockModeChanged/DocumentLockModeChangeVetoed event.


I put together some quick code, assuming that under certain codition, you do not want SAVE/QSAVE command run, instead, you run your own custom command. Here is the code:


Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.EditorInput

<Assembly: CommandClass(GetType(AlternativeCommandVB.AlternativeCmd))> 
<Assembly: ExtensionApplication(GetType(AlternativeCommandVB.AlternativeCmd))> 

Public Class AlternativeCmd
    Implements IExtensionApplication

    Private Shared docs As DocumentCollection = Nothing
    Private Shared _dwgName As String = ""
    Private Shared _runAltCmd As Boolean = False
    Private Const TARGET_CMD = "SAVE"

    Public Sub Initialize() _
            Implements Autodesk.AutoCAD.Runtime.IExtensionApplication.Initialize

        docs = Application.DocumentManager

        AddHandler docs.DocumentLockModeChanged, _
            AddressOf Docs_DocumentLockModeChanged

        AddHandler docs.DocumentLockModeChangeVetoed, _
            AddressOf Docs_DocumentLockModeChangeVetoed

    End Sub

    Public Sub Terminate() _
        Implements Autodesk.AutoCAD.Runtime.IExtensionApplication.Terminate

    End Sub

    Private Sub Docs_DocumentLockModeChanged( _
                sender As Object, e As DocumentLockModeChangedEventArgs)

        _dwgName = e.Document.Name
        _runAltCmd = False

        If e.GlobalCommandName.ToUpper().Contains(TARGET_CMD) Then
            If CheckVetoCondition(e.Document) Then
                _runAltCmd = True
            End If
        End If

    End Sub

    Private Sub Docs_DocumentLockModeChangeVetoed( _
                sender As Object, e As DocumentLockModeChangeVetoedEventArgs)

        If _runAltCmd AndAlso _dwgName.ToUpper() = e.Document.Name.ToUpper() Then

            e.Document.Editor.WriteMessage(vbCr & "Command """ & _
                                           TARGET_CMD & """ has been vetoed")
            e.Document.SendStringToExecute("MyAltCmd ", True, False, True)

        End If

    End Sub

    <CommandMethod("MyAltCmd")> _
    Public Shared Sub RunMyCustomCmd()

        Dim ed As Editor = Application.DocumentManager.MdiActiveDocument.Editor
        ed.WriteMessage(vbCr & _
                        "Execute my coomand, instead of ""Save"" command....Done" & vbCr)

    End Sub

    Private Function CheckVetoCondition(dwg As Document) As Boolean
        ''Do needed condition test
        Return True
    End Function

End Class

 Start AutoCAD and load this project. Then enter command SAVE or QSAVE, you would see from command line that the SAVE/QSAVE command is vetoed and the custom command "MyAltCmd" runs.



Valued Contributor
52 Posts
1 Kudo
Registered: ‎11-25-2008
Post 13 of 13

Re: Alternative Attribute command

06-04-2012 03:29 AM in reply to: scarta

Thanks now work correctly!!

Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Are you interested in helping shape the future of the Autodesk Community? To participate in this brief usability study, please click here. Your time and input is greatly appreciated!