Adding Xdata to a database that's not opened in the editor

Adding Xdata to a database that's not opened in the editor

amasutti
Explorer Explorer
448 Views
8 Replies
Message 1 of 9

Adding Xdata to a database that's not opened in the editor

amasutti
Explorer
Explorer

Hi, I'm Using C# to add Xdata to a block reference in a database that's opened in I guess you would call it a Side Data base (not in the drawing Editor). I'm checking for the Registered app and creating it if it doesn't exist. But when it comes to writing the Xdata I get an error EappIdNotFound. I'm Opening the Other drawing database Read-Write. Any Help Would be appreciated. Thanks in advance.

private void PutXdataDate (ObjectId objEntId, string strAppName,string Jdate)
        {
            Database objDB = objEntId.Database;
            Transaction objTrans = objDB.TransactionManager.StartTransaction();
            Entity objEnt = (Entity)objTrans.GetObject(objEntId, OpenMode.ForWrite, false);
            Database db = HostApplicationServices.WorkingDatabase;
            // RegAppTable acRegAppTbl;
            

            using (Transaction acTrans = db.TransactionManager.StartTransaction())
            {
               var acRegAppTbl = (RegAppTable) acTrans.GetObject(db.RegAppTableId, OpenMode.ForRead);
                if (acRegAppTbl.Has(strAppName) == false)
                {
                    RegAppTableRecord acRegAppTblRec = new RegAppTableRecord();
                    acRegAppTblRec.Name = strAppName;
                    acRegAppTbl.UpgradeOpen();
                    acRegAppTbl.Add(acRegAppTblRec);
                    acTrans.AddNewlyCreatedDBObject(acRegAppTblRec, true);
                    acTrans.Commit();
                 }
               

            }
            using (objTrans)
            {
            bool appFound = false;
            bool found_IO = false;
            bool found_Sig = false;
            if (objEnt.XData != null/* TODO Change to default(_) if this is not a reference type */ )
            {
                bool bChange = false;
                ResultBuffer objResBuf = objEnt.XData;
                ResultBuffer objNewResBuf = new ResultBuffer();
                //TypedValue objTypedVal;
                foreach (TypedValue objTypedVal in objResBuf)
                {
                    if (objTypedVal.TypeCode == 1001)
                    {
                        string otypval = (string) objTypedVal.Value;
                        if (otypval.ToString()  == strAppName)
                        {
                            appFound = true;
                            bChange = true;
                            objNewResBuf.Add(objTypedVal);
                        }
                        else
                            bChange = false;
                    }
                    if (bChange == true & objTypedVal.TypeCode != 1001)
                    {

                        if (objTypedVal.TypeCode == 1000 & Convert.ToString( objTypedVal.Value).Substring(1, 9).ToUpper() == "Jul_Date=".ToUpper())
                        {
                            TypedValue TV = new TypedValue((int) DxfCode.ExtendedDataAsciiString, "Jul_Date=" + Jdate);
                            objNewResBuf.Add(TV);
                            found_IO = true;
                        }
                        
                        else
                            objNewResBuf.Add(objTypedVal);
                    }
                    if (bChange == false)
                        objNewResBuf.Add(objTypedVal);
                }

                try
                {
                    objEnt.XData = objNewResBuf;
                }

                catch (System.Exception ex)
                {
                   System.Windows.MessageBox.Show (ex.Message);
                }
            }


            ResultBuffer objNewestResBuf = new ResultBuffer();

            if ((found_IO == false | found_Sig == false) & appFound == true)
            {
                if (objEnt.XData != null/* TODO Change to default(_) if this is not a reference type */ )
                {
                    bool bChange = false;
                    ResultBuffer objResBuf = objEnt.XData;
                    foreach (var objTypedVal in objResBuf)
                    {
                        if (objTypedVal.TypeCode == 1001)
                        {
                            string otypval = (string)objTypedVal.Value;
                            if (otypval.ToString() == strAppName)
                            {
                                bChange = true;
                                objNewestResBuf.Add(objTypedVal);
                            }
                            else
                                bChange = false;
                        }
                        if (found_IO == false & objTypedVal.TypeCode != 1001 & bChange == true)
                        {
                            TypedValue TV = new TypedValue((int)DxfCode.ExtendedDataAsciiString, "Jul_Date=" + Jdate);
                            objNewestResBuf.Add(TV);
                            found_IO = true;
                        }
                        
                        if (bChange == true)
                            objNewestResBuf.Add(objTypedVal);

                        if (bChange == false)
                            objNewestResBuf.Add(objTypedVal);
                    }

                    try
                    {
                        objEnt.XData = objNewestResBuf;
                    }

                    catch (System.Exception ex)
                    {
                        System.Windows.MessageBox.Show (ex.Message);
                    }
                }
            }
            else if (appFound == false)
            {
                TypedValue TV = new TypedValue((int)DxfCode.ExtendedDataRegAppName, strAppName);
                objNewestResBuf.Add(TV);

                TV = new TypedValue((int)DxfCode.ExtendedDataAsciiString, 
                                "Jul_Date=" + Jdate);
                objNewestResBuf.Add(TV);

                if (objEnt.XData != null)
                {                    ResultBuffer objResBuf = objEnt.XData;
                    foreach (var objTypedVal in objResBuf)
                        objNewestResBuf.Add(objTypedVal);
                }


                try
                {
                    objEnt.XData = objNewestResBuf;
                }

                catch (System.Exception ex)
                {
                   System.Windows.MessageBox.Show (ex.Message);
                }
            }



            objTrans.Commit();
            objTrans.Dispose();
                }
        }

 

0 Likes
Accepted solutions (1)
449 Views
8 Replies
Replies (8)
Message 2 of 9

GeeHaa
Collaborator
Collaborator

Nevermind I figured it out. I was creating the Registered app in the working Database not the Side Database. Instead of DN I created in in objDB. Thanks anyways.

0 Likes
Message 3 of 9

_gile
Consultant
Consultant

You add the registered application to the the RegAppTable of the working database (HostApplicationServices.WorkingDatabase). Is it the same as the 'objDb' (objEntId.Database)?



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 4 of 9

_gile
Consultant
Consultant
Accepted solution

It seems to me the code could be much simpler.

private static void PutXdataDate(ObjectId entId, string appName, string jDate)
{
    var db = entId.Database;
    using (var tr = new OpenCloseTransaction())
    {
        var regAppTable = (RegAppTable)tr.GetObject(db.RegAppTableId, OpenMode.ForRead);
        if (!regAppTable.Has(appName))
        {
            regAppTable.UpgradeOpen();
            var regApp = new RegAppTableRecord();
            regApp.Name = appName;
            regAppTable.Add(regApp);
            tr.AddNewlyCreatedDBObject(regApp, true);
        }

        var entity = tr.GetObject(entId, OpenMode.ForWrite);
        var xdata = new ResultBuffer(new TypedValue(1001, appName), new TypedValue(1000, "Jul_Date=" + jDate));
        entity.XData = xdata;
        tr.Commit();
    }
}


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 5 of 9

GeeHaa
Collaborator
Collaborator

Yes the Working Database is not the same as the Object Database. That was the Problem. But Don't you have to Take care of Other registered apps so you don't overwrite them?

0 Likes
Message 6 of 9

GeeHaa
Collaborator
Collaborator

By the Way I'm amasutti. I don't know why it didn't log me in by my Handle.

0 Likes
Message 7 of 9

GeeHaa
Collaborator
Collaborator

The only other problem I'm having is, I get a file error saving. Can you tell me what I'm doing wrong.

using (Database acCurDB = new Database(false, true))
                    {
                        acCurDB.ReadDwgFile(GlobalVariables.listOfFiles[x], FileOpenMode.OpenForReadAndWriteNoShare, true, "");
                        using (Transaction acTrans = acCurDB.TransactionManager.StartTransaction())
                        {
}
                            acCurDB.SaveAs(GlobalVariables.listOfFiles[x], DwgVersion.Newest);

 

0 Likes
Message 8 of 9

_gile
Consultant
Consultant

@GeeHaa  a écrit :

Don't you have to Take care of Other registered apps so you don't overwrite them?


You can try the code I posted. It will only overwrite the xdata for the specified application (if any).



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 9 of 9

amasutti
Explorer
Explorer

Never mind I figured it out. I needed to use the Overload that saved a Bak file as well.

0 Likes