Thursday, March 29, 2012

Linq and Civil 3D 2013

With Civil 3D 2013 also comes changes to the API. AutoCAD and Civil 3D are now being built with .NET Framework 4.0. One of the benefits of this change is the ability to use Linq to query information. Linq is short for Language Integrated Query and is created by Microsoft. With the .NET programming the API is getting away from storing information where it is easily accessed, but is located by getting ObjectIds and then having you get the objects from those ObjectIds. One example of this is the GetAlignmentIds() method of the CivilDocument.

Linq provides a fairly compact way to get the information out of those ObjectIds. If I was to try to try to see if an alignment name already exists I’d have to do a for each loop which takes quite a bit of code to perform. With Linq I can take that same process and turn it into a compact query.

bool nameExists = (from objId in civDoc.GetAlignmentIds().Cast<ObjectId>()
                               select (objId.GetObject(OpenMode.ForRead) as Alignment).Name).Contains(proposedAlignmentName);

I’ve gone from at least 5 lines of code down to two (if the code fit on this web page).

The Linq query is taking the ObjectIds from the list, converting the ObjectId to get the alignment, and then seeing if the name I’m trying to use exists. If it does then I get a true, if not I get a false value. Another benefit is that the query isn’t always run when it is hit in the code. In most cases it waits until the information is needed. This may provide performance improvements when running your code and an error happens before you need the information.

Wednesday, March 28, 2012

Civil 3D 2013–Raising Awarness

It is that time of the year where Autodesk releases the next version of Civil 3D. Here is some information regarding the new product for 2013.

The good news if you have a Design Suite is that it now includes Raster Design. There are some other products included now, but I’m less excited about those. Until Infrastructure Modeler has interoperability with Civil 3D it is pointless with small shops like mine.


There are also Cloud Units available with the Suites. Somehow I think this is going to be short term product, just because of the cheese name of the product.


Top new features!


Survey Query Functionality


Enables you to search for information from the survey. Why not have this for everything? Seems a bit short sighted to just have it for Survey Data. Why didn’t Autodesk put the time and money in getting Civil 3D objects to be viewable by Map? That way we could use existing functionality to look for all types of Civil 3D objects.

General Improvements


The improved Surface Boundary Definition allows you to create a hide boundary with another surface from a corridor boundary.




Pressure Networks!




Survey Query Dialog Box


Thursday, March 22, 2012

GrabCAD and You

GrabCAD and 3DConnexion are teaming up to give a way a 3D mouse and other prizes. I’m using it as an opportunity to pad my blog post count for the year. You can find information about the Giveaway here: GrabCAD’s website

Before you go and visit GrabCAD you might also want to check out this blog post from a fellow blogger. While it looks like GrabCAD has taken some steps to resolve the issue, its up to all of us to respect other’s wishes on what happens to their works until it becomes available in the public domain.

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.


Blog Widget by LinkWithin