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.
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.
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.
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.
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.
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.