Thursday, March 03, 2005
Extracting a BOM from blocks
Platform: AutoCAD 2004
Download: ExtractBOM.zip
Creating and maintaining Bill of Material lists or BOMs have always been a tedious task in AutoCAD. Many of the Autodesk vertical products (Mechanical, MDT, Inventor) have inherent tools to help but if you're stuck with vanilla AutoCAD like me, you know the pain and suffering involved in creating an accurate BOM list.
The company I currently work for has instituted the standard of blocking every item in their assembly drawings with intelligent names; this really speeds up the detailer's jobs and allows for easy reuse in later projects. Most of the drawings I work with are plan-view layouts, so I only have one view to worry about. I've taken a bit of my own time and expanded on their idea, what if one could extract a first level BOM (no sub-assemblies or sub objects listed) and export it to a CSV file? CSV files are great because they open natively in Microsoft Excel without all the hassle of programming a hook into Excel. Before anyone asks, yes, I know using a collection would be quicker when searching for duplicate items but VBA has a few quirks with collections so I opted to use a for-next loop, just to be safe.
There are a couple of neat tricks in this article, for one the 'GetAttrValue' function is a nicely encapsulated function to extract a single known attribute value. Also, most people seem to miss the fact that you can search for multiple strings while creating a selection set. Simply separate each string by a comma and end each term with an asterisk wild card. For example, to search for all blocks that start with the words 'TEST' or 'HARDWARE', use the following:
CreateSelectionSet(objSS, DXF_NAME, "TEST*,HARDWARE*")
This article is far from a complete BOM solution; it simply asks you to select items on screen, extracts an item number from each block name and a description from an attribute named 'DESC'. The code for this article is quite extensive (about 260 lines) so please download it from the link above. The zip file also includes a sample drawing I whipped together to show you how it works.
Once you have the DVB extracted and loaded, open the Macros window (press Alt+F8) and run the 'CreateBOM' macro to get things started. If for some reason the macro is not showing up, go to the VBAIDE and run the CreateBOM subroutine.
Here are some thoughts for future development:
If you enjoyed this article and would like to see an example of exporting the BOM to the screen, drop me a line.
Download: ExtractBOM.zip
Creating and maintaining Bill of Material lists or BOMs have always been a tedious task in AutoCAD. Many of the Autodesk vertical products (Mechanical, MDT, Inventor) have inherent tools to help but if you're stuck with vanilla AutoCAD like me, you know the pain and suffering involved in creating an accurate BOM list.
The company I currently work for has instituted the standard of blocking every item in their assembly drawings with intelligent names; this really speeds up the detailer's jobs and allows for easy reuse in later projects. Most of the drawings I work with are plan-view layouts, so I only have one view to worry about. I've taken a bit of my own time and expanded on their idea, what if one could extract a first level BOM (no sub-assemblies or sub objects listed) and export it to a CSV file? CSV files are great because they open natively in Microsoft Excel without all the hassle of programming a hook into Excel. Before anyone asks, yes, I know using a collection would be quicker when searching for duplicate items but VBA has a few quirks with collections so I opted to use a for-next loop, just to be safe.
There are a couple of neat tricks in this article, for one the 'GetAttrValue' function is a nicely encapsulated function to extract a single known attribute value. Also, most people seem to miss the fact that you can search for multiple strings while creating a selection set. Simply separate each string by a comma and end each term with an asterisk wild card. For example, to search for all blocks that start with the words 'TEST' or 'HARDWARE', use the following:
CreateSelectionSet(objSS, DXF_NAME, "TEST*,HARDWARE*")
This article is far from a complete BOM solution; it simply asks you to select items on screen, extracts an item number from each block name and a description from an attribute named 'DESC'. The code for this article is quite extensive (about 260 lines) so please download it from the link above. The zip file also includes a sample drawing I whipped together to show you how it works.
Once you have the DVB extracted and loaded, open the Macros window (press Alt+F8) and run the 'CreateBOM' macro to get things started. If for some reason the macro is not showing up, go to the VBAIDE and run the CreateBOM subroutine.
Here are some thoughts for future development:
- Filter out alternate views (top, side, front) of the same object
- Create the BOM in the drawing instead of exporting to a CSV
- Sort the BOM before exporting it to screen or file
- Create the BOM as an AutoCAD table entity (AutoCAD 2005 or greater required)
If you enjoyed this article and would like to see an example of exporting the BOM to the screen, drop me a line.