Monday, 27 May 2013



AutoCAD VBA - Calculate Polylines Length & Area

At the beginning of April I posted an Excel VBA code for creating a polyline in AutoCAD using coordinates from an Excel workbook. In the comments of that post Jake asked me to do almost the opposite procedure. So, below you will find an AutoCAD VBA add-in that can:
  • Calculate the length of all polylines, as well as the area of all closed polylines from a selected layer.
  • Calculate the length of all polylines, as well as the area of all closed polylines from all the layers of the current drawing.
  • Export the results to a new Excel workbook.
In general, I think that this add-in might be quite useful for civil engineers, as well as for anyone who uses polylines at his/her drawings and needs a way to calculate quickly their length and area.

I should point out that the add-in calculates the area from closed polylines only. So, this means that when the polyline was drawn, the closed option should have been used at the end. If a polyline was left opened or it was manually closed (by linking the last point with the first point), then the code will not take its area into account. However, its length will be measured. The video below has an example of polylines who look closed but they aren’t.


How to use it
  1. Open your AutoCAD.
  2. In command line write VBAMAN.
  3. Press the Load button in the menu that will appear.
  4. At the browser find the file Polyline Area.dvb.
  5. Choose the Enable Macros button and then press the Macros button.
  6. At the macros menu, press the Run button. A form will appear (see figure above).
  7. In the form you have two choices: you can either select a layer from the combo box or you can check the check box to loop through all the layers of the current drawing. In both cases press the Calculate Length & Area button after. The list box will be populated with information from all the polylines of the selected layer or all the layers of the drawing. Two text boxes below the list box will contain the total length and the total area.
  8. Finally, if you press the Export To Excel button a new workbook will be created and will contain all the results of the list box in order to be further processed (for reporting purposes).
If you have closed the form and want to re-use it you don't have to reload the file. You can simply write VBARUN at the command line and continue the procedure from step 6. If you have AutoCAD 2010 or a newer version, then you will have to download the VBA module from Autodesk, since from 2010 VBA is not installed with AutoCAD.


Demonstration video

The short video below demonstrates the use of add-in in practice. By the way, in this video you will listen for the time my terrible voice!!! I know that I sound like a robot, but, I hope that at least you can understand what I am trying to say... 

  


Download it from here


The file was tested with AutoCAD 2013, but it will probably work in either older or newer AutoCAD versions.


Read also

Draw A Polyline In AutoCAD Using Excel VBA
AutoCAD – Calculate The Total Length Of Lines
AutoCAD – Total Length Of Lines Per Layer

Did you like this post? If yes, then share it with your friends. Thank you!



Categories:


Mechanical Engineer (Ph.D. cand.), M.Sc. Cranfield University, Dipl.-Ing. Aristotle University, Thessaloniki - Greece.
Communication: tel. +30-6973513308, e-mail , Facebook , Twitter , Google+ and Linkedin. Full CV