February 26, 2009

For Nerds Only. Field Room Tags in AutoCAD.

In spite of today's powerful CAD and BIM systems, most drafting work is pretty basic. Still, AutoCAD and similar products rely on series of connected line and arc segments, usually separated by layers: CAD documents are still just drawings – mostly they do not imply any building data or structure. Definitely it's a pain if you have to populate CAD plans with space and room tags. For each change a drafter (or an architect) has to redraw the polylines and correct the attribute values of the room tags. I was looking for an AutoCAD script that simplifies the procedure of using the ‘field' feature of AutoCAD and link each room tag attribute to an area polylines. The advantage: if you change the polyline the value of the room tag changes immediately. Not quite BIM but maybe a help.

Start Visual Basic in AutoCAD. Type 'vbaide'. Paste the code and execute it. In the example you need one closed polyline and a two attributes in your room tag block. (1. room, 2. area) Please refer to the AutoCAD help if you like to integrate the tool to the user interface.

' AREA TAG TOOL
' fills an attribute with a field
' by anarchitecture

Sub fillField()
Dim mySelectionBlock As AcadObject
Dim mySelectionPoly As AcadObject
Dim myPointBlock As Variant
Dim myPointPoly As Variant
Dim tAtts As Variant
Dim varAttributes As Variant
Utility.GetEntity mySelectionBlock, myPointBlock, "Choose a Room Tag:"
varAttributes = mySelectionBlock.GetAttributes
Utility.GetEntity mySelectionPoly, myPointPoly, "Choose Polyline to be linked:"
' replace the 2nd attribute value by field value
' IMPORTANT: the tag [0.0001] converts in this example the metric drawing unit [cm] to [m]
' IMPORTANT: the value varAttributes(1).TextString replaces the 2nd attribute value in this case
varAttributes(1).TextString = "%<\AcObjProp.16.2 Object(%<\_ObjId " _ & mySelectionPoly.ObjectID & ">%).Area \f ""%lu2%pr2%ct8[0.0001]"">%"
ThisDrawing.Regen acAllViewports
End Sub

9 comment(s):

  1. Thanks! That's what we needed - we had it in some polish version which didn't work here. I'll try it tomorrow.

    ReplyDelete
  2. it sounds really useful. Is it possible to get a bit more detailed overview or something tutorial like. It says

    Run-time '9' error:
    Subscript out of range
    :(
    Btw, acad 06
    Thanks.

    ReplyDelete
  3. Sorry. I changed my original code (because my room tag got more lines). You have to change varAttributes(2).TextString to varAttributes(1).TextString if the area is in the second attribute (VBA counts the zero). My fault. (and the code line with the underscore _ should be one line). All the Best, Christoph

    ReplyDelete
  4. Cristoph,
    just to let you know. AutoCAD Architecture has a different entity called Space, and Sapce Tags are linked to that space so if you modify the space the area shown i the tag will update.
    You can also link SPaces together in Zones to get a total of the sum of the SPaces area.

    ReplyDelete
  5. Martí,
    Thanks. Never used AutoCAD architecture (only Revit). Too bad, most offices still rely on good old ACAD.

    ReplyDelete
  6. this is great..but Im getting a compile error - Function or interface marked as restriced or the function uses an automation type not supported in VB ...and it refers to the objectID bit in the code

    any ideas how to fix this?

    ReplyDelete
  7. the routine works great in acad2009. But in ACAD2010 i get an error..
    Could you please look into that??

    ReplyDelete
  8. What I know is that 2010 doesn't support VBA anymore - can't help because I still work on 2009. Sorry.

    ReplyDelete
  9. VBA has been removed from ACAD 2010 as default in an effort to get developers to use newer .NET based technology.
    You can still run VBA scripts by downloading the VBA enabler from AutoDesks website for you version of AutoCAD

    ReplyDelete