.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Cast Errors

2 REPLIES 2
SOLVED
Reply
Message 1 of 3
vince1327
390 Views, 2 Replies

Cast Errors

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{}

}

}
}

2 REPLIES 2
Message 2 of 3
jeff
in reply to: vince1327

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;

 

 

You can also find your answers @ TheSwamp
Message 3 of 3
vince1327
in reply to: jeff

Ah, gotcha! Thanks a million, that worked perfectly 😉

 

Cheers

Vince

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost