No More Drudgery (From the Trenches AutoCAD Tutorial)1 Aug, 2007 By: Salvatore Napolitano
Write your own VBA code for AutoCAD to draft underground ductile iron pipe.
There's no easy way to draft underground ductile iron pipelines for water and wastewater. Anyone who has drafted single-line ductile iron pipe knows that it's very laborious. Why is it difficult to draft ductile iron pipe and why does Visual Basic offer a good solution?
Ductile Iron Pipe Layout Rules
If you want to draft a pipeline that can be constructed by a contractor as designed, you must pay attention to both bends and deflections. Bends are the fittings used to change direction and they are at 90-, 45-, 22 1/2-, and 11 1/4-degree angles. Each pipe joint can be deflected by 2-8 degrees depending on the pipe diameter and joint type. For example, a 6" pipe with push-on-joint can be deflected as much as 5 degrees, and the same pipe diameter with a mechanical joint can be deflected as much as 7 degrees. DIPRA provides tables where you can look up the allowable deflection as shown below. Although, you don't want to be referring to tables while drafting!
When needed, you can look up deflection information in table, although you don't want to do this all the time while you're busy drafting.
We often draft many miles of pipeline, sometimes as many as 11 miles, and a program to help us would be very handy. Before writing a program, we searched for free programs first. Vaultic offers 2D and a 3D piping software, but the software doesn't consider bends and deflections. It uses a multiline for 2D piping, but we typically use a single line to represent the pipe and again there is no consideration for angles. The Vaultic software does have a multitude of 3D fittings that were very impressive if your company's mechanical department does not want to redraw hundreds of fittings.
Pick a Programming Language
When I found that there wasn't anything available, I set out to write my own program.
I knew I needed a dialog box so the user could have a drop-down list of only valid pipe sizes that vary with the pipe joint selected. The problem was I knew AutoLISP for AutoCAD, but I never bothered to learn Visual LISP or dialog control language (DCL) for dialog box interfacing. I certainly didn't know Visual Basic for Applications (VBA). In searching for a book to learn DCL for AutoLISP, I read two Cadalyst articles that convinced me to learn VBA rather than DCL:
- "Making the Case for VBA," by Barry Bowen, July 1, 2003.
- "Establishing a Dialog," by Bill Kramer, March 1, 1999.
Basically, the authors explained DCL programming was cumbersome and used only in CAD. VBA for AutoCAD was much more convenient for dialog boxes (a main goal), and VBA is used in many Windows applications, making it a valuable tool to learn.
Rather than teach myself DCL, I decided to learn VBA. The customer reviews raved about Joe Sutphin's book, "AutoCAD 2006: a Programmer's Reference," published by Apress. I became familiar with the language with this book, but I could not program after reading it. The fact that it is a reference should have been a clue that it was not a teaching book. For my next step, I ordered "Using Visual BASIC with AutoCAD 2002" by Andrew Roe, a Thompson Learning Autodesk Press book. This book was like taking a class. Believe me, if you read this book you can program in VBA also!
This was the information I needed to tackle developing a ductile iron pipe program with dialog boxes. The biggest surprise for me was that I was missing all the AutoLISP geometry functions. When you do your own programming, you'll need to develop your own functions such as the one shown below to find the distance between two points.
'FUNCTION: DIST BETWEEN TWO POINTS USING Public Function DistPtPt(PT1 As Variant, PT2 As Variant) As Double DistPtPt = Sqr((PT2(0) - PT1(0)) ^ 2 + (PT2(1) - PT1(1)) ^ 2) End Function
Be sure that your program draws lines at exact bends to the previous lines. Your program should then prompt you to Undo the last line or Deflect the last line to a maximum allowable limit based on the pipe size and fitting type. Study the flow chart below for details on programming decisions you'll need to make. There are a lot of geometry and angles, but it's well worth the drafting-time savings.
Keep in mind that there are numerous advantages to building your own VBA program:
- The program will pay for itself very quickly because it will cut drafting time significantly.
- The program will eliminate drafting errors in bend and deflection angles and in turn will reduce construction problems.
- You can always append the program to do more, such as automatic text labels, quantity takeoff, and offsets off of curb lines and fire hydrants.
I haven't added any enhancements yet, but using my basic program definitely takes the drudgery out drafting ductile iron pipe. Click here to download additional samples to help you write this handy program.
When developing your own VBA program, follow this decision tree.
About the Author: Salvatore Napolitano
In her easy-to-follow, friendly style, long-time Cadalyst contributing editor Lynn Allen guides you through a new feature or time-saving trick in every episode of her popular AutoCAD Video Tips. Subscribe to the free Cadalyst Video Picks newsletter, and we'll notify you every time a new video tip is published. All exclusively from Cadalyst!