Using SampleElevations() in AutoCAD Civil 3D 2013 .NET API

By Partha Sarkar

.NET version of SampleElevations() was not included in AutoCAD Civil 3D 2012 release. However, in 2013 release, it's included :  public Point3dCollection SampleElevations(ObjectId curveId);

It takes an ObjectId of Autodesk.AutoCAD.DatabaseServices.Curve and the curve can be a line, arc and so on. Here is a C# code snippet which demonstartes usage of this API function in AutoCAD Civil 3D 2013 :

public static void DemoSurfaceSampleElevations2013()
{
    // Get the AutoCAD Editor
    Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
 
    //select a surface
    PromptEntityOptions selSurface = new PromptEntityOptions("nSelect a Tin Surface: ");
    selSurface.SetRejectMessage("nOnly Tin Surface is allowed");
    selSurface.AddAllowedClass(typeof(TinSurface), true);
    PromptEntityResult resSurface = ed.GetEntity(selSurface);
    if (resSurface.Status != PromptStatus.OK) return;
    ObjectId surfaceId = resSurface.ObjectId;
 
    //select a polyline
    PromptEntityOptions selPline = new PromptEntityOptions("nSelect a polyline: ");
    selPline.SetRejectMessage("nOnly polylines allowed");
    selPline.AddAllowedClass(typeof(Polyline), true);           
    PromptEntityResult resPline = ed.GetEntity(selPline);
    if (resPline.Status != PromptStatus.OK) return;
    ObjectId plineId = resPline.ObjectId;
 
    Database db = Application.DocumentManager.MdiActiveDocument.Database;
    using (Transaction trans = db.TransactionManager.StartTransaction())
    {
        TinSurface surface = trans.GetObject(surfaceId, OpenMode.ForRead) as TinSurface;
 
        // SampleElevations(ObjectId curveId) returns a Point3dCollection
 
        Point3dCollection intPts = new Point3dCollection();
        intPts = surface.SampleElevations(plineId);
        if (intPts.Count != 0)
        {
            foreach (Point3d pt in intPts)
            {
                ed.WriteMessage(string.Format("{0} {1}", Environment.NewLine, pt.ToString()));                          
            }
        }
 
        trans.Commit();
    }
 
}

Comments

One response to “Using SampleElevations() in AutoCAD Civil 3D 2013 .NET API”

  1. BTW, it might be nice to note that you have to define SetRejectedMessage before you define AddAllowClass. Although your plugin will compile, it will throw an error if this is out of sequence when it is reached in process.
    Something I did not know!

Leave a Reply

Discover more from Autodesk Developer Blog

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

Continue reading