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

handlers not working

10 REPLIES 10
Reply
Message 1 of 11
Anonymous
1294 Views, 10 Replies

handlers not working

I use addhandlers to change color/layer and change it back.

 

AddHandler AcadAPP.BeginCommand, AddressOf callback_BeginCommand

AddHandler AcadAPP.EndCommand, AddressOf callback_EndCommand

 

Last time i wanted to add an handler to change back color/layer when a command gets cancelled.

I used an AddHandler doc.commandCancelled but somehow it messed op my other handlers (maybe because its doc. instead of AcadAPP.).

EndCommand doesnt work anymore even when i delete the CommandCancelled Handler and command.

I have deleted all commandcancelled and sometimes, when i try to debug in autocad it doesnt load autocad (freezes at "FILETAB" on startup)

This happens only if i place the red dot (don't know how its called) at AddHandler AcadAPP.EndCommand and AddHandler AcadAPP.BeginCommand.

 

Did someone also have this problem?

 

 

 

10 REPLIES 10
Message 2 of 11
Anonymous
in reply to: Anonymous

Maybe it's because of a mixture of using .NET and COM?

I never use COM, so I don't know of BeginCommand and EndCommand. Maybe you could try to use the .NET equivalents as well:

 

doc.CommandWillStart

doc.CommandEnded

doc.CommandCancelled

doc.CommandFailed

Message 3 of 11
Anonymous
in reply to: Anonymous

I'm using Commandname as string and that's not part of doc.commandended and doc.commandwillstart.

 

Sub callback_BeginCommand(ByVal CommandName As String)

 

 

I need Commandname to change the layer/color when using hatch, text or dim only.

I'm trying to get an old version without doc.commandcancelled but i modified so much in the meantime 😞

So if someone has the awnser for me it would be much apreciated because i dont have to copy/paste all in an old code.

 

 

 

 

Message 4 of 11
Anonymous
in reply to: Anonymous

Here's an example of how you can trap the start of the PLOT command in C#:

 

 

        private void callback_CommandWillStart(object sender, CommandEventArgs e)

        {

            string sCommand = e.GlobalCommandName.ToUpper();

            switch (sCommand)

            {

                case "PLOT":

                    // Do your thing...

                    break;

 

                default:

                    break;

             }

       }

 

Or in VB.NET:

       Private Sub callback_CommandWillStart(sender As Object, e As CommandEventArgs)
           Dim sCommandName As String = e.GlobalCommandName.ToUpper()
       End Sub

Message 5 of 11
Anonymous
in reply to: Anonymous

Thanks Henk,

That is what i was looking for:) I still have a little problem nevertheless 😞

 

Like i said, i use commandwillstart to change the color/layer and commandended to change to the previous color/layer (like the code below).

When i try to declare the current layer/color before i change it, it gives the already changed color/layer as value (db.Clayer is not the current layer but it is already the layer that i'm going to chage it in later in the code).

 

Private Sub callback_CommandWillStart2(sender As Object, e As CommandEventArgs)

Dim currentlayer = db.Clayer
Dim currentcolor = db.Cecolor

previouslayer = currentlayer
previouscolor = currentcolor

 

change color/layer

 

End Sub

 

Can i add the current color/layer the same way as the commandname? Something like this:

 

Private Sub callback_CommandWillStart(sender As Object, e As CommandEventArgs, d As Database)

Dim CommandName As String = e.GlobalCommandName.ToUpper()

Dim currentcolor as string = d.cecolor.ToString.ToUpper

Dim currentlayer as string = d.Clayer.ToString.ToUpper

 

I've atteched the error message

 

 

Message 6 of 11
Anonymous
in reply to: Anonymous

You cannot manipulate the structure of an eventhandler.
Declare your variables outside the eventhandlers, store the current values and set it back like this:

 

   Dim m_sCurrentLayer As String

   Private Sub callback_CommandWillStart(sender As Object, e As CommandEventArgs)
      Dim sCommandName As String = e.GlobalCommandName.ToUpper()
      If sCommandName = "LINE" Then
         ......
         m_sCurrentLayer = db.Clayer
         ......
      End If 
   End Sub

   Private Sub callback_CommandEnded(sender As Object, e As CommandEventArgs)
      Dim sCommandName As String = e.GlobalCommandName.ToUpper()
      If sCommandName = "LINE" Then
         ......
         db.Clayer = m_sCurrentLayer
         ......
      End If 
   End Sub

 

Message 7 of 11
Anonymous
in reply to: Anonymous

Yes i did that before but for some strange reason the db.Clayer (made it Bold in your example) is alrready the color that i'm going to change it in.

So for example, if i'm in layer "concrete" and i use a text command it has to change to layer "text" and turn back to concrete after the command.

Now, db.Clayer has already the value "text" so in Commandended it will change it to "text", what it already is.

 

 

 Dim m_sCurrentLayer As String

   Private Sub callback_CommandWillStart(sender As Object, e As CommandEventArgs)
      Dim sCommandName As String = e.GlobalCommandName.ToUpper()
      If sCommandName = "LINE" Then
         ......
         m_sCurrentLayer = db.Clayer
         ......
      End If 
   End Sub

   Private Sub callback_CommandEnded(sender As Object, e As CommandEventArgs)
      Dim sCommandName As String = e.GlobalCommandName.ToUpper()
      If sCommandName = "LINE" Then
         ......
         db.Clayer = m_sCurrentLayer
         ......
      End If 
   End Sub
Message 8 of 11
Anonymous
in reply to: Anonymous

I don't think AutoCAD already knows the layer you're intending to use Smiley Happy

 

So check your code: maybe you already have set the layer current yourself, or for some reason you are changing the value of m_sCurrentLayer before you give it back to db.Clayer? Debug your code with breakpoints in the handlers to see what is happening.

Message 9 of 11
moogalm
in reply to: Anonymous

Hi,

 

You need to pass layer objectId not the strings, for example you can following method in your handler, and accordingly restore layer in command end event.

Public Shared Sub SetLayerCurrent(slayerName As String)
	' Get the current document and database
	Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument
	Dim acCurDb As Database = acDoc.Database

	' Start a transaction
	Using acTrans As Transaction = acCurDb.TransactionManager.StartTransaction()
		' Open the Layer table for read
		Dim acLyrTbl As LayerTable
		acLyrTbl = TryCast(acTrans.GetObject(acCurDb.LayerTableId, OpenMode.ForRead), LayerTable)


		If acLyrTbl.Has(sLayerName) = True Then
			' Set the layer Center current
			acCurDb.Clayer = acLyrTbl(sLayerName)

			' Save the changes
			acTrans.Commit()

			' Dispose of the transaction
		End If
	End Using
End Sub

 

 

Message 10 of 11
Anonymous
in reply to: moogalm

Henk, aperently AutoCAD does know it.

Somehow using COM handlers like doc.CommandCancelled messed up my other handlers.

I deleted all COM handlers and i only use AcadAPP.BeginCommand and AcadAPP.EndCommand now.

The handlers are working fine for all commands except text,hatch and dim commands.

Also if i debug it in autoCAD i get this message when opening a drawing:

 

Application does not support just-in-time (JIT)
debugging. See the end of this message for details.

************** Exception Text **************
System.InvalidCastException: Unable to cast object of type 'Autodesk.AutoCAD.LivePreview.PreviewRuleProvider' to type 'Autodesk.AutoCAD.Internal.IPreviewContextProvider'.
at Autodesk.AutoCAD.LivePreview.PreviewContextService.LoadContext()
at Autodesk.AutoCAD.LivePreview.PreviewContextService.get_ContextProvider()
at Autodesk.AutoCAD.LivePreview.PreviewContextManager.OnIdleInitialize(Object sender, EventArgs e)
at System.EventHandler.Invoke(Object sender, EventArgs e)
at Autodesk.AutoCAD.ApplicationServices.Core.Application.OnIdle()


************** Loaded Assemblies **************

 

etc....

 

These two lines are interesting:

at Autodesk.AutoCAD.LivePreview.PreviewContextManager.OnIdleInitialize(Object sender, EventArgs e)
at System.EventHandler.Invoke(Object sender, EventArgs e)

 

This because i used EventArgs e for COM handlers but i already deleted them:

 

 Private Sub callback_CommandWillStart(sender As Object, e As CommandEventArgs)
           Dim sCommandName As String = e.GlobalCommandName.ToUpper()

 

 

So somehow my vbproject still has information in it from old COM handlers i already deleted.

Do i have to reset or something?

I already tried to delete all references and add them again but it didn't work 😞

 

Message 11 of 11
Anonymous
in reply to: Anonymous

I solved it by reinstalling AutoCAD. 

We will never know what it was but i'm releaved that it's solved.

it was beginning to frustrate me.

 

Thanks for all your help 🙂

 

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

Post to forums  

AutoCAD Inside the Factory


Autodesk Design & Make Report