.NET

Reply
Active Contributor
cadmaxza
Posts: 36
Registered: ‎06-23-2004
Message 1 of 10 (188 Views)

CommandEnded Event

188 Views, 9 Replies
01-27-2006 12:18 AM
I need to write a program that performs actions at the end of certain commands. To this end I have written the following code. The code should at least message "Kilroy was here" but it doesnt (although, what I really need is name of the command that just ended).
What is missing?
Thx.
Kevin.


Imports System
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.EditorInput
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.GraphicsInterface

Public Class Class1
Implements Autodesk.AutoCAD.Runtime.IExtensionApplication

Private CurrentDoc As Document

Private Shared Sub m_doc_Commandended(ByVal sender As System.Object, _
ByVal e As Autodesk.AutoCAD.ApplicationServices.CommandEventArgs)
Dim curdb As Database = Application.DocumentManager.MdiActiveDocument.Database
Dim tm As Autodesk.AutoCAD.DatabaseServices.TransactionManager = curdb.TransactionManager
Dim myT As Transaction = tm.StartTransaction()
MsgBox("Kilroy was here")
Try
MsgBox(sender.ToString)
Catch ex As Exception
myT.Abort()
MsgBox(ex.Message.ToString)
Finally
myT.Dispose()
End Try
End Sub
Valued Contributor
fmarcelino
Posts: 61
Registered: ‎10-25-2005
Message 2 of 10 (188 Views)

Re: CommandEnded Event

01-27-2006 07:17 AM in reply to: cadmaxza
Hi cadmaxza,

try this:

Imports System
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.EditorInput

Public Class ManageDoc

Private CurrentDoc As Document

Public Sub New()
CurrentDoc = Application.DocumentManager.MdiActiveDocument
AddHandler CurrentDoc.CommandEnded, AddressOf m_doc_CommandEnded
End Sub

Private Shared Sub m_doc_CommandEnded(ByVal sender As System.Object, _
ByVal e As Autodesk.AutoCAD.ApplicationServices.CommandEventArgs)
If e.GlobalCommandName = "LAYER" Then
MsgBox("Kilroy was here")
Else
MsgBox(sender.ToString)
End If
End Sub

If you need any help about documents justa ask.

Regards,
Filipe Marcelino
Active Contributor
cadmaxza
Posts: 36
Registered: ‎06-23-2004
Message 3 of 10 (188 Views)

Re: CommandEnded Event

01-30-2006 01:42 AM in reply to: cadmaxza
Thanks Filipe!!!

Next step. This functions on the drawing that was active when the NETLOAD was executed only. If I open another drawing, the function is not available in the other drawing. Is there maybe an AutoCAD setting? How do I enable this function for every drawing that is opened?

Thanks again,

Kevin.
Valued Contributor
fmarcelino
Posts: 61
Registered: ‎10-25-2005
Message 4 of 10 (188 Views)

Re: CommandEnded Event

01-30-2006 02:18 AM in reply to: cadmaxza
Hi cadmaxza.

What your asking is something more complex, but I already done it. This is your new class:

Imports System
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.EditorInput

Public Class ManageDoc

Private Shared WithEvents m_docMan As DocumentCollection
Private Shared m_doc As Document
Private Shared m_docsTable As Hashtable

Public Sub New()
m_Doc = Application.DocumentManager.MdiActiveDocument
DocumentEvents()
End Sub

Private Shared Sub m_doc_CommandEnded(ByVal sender As System.Object, ByVal e As Autodesk.AutoCAD.ApplicationServices.CommandEventArgs)
If e.GlobalCommandName = "LAYER" Then
MsgBox("Kilroy was here")
Else
MsgBox(sender.ToString)
End If
End Sub

Private Shared Sub DocumentEvents()
m_docsTable = New Hashtable
collectAllDocs()
m_docMan = Application.DocumentManager
End Sub

Private Shared Sub collectAllDocs()
Try
Dim docEnum As IEnumerator = Application.DocumentManager.GetEnumerator()
While docEnum.MoveNext()
Dim doc As Document = CType(docEnum.Current, Document)
addDoc(doc)
End While
Catch ex As System.Exception
MessageBox.Show(ex.Message + vbCrLf + vbCrLf + ex.StackTrace)
End Try
End Sub

Private Shared Sub addDoc(ByRef doc As Document)
If Not m_docsTable.ContainsKey(doc) Then
AddHandler doc.CommandEnded, AddressOf m_doc_CommandEnded
m_docsTable.Add(doc, True)
End If
End Sub

Private Shared Sub removeDoc(ByRef doc As Document)
If m_docsTable.ContainsKey(doc) Then
UndoADoc(doc)
m_docsTable.Remove(doc)
End If
End Sub

Private Shared Sub UndoADoc(ByRef doc As Document)
Try
If Not m_docsTable.Contains(doc) Then
Return
End If
m_doc = doc
RemoveHandler m_doc.CommandEnded, AddressOf m_doc_CommandEnded
Catch ex As System.Exception 'Caso ocorra alguma erro mostra uma mensagem de erro
MessageBox.Show(ex.Message + vbCrLf + vbCrLf + ex.StackTrace)
End Try
End Sub

Private Shared Sub m_docMan_DocumentToBeDestroyed(ByVal sender As Object, ByVal e As Autodesk.AutoCAD.ApplicationServices.DocumentCollectionEventArgs) Handles m_docMan.DocumentToBeDestroyed
removeDoc(e.Document)
End Sub

Private Shared Sub m_docMan_DocumentCreated(ByVal sender As Object, ByVal e As Autodesk.AutoCAD.ApplicationServices.DocumentCollectionEventArgs) Handles m_docMan.DocumentCreated
m_docMan = Application.DocumentManager
addDoc(e.Document)
End Sub

End Class

I've done this class taking ideias from the sample given by the sdk (EventsWatcher). If you need some kind of explanation of how it works just ask. I will not explain the class right now because I think It's easy to understand how it works, and it's ready to use. Anyway, if you need any explanation, feel free to ask me aind I will explain to you.

I hope it helps...

Regards,
Filipe Marcelino
*Kerry Brown
Message 5 of 10 (188 Views)

Re: CommandEnded Event

01-30-2006 02:18 AM in reply to: cadmaxza
I'm not a VB'er, but ..
if the command is registered with CommandFlags.Session it will run in the application context.

kwb


wrote in message news:5071875@discussion.autodesk.com...
Thanks Filipe!!!

Next step. This functions on the drawing that was active when the NETLOAD was executed only. If I
open another drawing, the function is not available in the other drawing. Is there maybe an AutoCAD
setting? How do I enable this function for every drawing that is opened?

Thanks again,

Kevin.
Valued Contributor
fmarcelino
Posts: 61
Registered: ‎10-25-2005
Message 6 of 10 (188 Views)

Re: CommandEnded Event

01-30-2006 02:27 AM in reply to: cadmaxza
Hi,

I know that you can specify CommandFlags, but I don't use it. I just declare my commands like this:

_
Public Shared Sub Test()
...
End Sub

So, I don't know how to answer you on this matter.


Regards,
Filipe Marcelino
Active Contributor
cadmaxza
Posts: 36
Registered: ‎06-23-2004
Message 7 of 10 (188 Views)

Re: CommandEnded Event

01-30-2006 02:41 AM in reply to: cadmaxza
Thx again Filipe. Havent tested it yet, looks a bit complex. I have a feeling that I'm heading down the wrong road though. See my new posting.
Cheers,
Kevin.
Active Contributor
cadmaxza
Posts: 36
Registered: ‎06-23-2004
Message 8 of 10 (188 Views)

Re: CommandEnded Event

01-30-2006 02:53 AM in reply to: cadmaxza
Kerry,

Please provide more details.

Thx,

Kevin.
*Kerry Brown
Message 9 of 10 (188 Views)

Re: CommandEnded Event

01-30-2006 04:11 AM in reply to: cadmaxza
in C# for registration in an application scope I use something like :

[CommandMethod("MySuperDuperCommand",CommandFlags.Session)]

In the ARX docs refer to Autodesk.AutoCAD.Runtime.CommandFlags Fields

kwb
///

wrote in message news:5071920@discussion.autodesk.com...
Kerry,

Please provide more details.

Thx,

Kevin.
Valued Contributor
fmarcelino
Posts: 61
Registered: ‎10-25-2005
Message 10 of 10 (188 Views)

Re: CommandEnded Event

01-30-2006 04:14 AM in reply to: cadmaxza
Hi Kerry,

yes, I know that you can specify Commands like that, but if you ommit the CommandFlags specification the command will work normally. Maybe I'm doing wrong but that was the way that I learned by myself. Feel free to correct me and teach me.

Regards,
Filipe Marcelino

You are not logged in.

Log into access your profile, ask and answer questions, share ideas and more. Haven't signed up yet? Register

Announcements
Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.

Need installation help?

Start with some of our most frequented solutions to get help installing your software.

Ask the Community