1
Text / Change the background mask property of AcDbMText object programmatically
« on: Yesterday at 08:36:35 PM »See for more an article written by Fenton Webb here
http://adndevblog.typepad.com/autocad/2013/05/change-the-background-mask-property-of-acdbmtext-object-programmatically-using-objectarx.html
VB.NET
Code: [Select]
Public Shared Function fixoGetEntity(tr As Transaction, ed As Editor, rx As RXClass, msg As String, ByRef id As ObjectId) As ErrorStatus
Dim es As ErrorStatus
Dim ent As Entity
id = ObjectId.Null
Dim peo As New PromptEntityOptions(msg)
peo.SetRejectMessage(vbLf & "You're missing, try again >>")
peo.AddAllowedClass(GetType(Entity), False)
Dim res As PromptEntityResult
res = ed.GetEntity(peo)
If res.Status <> PromptStatus.OK Then
es = ErrorStatus.PointNotOnEntity
End If
id = res.ObjectId
If id = ObjectId.Null Then
es = ErrorStatus.NullObjectId
End If
ent = TryCast(tr.GetObject(id, OpenMode.ForRead, False), Entity)
If ent.GetRXClass() <> rx Then
ed.WriteMessage(vbLf & "{0}Must be a type of ""{0}"" only!", rx.DxfName)
es = ErrorStatus.NotThatKindOfClass
End If
If ent Is Nothing Then
es = ErrorStatus.NotAnEntity
Else
es = ErrorStatus.OK
End If
Return es
End Function
<CommandMethod("BM")> _
Public Shared Sub test_setBackgroundFill_MText()
Dim doc As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument
Dim ed As Editor = doc.Editor
Dim db As Database = HostApplicationServices.WorkingDatabase
Dim id As ObjectId = ObjectId.Null
Using tr As Transaction = db.TransactionManager.StartTransaction()
If fixoGetEntity(tr, ed, RXClass.GetClass(GetType(MText)), vbLf & "Please pick an MText entity: ", id) = ErrorStatus.OK Then
Dim btr As BlockTableRecord = TryCast(tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite), BlockTableRecord)
' get entity by direct cast
Dim ent As Entity = DirectCast(tr.GetObject(id, OpenMode.ForRead), Entity)
If id.ObjectClass.DxfName = "MTEXT" Then
Dim mtx As MText = TryCast(ent, MText)
mtx.UpgradeOpen()
Dim color As Autodesk.AutoCAD.Colors.Color
If mtx.BackgroundFill Then
mtx.UseBackgroundColor = False
mtx.BackgroundFill = False
Else
mtx.BackgroundFill = True
color = color.FromColorIndex(ColorMethod.ByAci, CInt(1))
mtx.BackgroundFillColor = color
mtx.UseBackgroundColor = False
End If
mtx.RecordGraphicsModified(True)
End If
End If
tr.Commit()
End Using
End Sub
C#
Code: [Select]
public static ErrorStatus fixoGetEntity(Transaction tr, Editor ed, RXClass rx, string msg, out ObjectId id)
{
ErrorStatus es;
Entity ent;
id = ObjectId.Null;
PromptEntityOptions peo = new PromptEntityOptions(msg);
peo.SetRejectMessage("\nYou're missing, try again >>");
peo.AddAllowedClass(typeof(Entity), false);
PromptEntityResult res;
res = ed.GetEntity(peo);
if (res.Status != PromptStatus.OK)
es = ErrorStatus.PointNotOnEntity;
id = res.ObjectId;
if (id == ObjectId.Null)
es = ErrorStatus.NullObjectId;
ent = tr.GetObject(id, OpenMode.ForRead, false) as Entity;
if (ent.GetRXClass() != rx)
{
ed.WriteMessage("\n{0}Must be a type of \"{0}\" only!", rx.DxfName);
es = ErrorStatus.NotThatKindOfClass;
}
if (ent == null)
es = ErrorStatus.NotAnEntity;
else es = ErrorStatus.OK;
return es;
}
[CommandMethod("BM")]
static public void test_setBackgroundFill_MText()
{
Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
Editor ed = doc.Editor;
Database db = HostApplicationServices.WorkingDatabase;
ObjectId id = ObjectId.Null;
using (Transaction tr = db.TransactionManager.StartTransaction())
{
if (fixoGetEntity(tr, ed, RXClass.GetClass(typeof(MText)), "\nPlease pick an MText entity: ", out id) == ErrorStatus.OK)
{
BlockTableRecord btr = tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;
// get entity by direct cast
Entity ent = (Entity)tr.GetObject(id, OpenMode.ForRead);
if (id.ObjectClass.DxfName == "MTEXT")
{
MText mtx = ent as MText;
mtx.UpgradeOpen();
Autodesk.AutoCAD.Colors.Color color;
if (mtx.BackgroundFill )
{
mtx.UseBackgroundColor= false;
mtx.BackgroundFill=false;
}
else
{
mtx.BackgroundFill=true;
color = Color.FromColorIndex(ColorMethod.ByAci, (int)1);
mtx.BackgroundFillColor = color;
mtx.UseBackgroundColor=false;
}
mtx.RecordGraphicsModified(true);
}
}
tr.Commit();
}
}