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

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

7 REPLIES 7
SOLVED
Reply
Message 1 of 8
kevinsir
757 Views, 7 Replies

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

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

7 REPLIES 7
Message 2 of 8
khoa.ho
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

Message 3 of 8
khoa.ho
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

Message 4 of 8
kevinsir
in reply to: kevinsir

Thanks for your rely ! I will try !

Message 5 of 8
kevinsir
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 !

Message 6 of 8
khoa.ho
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

Message 7 of 8
kevinsir
in reply to: kevinsir

Thank you very much to give me so much help!I will try your suggestion again!
Message 8 of 8
kevinsir
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!

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