Get data from drawing without opening it

Get data from drawing without opening it

dennis
Advisor Advisor
2,168 Views
5 Replies
Message 1 of 6

Get data from drawing without opening it

dennis
Advisor
Advisor

...or can I open it as ReadOnly?

I want to gather some data from another drawing, specifically in my case, some block info and layout tab info.  I took some code I had for "Design Center" like importing of blocks for insertion.  But in this case, I would like to pull the data regardless if the drawing is in use or not.  One work around, I thought of, is to copy the drawing to my temp folder then open it.  But, I thought first to see if there is an easier way.

Current code I have so far, I feed the method the drawing name I want to read:

public static void fetchDwgData(Database acDb, Editor acEd, string dsDwg)

{

  DocumentCollection acDm = acApp.DocumentManager;

  Database dsDb = new Database(false, true);

  try

   {

    dsDb.ReadDwgFile(dsDwg, System.IO.FileShare.Read, true, "");

    ObjectIdCollection blkIds = new ObjectIdCollection();

    using (Transaction acTrans = acDb.TransactionManager.StartOpenCloseTransaction())

       {

      BlockTable dsBlkTbl = (BlockTable)acTrans.GetObject(dsDb.BlockTableId, OpenMode.ForRead, false);

      foreach (ObjectId btrId in dsBlkTbl)

           {

        using (BlockTableRecord btr = (BlockTableRecord)acTrans.GetObject(btrId, OpenMode.ForRead, false))

               {

        if (!btr.IsAnonymous && !btr.IsLayout)

                {

           //do stuff

                 }

             }

        }

      }

        dsDb.Dispose();

     }

   catch

     {

    //exiting due to error

     }

}

0 Likes
Accepted solutions (1)
2,169 Views
5 Replies
Replies (5)
Message 2 of 6

norman.yuan
Mentor
Mentor

Yes, you did it correctly in regard of getting data without opening it (in AutoCAD editor). So what is your question, then?

 

One minor thing, you may want to place dsDb.Dispose() in "finally{ } clause" to make sure the database is always disposed. Better yet, use

 

using (Database dsDb= new Database())

{

 

}

 

for more simplified code than try {...} finally{...}.

Norman Yuan

Drive CAD With Code

EESignature

Message 3 of 6

dennis
Advisor
Advisor

Thanks for the suggestion Norman.  The problem arises, if someone has the drawing open already, then I can't open the drawing "on the side".  So, is there a way to Open the drawing "on the side" regardless if it is in use or not by another user?  Or, do you think my thought to copy the drawing first to the temp folder is the way to go?

0 Likes
Message 4 of 6

_gile
Consultant
Consultant
Accepted solution

Hi,

 

Try with:

 

dsDb.ReadDwgFile(dsDwg, FileOpenMode.OpenForReadAndAllShare, true, null);


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 5 of 6

dennis
Advisor
Advisor

That's it Giles!

Thank you very much, I was just Googling around and was reading about the FileOpenMode method, thinking that would be my next 'try', then my email notification popped up regarding your reply.  It works just as I need it to work.  Which now, has me wanting to revisit a few of the programs I have written to make this change as well.  Very clean, thank you.

0 Likes
Message 6 of 6

brianchapmandesign
Collaborator
Collaborator

Just a shot in the dark but if you're just trying to import borders, etc from paperspace and the viewports, you can use the Layout->from template option.


"Very funny, Scotty. Now beam down my clothes.