PDA

View Full Version : C# sample attaching XRecord to a graphical object



ozwings
2009-09-16, 02:15 AM
Hello,

Can someone please let me know where to find C# sample code showing how to attach an XRecord to an ExtensionDictionary of a graphical object (e.g.a LINE) ?

I couldn't find a comprehensive example of that in the ObjectARX SDK. I am using AutoCAD 2009.

Best regards,
OzWings

_gile
2009-09-17, 11:48 AM
Hi,

Here's a trick


public void SetXrecord(ObjectId id, string key, ResultBuffer resbuf)
{
Document doc = acadApp.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
using (Transaction tr = db.TransactionManager.StartTransaction())
{
Entity ent = tr.GetObject(id, OpenMode.ForRead) as Entity;
if (ent != null)
{
ent.UpgradeOpen();
ent.CreateExtensionDictionary();
DBDictionary xDict = (DBDictionary)tr.GetObject(ent.ExtensionDictionary, OpenMode.ForWrite);
Xrecord xRec = new Xrecord();
xRec.Data = resbuf;
xDict.SetAt(key, xRec);
tr.AddNewlyCreatedDBObject(xRec, true);
}
tr.Commit();
}
}

public ResultBuffer GetXrecord(ObjectId id, string key)
{
Document doc = acadApp.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
ResultBuffer result = new ResultBuffer();
using (Transaction tr = db.TransactionManager.StartTransaction())
{
Xrecord xRec = new Xrecord();
Entity ent = tr.GetObject(id, OpenMode.ForRead, false) as Entity;
if (ent != null)
{
try
{
DBDictionary xDict = (DBDictionary)tr.GetObject(ent.ExtensionDictionary, OpenMode.ForRead, false);
xRec = (Xrecord)tr.GetObject(xDict.GetAt(key), OpenMode.ForRead, false);
return xRec.Data;
}
catch
{
return null;
}
}
else
return null;
}
}
}

Test commands


[CommandMethod("TEST1")]
public void Test1()
{
Document doc = acadApp.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
PromptEntityOptions peo = new PromptEntityOptions("\nSelect an entity: ");
peo.AllowNone = false;
PromptEntityResult per = ed.GetEntity(peo);
if (per.Status == PromptStatus.OK)
{
ObjectId id = per.ObjectId;
ResultBuffer resbuf = new ResultBuffer(
new TypedValue(1, "blah"),
new TypedValue(70, 256),
new TypedValue(40, 25.4));
SetXrecord(id, "test", resbuf);
}
}

[CommandMethod("TEST2")]
public void Test2()
{
Document doc = acadApp.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
PromptEntityOptions peo = new PromptEntityOptions("\nSelect an entity: ");
peo.AllowNone = false;
PromptEntityResult per = ed.GetEntity(peo);
if (per.Status == PromptStatus.OK)
{
ObjectId id = per.ObjectId;
ResultBuffer resbuf = GetXrecord(id, "test");
if (resbuf == null)
ed.WriteMessage("\nNone xrecord");
else
{
TypedValue[] result = resbuf.AsArray();
ed.WriteMessage("{0} {1} {2}", result[0], result[1], result[2]);
}
}
}