The Coding Draftsman

Changing the way you draft through code!

The “New” Tab in Civil 3D 2015 broke my DocumentActivated Event Handler!!!

Autodesk added the “New” tab in the 2015 version of AutoCAD and Civil 3D.  Many of the applications written for 2014 will run with no changes to your code.  There are a few little gotchas though.  One that I recently ran into is that the MDIActiveDocument can now be null.  This change came about due to the “New” tab.
New Tab



Most of the time we call our commands from within an active drawing window so you could go a long time without running into this little gotcha.  But when you are using the Document Manager event handlers you will run into this really fast.
The fix is really ease though.  All you have to do test the MDIActiveDocument for Null.  Here’s a quick little code example:

The code in line 2 creates the DocumentManager_DocumentActivated delegate.  The code in lines 4 through 7 tests if the current document is null.  If this test returns true a dialog box is displayed showing the current drawing file name.
The code in line 10 creates a command to named AddDocumentActivatedHandler.  This command activates our DocumentManager_DocumentActivated when ran from the command line.
The code in line 13 uses the += operator to add the DocumentManager_DocumentActivated delegate to the Application.DocumentManager.DocumentActivated event handler.
The code in line 16 creates a command named RemoveDocumentActivatedHandler.  This command deactivates the DocumentManager_DocumentActivated
The code in line 19 uses the –= operator to remove DocumentManager_DocumentActivated  from the Application.DocumentManager.DocumentActivated event handler.
That's all it takes to use the DocumentActivated event handler for AutoCAD and Civil 3D with the "New" tab.  In my next post I think we'll continue exploring the DocumentActivated event handler by creating a tool palette and manipulating some Civil 3D settings.

Using Inheritance with the Autodesk API


I don’t know about everyone else but I wasn’t (and I’m still not) an expert in C# and object orientated programming. When I started trying to program tools using the Autodesk APIs I was kind of lost.  I have sense spent a lot of time watching videos and reading books about the Dot Net Frame work and about C#. 
One very useful component of object orientated programming is inheritance.  Inheritance allows us to create a “base” class of properties and methods that another class can inherit.  I don’t know about you but I get kind of tired of typing
Document acadDoc = Application.DocumentManager.MdiActiveDocument;  and
CivilDocument civilDoc = CivilApplication.ActiveDocument; and the list goes on and on.  One way to save on some typing is to setup a class that has properties and methods to return values that we use in our code often.  Here is a sample class I created.




Our using statements take up lines 1 through 14. 

In Line 17 we see that all this code belongs to the namespace TCD_BloggingCode.  A namespace is used to declare a scope that contains related objects.  Namespaces can contain other namespaces, a class, a interface, struct, enumeration or delegate objects.  We use namespaces all the time in C# whether you realize it or not. When you type Autodesk.Autocad.ApplicationServices.Document. The Autodesk.Autocad.ApplicationServices are all namespaces.

In line 19 we create our class.  This is the class our command class will inherit.

Now lets take a look at the code in line 21. protected Document _acadDoc.  The protected keyword limits the access to this property to this class or any types that inherit this class.  The Document type is simply our standard AutoCAD document type.  I added an underscore to the standard acadDoc variable I use for the active AutoCAD document so that I know this is a property.
In line 23 we create the get block of code.  This gets the value of the property.  In this case it gets the current active document as shown in line 25.  I did not create a set parameter for this code because I want it to live in a read only state.

I’m going to jump down to code in line 45 now.  In line 45 I created a method to prompt the user to select a point on the screen and return a 3d point.  The code in this block is pretty straight forward. Line 47 creates a Point3d object, line 48 creates out PromptPointOptions, line 49 creates our PromptPointResult object, the code in line 50 sets our Point3d object to the value of the PromptPointResult object and finally the code in line 51 returns our Point3d object to the calling code.

Now it’s time to see how we can implement this into a Civil 3D command.


Our using statements are contained in lines 1 through 3.

The code in line 5 sets up our assembly type.  In this case we are create an assembly of the CommandClass with the type of TCD_BloggingCode.TCD_Commands.
The code in line 7 shows us that we are still working in the same namespace as before.
There is a new bit of code in line 9 that implements the inheritance of our TCD_CommandTools class.  To implement inheritance of a class you simply add the : <class name> to the class creation statement.
Now it’s time to create a new command.  The code in line 11 uses the CommandMethod to add a command named addPoint.

The code in line 12 creates the code block that will be called by the command.
On line 14 we create out using statement.  Notice that I used the _startTransaction property from the TCD_CommandTools class.  This saves a lot of typing.  Especially if you compare to the fully qualified name of Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Database.TransactionManager.StartTransaction();

The code in line 16 uses the _civilDoc property to add a point to the CogoPoints collection.  Notice that I used the _pt3d() method to prompt the user for the point.

The code in line 17 commits the changes to the database and we are at the end of our command.

And that’s all it takes to use inheritance with the AutoCAD and Civil 3D dot net API. 
Happy Coding!!!

Infrastructure Design Suite Premium is Available for Download

Every morning this week I've been checking the Autodesk subscription site to see if the Infrastructure Design Suite is available for download yet.  This morning I checked and finally hit pay dirt.

The software included is pretty much the same as the 2014 version which for the premium version is

  • AutoCAD
  • AutoCAD Map 3D
  • Storm and Sanitary Analysis
  • AutoCAD Raster Design
  • ReCap
  • Navisworks Simulate
  • InfraWorks
  • AutoCAD Civil 3D
  • Geotechnical Module
  • Bridge Module
  • Rail Layout Module
  • AutoCAD Utility Design
  • 3ds Max Design
  • Revit Structure
For those of you who got your company to spring for the Ultimate version you'll also have
  • Revit
  • Navisworks Manage
  • Robot Structural Analysis Professional
  • River and Flood Analysis Module
Now you just need to find yourself a fast internet connection and happy downloading.



AutoCAD 2015 Dot Net Wizards now available to download

The AutoCAD 2015 Dot Net Wizard is now available to download.  You can find it here.  I installed it this morning and found a rather alarming error.
Missing references
As you can see in the picture above many of the references for Civil 3D and Map 3D are missing.  This is probably caused by the new installation path Autodesk used when installing Civil 3D 2015.  If you look in the C:\Program Files\ Autodesk directory you’ll only find an AutoCAD 2015 folder. Inside this folder you’ll find a C3D folder and a Map folder.  These folders contain the missing reference files.  Below I list the path for each reference file needed.
C:\Program Files\Autodesk\AutoCAD 2015\AcCoreMgd.dll
C:\Program Files\Autodesk\AutoCAD 2015\AcDBMgd.dll
C:\Program Files\Autodesk\AutoCAD 2015\AcMgd.dll
C:\Program Files\Autodesk\AutoCAD 2015\AecBaseMgd.dll
C:\Program Files\Autodesk\AutoCAD 2015\C3D\AeccDbMgd.dll
C:\Program Files\Autodesk\AutoCAD 2015\Map\Autodesk.Map.Platform.dll
C:\Program Files\Autodesk\AutoCAD 2015\Map\OSGeo.MapGuide.Foundation.dll
C:\Program Files\Autodesk\AutoCAD 2015\Map\OSGeo.MapGuide.Geometry.dll
C:\Program Files\Autodesk\AutoCAD 2015\Map\OSGeo.MapGuide.PlatformBase.dll
It is an easy enough fix.  Just remove the missing reference files and browse to the above locations to replace them.  Hopefully Autodesk will get this fixed soon.

Civil 3D 2015 Available for Download from Autodesk Subscription


I downloaded and installed the newest release of Civil 3D over the weekend.  I haven't gotten a lot of time to explore this release yet, but since I'm in my third year of my three year upgrade cycle I'll be up to my elbows in it really soon.

Of course, being me, the first thing I looked at was the new features to the APIs.

Civil 3D 2015 Developers Guide will show you what's new.  A lot of your 2014 code will work on 2015.  So far the only edits that I've had to make refers to point creation.

In 2014 to add a point to drawing the follow code worked:

ObjectId ptId = civilDoc.CogoPoints.Add(pt3d);

In 2015 Autodesk added a useNextPointNumSetting argument to this method.  Set the argument true to use the next point number setting and false if you want to set the point number manually.

ObjectId ptId = civilDoc.CogoPoints.Add(pt3d, false);

It's not a huge change but it will crash your code if you don't look out for it.

I haven't spent enough time with this software to really develop an opinion on this release yet.  I am liking the new dark theme.  I'll have a lot more information to share as I work though setting up the software for production use.




Add a General Note Label Style: Part 5 Inserting Note Label into a Drawing

So far in this series of posts we’ve created a new General Label Style, Edited the the General Properties, set the Text Component properties and set the Dragged State Components.  So we have a complete label style generated form the API.  Today we are going to look at how to add a Note Label to a drawing file using the API.


This is going to be a short post.  Mostly because it is surprisingly easy to accomplish this.
The code in line one creates our method named AddGeneralNoteLabelToDrawing.  We are required to pass this method two arguments (got to love that if you’ve been following along).  The style name, which is used to set the style of the note label, and the location which is a Point3d type, which is used to set the location of the note label.

In line three we get our active AutoCAD document and line four gets our active Civil 3D Document.
Line five opens our database transaction and begins our using block.

In line seven we use the NoteLabel.Create() method to create our note label.  This method has two arguments.  The database (which is simply the AutoCAD Document Database) and the location (which is a Point3d Object).  It returns the object id of our shinny new note label.

The code in line eight uses the GetObject() method to return our NoteLabel object.  This allows us to edit any properties within the object.

In line nine we set our NoteLabel object to use the style name provided by the calling code.
And line ten commits our changes to the database. 

Now lets create our command.


The code in line one creates our command.  The text added here is what is typed into the command line.
In line two we create the method used by the command method.  This method’s name does not need to match the command method name.

In line four we get our active AutoCAD Document.

In line five we create a PromptPointOptions object.  This object will set the message that appears at the command line when the user is prompted to select a point on the screen.

In line six we create a PromptPointResult object.  This object is how the AutoCAD API stores an action that takes place at the command line.  There are several different kinds of these object.  Since we are looking for a point we use this one.

The code in line seven uses our newly created method to add the note label to the drawing file.  Since our previous command added an Asphalt label style I continued on with that here.

And that my friends is how you add a General Note Label to the drawing file.

Adding a General Note Label Style: Part 4 Dragged State Properties

Dragged State Properties
The dragged state properties tab has two property groups.  Both of these groups are located under the LabelStyle.Properties field.
The LabelStyle.Properties.Leader group contains properties that control how the leader is displayed.  The leader has its own color, line type and line weight.  These are independent of the label style’s color, line type and lineweight. This is also where we setup the Arrow Head Style, Size, Visibility and Type.

The LabelStyle.Properties. DraggedStateComponents group contains the properties that control how the label style text components display when the label is dragged from its insertion point.
We will take a look at these properties in detail while we look at the code that changes these properties.

Before we start looking at the code there is one property that I need to mention.  The Arrow Head Style property actually requires a string value instead of the normal enumeration value that we are accustomed to.  Since I really didn’t want to have to remember all the various options and how they are spelled I created a new class to help (see the code below).



This class creates a new object named TCD_ArrowHeadType.  It allows us to create a new object and have a list of all the options for the arrow head.  To use it simply enter the following code:
TCD_ArrowHeadType arrowHeadType = new TCD_ArrowHeadType();
Then we can use the dot numerator to select our arrow head style form the list.
Now lets take a look at the code to set the Leader and Dragged State Components properties of the label style.


The code in line one creates our method named setDraggedStateProperties.  There are a lot of agreements here.  20 if you are keeping count.  One is the Label Style Id and the other 19 are the values to be passed to the 19 properties.

In line six we get our active AutoCAD document and line seven acquires the active Civil 3D Document.
The code in line eight starts our transaction and begins our using statement block.
In line ten we use the trans.GetObject() method to get our label style from the label style id the calling code supplied, and open it with write privileges.

The code in lines 11 through 29 sets the various properties.

Line 11 sets the ArrowHeadStyle property value.  This value accepts a String type.  For this reason I created the TCD_ArrowHeadType class.

Line 12 sets the ArrowHeadSize property value.  This value accepts a Double type.  Since my units are in feet and the arrow head size is in inches we divide the text height by 12.

Line 13 sets the Visibility property value.  This value accepts a Boolean type.  Enter true to make the leader visible or false to make the leader invisible.

Line 14 sets the Shape property value.  This value accepts an Enumeration type of LeaderShapeType.  This can be set to either Spline or Straight.

Line 15 sets the Color property value of the leader.  This value accepts a Color type.

Line 16 sets the LineType property value for the leader.  This value accepts a String value of the linetype name.

Line 17 sets the LineWeight property value.  This value accepts an Enumeration value of type LineWeight.

In line 18 we begin setting the DraggedStateComponents properties.  First is the DisplayType property value.  This value accepts an enumeration value of type LabelContentDisplayType.  The options here are Stacked Text or As Composed.  The Stacked Text option displays all the contents as if it were one piece of MText.  The justification is controlled by the dragged state component properties.  The As Composed option keeps all the settings from text component settings.

Line 19 sets the BorderVisibility property value.  This value accepts a Boolean value and controls if the boundary is visible.

Line 20 sets the BorderType property value.  This value accepts an Enumeration value of type TextBorderType.  The options available are Circular, Rectangle and Rounded Rectangle.
Line 21 sets up the UseBackgroundMask property value.  This value accepts a Boolean value and controls if the background mask is used.

Line 22 sets up the Gap property value.  This value accepts a Double value.  It controls the gap between the text and border.  It also controls the gap between the leader and the text.  This setting also requires that we divide it by 12.

Line 23 sets the TextHeight property value. This value accepts a Double value.  It controls the height of the text when it is dragged from the insertion point.  This is also another property that must be divided by 12 to the desired number.

Line 24 sets the LeaderAttachment property value.  This value accepts a Enumeration value of type LeaderAttachmentType and has the following options: Top of top line, Middle of top line, Middle, Middle of bottom line and Bottom of bottom line.

Line 25 sets the LeaderJustification property value.  This value accepts a Boolean value that controls if the text is justified to the side the leader is attached to.  This means that if the label is dragged to the Left side of the object the text will be right justified and if it’s dragged to the right side the object the text will will left justified with the value set to true.  If the value is set to false the text will remain left justified everywhere.

Line 26 sets the Color property value.  This value accepts a Color type.

Line 27 sets the LineType property value.  This value accepts a String type containing the name of the linetype that should be used.

Line 28 sets the LineWeight property value.  This value accepts an enumeration value of type LineWeight.
Line 29 sets the MaxTextWidth property value.  This value accepts a Double type.  It controls the width the text is allowed to be before it wraps to another line.

And finally line 30 commits our changes to the database.

Now lets add this method to our command that we added to in Adding General Note Style: Part 3 Text Component Properties.


Our new code starts at line 16 where we create the arrowheadtype variable.

In lines 17 through 19 we use the method we just created to set our dragged state components and leader properties.

And that’s all for today folks.  See you soon.

Adding a General Note Style: Part 3 Text Component Properties

Properties properties and more properties.  I swear I’m going to dream about properties tonight. Fun fact, each text component has 20 properties. Lets take a look at the code needed to set these 20 properties.


The code in line one creates our method named setTextComponentProperties.  It spills over a few more lines… like to line four.  All the properties (except the anchor component) has an argument here.  We also have to supply this method with the label style id.
In line six we connect to AutoCAD’s active document and in line seven we get our Civil 3D document.
The code in line 8 creates our using statement block and starts our transaction.
In line ten we get our label style object from the label style id supplied from the calling code.
In line 11 we create an object id variable (id) and get the text component’s object id.
The code in line 12 gets the Text Component object and opens it for write so that we can edit the properties.
Lines 13 through 29 sets the properties of the text component.
In line 13 we set the Anchor Location value.  This property accepts an AnchorPointType.  This time is an enumeration that includes Top left, Top Center, Top right, Middle left, Middle center, Middle right, Bottom left, Bottom Center and Bottom right.
Line 14 sets the visibility of the text component.  This is either true (visible) or false (invisible).
In line 15 we set the text height value.  This property accepts a double type.  Since my units are in feet I have to divide this double by 12 to get the desired height.
The code in line 16 sets the text attachment property.  This property controls how the text is attached to the anchor component.  It accepts a LabelTextAttachementType which is an enumeration type.  The options are Top left, Top Center, Top right, Middle left, Middle center, Middle right, Bottom left, Bottom Center and Bottom right.
In line 17 we set the text rotation angle value.  This value accepts a double in radians.  We use the convertDegreetoRadian method to do this conversion.  This method was included in my previous post Adding a General Note Style: Part 1
We set the x offset value in line 18 and the y offset value in line 19.
The code in line 20 sets the color of the text object.  This color can be set independent of all the other colors in the style.
In line 21 we set the lineweight for the text.  This is set by using the enumeration type LineWeight.  We can set this to ByLayer or ByBlock. Or to any of the line weights install in the active document.
The max width of the text is set in line 22.  This property is set so that when we create a label style the text will wrap when it reaches this limit.
In line 23 we set the visibility of the text boundary.
In line 24 we set the border type property.  This property accepts the enumeration type TextBorderType.  The options are Rectangle,  Rounded Rectangle and Circle.
We set the background mask property in line 25.  Remember wipeouts anyone?  Background masks accomplish the same task as the wipeout did.  Allowing you to place text over lines without having to break the line around the text.
In line 26 we set the gap value.  The gap is the distance between the text and the text boundary.
Lines 27 through 29 sets the border color, the linetype and the lineweight. 
And finally line 30 commits our transaction to the database.
Now lets add our new method to our command from the Part 2.


This command adds a label style named asphalt.  The contents of this style simply reads Asphalt and the text is rotated to 45 deg.
In the next post we’ll cover the dragged state properties.
Have a good day and keep coding!!! Smile

Visual Studio Online

I have a sick child and wife at home.  It’s flu season here in central Illinois.  That said I've spent more time cleaning up various unsavory items and washing clothes than blogging.  I am working on the next segment in the general note styles saga, but I just wanted to make sure all of you know about a new product from Microsoft.  Visual Studio Online (formally Team Foundation) is a service that allows programmers to work in teams online.  While I haven’t graduated to having my own team yet. I have still found this service very useful. 
Using visual studio online I can store my code in the cloud and have access to it from any of my computers.  As long as I can install Visual Studio (and the express version also works) you can check out your code and work on it.  When you are finished you can check your code back in.  The service is free for up to five team members and costs only $20 ($10 if you get in within the current promotion) a month per user.  I am verily scratching the surface of service’s usefulness but so far I am a huge fan.
For more information visit the website @ http://www.visualstudio.com/en-us/products/visual-studio-online-overview-vs.aspx
Well now I need to get back to getting everyone healthy at my house again.  More Civil 3D coding coming soon.
So stay tuned.

Adding a General Note Style: Part 2 Text Style Components.

Layout Tab 2
Today we are going take a look at the layout tab on the Label Style Composer dialog box.  This tab is where we add or edit label style components.  There are five label style component types we can add to a label style. Text, Line, Block, Reference Text and Tick.  The reference text type has six types associated with it. Alignment, Cogo Point, Parcel Profile, Surface and Survey Figure.  Today we are going to explore the Text type.  We’ll take a look at adding some of the other types in future posts.
Autodesk.Civil.DatabaseServices.Styles.LabelStyleComponentType.Text


Text Compnent Properties
Pictured to the left is the list of properties and values we have available to us. As you can see we have a lot of options we can set here.  Our code will cover how we can edit the settings for each of these properties. 
Take note of the Contents property under the text node.  This property controls the text that will show on the screen.  We can get a great deal of data from our model here. Pictured below are Property string options available to the General Note Style:contents avaiable
Property strings allow us to pull data from our drawing file and use that data in our annotation.  For instance we could add the Northing and Easting to our label by adding the property strings for those options.  The northing property string looks like this <[Northing(Uft|P4|RN|AP|GC|UN|Sn|OF)]> and the easting property string is <[Easting(Uft|P4|RN|AP|GC|UN|Sn|OF)]>.  The tricky part is creating the property string.  We’ll look at that in depth in another post.  Now lets jump into some code.
We are going to keep things simple in this post.  Since we are creating a new label style we know that there is only one text style component.  Later we will take a look at editing a style with multiple components.

The code in line one creates our method named setTextComponentContents.  This method requires the calling code to supply it with the object id of the label style to be edited, the content string (this text that is displayed when the label is used) and the component name.  The component name is the text that will show up in components drop down list.
In line three we get our active AutoCAD document and in line four we obtain our active Civil 3D document.
The code in line five starts our transaction with the AutoCAD database.
In line seven we use the object id the calling code supplied us and we open our label style for write.
Lets take a closer look at the code in line eight.
LabelStyleTextComponent textComponent = trans.GetObject(lblStyle.GetComponents(LabelStyleComponentType.Text)[0], OpenMode.ForWrite) as LabelStyleTextComponent;
Here we are using the GetObject method from the transaction object to return our component.  We are supplying our GetObject method with the ObjectId a little differently this time though.  The code lblStyle.GetComponents(LabelStyleComponentType.Text) uses the GetComponents method to retrieve the text label components. We took this one step further by adding [0]. By adding this code to the end of the GetComponents method we retrieve the first (and in our case only) text component.
The code in line nine sets the Name property of the text component.
The text in line ten sets the Text.Contents.Value property of the text component.
And the code in line 11 commits the changes to the database.
Now lets create a command to test what we’ve covered so far.

This command tests to see if a general note label style named Asphalt exists.  If the label style does not exist the command will create it.
The code in line one creates our command named AddGeneralLabelStyle.
Line two creates the method that our command will execute.
The code in line four uses a method we created in a previous post.  The getGeneralLabelStyle method is used to search the General Note Style collection for a style with the name Asphalt.
If the general label style does not exist the code in line seven adds a new style to the collection.
The code in line nine uses the setGeneralProperties method to set the properties found on the general tab of the label composer dialog box.  This method was also created in part one of this series.
The code in line ten sets the contents of our label style.  At this point our new label style has been created and the text in the label will read ASPHALT.
In the next post we’ll take a look at how to set the properties of a label text component.

Adding a General Note Style: Part 1

Today we’ll take a look at the code needed to add a general note style and to edit the general properties.  In the composer we’ll be setting the properties located on the Information Tab and the General Tab.
Information Tab and General Note Tab
Now lets take a look at the code to create a new General Note Style.

The code in line one creates a function named addGeneralLabelStyle.  This function requires us to supply the name for the style and returns the object id of the newly created General Label Style.
In line three we create a null object id object.  This object will later store the id of the newly created label style.
The code in line four gets our active AutoCAD document, and the code in line five gets our active Civil 3D document.
Line six begins our using block.  We use the acadDoc.Database.TransactionManager.StartOpenCloseTranscation() method to create our transaction object.
The code in line eight is one line of code we’ve written everything else to get to.  The code civilDoc.Styles.LabelStyles.GeneralNoteLabelStyles.Add(name) creates a new item in the general note label Styles collection.  It also returns the object id for the object.  We return this object id to the calling code so that we can edit the properties of this style in another function.
Now lets take a look at the getGeneralLabelStyle code.

The code in line one creates our function named getGeneralLabelStyle.  This function requires us to supply the name of the style to search for, and it returns the object id.  Note that this function will return a ObjectId.Null object if the style does not exist.
In line three we create a null object id object named lblId.  This object will later store our general label style objectId (if the style exists else it will remain null).
The code in line four obtains our current AutoCAD document, and the code in line gets the current Civil 3D document.
In line six we use the civilDoc.Styles.LabelStyles.GeneralNoteLabelStyles.Contains(name) method to test if the general note style exists in the drawing.  This method returns the a Boolean value.
The code in line seven uses an if statement to see if the code in line six is true.  If it is the if block is executed.
Line nine is the ever present using statement.  In fact I have to create this code so often that I created a code snippet in visual studio for it.
In line 11 we begin our foreach code block.  This block iterates through all the General Note Styles looking for the name that is supplied from the calling code.
The code in line 13 obtains the LabelStyle obeject by using the trans.GetObject() method.  This method requires us to supply and id (which comes from the foreach statement) and the OpenMode type.  In this instance we are opening the style to read since we only need to see if the name of this style matches the name provided by the calling code.
In line 14 we test if the name of the label style equals the name provided by the calling code.  If this returns true, the code in line 16 sets the objectId value to be returned.
The code in line 19 commits the transaction to the database.
And finally the code in line 22 returns the objectid value or an objectId.Null value to the calling code.
Now lets turn our attention to the method that will set the general properties for the label.

The code in lines one through three setup our method named setGeneralProperties.  This method requires a lot of information be passed to it. The labelStyleId, textStyle, layer, visibility, orientationReference, planReadibility, prBias and flipAnchors.  All these variables coincide with the properties found on the general tab of the label composer.
The code in line five obtains our current AutoCAD document, and the code in line six gets the current Civil 3D document.
In Line seven we open our database transaction.
The code in line nine gets the LabelStyle object and opens it in write mode.
The code in line ten sets the text style, line 11 sets the layer value, and line 12 sets the visibility property.
The code in line 13 sets the Orientation Reference property.  This property is set to one of three values: Object, View or WorldCoordinateSystem.  The object orientates the label according to the object it is annotating.  This really doesn’t apply to note labels since they don’t reference an object.  The view orientation allows the note to rotate based on the orientation of the viewport.  This is more than likely the one you’ll use most often.  The WorldCoordinateSystem orientates the object based on the current coordinate system.
The code in line 14 sets the forced insertion property. On the General Note style this property doesn’t really do much.  You’ll use this property more often on General Segment labels.  It basically controls if the label is above or below the line by default.
Civil 3D labels have the ability to remain plan readable.  On a set of construction plans the text should always be readable from left to right or from bottom to top.  The planReadability property (which is a boolean) controls if this ability is applied to the style.
The PlanReadabilityBias controls at what degree the text will flip to allow for PlanReadibility.  The default is 110 degrees.  The value of this property only accepts radian measurements, so we must convert the degrees the calling code supplied with a radian number.  The code in line 16 convert radians to degrees.  And the code in line 17 sets the PlanReadibilityBias property.
Many styles can be flipped from one side of an object to another.  If you set the flip anchors property to false, the label will be a mirrored version of the original.

A Look Into Civil 3D Styles

Civil 3D styles are the backbone of Civil 3D.  They control everything from how an object is displayed to how an object is annotated. Creating these styles is probably the most time consuming aspect to setting up Civil 3D.   I've spent the last 7 years of my career setting up and tweaking the styles in our templates.  If you really want to get to most of your Civil 3D setup then you have to leverage the Styles to your advantage.

So far on this blog we've looked at some pretty basic styles.  That however is about to change.  As we move forward with our study of the Civil 3D Dot Net API we’ll be diving into the more complex styles.  Before we start coding though I think it’s important to take a look at what makes up these styles.  To do this we’ll use the General Note style as an example.

The Label Style Composer dialog is displayed both when we create a new style and when we edit an existing one.  The layout for this composer is the same weather you are creating an alignment station offset label or a general note.  This dialog is organized using tabs.  There are always five tabs to this composer.  Information, General, Layout, Dragged State and Summary. Each tab has settings that are appropriately grouped together.

First off is the Information tab.  This tab sets the style name and the description.  It also tells us who created the style and when.  We can also see who last modified the style and when that modification took place. This information comes in handy if we need to find the latest version of a style.

Information Tab


Next we'll look at the General Tab.

General Tab


The General Tab is laid out with a property tree on the left side and a preview of the label on the right side. These properties control the Base properties for the label style, such as the Text Style, Visibility and the Layer. We'll look at each of these properties in another post.  For now just note that all the various Civil 3D label styles have these core properties.

The Layout Tab:

Layout Tab


The Layout tab looks a lot like General tab.  It has a property tree on the left and the preview on the right.  However above the property tree you’ll find a list of components.  Each component has its own property list.  This means we’ll have to code for each type of component in the style.  The General Note style has the following components: Text, Line, Block, Reference and Tick. The Reference component also has several types. Alignment, Cogo Point, Parcel, Profile, Surface and Survey Figure.  I will dedicate a post to creating and editing each component type.

Next we have the Dragged State Tab:


The Dragged State tab controls the behavior of the label when it is dragged from the insertion point.  It sets up the leader properties and the text properties.  Again this tab will get a post all it's own.  There are a lot of options that can be set here.

The fifth tab is the Summary tab.  This tab simply summarizes the properties set in the other four tabs.  There is nothing to program for on this tab.

In my next post we'll start exploring the label style in greater detail.  See you soon!

Happy Coding!!!

Selection Sets: And the Coder said, “Select me a block!”

Today we are going to step away form Civil 3D styles and take a look at selection sets. Selection set methods and properties can be found in the AutoCAD.EditorInput namespace. The selection set is a great tool.  It’s basic existence allows us to select objects from the screen and edit them some how.  The selection set can however get the coder into a little bit of trouble.  You can’t always trust a user to read the command line prompt and select the type of object you asked for.  If you ask for a block and get a line the program may crash.  You have two options to keep the user from crashing your code.  The first (and in my opinion the best) is to apply a filter to the selection set that only allows the type you are asking for to be selected.  This is a very clean and user friendly way to accomplish  the task at hand.  The user is prompted to select objects and each object they select is tested against the filter as they select them.  The second way is to test the type of the object after the selection set has been closed.  This can get really messy.  If the user didn’t select an object that’s needed you’ll have to keep prompting until they do.

Before we look at this code I have one quick little note to go over.  In the past post I have always used the full class names for all objects.  For this post I am going to add using statements to shorten some of the code.  The using statements needing to be included for this code to work are as follows:

using System;
using System.Collections.Generic;
using System.Linq;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.EditorInput;

Now lets take a look at the code required to create a basic selection set.

The code in lines 1 through 5 are pretty basic.  We use the command method to create our AutoCAD command.  Then we create our method.  Then we get our active drawing file and the editor object for said file.

Line 6 is where we begin to create our selection set.  To start this we create an instance of a PromptSelectionOptions object. The PromptSelectionOptions object is responsible for setting up the options for the GetSelection method that will be used later.  In this object we can set the message for adding property (like we do in Line 7) plus many many others.  For instance if we want the user to select one object at a time we can set the singleOnly property to true.

The code in line 8 creates an instance of a PromptSelectionResult object.  This object is used to store the result of the getSelection method.  Which uses our PromptSelectionOptions object as it’s argument.

Line 9 tests the status of the PromptSelectionResult object.  The status tells us if everything went okay or if something went wrong during the execution of the code in line 8.

Assuming our prompt result status is “OK” we now move on to the code in line 11.  This code simply creates an integer, so that we can tell the user the position of the selected object in the selection set when we report the selected object type at the command line.

In line 14 we use a foreach statement to iterate through the selection set.  The selection set object is actually a collection of a type called SelectedObject.  Note that this collection located in the value property of the PromptSelectionResult object.

The code in line 15 creates an Entity object by using the Transaction.GetObject method.  Since we are not editing these objects right now we can open the Entity in read mode.  While the code in line 17 writes our object type to the command line. This is really just to show us that the selection set is working.

The code is line 18 uses the ++ operator that is simply shorthand for index = index + 1;

And our last line of code commits the transaction.

And that my friends is a basic selection set, but wait there’s more.  For your coding pleasure we are going to take a look at selection set filters now.

Selection filters are confusing to say the least.  I’ll do my best to demystify all the typedObjects and typedObject arrays needed to accomplish this task.  Here is the code.



Much of the code above is recycled from our basic selection set code.  The changes start with the code in line 8.  The first time I read an example of a selection set filter I was completely lost.  It took me a long time to wrap my head around everything going on in this line of code.  Here is what i finally figured out.

TypedValue[] tVs = new TypedValue[] { new TypedValue((int)DxfCode.Start, RXClass.GetClass(typeof(BlockReference)).DxfName) };

Lets break this code down a little. First you have TypedValue[] tVs =.  Well this is easy enough. We are creating an array of TypedValue objects. 

Next we have the new TypedValue[] section of our code.  This little bit of code simply creates our instance of the TypedValue array.

Next we have the code that populates the TypedValue array. { new TypedValue((int)DxfCode.Start, RXClass.GetClass(typeof(BlockReference)).DxfName) };

TypedValue objects have two arguments. 

The first argument sets the filter type. To do this we apply the int cast to the DxfCode for the type of filter we need.  If you take a look at list of options in intellisense (in Visual Studio) you’ll find a lot of options.  We used the DxfCode.Start option because we are creating a filter for an object’s type name.  In other words we are looking for some type of object. Such as lines, arcs or block references.  We could use DxfCode.BlockName if we wanted to filter for a specific block by name.  Or the DxfCode.LayerName to filter for objects on a specific layer.

The second argument specifies the value we are filtering for.  In our code above we are filtering for block references. The block reference class name is Autodesk.AutoCAD.DatabaseServices.BlockReference.  However, the DxfName for this class is “Insert”.  So you could actually replace our entire second argument with a simple string “Insert”.  I would advise against this however.  It’s simple enough to figure out the DxfName of a few objects.  But what about all the Civil 3d objects?  For instance the Autodesk.Civil.DatabaseServices.GeneralSegmentLabel’s DxFName is General_Segment_Label.  The RXClass.GetClass(typeof(whatever AutoCAD or Civil 3D type you need)).DxfName eliminates the need to memorize and also eliminates the chance of a simple typo making you bang your head on your desk for an hour too. 

The code in line 9 creates the selectionFilter object and applies our TypedValue array to it.

Take note that the code in line 10 has changed slightly as well.  One of the overrides for the GetSelection method allows us to add both the prompt selection options object and our filter object.

Also take note that we didn’t have to reduce our object down to the lowest denominator (as an entity) in line 18 like we had to in the basic selection set code.  Since we filtered for the block reference it’s safe for us to open the selected object as a block reference instead of an entity.

And that my friends is all for today. Cheers!!!







The magical Feature Line

Feature lines become more and more useful with every release of Civil 3D.  This object is kind of jack of all trades.  They can be breaklines for surfaces.  Edge of pavements and right of ways for corridor models, and they have a lot uses in grading objects.  Mastering the feature line is critical for making Civil 3D work for you.  Like all the Civil 3D objects feature lines get their display setting from styles. 
First we’ll take a look at creating a Feature Line Style.

The code in line 1 creates our createFeatureLineStyle function.  I set this function up so that it returns the objectId for the Feature Line Style that is created.  This can be useful if we want to edit the style in the same command we create in.  The function requires us to enter name for the Feature Line Style.
The code in lines 3 and 4 connect us to our active AutoCAD and Civil 3D documents.
The code in line 5 opens a transaction.  The using statement allows us to create a code block to contain all the code that needs the transaction to be open to run.  It also disposes the transaction for us after the code block executes.
The code in line 7 creates the Feature Line Style.  Feature Line Styles are stored in the Feature Line Style Collection.  To create a new style we access the collection through the civilDoc object, and use the Add method to create our new style.  The add method requires us to supply the new Feature Line Style with a name and nothing else.
Line 8 commits the changes to the database.  We use the transaction object (trans) that we created in line 5 to accomplish this task.  If you omit the commit method in your code you won’t get an error but nothing will happen in your database.
finally line 9 returns the Feature Line Style object id to the calling code.  The calling code can store this value and be able to access the Feature Line Style to edit the various properties.
I’m leaving a lot of the error proofing to the calling code here.  For instance the calling code should test to see if the style that needs to be created or edited already exists in the drawing.  If you try to create a new Feature Line Style with a name of a style that already exists you would encounter an error.
The next block of code that we’ll look will fetch the object Id of a Feature Line Style that already exists in the drawing. Using this method we can call up any existing Feature Line Styles for editing.

The code in Line 1 creates our getFeatureLineStyleId function.  This function requires the calling code to supply it with a name to search for.  If that name is found it will return the objectId from the database.  If the name is not found it will return objectId.Null.  This allows the calling code to test against the objectId.Null value in case the Feature Line Style does not exist.
The code in lines 3 and 4 gets our active AutoCAD and Civil 3D documents.
The code in line 5 creates a variable to store the Feature Line Style object Id.  Initially this value is set to objectId.Null.  This allows us to pass the null value to the calling code should the Feature Line Style not exist.
Line 6’s code creates opens our transaction (trans) and begins the using statement block of code.
The code in line 8 creates variable to store the Feature Line Style Collection.  This collection is obtained by getting the Feature Line Styles property from the active Civil 3D document (civilDoc).
This allows the for each statement in line 9 to iterate through the collection.  Each object Id stored in the collection opened by the code in line 11.  Then the if statement in line 12 test if the Feature Style’s name from the collection is the same as the name provided by the calling code.  If this statement returns true the object id from the collection is stored in the featureLineStyleId created in line 5.
Line 17 then returns the value of featureLineStyleId variable to the calling code.  This value will either be the object Id requested or will return the object Id null should the Feature Line Style not exist.
We now have the ability to create and to retrieve Feature Line Styles.  The next step is to edit the properties of the Feature Line Style to suet our needs.
Many Civil 3D styles use other styles as part of their display.  The Marker Styles (as we just went over in previous posts are used widely throughout Civil 3D.  The Feature Line Style uses the Marker Styles to denote things such as the beginning, ending and internal vertices of the Feature Line when shown in a profile view.  As well as a few others.  The next block of code sets these Marker Style preferences.

This method requires the calling code to supply us with a lot of information.  First it must give us a valid object Id for the Feature Line Style to be edited. Then it must supply us with the names for the beginning profile vertex marker style, the ending profile vertex marker style, the internal profile vertex marker style and the section marker style.
Lines 7 and 8 gets our active AutoCAD and Civil 3D documents and Line 9 opens up our ever present database transaction (trans).
The code in line 11 opens the Feature Line Style database record with write privileges, and creates the Feature Line Style object so we can edit it’s properties.
The code in lines 12 through 15 sets the various properties to the values passed in by the calling code.  Line 12 sets the ProfileBeginningVertexMarkerStyleName property,  line 13 sets the ProfileEndingVertexMarkerStyleName property, line 14 sets the ProfileInternalVertexMarkerStyleName property and line 15 sets the SectionMarkerStyleName property.
Line 16 then commits the changes to the database.
Now that we’ve edited the various Marker Style properties within the Feature Line Style it’s time to take a look at setting the display properties for the style.  I noticed some odd behavior when I was debugging this code block.  No matter what I did the display properties would not change.  I double checked that I had committed the changes to the database and I had.  The code produced no errors but the display changes just would not change while in debugging mode. After much cursing and a couple of cups of joe I decided to just load the dll file into Civil 3D and test it without using the Visual Studio debugging software.  It all worked.  Everything work exactly like it should.  So quick tip.  If you get strange behavior form your code while you are running Civil 3D through Visual Studio load it straight into Civil 3D and test it.

The display styles require several methods to complete the style settings.  Since the code for these methods are almost identical we are only going to look at the first method in detail.
The code in line 1 creates our method to edit the display when viewed as a model.  The method requires that the calling code supply a lot of information.  Such as the Feature Line Style object Id, the color for the object, the layer name, linetype, linetype scale, lineweight and the visibility setting.
The code in lines 3 and 4 gets the active AutoCAD and Civil 3D documents.
The code in line 5 creates and opens our database transaction. 
The code in line 7 creates a Feature Line Style object from the object Id passed from the calling code.  It also opens the database record with write privileges.
The code in line 8 creates a Display Style object.  We use the GetFeatureLineDisplayStyleModel to create this object.  This allows us to edit the display style object that controls the view of the feature line while displayed in a model.
The code in lines 9 through 14 set the various properties of the Display Style object. Line 9 applies the color to the display, line 10 applies the layer name, line 11 applies the linetype, line 12 applies the linetype scale, line 13 applies the lineweight and finally line 14 sets the visibility to on (true) or off (false).
The code in line 15 is the all important trans.Commit code that commits the changes to the database.
Now that we have our methods completed it’s time to create our commands to pull all these methods together.  To do this I created three commands.  The first command creates a blank Feature Line Style.  The second command edits the style’s marker settings.  While the third command edits the display style.

Lets take a look at the createFLS (create feature line style) command first.
Line 1 creates the command using the CommandMehtod and names it createFLS and the code in line 2 creates the method used by the command.
I created the feature line methods in a class named FeatureLine, and the commands in the Command class.  The code in line 4 creates a flsUtility object which allows us to use the methods in the FeatureLine class.
The code in Line 5 creates a Feature Line Style named “Proposed RD EOP”.
The code in line 6 sends a message to the command line telling the user that the Feature Line Style was created.
Next up is the editFLS command.  This command sets the Marker Style settings for the style.
Line 9 creates the command named editFLS and line 10 creates the method that the command will use.
The code in line 12 creates our Feature Line utility that allows us to access the methods in the Feature Line class.
The code in line 13 uses the getFeatureLineStyleId to get the object Id of the Feature Line Style to be updated.
The code in line 14 uses the edistFeatureLineMarkerStyles method to set the Feature Line Style marker settings.
The code in line 15 sends a message to the command line that tells the user that the Feature Line marker settings have been updated.
Finally, this brings to the editFLSDisplay command.  This command edits the Feature Line Style display settings.
The code in line 18 creates the command named editFLSDisplay and the code in line 19 creates the method the command will use.
The code in line 21creates our Feature Line utility that allows us to access the methods in the Feature Line class.
The code in line 22 uses the getFeatureLineStyleId to get the object Id of the Feature Line Style to be updated.
The code in lines 23 through 29 update the individual display styles for the Feature Line Style.
The code in line 30 sends a message to the command line informing the user that the Feature Line Style has been updated.
And that my friends is how you create and edit a feature line style.

Editing Marker Style Properties Continued

In the previous post we created methods to control each set of properties used to makeup a Marker Style.  In this post we’ll use those methods to create an AutoCAD command to create and edit a Markup Style.

The first Command we’ll look at will create a new Marker Style using a custom marker.



The first line of  code uses the CommandMethod method to create an AutoCAD command named CreateMSwCustomMarker.

Line 2 creates a method using the same name.

Line 4 uses the CreateMarkerStyle named Test Marker Style 1.

Line 5 uses the getMSId to return the objectId and stores it in the msId variable.

Line 6 tests to insure that marker style was created.  If the style was not created msId will be ObjectID.Null.

Line 8 sets the MarkerDisplayType to UseCustomMarker

Line 9 tests the MarkerDisplayType to see if it is equal to UseVerticalLineForMarker.

If the test in line 9 returns false lines 11 to 23 are executed.

Line 11 sets the MarkerDisplayType  property.

Line 12 tests to see if the MarkerDisplayType is UseCustomMarker.  If this is true then line 14 will call the setCustomMarker method.

Line 16 tests to see if the MarkerDisplayType is UsePointForMarker.  There isn’t any code that needs to be executed should these be set to true.

Line 20 tests to see if the MarkerDisplayType is UseSymbolForMarker.  If this is true then line 22 calls the method setSymbolStyle.  Remember that the block used must be inserted into the drawing file before we can use it in the marker style.

Line 25 test to see if the MarkerDisplayType is UseVerticalLineForMarker.  This display type must be handled differently.  If you remember from the previous post we can’t just set the property and move on.  If this test returns true line 27 uses the setMarkerDisplayType method to set the marker symbols.

Line 29 uses the setMarkerRotation to setup the marker rotation.

Line 30 uses our SetMarkerSizeOptions method to setup the different size options.

Line 31 creates a color to be used in the display plan code in Line 32

Line 32 uses our setMarkerDisplayModel method to create the display setup for the model space.  Lines 33 to 38 set colors and sets up the various MarkerDisplayTypes.

And there we have it.  Now we have methods used to edit specific properties and a command that pulls it all together.

Editing Marker Style Properties

In the last post we covered how to add a marker style to a Civil 3D drawing.  In this post we’ll look at all the properties and how to edit them using the Dot Net API. 
Since you can use an AutoCAD block as a marker the possibilities are pretty endless.  Before we can edit the properties of the marker style we must obtain the objectID.  To do that I wrote a function that will allow us to get the objectID by searching the collection for the marker style name.

The code in lines 1 through 6 should be looking pretty familiar by now.  We created a function that returns an object ID when supplied with the name of an existing marker style.  Then we got the AutoCAD and Civil 3D documents.
in line 7 we created a variable to store the object ID that we’ll return to calling code. The value is set to objectId.Null.  This will allow a null value to be returned if the marker style name does not exist in the collection.  This will mean that we will need to test for the null value when creating the code that calls this function.
In line 8 we use the contains method from the marker styles collection object to see if the name exists in the collection. 
Line 9 is our ever present using statement that starts our transaction.
The if conditional statement in line 11 tests to see if the our msExists variable is true.  If it is then. The for each code in line 13 is executed.
The code in line 15 gets the marker style object from the objectId.
Line 16 tests to see if the name property of the marker style equals the name passed to the function from the calling code. If it is then lines 18 and 19 send a message to the user that the marker style was found.
Line 20 replaces the null value for the msStyleId variable with the object ID for the marker style.
Finally the code in line 25 returns either the null value or the objectId for the marker style requested.
Now lets write a quick test command.

This code will test the function against a marker style named “Test Marker Style”.
The first property we’ll set is the Marker Display Type.  This property determines with of the options will be used to create our display for the marker style.  This property is enumerated with the following options: UseCustomMarker, UsePointForMarker, UseSymbolForMarker and UseVerticalLineForMarker.
Marker Display Type
Now lets take a look at code that changes these properties.

Lines 1 through 3 create a new function that returns the marker display type.  The calling code must supply this function with the Marker Style ID, and the Marker Display Type.
Lines 5 through 10 gets our active documents and opens our transaction.
Lines 11 and 12 gets the marker style from the marker style id and opens the database record in the for write state.
In the process of writing and testing this code I found that you could not assign the UseVerticalLineForMarker to the marker style object.  If you try this it will produce and error.  To get around this error Line 13 tests if the UseVerticalLineForMarker value has been passed.  If the test returns true the code in lines 15 through 17 sets the Marker Type, Custom Marker Style and Custom Marker Superimpose Style so that the Vertical Line is used for the custom Marker.  If the test returns false then the Marker Type is set to the Marker Display Type is set to the Marker Display Type passed from the calling code.
Line 23 commits the changes to the database.
Finally line 24 returns the marker type to the calling code.
If you opt to use the UsePointForMarker the other options are greyed out and there are no more display options.
If you opt to use the UseCustomMarker then we’ll have to set a few more properties.  The Custom Marker Type property are shown of the left side of the picture below.  The enumeration options for this property are CustomMarkerBlank, CustomMarkerDot, CustomMarkerPlus, CustomMarkerVLine and CustomMarkerX.  The Custom Marker Superimpose Type are shown on the right side in the picture below.  The enumeration options for this property are Circle, None, Square and SquareCircle.
Custom Marker
Now lets take a look at the code needed to change these properties.

Line 1 creates a new method called setCustomMarker.  This method requires to the calling code to supply the marker style object id, The custom marker type and the custom marker superimpose type.
Lines 5 through 9 are typical.  They get the active documents and open a transaction.
The code in lines 11 and 12 creates the marker style object and opens the database record for write capabilities.
Line 13 sets the CustomMarkerStyle to the one supplied by the calling code.
Line 14 sets the CustomMarkerSuperimposedStyle to the one supplied by the calling code.
Finally line 15 commits these changes to the database.
We can also use an AutoCAD block for our marker.  The following method allows us to set the symbol name for the marker style.

This function requires that the calling code provide the marker style Id as well as the block name to be used and returns true if the block symbol was inserted into the marker style.  It returns false if the block name was not found.  This allows the calling code to insert the block and try again.
The code in lines 3 through 7 gets our documents and opens our transaction.
We cannot add a block to the marker style unless it already exists in the drawing file.  We must test to see if the block exists before we try to add the block to the marker style.
Line 10 uses the active document’s (acadDoc) Database.BlockTableId property to return the block table Id.
Line 11 opens the block table to read.
Line 12 creates a Boolean variable to hold our test result.
Line 13 begins a foreach statement block that iterates through the block table.
Line 15 takes the block table record Id and opens the block table record for read.
Lines 17 through 20 tests if the name in the block table record equals the name the calling code supplied.
If the block exists in the drawing lines 24 though 27 are executed.  The marker style is first opened with write privileges.  Then the block name is applied to the MarkerSymbolName property.  And finally the changes are committed to the database.
If the block does not exist in the drawing lines 31 and 32 are executed.  Line 31 gets the AutoCAD editor object.  This allows us to print a message to the command line.  Line 32 prints the message that the block does not exist in the document. 
Finally the result of the block exists test is return to the calling code.
Now it’s time to set the rotation value for the marker style.  The following code accomplishes this task.

This method requires the calling code to supply the marker style Id and a double for the rotation value in degrees.
Lines 1 though 7 are typical for getting the documents and opening our using statement.
Lines 9 and 10 opens the marker style with write privileges.
Line 11 converts our degrees rotation (supplied by the calling code) to radians.  AutoCAD almost always using radians when entering an angle value.  It would be useful to create a method to do this conversion.
Line 12 sets the MarkerRotationAngle  to the radian rotation value calculated in line 11.
Finally line 13 commits the change to the database.
Next up is to set the size properties for the marker style.  If we select to use the fixed scale option here we have addition properties to edit.  For this reason we are going to add two methods instead of one.  The second method will allow the calling code to supply different data to our method allowing us to use the fixed scale option.
Marker Size Options

Line 1 creates a method called setMarkerStyleOptions that requires the calling code to supply the Marker Style Id, Marker Size Type and a double for the Marker Size.
Lines 3 through 7 gets the documents and opens our transaction.
Lines 9 and 10 opens our marker style object with writing privileges.
Line 11 sets the MarkerSizeType property.
Line 12 divides the marker size by twelve.  This is required to convert the number that was supplied by the calling code to inches.
Line 13 sets the MarkerSize property to the number calculated in line 12.
Line 14 commits the changes to the database.
The code in line 18 creates an override method that requires the calling code to supply the Marker Style Id, a Vector 3d, and the Marker Size.  Since this code is used to set the Marker Size Type to the use fixed scale type it is not required to supply a Marker Size Type.
The differences in this code starts with line 28.  This code sets the MarkerSizeType property to FixedScale.
Line  29 converts the marker size to inches and line 30 applies that conversion to the to the MarkerSize Property.
Line 30 sets the MarkerFixedSizeScale to the Vector 3D supplied to by the calling code.
Finally line 32 commits these changes to the database.
The next step is to set the Marker Orientation.  There are only two options for this.  We can set the marker to rotate to the world coordinates or to the current view.
Marker Orientation

This code creates a method called setMarkerOrientation and requires the calling code to supply the Marker Style Id and the orientation option.
Lines 3 through 7 do the typical job of getting our documents and opens our transaction.
Lines 9 and 10 opens the Marker Style object with writing privileges.
Line 11 sets the Orientation property.
Line 12 commits the changes to the database.
The final properties to change on the Marker Style Display.  These properties control the layer, color, linetype, lineweight, linetype scale and visibility.
Marker Display Options

The four methods created in this code are very much alike.  The only real difference is that each method calls a different view direction. One for the plan view, one for the model view, one for the profile view and one for the section view.  Since these methods are so alike we will only go through one method here.
Line 1 set the setMarkerDisplayPlan and requires the calling code supply the Marker Sytle Id, color, layer name, linetype, linetype scale, lineweight and visibility.
Lines 5 through 9 gets the typical documents and opens our transaction.
Lines 11 and 12 open the marker style object with write privileges.
Line 13 uses the getMarkerDisplayModel method to return the markerDisplayStyle  object.
Line 14 sets the color for the marker style.
Line 15 sets the layer for the marker style.
Line 16 sets the linetype for marker style.
Line 17 sets the linetype scale.
Line 18 sets the lineweight for the marker style.
Line 19 sets the visibility property for the marker style.
Finally the line 20 commits the changes to the database.
In the next post we’ll cover putting all these methods and functions into an AutoCAD command so we can see them in use.

Creating Marker Styles

I started this blog to learn the Civil 3D Dot Net API.  As anyone following this blog knows I got a little sidetracked and haven’t been able to work on it lately.  This post marks my return to writing about the Civil 3D Dot Net API.  We are going to start working our way through the settings tab. We’ll start off with the Marker Styles and learn how to create and edit the them.

Most Civil 3D objects use the Marker style.  For instance the feature line style uses markers to denote the beginning, internal and ending vertices.  Setting up these styles can save a lot drafting time when it comes to annotating a plan.

Creating a Marker Style using the Dot Net API

Lets take a closer look at this code.

First we create a function that returns an objectid and requires that we supply a name for the Marker Style.

Next in lines 3 through 6 we connection to active AutoCAD and Civil 3D documents.

In lines 7 & 8 we started our transaction with the using statement.

Marker styles are stored within the Civil 3D document object under the property name “MarkerStyles.”  As you can see we connected to this object in lines 10 & 11.

Line 12 of the code is where we actually create the new marker style. We got the Marker style collection so that we could use the add method to create our new style.  This is method returns the object id of the newly created style.

The code in lines 13 through 15 are used to read the database entry into a MarkerStyle object.  This step allows us to edit the properties of the Marker style.  We will return to this subject later in the post.

Line 15 commits the transaction to the database.  Without this line of code nothing would actually change in the drawing file.

And finally Line 17 returns the object id of our shinny new marker style to be stored for editing later.

Next we need to create a command so we can test our code.

This is pretty straight forward.  Simply call the CreateMarkerStyle method and supply a name for the new style.

Civil 3D Settings: Ambient Settings

If someone was going to write about all the different settings in the ambient setting page it would probably be the most boring novel of all time.  And since I don't want to bore you all to death (anymore than I already have with these settings posts) I am simply going to take the elevation setting as an example and show you how you'd change that settings.  So anyway here we go.

The ambient settings tab of the Drawing Settings dialog box controls various settings such as the precision of elevations, angles, area, volume, and distances.  Just to name a few. Most people probably just leave this tab set to the default settings.  However if you are going to write an add on that works with volume calculations you might want to check and make sure that software is set to what you expect it to be.  This tab is where you'd go to change that setting.


As you can see in the screen shot above the Elevation property has 4 settings.  Each of these settings have need a different type of object passed to it.
  • Unit: Autodesk.Civil.LinearUnitType
  • Precision: int
  • Rounding: Autodesk.Civil.RoundingType
  • Sign: Autodesk.Civil.SignType
Our method that we will create will need all these types passed to it when the method is called.  Lets take a look at the code.
The code in line three changes the unit used for the elevation.
civilDoc.Settings.DrawingSettings.AmbientSettings.Elevation.Unit.Value = unit;
If you've been following along with these posts the civilDoc.Settings.DrawingSettings part of this code should look really familiar. The AmbientSettings gets us to the Ambient settings tab.  The Elevation property is used to access all four of the settings listed above. We selected the Unit setting to edit and the Value property allows us to change the setting.  The variable Unit was created in line 1 of the code and is passed to this method when it is called.
That is how you can edit you properties of the Ambient Settings tab.

Civil 3D Settings: Abbreviations

In the last couple of posts we've covered the Units and Zone tab and the Object Layers tab of the Drawing settings dialog box in Civil 3D.  The next tab we are going to cover is the Abbreviations tab.

As you can see above, the abbreviations tab is sorted into 5 categories; General Text, Alignment Geometry Point Text, Alignment Geometry Point Entity Data, Superelevation and Profile. Each category has a slightly different approach to accessing them.  Up first is the General Text code.

The general text category has 3 entries.  Infinity, Left and Right.  Below is the code for setting each entry.
As you can see each method is very similar.  All that's required is to use the Civil 3D Document object to access the Settings.DrawingSettings.AbbreviationsSettings.GeneralText property.  This property contains properties for each type of the three text objects.  You select which text object you want to set and then set it to the string that was supplied when the method is called.

The rest of types are setup a little differently.  And to save a ton of typing I set these up so that we can pass the abbreviation type and the abbreviation string to the method. Lets take a look at the Alignment Geometry Point Text category now.

The Alignment Geometry Point Text object is: Autodesk.Civil.Land.Settings.AbbreviationAlignmentType. This type has a property for each of the 19 entries in this category.  For example if you want to edit the Station Equation Increasing setting you'd pass this method the Autodesk.Civil.Land.Settings.AbbreviationAlignmentType.StationEquationIncreasing type and the string that you want the abbreviation to be.  As you can see it's all pretty simple.
The code for the rest of the categories are similar to this one.  Here is the code needed to change each setting.


Civil 3D Drawing Settings Object Layers


In the previous post we covered how to edit the settings on the "Units and Zone" tab on the Drawing Settings dialog box.  This post is going to cover how to edit the "Object Layer" properties.  And let me tell you there are lots of them.  I am going to explain the code from one and then just list the others.  This is the part of programming that is very repetitive.  To start open our ongoing project and open the "C3DTC_Civil_Settings_Tools" class.  All the code for this class is available later in this post.


I set these methods up with a constructor override so that if you want to just set the layer name and not the other properties you can. You'll notice that this requires two methods with the same name. Lets take a look at how to change all the properties.
When we first setup this class we connected to the active civil document (civilDoc). This object is used to access the drawing settings.  All the code to edit these settings use the same method: "GetObjectLayerSetting".  This method accesses an enumerated object that allows us to easily select the object layer that we need to modify: "Autodesk.Civil.Settings.SettingsObjectLayerType". You simply add a dot numerator and then select the object layer from the list.  Once you select the object layer to modify you can add dot numerator and select the property you'd like to modify.  Below is a list of properties and the object types required to modify them.
  • LayerName: Requires a string type.  This string must match a name of the layer currently in the active civil 3d document.
  • Modifier: This requires an "Autodesk.Civil.Settings.ObjectLayerModifierType."  This is another enumerated property that allows you to select from a list of available modifier types.  This property allows us to add a prefix for a suffix to the object layer.
  • ModifierValue:  Requires a string type.  This property sets the prefix or suffix.  If you add a asterisk the civil 3D object name will be used for the prefix or suffix.
  • Locked:  Requires a Boolean type.  True means this setting is locked at this level and cannot be overridden anyplace else.  False allows users to override this setting at the feature or command level.
As promised here is the code to change all the various object layers and their properties.  Feel free to copy and past.  Cause the is a lot of them.  Happy Coding!!!!