.NET

.NET

Reply
Valued Contributor
kevinsir
Posts: 78
Registered: ‎08-20-2012
Message 1 of 8 (357 Views)
Accepted Solution

How can I catch the message or event when I begin to edit Mtext entity?

357 Views, 7 Replies
09-12-2012 09:00 PM

Hello!Is there anyway I can catch the message or event when I begin to edit Mtext entity?

Because I want to do something when the mtext   began to input

Anyone can help me? Thanks

Mentor
khoa.ho
Posts: 208
Registered: ‎09-15-2011
Message 2 of 8 (349 Views)

Re: How can I catch the message or event when I begin to edit Mtext entity?

09-12-2012 11:03 PM in reply to: kevinsir

You can add an event to the editor for mouse dragging before defining an MText box:

 

[CommandMethod("CatchAddedMText")]
public static void CatchAddedMText()
{
	Document doc = Application.DocumentManager.MdiActiveDocument;
	Editor ed = doc.Editor;

	ed.Dragging += EdOnDragging;
}

private static void EdOnDragging(object sender, DraggingEventArgs draggingEventArgs)
{
	var ed = (Autodesk.AutoCAD.EditorInput.Editor)sender;
	if (ed.Document.CommandInProgress == "MTEXT")
	{
		// Do something here before the MText begin to input
		
	}
}

 

The event LeavingQuiescentState catches the editor entering busy mode, it will not know the ongoing command.

 

The event PointMonitor gets called every time when moving the mouse so it will not use as it consumes the CPU resource.

 

We try to use an event that is typical to draw MText and less called from the editor event handler. I think the event Dragging is a good choice to capture MText creation, and allow to do something before typing the MText.

 

-Khoa

Mentor
khoa.ho
Posts: 208
Registered: ‎09-15-2011
Message 3 of 8 (343 Views)

Re: How can I catch the message or event when I begin to edit Mtext entity?

09-12-2012 11:26 PM in reply to: kevinsir

I revise the code for modifying an existing MText, the previous code is just for creating a new MText:

 

[CommandMethod("CatchMTextEvents")]
public static void CatchMTextEvents()
{
	Document doc = Application.DocumentManager.MdiActiveDocument;
	Editor ed = doc.Editor;

	// Event for MText creation
	ed.Dragging += EdOnDragging;
	// Event for MText modifying
	ed.PromptForEntityEnding += EdOnPromptForEntityEnding;
}

private static void EdOnDragging(object sender, DraggingEventArgs draggingEventArgs)
{
	var ed = (Autodesk.AutoCAD.EditorInput.Editor)sender;
	if (ed.Document.CommandInProgress == "MTEXT")
	{
		// Do something here before the MText begin to input
		
	}
}

private static void EdOnPromptForEntityEnding(object sender, PromptForEntityEndingEventArgs promptForEntityEndingEventArgs)
{
	var ed = (Autodesk.AutoCAD.EditorInput.Editor) sender;
	if (ed.Document.CommandInProgress == "DDEDIT")
	{
		// Do something here before the MText begin to modify

	}
}

 

-Khoa

Valued Contributor
kevinsir
Posts: 78
Registered: ‎08-20-2012
Message 4 of 8 (318 Views)

Re: How can I catch the message or event when I begin to edit Mtext entity?

09-13-2012 09:59 PM in reply to: kevinsir

Thanks for your rely ! I will try !

Valued Contributor
kevinsir
Posts: 78
Registered: ‎08-20-2012
Message 5 of 8 (311 Views)

Re: How can I catch the message or event when I begin to edit Mtext entity?

09-14-2012 01:47 AM in reply to: kevinsir

I'm sorry that "EdOnPromptForEntityEnding" is not work in CAD2008! e...Do you know other event about that? Because I want the event when the curor is appearing and word  can be input !

Mentor
khoa.ho
Posts: 208
Registered: ‎09-15-2011
Message 6 of 8 (294 Views)

Re: How can I catch the message or event when I begin to edit Mtext entity?

09-14-2012 08:09 AM in reply to: kevinsir

I tested this code works on AutoCAD 2009, and the latest 2013. I don't use AutoCAD 2008 now so I don't know which event methods are available on 2008 but missing from 2009 and above.

 

This solution is not really perfect. The idea is to catch the current command in progress on the editor event handler. If the current command is DDEDIT (type ED in AutoCAD), it will catch the MText on beginning to modify. It will not work if you double-click to edit the selected MText. You can play around with other event methods in AutoCAD 2008 to find out which one is most closed to the PromptForEntityEnding event.

 

Another solution is to search for all MTexts of the current drawing database, then bind them with the event Modified. And for each MText creation, you have to bind this event method to it.

 

I think might someone have another idea with MText overrule, or something else. This topic is open for everyone who has the good solutions.

 

-Khoa

Valued Contributor
kevinsir
Posts: 78
Registered: ‎08-20-2012
Message 7 of 8 (278 Views)

Re: How can I catch the message or event when I begin to edit Mtext entity?

09-14-2012 04:32 PM in reply to: kevinsir
Thank you very much to give me so much help!I will try your suggestion again!
Valued Contributor
kevinsir
Posts: 78
Registered: ‎08-20-2012
Message 8 of 8 (229 Views)

Re: How can I catch the message or event when I begin to edit Mtext entity?

09-17-2012 11:27 PM in reply to: khoa.ho

Hi! EveryOne ,Mybe I have solved it

For add Mtext I use three event

private bool startcontrol=false;

private  void ed_DraggingEnded(object sender, DraggingEndedEventArgs e)       

{           

if (ed.Document.CommandInProgress == "MTEXT")           

{               

if (startcontrol)               

{                   

OpenForm(false);

              

}

}           

startcontrol = false;      

}

private void ed_PromptingForPoint(object sender, PromptPointOptionsEventArgs e)       

{           

if (ed.Document.CommandInProgress == "MTEXT")          

{               

if (e.Options.Keywords.Count == 7)

{                   

startcontrol = true;                   

return;               

}                               

}           

startcontrol = false;       

}

private  void ed_PromptedForPoint(object sender, PromptPointResultEventArgs e)       

{           

if (ed.Document.CommandInProgress == "MTEXT")           

{               

if (e.Result.Status == PromptStatus.OK)               

{                   

if (startcontrol)                   

{                       

return;          

}                  

}

}           

startcontrol = false;       

}

 

private void OpenForm(bool b)

{

// Do something here before the MText begin to input

}

 

 

///Then for edit the mtext I use another three events which could cover double-click mtext and input Mtedit command

private void EdOnPromptForEntityEnding(object sender, PromptForEntityEndingEventArgs promptForEntityEndingEventArgs)       

{         

if (ed.Document.CommandInProgress =="MtEdit")           

{               

startcontrol = true;

}       

}

 

private  void Database_ObjectOpenedForModify(object sender, ObjectEventArgs e)       

{           

if (e.DBObject.ClassID.ToString() == MtextguidClass)           

{               

if (startcontrol)               

{                   

OpenForm(true);

}

}

startcontrol = false;           

}

private void ed_PromptForSelectionEnding(object sender, PromptForSelectionEndingEventArgs e)       

{           

Database database = ((Autodesk.AutoCAD.EditorInput.Editor)sender).Document.Database;           

if (e.Selection.Count == 1)           

{               

using (Transaction tr = database.TransactionManager.StartTransaction()) 

{

                    ObjectId[] objectids = e.Selection.GetObjectIds();

                    DBObject dbobject = objectids[0].GetObject(OpenMode.ForRead);                   

if (dbobject.ClassID.ToString() == MtextguidClass)                   

{                         startcontrol = true;                       

return;                   

}

              

}           

}           

startcontrol = false;       

}

 

Maybe it is not perfect ,But has reached my request!

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 or visit the Installation and Licensing Forum to get help installing your software.