Want to use only StartPoint and EndPoint as input to .NET API SampleElevations()?

By Partha Sarkar

This is in continuation of my previous post on using SampleElevations() in AutoCAD Civil 3D 2013 .NET API. I was exploring if we have to mimic Civil 3D COM API AeccSurface::SampleElevations(X1,Y1, X2,Y2) which takes a StartPoint and EndPoint and returns an array of doubles consisting of points derived from the surface along the line, how do we achieve this using the new .NET API. 

The new .NET API (2013) TinSurface.SampleElevations(curveId) takes an ObjectId. To get an ObjectId, we need to add the new entity (Line / Pline) from a set of Points (StartPoint and EndPoint) and later dispose it. Here is a code snippet which demonstrates the same :

// 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 two points 
    PromptPointOptions getPointOptions1 = new PromptPointOptions("nPick A Point Inside the Surface Boundary : ");
    PromptPointResult getPointResult1 = ed.GetPoint(getPointOptions1);
 
    PromptPointOptions getPointOptions2 = new PromptPointOptions("nPick 2nd Point Inside the Surface Boundary : ");
    PromptPointResult getPointResult2 = ed.GetPoint(getPointOptions2);
 
    Database db = Application.DocumentManager.MdiActiveDocument.Database;
 
    using (Transaction trans = db.TransactionManager.StartTransaction())
    {
        TinSurface surface = trans.GetObject(surfaceId, OpenMode.ForRead) as TinSurface;
 
        // create a polyline from the two points to use in SampleElevations(ObjectId curveId)
        Polyline pline = new Polyline();
        Point2d pt1 = new Point2d(getPointResult1.Value.X, getPointResult1.Value.Y);
        Point2d pt2 = new Point2d(getPointResult2.Value.X, getPointResult2.Value.Y);
        pline.AddVertexAt(0, pt1, 0, 0, 0);
        pline.AddVertexAt(1, pt2, 0, 0, 0);
 
        // open the current space for write
        BlockTableRecord btr = (BlockTableRecord)trans.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);                                
        btr.AppendEntity(pline);
 
        // SampleElevations(ObjectId curveId) returns a Point3dCollection
        Point3dCollection intPts = new Point3dCollection();
        intPts = surface.SampleElevations(pline.ObjectId);
        if (intPts.Count != 0)
        {
            foreach (Point3d pt in intPts)
            {
                ed.WriteMessage(string.Format("{0} {1}", Environment.NewLine, pt.ToString()));
            }
        } 

    }

Comments

Leave a Reply

Discover more from Autodesk Developer Blog

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

Continue reading