Using Pipe Sample VBA

Welcome to our newest poster, Christopher Fugitt of Tetra Tech in San Luis Obispo, CA. He runs his own blog at Civil 3D Reminders. I thought some of his posts on modifying the pipe rules and hacking VBA were great, so I invited him to post some of them here. Let me (and him) know what you think in the comments below! – JW

Civil 3D ships with some Sample VBA code for various Civil 3D objects. They provide some good starting points for creating your own programs and not have to wait for Autodesk to add it to the program in a later release. In the Autodesk Discussion Group a person was looking to create points from a pipe network with the z elevation being the invert of the pipe.

To start load the VBA sample pipe project. To do this type VBAMAN at the command line and click the Load button on the right. The sample file is located at: C:\Program Files\AutoCAD Civil 3D 2008\Sample\Civil 3D API\Vba\Pipe\PipeSample.dvb. You may want to click the SaveAs button to give it a name that is more appropriate to what you are doing and place it in a convenient location. Then click the Visual Basic Editor button in the lower left hand corner.

VBA Manager

We will just be editing code in this post on not covering everything that is possible. The steps to do what we want are below:

Expand the tree on the left of the screen to see a list of modules in the project. Double click on the ExcelFunctions module to get the module window to come front and center.

Bring To Front

Scroll down to where the module repeats sheetPipes.Cells(iRowPipes, 1).Value … These lines export the pipe information to Excel and puts it in the correct row and column. The iRowPipes indicates the Row and the number after iRowPipes is the column.

Code Example 1

Since we only want PNEZD for the output we need to change the lines in the above code. Below is the revised lines of code that need to be added. You can copy and paste this and replace the lines of code in the program. The oPipe is the pipe object, the words after the . represents the information from the pipe that will be put into Excel.

sheetPipes.Cells(iRowPipes, 1).Value = iRowPipes
sheetPipes.Cells(iRowPipes, 2).Value = oPipe.startPoint.Y
sheetPipes.Cells(iRowPipes, 3).Value = oPipe.startPoint.X
sheetPipes.Cells(iRowPipes, 4).Value = oPipe.startPoint.Z
sheetPipes.Cells(iRowPipes, 5).Value = “Pipe Start” & ” Pipe Description ” & oPipe.Description
iRowPipes = iRowPipes + 1
sheetPipes.Cells(iRowPipes, 1).Value = iRowPipes
sheetPipes.Cells(iRowPipes, 2).Value = oPipe.EndPoint.Y
sheetPipes.Cells(iRowPipes, 3).Value = oPipe.EndPoint.X
sheetPipes.Cells(iRowPipes, 4).Value = oPipe.EndPoint.Z
sheetPipes.Cells(iRowPipes, 5).Value = “Pipe End” & ” Pipe Description ” & oPipe.Description

The oPipe.EndPoint.Z value is for the center of the pipe. The poster wanted the top of pipe or invert as the Z elevation. To get the invert or Top of Pipe we will need to use a mathematic formula to get the correct value. To get the Invert take the Z value and subtract 1/2 of the pipe diameter. To get the top of pipe take the Z value and add 1/2 of the pipe diameter and add in the wall thickness of the pipe. The formulas below assume it is a circular pipe, if you have a different shape you will need to adjust the formulas accordingly. Revise the respective lines of code with the ones below to get the value you want. Make sure you note that you will need to change the EndPoint or StartPoint wording to get it to work correctly.

‘Pipe Invert:
sheetPipes.Cells(iRowPipes, 4).Value = oPipe.startPoint.Z – oPipe.InnerDiameterOrWidth / 2
‘Top of Pipe:
sheetPipes.Cells(iRowPipes, 4).Value = oPipe.EndPoint.Z + oPipe.InnerDiameterOrWidth/2 + oPipe.WallThickness

We now need to delete or hide the items in the program we don’t need. In VBA to do this we can either delete the code outright or use a ‘ in front of the lines that we don’t want. In this case I will use the ‘ in front of any other line in the code that starts with sheetPipes.Cells or sheetStructures.Cells.

Code Example 2

Now all we need to do is Press the play button to get the program to run and give us the point list that we want. The play button is at the top of the screen in the Visual Basic Editor. Now just delete any information in the Excel file that you don’t need and save the file in the format you require and import the file back into Civil 3D. You will definitely want to check the numbers that are outputted the first couple of times to verify that the numbers are correct. Since you spent the time to create the program you will probably want to create a Toolbar button to run the program from. One method to do this may be found here.

One comment

  1. […] my previous post on how to load a VBA […]