Templateusing Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
using Autodesk.Civil.ApplicationServices;
using Autodesk.Civil.DatabaseServices;
using Autodesk.Civil.DatabaseServices.Styles;
using Autodesk.Civil.Settings;
using System;
namespace BePro
{
public class ProCADCheatSheet
{
[CommandMethod("template")]
public static void ProCadExample()
{
using (DocumentLock acLckDoc = AcadDoc.LockDocument())
{
using (Transaction MyTrans = MyDatabase.TransactionManager.StartTransaction())
{
try
{
//do activity here
}
catch (System.Exception ex)
{
MyEditor.WriteMessage("Error: ==>\n{0}\nTrace: ==>\n{1}", ex.Message, ex.StackTrace);
}
MyTrans.Commit();
}
}
}
}
}
Civil 3d 2018 C#.Net Cheat Sheet 1/2
Accessing Application and Document ObjectsCivilDocument civdoc = CivilApplication.ActiveDocument; // the root object for the Civil 3d .net heiarchy. For example use
“civdoc.Styles.LabelStyles.GeneralNoteLabelStyles[0]” to get the first note label style in the document
Using Collections Within the Document Objectforeach ( ObjectId objId in civdoc.CorridorCollection) // using the CivilDocument (civdoc) we can collect and iterate through almost every element inside a
civil 3D drawing such as the alignments, surfaces, points, assemblies, corridors and more
Accessing Drawing, Feature, and Command SettingsSettingsAlignment alignmentSettings = civdoc.Settings.GetSettings<SettingsAlignment>(); // get the command settings for alignments
Autodesk.Civil.Settings.SettingsAmbient.SettingsAngle angleSettings = alignmentSettings.Angle; // alters ambient settings
Label Style Object ObjectId labelStyleId = civdoc.Styles.LabelStyles.PointLabelStyles.LabelStyles.Add(); // create a point label style
var newTextComponent = ts.GetObject(textCompCol[0], OpenMode.ForWrite) as LabelStyleTextComponent; // select the first text component in a collection
newTextComponent.Text.Contents.Value = "SPD=<[Design Speed(P0|RN|AP|Sn)]>"; // edit the value of a text component
newTextComponent.Text.Contents.Value += "STA=<[Station Value(Uft|FS|P2|RN|AP|Sn|TP|B2|EN|W0|OF)]>"; // add a value to a text component
SurfacesObjectId surfaceId = doc.GetSurfaceIds()[0]; // collect the first surface in the document
ObjectId surfaceId = TinSurface.Create(surfaceName, surfaceStyleId); // create a tin surface
oSurface.BreaklinesDefinition.AddNonDestructiveBreaklines(new ObjectIdCollection(lines), 1); // add breaklines to a surface
SurfaceStyle surfaceStyle = styleId.GetObject(OpenMode.ForWrite) as SurfaceStyle; // edit a surface style
surfaceStyle.GetDisplayStylePlan(SurfaceDisplayStyleType.Triangles).Visible = true; // turn surface triangles on
AlignmentsObjectId testAlignmentID = Alignment.Create(doc, "New Alignment", ObjectId.Null, "0", "Basic", "All Labels"); // create a new alignment
StationEquation myStationEquation = myAlignment.StationEquations.Add(80, 0, StationEquationType.Increasing); // add a station equation
ProfilesObjectId profileId = Profile.CreateFromSurface("My Profile", alignID, surfaceId, layerId, styleId, labelSetId); // create profile from surface
ObjectId oProfileId = Profile.CreateByLayout("My Profile", alignID, layerId, styleId, labelSetId); // create profile by layout
oProfilePVI = oProfile.PVIs.AddPVI(607.4, -64.3); // add a pvi to a profile at station 607.4, elevation -64.3
Profile ViewObjectId ProfileViewId = ProfileView.Create(doc, "New Profile View", pfrVBSStyleId, alignID, ptInsert); // create a profile view
ObjectId profileViewStyleId = doc.Styles.ProfileViewStyles.Add("New Profile View Style"); // create a profile view style
oProfileViewStyle.GetDisplayStylePlan(ProfileViewDisplayStyleType.TopAxis).Visible = true; // turn the top axis on
oProfileViewStyle.TopAxis.MajorTickStyle.LabelText = "<[Station Value(Um|FD|P1)]> m"; // alter the station value for top axis major tick style
oProfileViewStyle.TopAxis.MajorTickStyle.Interval = 164.041995; // set the interval of the major tick style for the top axis to 164.04
Pipes and Structures SettingsPipeNetwork oSettingsPipeNetwork = doc.Settings.GetSettings<SettingsPipeNetwork>() as SettingsPipeNetwork; // access the pipe networks settings
oSettingsPipeNetwork.Angle.Unit.Value = Autodesk.Civil.AngleUnitType.Radian; // alter pipe network angle unit value to be radian
ObjectId pipeId = oNetwork.GetPipeIds()[0]; // get the first pipe in a pipe network
PartDataField[] oDataFields = oPipe.PartData.GetAllDataFields(); // collect the datafields for a pipe (size, description, flowrate, etc)
ObjectIdCollection oIdCollection = doc.GetPipeNetworkIds(); // collect all pipe networks. To get the first network simply use doc.GetPipeNetworkIds()[0], or to
get a network by name use doc.GetPipeNetworkIds()[“nameofnetwork”];
oNetwork.AddLinePipe(oid, psize, line, ref oidNewPipe, false); // create a pipe
ntwrk.AddStructure(fam.ObjectId, structId, pipe.StartPoint, 0, ref newStructId, false); // create a structure
ObjectId mystyid = Autodesk.Civil.ApplicationServices.CivilApplication.ActiveDocument.Styles.PipeStyles["nameofstyle"]; // collect a pipe style by name
PointsCogoPointCollection cogoPoints = _civildoc.CogoPoints; // gather a collection of the points in a document
ObjectId pointId = cogoPoints.Add(location); // create a point
ObjectId pointGroupId = _civildoc.PointGroups.Add("Example Point Group"); // create a point group
Gathering Database and Document Objects // storing items like the document and database makes it easier to use through a solution
public static Database db => doc.Database;
public static Document doc => Autodesk.AutoCAD.ApplicationServices.Core.Application.DocumentManager.MdiActiveDocument;
public static DocumentLock lockdoc => doc.LockDocument();
public static CivilDocument civdoc => CivilApplication.ActiveDocument;
public static Editor CurrentEditor => Application.DocumentManager.MdiActiveDocument.Editor;
10/2017
compiled by
Brian Chapman
http://www.pro-cad.net
.dwg hierarchy
Images below represent the Civil 3D API Object Model and are from the Autodesk Autocad Civil 3d 2018 Developer’s Guide located here: http://help.autodesk.com/view/CIV3D/2018/ENU/?guid=GUID-E338F992-8803-43DF-97BB-6C8CF2473AA9 where it notes these are useful for determining which
class instances are required to create an object of a particular type, or which objects can be accessed from an existing instance.
10/2017
compiled by
Brian Chapman
http://www.pro-cad.net
An object with a classname of “AeccObject”
An object with a classname of “AeccCollection”. This object is a list of other objects
which can be enumerated. It also usually has a Count property and Add, Item, and
Remove methods
You can obtain object B from a property or method of object A
From object X you can obtain a collection Y, which is a subset of a larger collection
Civil 3d 2018 C#.Net Cheat Sheet 2/2