// (C) kdub Services 2011-09-09
// @ theSwamp
//
using System;
using System.Text;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using AcadApp = Autodesk.AutoCAD.ApplicationServices.Application;
using kdubTesting;
[assembly
: CommandClass
(typeof (MyCommands
))]
namespace kdubTesting
{
public class MyCommands
{
[CommandMethod("Doit", CommandFlags.Modal)]
public static void MyCommand() // This method can have any name
{
string myString = GetUserInput();
if (myString == string.Empty)
myString = "Ooooopsie";
Application.ShowAlertDialog(myString);
}
//
public static string GetUserInput()
{
Editor ed = AcadApp.DocumentManager.MdiActiveDocument.Editor;
PromptStringOptions stropts
= new PromptStringOptions
("\nType textValue or ENTER to select text/Mtext: "); stropts.AllowSpaces = true;
PromptResult res = ed.GetString("\nType textValue or ENTER to select MText: ");
if (res.Status != PromptStatus.OK)
return string.Empty;
string stuff = res.StringResult;
if (stuff == string.Empty)
stuff = GetStringStuff();
return stuff;
}
//
// // Mtext Fragment Concept Thanks to Tony T. 2006
//
static private StringBuilder _sData = null;
static private int _sCount = 0;
public static string GetStringStuff()
{
Editor ed = AcadApp.DocumentManager.MdiActiveDocument.Editor;
string mtextValue = string.Empty;
ObjectId id
= SelectSpecificObjectType
("\nSelect MText entity: ",
typeof(MText
)); if( !id.IsNull )
{
_sData
= new StringBuilder
(); _sCount = 0;
using( Transaction tr = id.Database.TransactionManager.StartTransaction() )
{
MText mtext = (MText)tr.GetObject(id, OpenMode.ForRead);
mtext
.ExplodeFragments(new MTextFragmentCallback
(FragmentCallback
)); mtextValue = _sData.ToString();
ed.WriteMessage("\nMText.Contents: {0}", mtext.Contents);
ed.WriteMessage("\n{0} MText fragments: {1}", _sCount, _sData.ToString());
}
}
return mtextValue;
}
private static ObjectId SelectSpecificObjectType(string prompt, System.Type objectClass)
{
Editor ed = AcadApp.DocumentManager.MdiActiveDocument.Editor;
using (Transaction tr = ed.Document.Database.TransactionManager.StartTransaction())
{
while (true)
{
PromptEntityResult res = ed.GetEntity(prompt);
if (res.Status != PromptStatus.OK)
return ObjectId.Null;
DBObject ob = tr.GetObject(res.ObjectId, OpenMode.ForRead);
if (objectClass.IsAssignableFrom(ob.GetType()))
return res.ObjectId;
ed.WriteMessage("\nInvalid selection, {0} entity expected",
RXClass.GetClass(objectClass).DxfName);
}
}
}
static MTextFragmentCallbackStatus FragmentCallback(MTextFragment fragment, object unused)
{
_sData.Append(fragment.Text);
++_sCount;
return MTextFragmentCallbackStatus.Continue;
}
}
}