Using VBA from Access 2007 to search in AutoCAD (dwg) files
I have hundreds of DWG, AutoCAD files that I would like search and catalog into an MS Access Database.
Basically, I would like to search the DWG's and extract whatever description is in the Title box as well as the Date and bring everything over to Acc开发者_高级运维ess making it a searchable catalog.
For example, I have a file name T-25682.DWG, which is titled Machine Spacer and created 01/20/2010.
I would extract that info form the DWG file and insert it into the Access database as such:
== ID == == DESCRIPTION == == CREATED ON == == FILENAME ==
1 Machine Spacer 01/20/2010 T-25682.dwg
How can I approach and solve this problem? Is there an AutoCAD library I can use with Access? How can I search in a DWG file?
I would avoid VBA altogether, AutoLISP can do the job for with far less pain. Here's how:
Create the "data extraction and writing to Access" functionality inside an AutoLISP file. The freely available ADOLisp library will make it a breeze writing to Access, if that fails, or you aren't able to do it you can always just write to a csv file...
Once you're able to do that for a single dwg file, create a script (using anything that can copy and open files, AutoLisp works too) to do the following:
- Copy that lisp file into a directory where your dwg files are, naming the file as acaddoc.lsp.
- Sequentially open every dwg in the directory. Upon opening, acaddoc.lsp will run and do its stuff.
- Delete acaddoc.lsp from that directory (else it will run every time you open a dwg in there).
- Repeat for every directory you have dwg files in which you wish to catalog.
Notes:
- Make sure that acaddoc.lsp closes the drawing when its done (or makes AutoCAD quit, depending on how you're opening the files).
- For this to work, your title blocks need to be reliable, make sure you add some error checking. You can use the script to log any issues to a text file as they find them...
AutoLisp is really really easy, for help with learning go to AutoLISP Beginners' Tutorials.
For the best place to ask questions and search for code snippets from previous answered questions, see Visual LISP, AutoLISP and General Customization.
If you have a full version of AutoCAD you can try the Data Extraction Wizard. This works quite well for attributed blocks. If this is no good, the best places to try are the Swamp or the AutoCAD forums or AUGI.
VBA is deprecated in the last 3 versions of AutoCAD in favour of the .NET API, FYI
+edit+ Have a look at this (free) chapter on AutoCAD external database connectivity.
精彩评论