Author Topic: error <eWrongDatabase>  (Read 5142 times)

0 Members and 1 Guest are viewing this topic.

volody1983

  • Guest
error <eWrongDatabase>
« on: September 10, 2010, 08:06:07 AM »
Below is a simple example of drawing Circle in a closed file.
When drawing receive an error <eWrongDatabase>. This is a bug, I can not, or so thought of Autodesk?
I use AUtocad2010.
Sorry for my english!!!


Code: [Select]
[CommandMethod("DrawCircleToFile", CommandFlags.Session)]
public static void DrawCircleToFile()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
using (DocumentLock doclock = doc.LockDocument()) {
Database NewDB = GetDataBaseFromFile("C:\\Тест.dwg");
DrawCircle(new Point3d(0, 0, 0), NewDB);
NewDB.Dispose();
}
}

//Получает Database файла
private static Database GetDataBaseFromFile(string FileName)
{
Database NewDB = new Database(false, true);
NewDB.ReadDwgFile(FileName, FileOpenMode.OpenForReadAndWriteNoShare, false, null);
return NewDB;
}

//тупо рисует окружность в пространстве модели
       private static void DrawCircle(Point3d Center, Database Db)
        {
            Vector3d normal = new Vector3d(0.0, 0.0, 1.0);
            Circle Pcirc = new Circle(Center, normal, 100.0);
            Pcirc.SetDatabaseDefaults();
            // Dim CircId As New ObjectId ' = ObjectId.Null
            //  CreateNewLayer(LayerName, Drawing.Color.Yellow, 2, LineWeight.LineWeight200, False, Db)
            using (Transaction acTrans = Db.TransactionManager.StartTransaction())
            {
                //Во первых - нужно обязательно приводить к типу BlockTable, иначе получим ошибку компиляции
                BlockTable bt = (BlockTable) acTrans.GetObject(Db.BlockTableId, OpenMode.ForRead, false, true);
                //Во вторых - помимо того, что должно быть приведение, объекту bt параметр должен передаваться в квадратных, а не в круглых скобках иначе опять же - ошибка компиляции
                BlockTableRecord btr = (BlockTableRecord)acTrans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite, false, true);
                Pcirc.Layer = "0";
                btr.AppendEntity(Pcirc);
                acTrans.AddNewlyCreatedDBObject(Pcirc, true);
                acTrans.Commit();
            }
        }

« Last Edit: September 10, 2010, 08:43:44 AM by volody1983 »

Alexander Rivilis

  • Bull Frog
  • Posts: 214
  • Programmer from Kyiv (Ukraine)
Re: error <eWrongDatabase>
« Reply #1 on: September 10, 2010, 09:57:34 AM »
Temporary switch HostApplicationServices.WorkingDatabase to opened Database

n.yuan

  • Bull Frog
  • Posts: 348
Re: error <eWrongDatabase>
« Reply #2 on: September 10, 2010, 10:34:10 AM »
You may have already realized from Alexander's reply, the error you get is because of wrong database (well, the error has said it, hasn't it?).

The true offending line of code is

Pcirc.SetDatabaseDefault();

This method has an overload that takes an argument of Database type. Without passing a Database parameter, AutoCAD will use current WorkingDatabase, not the database of the opened file. This, when you append the new circle to the database of opened file, you get the eWrongDatabase exception, of course.

So, either set the HostApplicationServices.WorkingDatabase to the database of opened file as Alexander suggested, or simply do:

Pcirl.SetDatabaseDefault(Db);

BTW, you do not need to lock current document, because your process has nothing to do with it. I would:

using (Database newDB=GetNewDatabaseFromFile(....))
{
    DrawCircle(...);
    //Save the change if needed
}

Below is a simple example of drawing Circle in a closed file.
When drawing receive an error <eWrongDatabase>. This is a bug, I can not, or so thought of Autodesk?
I use AUtocad2010.
Sorry for my english!!!


Code: [Select]
[CommandMethod("DrawCircleToFile", CommandFlags.Session)]
public static void DrawCircleToFile()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
using (DocumentLock doclock = doc.LockDocument()) {
Database NewDB = GetDataBaseFromFile("C:\\Тест.dwg");
DrawCircle(new Point3d(0, 0, 0), NewDB);
NewDB.Dispose();
}
}

//Получает Database файла
private static Database GetDataBaseFromFile(string FileName)
{
Database NewDB = new Database(false, true);
NewDB.ReadDwgFile(FileName, FileOpenMode.OpenForReadAndWriteNoShare, false, null);
return NewDB;
}

//тупо рисует окружность в пространстве модели
       private static void DrawCircle(Point3d Center, Database Db)
        {
            Vector3d normal = new Vector3d(0.0, 0.0, 1.0);
            Circle Pcirc = new Circle(Center, normal, 100.0);
            Pcirc.SetDatabaseDefaults();
            // Dim CircId As New ObjectId ' = ObjectId.Null
            //  CreateNewLayer(LayerName, Drawing.Color.Yellow, 2, LineWeight.LineWeight200, False, Db)
            using (Transaction acTrans = Db.TransactionManager.StartTransaction())
            {
                //Во первых - нужно обязательно приводить к типу BlockTable, иначе получим ошибку компиляции
                BlockTable bt = (BlockTable) acTrans.GetObject(Db.BlockTableId, OpenMode.ForRead, false, true);
                //Во вторых - помимо того, что должно быть приведение, объекту bt параметр должен передаваться в квадратных, а не в круглых скобках иначе опять же - ошибка компиляции
                BlockTableRecord btr = (BlockTableRecord)acTrans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite, false, true);
                Pcirc.Layer = "0";
                btr.AppendEntity(Pcirc);
                acTrans.AddNewlyCreatedDBObject(Pcirc, true);
                acTrans.Commit();
            }
        }