Thursday, March 08, 2012

Alignment Length Report

A long time ago I created an Alignment Length Report that created a report of all of the alignment lengths. The post was done utilizing VBA. Since VBA is on the way out, and I got a request for an updated report I decided to update the older blog post.

The first step is to dig out a starting point. In this case I decided to start with the Pipe Sample code located in this location: C:\Program Files\Autodesk\AutoCAD Civil 3D 2011\Sample\Civil 3D API\DotNet\VB.NET\PipeSample

Once we have it, open it up in Microsoft Visual Studio Express for VBA (or the professional version of the software). Next load the references, loading them is from the Project Properties.


Since we are going to export the data to Excel, we will need to add the Microsoft.Office.Interop.Excel. You may find that at the location in the image below or you will find it here: C:\ProgramData\Autodesk\C3D 2011\enu\Data\Reports\Net


I’m going to use PipeSample command and call it AlignmentsLengthReport. Delete the lines shown below.


Next change the Imports Statement to go from PipeNetwork to Land.


Also add “Imports Excel = Microsoft.Office.Interop.Excel” to the list.

Next delete all of the code blocks that are complaining in the error list. Make sure to keep the GetBaseObjects() function.

Now we can put in the code.

    <CommandMethod("AlignmentsLengthReport")> _
Public Sub CommandPipeSample()
Using m_tm.StartTransaction
If m_bIsCreated = False Then
Dim excelApp As Excel.Application
Dim excelWorkbook As Excel.Workbook
Dim excelSheet As Excel.Worksheet

excelApp = CreateObject("Excel.Application")
excelApp.Visible = True

' Get a new workbook
excelWorkbook = excelApp.Workbooks.Add
excelSheet = excelWorkbook.ActiveSheet

Dim row As Integer = 1
Dim column As Integer = 1

excelSheet.Cells(row, 1).Value = "Alignment Name"
excelSheet.Cells(row, 2).Value = "Alignment Length"

row += 1

Dim objIds As ObjectIdCollection = m_oDocument.GetAlignmentIds()
For Each alignmentObjId As ObjectId In objIds
Dim alignment As Alignment = DirectCast(alignmentObjId.GetObject(OpenMode.ForRead), Alignment)
excelSheet.Cells(row, 1).Value = alignment.Name
excelSheet.Cells(row, 2).Value = alignment.Length
row += 1
ElseIf m_bIsCreated Then
MsgBox("Please zoom to the network with the name of " + NETWORK_NAME)
End If
End Using
m_doc.Editor.WriteMessage("Error outputting results")
End Try
End Sub

The code creates a worksheet in Excel and then goes through each alignment and exports the alignment example. The code works better than the older version in that it directly gets the alignments, rather then going through all of the objects in the drawing looking for them.

The source code and dll may be found here. It is contained within a ZIP file, but the file extension has been changed to Change. Change the file extension to zip and extract the contents. Click on the dll and go to it’s properties and choose to Unblock the file. Once in Civil 3D 2011 type NETLOAD at the command line. Then type the AlignmentsLengthReport at the command line. It will then open Excel and output the results.

If you want to use the program in a different version of Civil 3D you will need to update the references and rebuild the project.

No comments:


Blog Widget by LinkWithin