.NET

.NET

Reply
Distinguished Contributor
vince1327
Posts: 117
Registered: ‎11-02-2011
Message 1 of 3 (170 Views)
Accepted Solution

Removing Default Layers - Logic Issue?

170 Views, 2 Replies
01-19-2012 07:00 AM

Hey Everyone,

 

I'm just finishing up with a command that sets up custom paperspaces for our designers, and once it's done, it deletes the default Layout1 and Layout2 paperspace. The error i'm getting though is the following:

 

If a drawing contains Layout1 and Layout2, both are deleted as they should be

 

If a drawing contains Layout1 but not Layout2

or 

If a drawing contains Layout2 but not Layout1

 

I get an error and it crashes. This seems really stupid i know, but i must be doing something wrong here. I'm using if statements to determine if Layout1 exists, then delete it, and if Layout2 exists, then delete it. However, it seems that everytime, if one of those layouts are missing, it still assumes it's there, tries to delete it, and errors out because it doesn't exist. I've tried many variations of "if" statements. If someone could point me in the right direction, that would be awesome.

 

My Code:

public class CHDelete
{

// This function will delete Layout1 and Layout2
[Autodesk.AutoCAD.Runtime.CommandMethod("CHDelete")]
public void CHRENAMECOMMAND()
{
Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;


using (DocumentLock acLckDoc = doc.LockDocument())
using (Transaction trx = db.TransactionManager.StartTransaction())
{
LayoutManager lm = LayoutManager.Current;
LayerTable acLyrTbl; acLyrTbl = trx.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;


if (acLyrTbl.Has("Layout1") == false)
{
lm.DeleteLayout("Layout1");
}

if (acLyrTbl.Has("Layout2") == false)
{
lm.DeleteLayout("Layout2");
}

 

trx.Commit();
ed.Regen();


}

}
}

 

Hi,

 

your code looks like you have difficulties in differencing the object types of "Layer" and "Layout".

On one side you write you want to delete the "Layout1" but you look to

trx.GetObject(db.LayerTableId,...

and that does not fit together!

 

- alfred -

*Expert Elite*
Alfred.NESWADBA
Posts: 9,595
Registered: ‎06-29-2007
Message 2 of 3 (165 Views)

Re: Removing Default Layers - Logic Issue?

01-19-2012 07:25 AM in reply to: vince1327

Hi,

 

your code looks like you have difficulties in differencing the object types of "Layer" and "Layout".

On one side you write you want to delete the "Layout1" but you look to

trx.GetObject(db.LayerTableId,...

and that does not fit together!

 

- alfred -

-------------------------------------------------------------------------
Alfred NESWADBA
Ingenieur Studio HOLLAUS ... www.hollaus.at
-------------------------------------------------------------------------
Distinguished Contributor
vince1327
Posts: 117
Registered: ‎11-02-2011
Message 3 of 3 (164 Views)

Re: Removing Default Layers - Logic Issue?

01-19-2012 07:27 AM in reply to: Alfred.NESWADBA

Wow, that was the typo of all typos, thanks a million! I was a bit sleepy when i put this together and didn't even realize.

 

Cheers!

Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Do you have 60 seconds to spare? The Autodesk Community Team is revamping our site ranking system and we want your feedback! Please click here to launch the 5 question survey. As always your input is greatly appreciated.