How to Delete Layer using C#.NET.

How to Delete Layer using C#.NET.

Anonymous
Not applicable
2,884 Views
12 Replies
Message 1 of 13

How to Delete Layer using C#.NET.

Anonymous
Not applicable
Hey,
How to Delete Layer using C#.NET.
I have tried Erase method of LayerTableRecord but it is giving me error.
Please take me out of this.
I hv searched forum but not found any relevance ans.
0 Likes
2,885 Views
12 Replies
Replies (12)
Message 2 of 13

Anonymous
Not applicable

Show the code that is not working so someone can
help you fix it...


style="PADDING-RIGHT: 0px; PADDING-LEFT: 5px; MARGIN-LEFT: 5px; BORDER-LEFT: #000000 2px solid; MARGIN-RIGHT: 0px">
Hey,
How to Delete Layer using C#.NET. I have tried Erase method of
LayerTableRecord but it is giving me error. Please take me out of this. I hv
searched forum but not found any relevance ans.
0 Likes
Message 3 of 13

Anonymous
Not applicable
This is a function i have used to delete all layers that i hv created using function createLayer() --->
private void eraseLayers()
{
Database db = HostApplicationServices.WorkingDatabase;
Editor editor = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;

try
{
using (Transaction tran = db.TransactionManager.StartTransaction())
{
LayerTable layerTable = (LayerTable)tran.GetObject(db.LayerTableId, OpenMode.ForWrite);

SymbolTableEnumerator sysTabEnum = layerTable.GetEnumerator();

while (sysTabEnum.MoveNext() == true)
{
ObjectId objEntr = (ObjectId)sysTabEnum.Current;
LayerTableRecord ltr = (LayerTableRecord)tran.GetObject(objEntr, OpenMode.ForWrite);
editor.WriteMessage(" Layer Name: "+ltr.Name);
ltr.Erase();
}
tran.Commit();
}
}
catch (System.Exception e)
{
editor.WriteMessage("Error in iterateLayers :: " + e.Message);
}
}
0 Likes
Message 4 of 13

Anonymous
Not applicable

No need to enumerate the table as you can just
check the layertable.Has() method. See

the tags use here to post so we can read you
code...

 


size=2><code>
[code]
{code}     

      using (Transaction tr =

       
db.TransactionManager.StartTransaction())
     
{
        LayerTable layerTable
=
         
(LayerTable)tr.GetObject(db.LayerTableId,
OpenMode.ForRead);
        //drawing may
have objects that use this
layer...
        if
((layerTable.Has(layername) &&

          layerTable[layername]
!= db.Clayer))
       
{
          LayerTableRecord ltr
=
           
(LayerTableRecord)
           
tr.GetObject(layerTable["foo"],
OpenMode.ForRead);
         

          if (ltr !=
null)
         
{
           
ltr.UpgradeOpen();
           
ltr.Erase();
           
tr.Commit();
         
}
       
}
     
}
{code}
[/code]
</code>


style="PADDING-RIGHT: 0px; PADDING-LEFT: 5px; MARGIN-LEFT: 5px; BORDER-LEFT: #000000 2px solid; MARGIN-RIGHT: 0px">
This
is a function i have used to delete all layers that i hv created using
function createLayer() --->
private void eraseLayers() { Database db =
HostApplicationServices.WorkingDatabase; Editor editor =
Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
try { using (Transaction tran = db.TransactionManager.StartTransaction()) {
LayerTable layerTable = (LayerTable)tran.GetObject(db.LayerTableId,
OpenMode.ForWrite); SymbolTableEnumerator sysTabEnum =
layerTable.GetEnumerator(); while (sysTabEnum.MoveNext() == true) { ObjectId
objEntr = (ObjectId)sysTabEnum.Current; LayerTableRecord ltr =
(LayerTableRecord)tran.GetObject(objEntr, OpenMode.ForWrite);
editor.WriteMessage(" Layer Name: "+ltr.Name); ltr.Erase(); } tran.Commit(); }
} catch (System.Exception e) { editor.WriteMessage("Error in iterateLayers ::
" + e.Message); } }
0 Likes
Message 5 of 13

Anonymous
Not applicable

Actually this looks like crap in the
webbrowser...


style="PADDING-RIGHT: 0px; PADDING-LEFT: 5px; MARGIN-LEFT: 5px; BORDER-LEFT: #000000 2px solid; MARGIN-RIGHT: 0px">
"Paul Richardson" <prichardson<lastpoint> wrote in message
href="news:6102696@discussion.autodesk.com">news:6102696@discussion.autodesk.com
...


No need to enumerate the table as you can just
check the layertable.Has() method. See

the tags use here to post so we can read you
code...

 


size=2><code>
[code]
{code}     

      using (Transaction tr =

       
db.TransactionManager.StartTransaction())
     
{
        LayerTable layerTable
=
         
(LayerTable)tr.GetObject(db.LayerTableId,
OpenMode.ForRead);
        //drawing may
have objects that use this
layer...
        if
((layerTable.Has(layername) &&

         
layerTable[layername] !=
db.Clayer))
       
{
          LayerTableRecord
ltr =
           
(LayerTableRecord)
           
tr.GetObject(layerTable["foo"],
OpenMode.ForRead);
         

          if (ltr !=
null)
         
{
           
ltr.UpgradeOpen();
           
ltr.Erase();
           
tr.Commit();
         
}
       
}
     
}
{code}
[/code]
</code>


style="PADDING-RIGHT: 0px; PADDING-LEFT: 5px; MARGIN-LEFT: 5px; BORDER-LEFT: #000000 2px solid; MARGIN-RIGHT: 0px">
This
is a function i have used to delete all layers that i hv created using
function createLayer() --->
private void eraseLayers() { Database db
= HostApplicationServices.WorkingDatabase; Editor editor =
Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
try { using (Transaction tran = db.TransactionManager.StartTransaction()) {
LayerTable layerTable = (LayerTable)tran.GetObject(db.LayerTableId,
OpenMode.ForWrite); SymbolTableEnumerator sysTabEnum =
layerTable.GetEnumerator(); while (sysTabEnum.MoveNext() == true) { ObjectId
objEntr = (ObjectId)sysTabEnum.Current; LayerTableRecord ltr =
(LayerTableRecord)tran.GetObject(objEntr, OpenMode.ForWrite);
editor.WriteMessage(" Layer Name: "+ltr.Name); ltr.Erase(); } tran.Commit();
} } catch (System.Exception e) { editor.WriteMessage("Error in iterateLayers
:: " + e.Message); } }
0 Likes
Message 6 of 13

Anonymous
Not applicable

missed a 'foo'. change to match variable you
use for

layername...


style="PADDING-RIGHT: 0px; PADDING-LEFT: 5px; MARGIN-LEFT: 5px; BORDER-LEFT: #000000 2px solid; MARGIN-RIGHT: 0px">
"Paul Richardson" <prichardson<lastpoint> wrote in message
href="news:6102696@discussion.autodesk.com">news:6102696@discussion.autodesk.com
...


No need to enumerate the table as you can just
check the layertable.Has() method. See

the tags use here to post so we can read you
code...

 


size=2><code>
[code]
{code}     

      using (Transaction tr =

       
db.TransactionManager.StartTransaction())
     
{
        LayerTable layerTable
=
         
(LayerTable)tr.GetObject(db.LayerTableId,
OpenMode.ForRead);
        //drawing may
have objects that use this
layer...
        if
((layerTable.Has(layername) &&

         
layerTable[layername] !=
db.Clayer))
       
{
          LayerTableRecord
ltr =
           
(LayerTableRecord)
           
tr.GetObject(layerTable["foo"],
OpenMode.ForRead);
         

          if (ltr !=
null)
         
{
           
ltr.UpgradeOpen();
           
ltr.Erase();
           
tr.Commit();
         
}
       
}
     
}
{code}
[/code]
</code>


style="PADDING-RIGHT: 0px; PADDING-LEFT: 5px; MARGIN-LEFT: 5px; BORDER-LEFT: #000000 2px solid; MARGIN-RIGHT: 0px">
This
is a function i have used to delete all layers that i hv created using
function createLayer() --->
private void eraseLayers() { Database db
= HostApplicationServices.WorkingDatabase; Editor editor =
Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
try { using (Transaction tran = db.TransactionManager.StartTransaction()) {
LayerTable layerTable = (LayerTable)tran.GetObject(db.LayerTableId,
OpenMode.ForWrite); SymbolTableEnumerator sysTabEnum =
layerTable.GetEnumerator(); while (sysTabEnum.MoveNext() == true) { ObjectId
objEntr = (ObjectId)sysTabEnum.Current; LayerTableRecord ltr =
(LayerTableRecord)tran.GetObject(objEntr, OpenMode.ForWrite);
editor.WriteMessage(" Layer Name: "+ltr.Name); ltr.Erase(); } tran.Commit();
} } catch (System.Exception e) { editor.WriteMessage("Error in iterateLayers
:: " + e.Message); } }
0 Likes
Message 7 of 13

Anonymous
Not applicable

I'm just curious, I notice you checked
for current layer but what if the layer is not empty, frozen or
locked?

 

Joe ...

 


style="PADDING-RIGHT: 0px; PADDING-LEFT: 5px; MARGIN-LEFT: 5px; BORDER-LEFT: #000000 2px solid; MARGIN-RIGHT: 0px">
"Paul Richardson" <prichardson<lastpoint> wrote in message
href="news:6102699@discussion.autodesk.com">news:6102699@discussion.autodesk.com
...


missed a 'foo'. change to match variable you
use for

layername...


style="PADDING-RIGHT: 0px; PADDING-LEFT: 5px; MARGIN-LEFT: 5px; BORDER-LEFT: #000000 2px solid; MARGIN-RIGHT: 0px">
"Paul Richardson" <prichardson<lastpoint> wrote in message
href="news:6102696@discussion.autodesk.com">news:6102696@discussion.autodesk.com
...


No need to enumerate the table as you can just
check the layertable.Has() method. See

the tags use here to post so we can read you
code...

 


size=2><code>
[code]
{code}     

      using (Transaction tr =

       
db.TransactionManager.StartTransaction())
     
{
        LayerTable layerTable
=
         
(LayerTable)tr.GetObject(db.LayerTableId,
OpenMode.ForRead);
        //drawing
may have objects that use this
layer...
        if
((layerTable.Has(layername) &&

         
layerTable[layername] !=
db.Clayer))
       
{
          LayerTableRecord
ltr =
           
(LayerTableRecord)
           
tr.GetObject(layerTable["foo"],
OpenMode.ForRead);
         

          if (ltr !=
null)
         
{
           
ltr.UpgradeOpen();
           
ltr.Erase();
           
tr.Commit();
         
}
       
}
     
}
{code}
[/code]
</code>


style="PADDING-RIGHT: 0px; PADDING-LEFT: 5px; MARGIN-LEFT: 5px; BORDER-LEFT: #000000 2px solid; MARGIN-RIGHT: 0px">
This
is a function i have used to delete all layers that i hv created using
function createLayer() --->
private void eraseLayers() { Database
db = HostApplicationServices.WorkingDatabase; Editor editor =
Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
try { using (Transaction tran = db.TransactionManager.StartTransaction())
{ LayerTable layerTable = (LayerTable)tran.GetObject(db.LayerTableId,
OpenMode.ForWrite); SymbolTableEnumerator sysTabEnum =
layerTable.GetEnumerator(); while (sysTabEnum.MoveNext() == true) {
ObjectId objEntr = (ObjectId)sysTabEnum.Current; LayerTableRecord ltr =
(LayerTableRecord)tran.GetObject(objEntr, OpenMode.ForWrite);
editor.WriteMessage(" Layer Name: "+ltr.Name); ltr.Erase(); }
tran.Commit(); } } catch (System.Exception e) { editor.WriteMessage("Error
in iterateLayers :: " + e.Message); }
}
0 Likes
Message 8 of 13

Anonymous
Not applicable

Locked or frozen won't matter... Obviously you
cannot delete a layer that entities are using.


style="PADDING-RIGHT: 0px; PADDING-LEFT: 5px; MARGIN-LEFT: 5px; BORDER-LEFT: #000000 2px solid; MARGIN-RIGHT: 0px">


I'm just curious, I notice you checked
for current layer but what if the layer is not empty, frozen or
locked?

 

Joe ...

 


style="PADDING-RIGHT: 0px; PADDING-LEFT: 5px; MARGIN-LEFT: 5px; BORDER-LEFT: #000000 2px solid; MARGIN-RIGHT: 0px">
"Paul Richardson" <prichardson<lastpoint> wrote in message
href="news:6102699@discussion.autodesk.com">news:6102699@discussion.autodesk.com
...


missed a 'foo'. change to match
variable you use for

layername...


style="PADDING-RIGHT: 0px; PADDING-LEFT: 5px; MARGIN-LEFT: 5px; BORDER-LEFT: #000000 2px solid; MARGIN-RIGHT: 0px">
"Paul Richardson" <prichardson<lastpoint> wrote in message

href="news:6102696@discussion.autodesk.com">news:6102696@discussion.autodesk.com
...


No need to enumerate the table as you can
just check the layertable.Has() method. See

the tags use here to post so we can read you
code...

 


size=2><code>
[code]
{code}     

      using (Transaction tr =

       
db.TransactionManager.StartTransaction())
     
{
        LayerTable layerTable
=
         
(LayerTable)tr.GetObject(db.LayerTableId,
OpenMode.ForRead);
        //drawing
may have objects that use this
layer...
        if
((layerTable.Has(layername) &&

         
layerTable[layername] !=
db.Clayer))
       
{
         
LayerTableRecord ltr
=
           
(LayerTableRecord)
           
tr.GetObject(layerTable["foo"],
OpenMode.ForRead);
         

          if (ltr !=
null)
         
{
           
ltr.UpgradeOpen();
           
ltr.Erase();
           
tr.Commit();
         
}
       
}
     
}
{code}
[/code]
</code>


style="PADDING-RIGHT: 0px; PADDING-LEFT: 5px; MARGIN-LEFT: 5px; BORDER-LEFT: #000000 2px solid; MARGIN-RIGHT: 0px">
This
is a function i have used to delete all layers that i hv created using
function createLayer() --->
private void eraseLayers() { Database
db = HostApplicationServices.WorkingDatabase; Editor editor =
Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
try { using (Transaction tran =
db.TransactionManager.StartTransaction()) { LayerTable layerTable =
(LayerTable)tran.GetObject(db.LayerTableId, OpenMode.ForWrite);
SymbolTableEnumerator sysTabEnum = layerTable.GetEnumerator(); while
(sysTabEnum.MoveNext() == true) { ObjectId objEntr =
(ObjectId)sysTabEnum.Current; LayerTableRecord ltr =
(LayerTableRecord)tran.GetObject(objEntr, OpenMode.ForWrite);
editor.WriteMessage(" Layer Name: "+ltr.Name); ltr.Erase(); }
tran.Commit(); } } catch (System.Exception e) {
editor.WriteMessage("Error in iterateLayers :: " + e.Message); }
}
0 Likes
Message 9 of 13

Anonymous
Not applicable
Hey Thanks Paul, I got it.







Also i will take care of tag next time but how to use these tags.

I tried in following way but didnt work.



<code>

[code]

if(l == null)

ed.write("aaa");

[/code]

</code>
0 Likes
Message 10 of 13

Anonymous
Not applicable

The tags don't seem to be working - the way I did
it anyway. I saw Tony do it in a post

recently - worked for him.


style="PADDING-RIGHT: 0px; PADDING-LEFT: 5px; MARGIN-LEFT: 5px; BORDER-LEFT: #000000 2px solid; MARGIN-RIGHT: 0px">
Hey
Thanks Paul, I got it.







Also i will take care of
tag next time but how to use these tags.

I tried in following way but
didnt work.



<code>

[code]

if(l ==
null)

ed.write("aaa");

[/code]

</code>
0 Likes
Message 11 of 13

Anonymous
Not applicable
It looks like your're posting in HTML rather than plain text.

--

http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2009
Supporting AutoCAD 2000 through 2009
http://www.acadxtabs.com

Introducing AcadXTabs 2010:
http://www.caddzone.com/acadxtabs/AcadXTabs2010.htm


"Paul Richardson" wrote in message news:6102697@discussion.autodesk.com...
Actually this looks like crap in the webbrowser...
"Paul Richardson" wrote in message news:6102696@discussion.autodesk.com...
No need to enumerate the table as you can just check the layertable.Has() method. See
the tags use here to post so we can read you code...


[code]
{code}
using (Transaction tr =
db.TransactionManager.StartTransaction())
{
LayerTable layerTable =
(LayerTable)tr.GetObject(db.LayerTableId, OpenMode.ForRead);
//drawing may have objects that use this layer...
if ((layerTable.Has(layername) &&
layerTable[layername] != db.Clayer))
{
LayerTableRecord ltr =
(LayerTableRecord)
tr.GetObject(layerTable["foo"], OpenMode.ForRead);

if (ltr != null)
{
ltr.UpgradeOpen();
ltr.Erase();
tr.Commit();
}
}
}
{code}
[/code]

wrote in message news:6102599@discussion.autodesk.com...
This is a function i have used to delete all layers that i hv created using function createLayer() ---> private void eraseLayers() { Database db = HostApplicationServices.WorkingDatabase; Editor editor = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor; try { using (Transaction tran = db.TransactionManager.StartTransaction()) { LayerTable layerTable = (LayerTable)tran.GetObject(db.LayerTableId, OpenMode.ForWrite); SymbolTableEnumerator sysTabEnum = layerTable.GetEnumerator(); while (sysTabEnum.MoveNext() == true) { ObjectId objEntr = (ObjectId)sysTabEnum.Current; LayerTableRecord ltr = (LayerTableRecord)tran.GetObject(objEntr, OpenMode.ForWrite); editor.WriteMessage(" Layer Name: "+ltr.Name); ltr.Erase(); } tran.Commit(); } } catch (System.Exception e) { editor.WriteMessage("Error in iterateLayers :: " + e.Message); } }
0 Likes
Message 12 of 13

Anonymous
Not applicable
Joe.... errrrrr I mean "Steve" (lol)...

Where did you get the idea that you can't purge a
frozen or locked layer?

--

http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2009
Supporting AutoCAD 2000 through 2009
http://www.acadxtabs.com

Introducing AcadXTabs 2010:
http://www.caddzone.com/acadxtabs/AcadXTabs2010.htm


"Steve" wrote in message news:6102839@discussion.autodesk.com...
I'm just curious, I notice you checked for current layer but what if the layer is not empty, frozen or locked?

Joe ...

"Paul Richardson" wrote in message news:6102699@discussion.autodesk.com...
missed a 'foo'. change to match variable you use for
layername...
"Paul Richardson" wrote in message news:6102696@discussion.autodesk.com...
No need to enumerate the table as you can just check the layertable.Has() method. See
the tags use here to post so we can read you code...


[code]
{code}
using (Transaction tr =
db.TransactionManager.StartTransaction())
{
LayerTable layerTable =
(LayerTable)tr.GetObject(db.LayerTableId, OpenMode.ForRead);
//drawing may have objects that use this layer...
if ((layerTable.Has(layername) &&
layerTable[layername] != db.Clayer))
{
LayerTableRecord ltr =
(LayerTableRecord)
tr.GetObject(layerTable["foo"], OpenMode.ForRead);

if (ltr != null)
{
ltr.UpgradeOpen();
ltr.Erase();
tr.Commit();
}
}
}
{code}
[/code]

wrote in message news:6102599@discussion.autodesk.com...
This is a function i have used to delete all layers that i hv created using function createLayer() ---> private void eraseLayers() { Database db = HostApplicationServices.WorkingDatabase; Editor editor = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor; try { using (Transaction tran = db.TransactionManager.StartTransaction()) { LayerTable layerTable = (LayerTable)tran.GetObject(db.LayerTableId, OpenMode.ForWrite); SymbolTableEnumerator sysTabEnum = layerTable.GetEnumerator(); while (sysTabEnum.MoveNext() == true) { ObjectId objEntr = (ObjectId)sysTabEnum.Current; LayerTableRecord ltr = (LayerTableRecord)tran.GetObject(objEntr, OpenMode.ForWrite); editor.WriteMessage(" Layer Name: "+ltr.Name); ltr.Erase(); } tran.Commit(); } } catch (System.Exception e) { editor.WriteMessage("Error in iterateLayers :: " + e.Message); } }
0 Likes
Message 13 of 13

Anonymous
Not applicable
Yes I was - Thanks.
"Tony Tanzillo" wrote in message news:6103296@discussion.autodesk.com...
It looks like your're posting in HTML rather than plain text.

--

http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2009
Supporting AutoCAD 2000 through 2009
http://www.acadxtabs.com

Introducing AcadXTabs 2010:
http://www.caddzone.com/acadxtabs/AcadXTabs2010.htm


"Paul Richardson" wrote in message news:6102697@discussion.autodesk.com...
Actually this looks like crap in the webbrowser...
"Paul Richardson" wrote in message news:6102696@discussion.autodesk.com...
No need to enumerate the table as you can just check the layertable.Has() method. See
the tags use here to post so we can read you code...


[code]
{code}
using (Transaction tr =
db.TransactionManager.StartTransaction())
{
LayerTable layerTable =
(LayerTable)tr.GetObject(db.LayerTableId, OpenMode.ForRead);
//drawing may have objects that use this layer...
if ((layerTable.Has(layername) &&
layerTable[layername] != db.Clayer))
{
LayerTableRecord ltr =
(LayerTableRecord)
tr.GetObject(layerTable["foo"], OpenMode.ForRead);

if (ltr != null)
{
ltr.UpgradeOpen();
ltr.Erase();
tr.Commit();
}
}
}
{code}
[/code]

wrote in message news:6102599@discussion.autodesk.com...
This is a function i have used to delete all layers that i hv created using function createLayer() ---> private void
eraseLayers() { Database db = HostApplicationServices.WorkingDatabase; Editor editor =
Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor; try { using (Transaction tran =
db.TransactionManager.StartTransaction()) { LayerTable layerTable = (LayerTable)tran.GetObject(db.LayerTableId, OpenMode.ForWrite);
SymbolTableEnumerator sysTabEnum = layerTable.GetEnumerator(); while (sysTabEnum.MoveNext() == true) { ObjectId objEntr =
(ObjectId)sysTabEnum.Current; LayerTableRecord ltr = (LayerTableRecord)tran.GetObject(objEntr, OpenMode.ForWrite);
editor.WriteMessage(" Layer Name: "+ltr.Name); ltr.Erase(); } tran.Commit(); } } catch (System.Exception e) {
editor.WriteMessage("Error in iterateLayers :: " + e.Message); } }
0 Likes