Create a Corridor with Civil3D 2014 API

By Augusto Goncalves

After the quick summary of new Corridor APIs available on Civil 3D 2014, this post show how create a corridor and add a baseline with alignment and profile.

Unlike other previous APIs, like Alignment and Surface, where the Create method was at the class, with Alignment.Create and TinSurface.Create respectively, to create a corridor we need to access the collection under CivilDocument and then call Add method. Like other APIs, this method returns the ObjectId of the newly created Corridor.

The following quick sample demonstrate it. Note that the same idea applies to Baseline – access the collection and call its Add method.

[CommandMethod("testCreateCorridor")]

public static void CmdCreateCorridor()

{

  Database db = Application.DocumentManager.

    MdiActiveDocument.Database;

  CivilDocument civilDoc = CivilApplication.ActiveDocument;

 

  using (Transaction trans = db.

    TransactionManager.StartTransaction())

  {

    // Create a new Corridor

    ObjectId newCorridorId = civilDoc.

      CorridorCollection.Add("Corridor 1");

    Corridor corridor = trans.GetObject(

      newCorridorId, OpenMode.ForWrite) as Corridor;

 

    // Get the first alignment of this drawing

    ObjectId alignmentId = civilDoc.GetAlignmentIds()[0];

    Alignment alignment = trans.GetObject(

      alignmentId, OpenMode.ForRead) as Alignment;

 

    // Get the first profile of this alignment

    ObjectId profileId = alignment.GetProfileIds()[0];

 

    // Create the baseline

    Baseline baseline = corridor.Baselines.Add(

      "New Baseline", alignm
entId, profileId);

  }

}


Comments

2 responses to “Create a Corridor with Civil3D 2014 API”

  1. Hi. When I tried to use your code, I got this:
    “Cannot convert type ‘Autodesk.AutoCAD.DatabaseServices.DBObject’ to ‘Autodesk.Civil.DatabaseServices.Corridor’ via a reference conversion, boxing conversion, unboxing conversion, wrapping conversion, or null type conversion”
    Can you help me, please? Thank you so much.

  2. Hi Arman,
    Not sure where it’s failing for you… this code is quite simple and should work, so the problem may be on a different part of your code. Can you provide more details?
    Regards,
    Augusto Goncalves

Leave a Reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading