.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

CommandEnded Event

9 REPLIES 9
Reply
Message 1 of 10
Anonymous
1872 Views, 9 Replies

CommandEnded Event

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
9 REPLIES 9
Message 2 of 10
fmarcelino
in reply to: Anonymous

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
Message 3 of 10
Anonymous
in reply to: Anonymous

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.
Message 4 of 10
fmarcelino
in reply to: Anonymous

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
Message 5 of 10
Anonymous
in reply to: Anonymous

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.
Message 6 of 10
fmarcelino
in reply to: Anonymous

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
Message 7 of 10
Anonymous
in reply to: Anonymous

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.
Message 8 of 10
Anonymous
in reply to: Anonymous

Kerry,

Please provide more details.

Thx,

Kevin.
Message 9 of 10
Anonymous
in reply to: Anonymous

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.
Message 10 of 10
fmarcelino
in reply to: Anonymous

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

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost