You can't add an object that is in one database,
to another database.
You have to create a copy of the object and add
the copy to the destination database.
That's why you're getting eAlreadyInDb.
See the DeepCloneObjects and WBlockCloneObjects
methods of the Database for ways of copying and
adding the copies to another owner/database in one
step.
--
http://www.caddzone.com
AcadXTabs: MDI Document Tabs for AutoCAD 2008
Supporting AutoCAD 2000 through 2008
http://www.acadxtabs.com
"HeNkL" wrote in message news:5624623@discussion.autodesk.com...
Hello all,
I 'm trying to create a function that simulates the AutoCAD command "Layout
from Template". The added code almost works: it recognizes the desired
layoutname in the layoutdictionary of the template, and I also get no errors
when I create a new Layout object with the found ObjectId. When I read its
properties, I also see the correct layoutname.
But an error "eAlreadyInDb" appears when the line "idLayout =
db.AddDBObject((DBObject)oLayout);" is executed, and the Layout is not added
to the current drawing. What am I missing here, is this a wrong method to
add a Layout object to a database? Or has anyone another solution for this
tool?
Thanks,
Henk
using System;
using System.Collections;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
public static ObjectId importLayout(string filename, string layout)
{
ObjectId idLayout = ObjectId.Null;
DocumentLock oLock =
acadApp.DocumentManager.MdiActiveDocument.LockDocument();
Database db = HostApplicationServices.WorkingDatabase;
Transaction trans = db.TransactionManager.StartTransaction();
try
{
Database dbTemplate = new Database(false, false);
dbTemplate.ReadDwgFile(filename, System.IO.FileShare.Read,
true, null);
DBDictionary dbdLayout =
(DBDictionary)trans.GetObject(dbTemplate.LayoutDictionaryId,
OpenMode.ForRead, false, false);
foreach (DictionaryEntry deLayout in dbdLayout)
{
string sLayout = deLayout.Key.ToString();
if (sLayout.ToUpper() == layout.ToUpper())
{
idLayout = (ObjectId)deLayout.Value;
Layout oLayout = (Layout)trans.GetObject(idLayout,
OpenMode.ForRead, false, false);
idLayout = db.AddDBObject((DBObject)oLayout);
break;
}
}
trans.Commit();
}
catch (Autodesk.AutoCAD.Runtime.Exception ex)
{
trans.Dispose();
MessageBox.Show(ex.ToString(), "Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
catch (System.Exception ex)
{
trans.Dispose();
MessageBox.Show(ex.ToString(), "Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
oLock.Dispose();
}
return idLayout;
}