.NET

.NET

Reply
Active Contributor
skeletank
Posts: 26
Registered: ‎08-25-2010
Message 1 of 8 (1,032 Views)
Accepted Solution

Programmatically refresh layer usage information .NET

1032 Views, 7 Replies
10-12-2010 01:03 PM

In C# I noticed that when I loop through the LayerTableRecords in the LayerTable some of the layers in the drawing do not show up.  If I look at the layers in the UI it shows the missing layers with a grey icon for their status while the ones that it does detect have a blue icon for their status.  The status has to do with which layers that the drawing believes are in use.  In the UI you can press a refresh button and this will fix the problem.  I want to know how I can fix this through the .NET API, COM API, or command line.

It seems that the problem was that one of the entities in the particular drawing was corrupt. When one of the users recreated the drawing (including re-adding some xrefs) and ran the program it worked just fine. I also double checked the code and it was definitely hitting the correct layers this time. I am however open to suggestions or solutions on how to detect corruption problems in AutoCAD drawings.

Valued Mentor
Jeffrey_H
Posts: 372
Registered: ‎01-20-2010
Message 2 of 8 (1,021 Views)

Re: Programmatically refresh layer usage information .NET

10-12-2010 01:43 PM in reply to: skeletank

Have not tested it but have you tried the LayerTable method GenerateUsageData

You can also find your answers @ TheSwamp
Active Contributor
skeletank
Posts: 26
Registered: ‎08-25-2010
Message 3 of 8 (984 Views)

Re: Programmatically refresh layer usage information .NET

10-13-2010 06:31 AM in reply to: Jeffrey_H

I tried calling GenerateUsageData before doing a foreach loop on the LayerTable but it still didn't return all the layers.

Active Contributor
skeletank
Posts: 26
Registered: ‎08-25-2010
Message 4 of 8 (968 Views)

Re: Programmatically refresh layer usage information .NET

10-13-2010 08:53 AM in reply to: skeletank

Another thing that I noticed is that this problem only occurs when I open the database using Database.ReadDwgFile.  If I first open the drawing using Application.DocumentManager.Open and then get the database from MdiActiveDocument then all of the layers show up correctly.  I would open the drawing but it is much faster to alter the database.  All that I'm doing is setting Freeze/On/Locked on the layers.

Valued Mentor
Jeffrey_H
Posts: 372
Registered: ‎01-20-2010
Message 5 of 8 (963 Views)

Re: Programmatically refresh layer usage information .NET

10-13-2010 10:03 AM in reply to: skeletank

Can you post your code that would help alot for helping you out.

You can also find your answers @ TheSwamp
Active Contributor
skeletank
Posts: 26
Registered: ‎08-25-2010
Message 6 of 8 (960 Views)

Re: Programmatically refresh layer usage information .NET

10-13-2010 10:25 AM in reply to: skeletank

The following shows two examples of enumerating through the layers.  The second option will work, but I as I said earlier I do not want to open the drawing.

 

Here's the code for just opening the database:

 

 

using (Database database = new Database(false, true))
        {
          database.ReadDwgFile(drawingFilePath, FileShare.ReadWrite, true, String.Empty);

          using (Transaction transaction = database.TransactionManager.StartTransaction())
          {
            LayerTable layerTable = transaction.GetObject(database.LayerTableId, OpenMode.ForWrite) as LayerTable;
            database.Clayer = database.LayerZero;
            
            foreach (ObjectId layerID in layerTable)
            {
              LayerTableRecord layer = (LayerTableRecord)transaction.GetObject(layerID, OpenMode.ForWrite);

              //Do something to layer
            }

            transaction.Commit();
          }
        }

 

Here's the code when opening the drawing:

 

 

 

Application.DocumentManager.Open(drawingFilePath, false);

      Document document = Application.DocumentManager.MdiActiveDocument;
      Database database = document.Database;
      DocumentLock documentLock = document.LockDocument(DocumentLockMode.Write, null, null, true);

      using (documentLock)
      {
        using (Transaction transaction = database.TransactionManager.StartTransaction())
        {
          LayerTable layerTable = transaction.GetObject(database.LayerTableId, OpenMode.ForWrite) as LayerTable;

          database.Clayer = database.LayerZero;

          foreach (ObjectId layerID in layerTable)
          {
            LayerTableRecord layer = (LayerTableRecord)transaction.GetObject(layerID, OpenMode.ForWrite);

            //Do something to layer
          }
        }
      }

 

Active Contributor
skeletank
Posts: 26
Registered: ‎08-25-2010
Message 7 of 8 (895 Views)

Re: Programmatically refresh layer usage information .NET

10-19-2010 05:47 AM in reply to: skeletank

It seems that the problem was that one of the entities in the particular drawing was corrupt. When one of the users recreated the drawing (including re-adding some xrefs) and ran the program it worked just fine. I also double checked the code and it was definitely hitting the correct layers this time. I am however open to suggestions or solutions on how to detect corruption problems in AutoCAD drawings.

Active Contributor
harryliu3140
Posts: 46
Registered: ‎10-29-2009
Message 8 of 8 (399 Views)

Re: Programmatically refresh layer usage information .NET

05-13-2013 04:07 PM in reply to: skeletank

Exactly the same issue hasn't been solved in ACAD2012. I tried to Audit and recover my dwg file and found nothing corrupted. How many years needed to solve it?

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.