Creating Civil 3D TIN Surface from Contours (Polylines) using .NET API

By Partha Sarkar

You have a DWG file with contour lines which are actually polylines with elevation values and you want to create a Civil 3D TIN Surface using those Polylines.

Here is a screenshot which shows few Polylines in a DWG file :

  Civil3D_Contour_Lines

 

 

If want to create a Civil 3D TIN Surface using these Polylines, first you need to create an empty TinSurface by using TinSurface.Create() API and then using SurfaceOperationAddContour.AddContours() API you need to add the Polylines to the TinSurface. 

Here is a C#.NET code snippet :

// Get the AutoCAD Editor
Editor ed = AcadApp.DocumentManager.MdiActiveDocument.Editor;
Database db = AcadApp.DocumentManager.MdiActiveDocument.Database;
CivilDocument civilDoc = CivilApplication.ActiveDocument;
using (Transaction trans = db.TransactionManager.StartTransaction())
{
 
  try
  {
    // Select a Surface style to use 
    ObjectId styleId = civilDoc.Styles.SurfaceStyles["Slope Banding (2D)"];
 
    //Contour Entities collection
    ObjectIdCollection contourEntitiesIdColl = newObjectIdCollection();
 
    // In the following line I use a custom function GetContourEntities() 
    // which creates a collection of all the contour polyline Ids
    contourEntitiesIdColl = GetContourEntities(); 
 
    if (styleId != null && contourEntitiesIdColl != null)
    {
      // Create an empty TIN Surface
      ObjectId surfaceId = TinSurface.Create("TIN_Surface_From_Contours", styleId);
      TinSurface surface = trans.GetObject(surfaceId, OpenMode.ForWrite) asTinSurface;
 
      //public SurfaceOperationAddContour AddContours( ObjectIdCollection contourEntities, double midOrdinateDistance,
      // double maximumDistance,  double weedingDistance,  double weedingAngle )
 
      surface.ContoursDefinition.AddContours(contourEntitiesIdColl, 1.0, 100.00, 15.0, 4.0);            
 
    }   
 
    trans.Commit();
  }
 
  catch (System.Exception ex)
  {
    // handle the exception
    ed.WriteMessage("Surface Creation from Contours Failed !" + "  " +  ex.Message);
  }
 
}

 

 

And a TinSurface created using Polylines :

 

Civil3D_Contour_Lines_used2create_TinSurface


Comments

Leave a Reply

Discover more from Autodesk Developer Blog

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

Continue reading