Hey Everyone,
I'm new to these forums and was wondering if someone could give me a hand. I'm trying to create multiple layouts (paperspace) using C#. I'm using Visual Studio 2010 and Autocad 2012 (x64). I found a bit of VB code and am trying to translate it to C# and am stuck on one line. The error i get is the following:
"Error 1 Cannot implicitly convert type 'Autodesk.AutoCAD.DatabaseServices.DBObject' to 'Autodesk.AutoCAD.DatabaseServices.Layout'. An explicit conversion exists (are you missing a cast?) "
I think i need to cast it but am unsure as to how. Is there an easy way to fix this or an even easier way to accomplish what i'm trying to do?
Thanks in advance
Vince
class CHPaperSpace
{
[Autodesk.AutoCAD.Runtime.CommandMethod("CHPAPERSPACE")]
// This will be the function associated with my command
public void CHPAPERSPACECOMMAND()
{
int i = 1;
Document doc = Application.DocumentManager.MdiActiveDocument;
Editor ed = doc.Editor;
DocumentLock doclock = doc.LockDocument();
Database db = doc.Database;
Autodesk.AutoCAD.DatabaseServices.TransactionManager trm = db.TransactionManager;
try
{
Transaction tr = trm.StartTransaction();
LayoutManager oLayoutManager = Autodesk.AutoCAD.DatabaseServices.LayoutManager.Current;
try
{
ObjectId oLayoutObjectId;
oLayoutObjectId = oLayoutManager.CreateLayout("CHPaperSpace" + i++);
Layout oLayout = tr.GetObject(oLayoutObjectId, OpenMode.ForWrite);
oLayout.Initialize();
ed.Regen();
}
catch
{
tr.Commit();
doclock.Dispose();
}
}
catch{}
}
}
}
Solved! Go to Solution.
Solved by jeff. Go to Solution.
Since Layout derives from DBobject and DBobject does not contain all the methods as Layout it is considered a 'narrowing conversion' which the compiler does not allow implicit narrowing conversions.
Do it explicity
Layout oLayout = (Layout)tr.GetObject(oLayoutObjectId, OpenMode.ForWrite); //or Layout oLayout = tr.GetObject(oLayoutObjectId, OpenMode.ForWrite) as Layout;