.NET

Reply
Valued Contributor
scarta
Posts: 52
Registered: ‎11-25-2008
Message 11 of 13 (95 Views)

Re: Alternative Attribute command

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

I try this but don't work...

<

CommandMethod("AddDocColEvent")> _

 

PublicSubAddDocColEvent()

 

Dim doc As Autodesk.AutoCAD.ApplicationServices.Document

doc = Autodesk.AutoCAD.ApplicationServices.

Application.DocumentManager.MdiActiveDocument

 

AddHandler doc.DocumentLockModeChanged, NewObjectEventHandler(AddressOfdocColDocAct)

 

EndSubPublicSub docColDocAct(ByVal sender AsObject, docColDocActEvtArgs AsDocumentLockModeChangedEventArgs)

ApplicationServices.

Application.ShowAlertDialog(docColDocActEvtArgs.Document.Name & _

 

" was activated.")

 

EndSub

End

Class

*Expert Elite*
norman.yuan
Posts: 1,021
Registered: ‎04-27-2009
Message 12 of 13 (93 Views)

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
                e.Veto()
            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.

 

HTH

Valued Contributor
scarta
Posts: 52
Registered: ‎11-25-2008
Message 13 of 13 (74 Views)

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
Need installation help?

Start with some of our most frequented solutions or visit the Installation and Licensing Forum to get help installing your software.