Author Topic: Layer list from NON active document  (Read 1184 times)

0 Members and 1 Guest are viewing this topic.

Oak3s

  • Guest
Layer list from NON active document
« on: September 06, 2011, 02:23:41 PM »
Is it possible to get a layertable from a non active document? If so, can someone point me in the right direction.

What I would like to explore building is a tool displaying a layer list (just the names) from a closed dwg.

Any help is appreciated.

BlackBox

  • King Gator
  • Posts: 3770
Re: Layer list from NON active document
« Reply #1 on: September 06, 2011, 02:46:04 PM »
"How we think determines what we do, and what we do determines what we get."

Oak3s

  • Guest
Re: Layer list from NON active document
« Reply #2 on: September 06, 2011, 03:47:26 PM »
Awesome. Thank you. I was just there and I think this will get me started.

BlackBox

  • King Gator
  • Posts: 3770
Re: Layer list from NON active document
« Reply #3 on: September 06, 2011, 03:49:25 PM »
Happy to help; Good luck!
"How we think determines what we do, and what we do determines what we get."

Jeff H

  • Needs a day job
  • Posts: 6149
Re: Layer list from NON active document
« Reply #4 on: September 06, 2011, 04:08:41 PM »
I just happen to start messing with something that was grabbing everything from a closed drawings model space.
And hard-coded it quickly for testing making sure I set all the parameters correct in functions and trying it in different scenarios with drawing open etc......
Code: [Select]
        [CommandMethod("LoadStandardDetail")]
        public void LoadStandardDetail()
        {
            string fileName = @"C:\Users\Jeff\Documents\HCSgroup\Detail Library\E401.dwg";
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            using (Database detailDb = new Database(false, true))
            {               
                detailDb.ReadDwgFile(fileName, FileOpenMode.OpenForReadAndWriteNoShare, true, null);
                using (Transaction detailTrx = detailDb.TransactionManager.StartTransaction())
                {
                    BlockTable detailBt = (BlockTable)detailTrx.GetObject(detailDb.BlockTableId, OpenMode.ForRead);
                    BlockTableRecord detailModelSpace = (BlockTableRecord)detailTrx.GetObject(detailBt[BlockTableRecord.ModelSpace], OpenMode.ForRead);
                    ObjectIdCollection detailEntitiesIds = new ObjectIdCollection();
                    foreach (ObjectId objId in detailModelSpace)
                    {
                        detailEntitiesIds.Add(objId);
                    }
                    using (Transaction trx = db.TransactionManager.StartTransaction())
                    {
                        BlockTable bt = (BlockTable)trx.GetObject(db.BlockTableId, OpenMode.ForRead);                       
                        ObjectId modelSpaceId = bt[BlockTableRecord.ModelSpace];
                        IdMapping map = new IdMapping();
                        detailDb.WblockCloneObjects(detailEntitiesIds, modelSpaceId, map, DuplicateRecordCloning.MangleName, false);
                        trx.Commit();
                    }
                    detailTrx.Commit();
                }
            }

        }

 
Which can be modified easily to print all the layers from a closed drawing to the command line and then modify to your specifications
 
Code: [Select]
        [CommandMethod("PrintLayers")]
        public void PrintLayers()
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;
            string fileName = @"C:\Users\Jeff\Documents\HCSgroup\Detail Library\E401.dwg";
            List<string> layerNames = new List<string>();
            using (Database detailDb = new Database(false, true))
            {               
                detailDb.ReadDwgFile(fileName, FileOpenMode.OpenForReadAndWriteNoShare, true, null);
                using (Transaction detailTrx = detailDb.TransactionManager.StartTransaction())
                {
                    LayerTable detailLayerTbl = (LayerTable)detailTrx.GetObject(detailDb.LayerTableId, OpenMode.ForRead);
                   
                    foreach (ObjectId objId in detailLayerTbl)
                    {
                        LayerTableRecord layerTblRecord = (LayerTableRecord)detailTrx.GetObject(objId, OpenMode.ForRead);
                        layerNames.Add(layerTblRecord.Name);
                    }                 
                    detailTrx.Commit();
                }
            }
            foreach (string layerName in layerNames)
            {
                ed.WriteMessage("\n{0}", layerName);
            }
        }
    }
}