Reading xrecord data and writing it to the commandline

Reading xrecord data and writing it to the commandline

Anonymous
Not applicable
2,182 Views
1 Reply
Message 1 of 2

Reading xrecord data and writing it to the commandline

Anonymous
Not applicable

Hello there,

 

Can any of you help me to read the xrecord data i've created  with the NewDoc command and write it to te commandline.

 

I've create a function ReadXrec that checks if my dictionary excists but i don't know how to get to the records. 

 

 

Here is my code:

 

public class AsbvDocument
    {
        private Database acDbNewDoc = new Database();

        private const string _companyName = "Acme";
        private const string _applicationName = "AcmeApp";

        public AsbvDocument()
        { 
        }

        [CommandMethod("NewDoc")]
        public void NewDoc()
        {
            using (Transaction trans = acDbNewDoc.TransactionManager.StartTransaction())
            {
                // create a base level dictionary entry
                ObjectId companyId = CreateBaseDictionaryEntry(_companyName);
                // create a child entry
                ObjectId appId = CreateDictionaryEntry(companyId,_applicationName);
                //Create a new record
                Xrecord xRec = new Xrecord();
                //We want to add the record information
                xRec.Data = new ResultBuffer(
                    new TypedValue((int)DxfCode.Text, "GUID ObjectID"),    // NEN2580 OBJECTID
                    new TypedValue((int)DxfCode.Text, "GUID BouwLaagID"),  // NEN2580 BOUWLAAGID
                    new TypedValue((int)DxfCode.Text, "DWG + PAD"));       // DWGNAAM + PAD

                // open the childId for read
                DBDictionary childEntry = trans.GetObject(appId, OpenMode.ForRead, false) as DBDictionary;
                // check to see if it already exists
                if (childEntry.Contains("Data1") == false)
                {
                    // upgrade the childEntry to write status
                    childEntry.UpgradeOpen();
                    // if not add the xrecord
                    childEntry.SetAt("Data1", xRec);
                    // tell the transaction manager about the new object
                    acDbNewDoc.TransactionManager.AddNewlyCreatedDBObject(xRec, true);
                }
                // commit the transaction
                trans.Commit();
            }
            // Save the document
            acDbNewDoc.SaveAs(@"c:\temp\XrecDWG.dwg", DwgVersion.Current);
        }

        // create a base company entry into the Named objects dictionary
        public ObjectId CreateBaseDictionaryEntry(string companyName)
        {
            ObjectId companyDictId = default(ObjectId);
            try
            {
                // start a new transaction
                using (Transaction trans = acDbNewDoc.TransactionManager.StartTransaction())
                {
                    //First, get the NOD...
                    DBDictionary NOD = trans.GetObject(acDbNewDoc.NamedObjectsDictionaryId, OpenMode.ForRead, false) as DBDictionary;
                    // Define a corporate level dictionary
                    DBDictionary companyDict = default(DBDictionary);
                    try
                    {
                        // Just throw if it doesn’t exist do nothing else.
                        companyDict = trans.GetObject(NOD.GetAt(companyName), OpenMode.ForRead) as DBDictionary;
                        companyDictId = companyDict.ObjectId;
                    }
                    catch
                    {
                        // Doesn't exist, so create one, and set it in the NOD
                        companyDict = new DBDictionary();
                        // upgrade from read to write status
                        NOD.UpgradeOpen();
                        NOD.SetAt(companyName, companyDict);
                        // tell the transaction manager about the new object
                        acDbNewDoc.TransactionManager.AddNewlyCreatedDBObject(companyDict, true);
                        companyDictId = companyDict.ObjectId;
                    }
                    // commit the changes
                    trans.Commit();
                }
            }
            finally
            {
                // no need to call as will be done automatically
                // trans.Dispose()
            }
            // return the company diction
            return companyDictId;
        }

        public ObjectId CreateDictionaryEntry(ObjectId parentDictId, string childDictName)
        {
            ObjectId childDictId = default(ObjectId);
            try
            {
                // start a new transaction
                using (Transaction trans = acDbNewDoc.TransactionManager.StartTransaction())
                {
                    //First, get the parent dictionary...
                    DBDictionary parentDict = trans.GetObject(parentDictId, OpenMode.ForRead, false) as DBDictionary;
                    // try and get the child dictionary entry
                    DBDictionary childDict = default(DBDictionary);
                    try
                    {
                        // Just throw if it doesn’t exist do nothing else.
                        childDict = trans.GetObject(parentDict.GetAt(childDictName), OpenMode.ForRead) as DBDictionary;
                        childDictId = childDict.ObjectId;
                    }
                    catch
                    {
                        // Doesn't exist, so create one, and set it in the NOD
                        childDict = new DBDictionary();
                        // upgrade from read to write status
                        parentDict.UpgradeOpen();
                        parentDict.SetAt(childDictName, childDict);
                        // tell the transaction manager about the new object
                        acDbNewDoc.TransactionManager.AddNewlyCreatedDBObject(childDict, true);
                        childDictId = childDict.ObjectId;
                    }
                    // commit the transaction
                    trans.Commit();
                }
            }
            finally
            {
                // no need to call as will be done automatically
                // trans.Dispose()
            }
            // return the company diction
            return childDictId;
        }

        [CommandMethod("ReadXrec")]
        public void GetDictKeys()
        {
            Database db = HostApplicationServices.WorkingDatabase;
            DBDictionary NOD = default(DBDictionary);
            ObjectId idNod = db.NamedObjectsDictionaryId;
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                NOD = tr.GetObject(idNod, OpenMode.ForRead, false) as DBDictionary;
                if ((NOD != null))
                {
                    
                    if (NOD.Contains(_companyName))
                    {
                        DBDictionary dbDict = default(DBDictionary);
                        ObjectId idDict = NOD.GetAt(_companyName);
                        dbDict = tr.GetObject(idDict, OpenMode.ForRead) as DBDictionary;
                        if ((dbDict != null))
                        {
                            //IDictionary dictionary = dbDict;
                            if (dbDict.Contains(_applicationName))
                            {
                                // How can i write the data ive added to the command line
                            }
                        }
                    }
                }
            }
        }
    }

 

I Hope someone of you guys can help me.

 

Kind regards,

 

Irvin

Netherlands

0 Likes
2,183 Views
1 Reply
Reply (1)
Message 2 of 2

jamierobertson1
Enthusiast
Enthusiast

Something like

 

 

if (dbDict.Contains(_applicationName))
{
    Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
    Xrecord xrec = (Xrecord)tr.GetObject(dbDict.GetAt(_applicationName),OpenMode.ForRead);
    TypedValue[] res = xrec.Data.AsArray();
    for (int i = 0; i < res.Count(); i++)
    {
         ed.WriteMessage(res[i].Value.ToString()+"\n");
    }
}

 

 

0 Likes