1. 访问曲面

//遍历文档中的所有曲面,并打印出名称和曲面类型
var surfaceIds = civilDoc.GetSurfaceIds();
foreach (ObjectId id in surfaceIds)
{
    Surface surface = id.GetObject(OpenMode.ForRead) as Surface;
    ed.WriteMessage($"Surface:{surface.Name}\nType:{surface.GetType()}");
}
//提示用户选择三角网曲面,并返回ObjectID
public ObjectId PromptFromTinSurface()
{
    string prompt="请选择三角网曲面";
    PromptEntityOptions peo=new PromptEntityOptions($"\n{prompt}");
    peo.SetRejectMessage("\n选择的对象不是三角网曲面");
    peo.AddAllowedClass(typeof(TinSurface),true);
    PromptEntityResult per = ed.GetEntity(peo);
    if(per.Status==PromptStatus.OK) return per.ObjectId;
    return ObjectId.Null;
}

获取曲面属性

public void SurfaceProperties()
{
    using (var tr = db.TransactionManager.StartTransaction())
    {
        try
        {
            ObjectId surfaceId = civilDoc.GetSurfaceIds()[0];
            Surface surface = surfaceId.GetObject(OpenMode.ForRead) as Surface;
            GeneralSurfaceProperties genProps = surface.GetGeneralProperties();
            var propsMsg=$"\n{surface.Name}的通用属性\n----------------------\nMin X:{genProps.MinimumCoordinateX}" +
                         $"\nMin Y:{genProps.MinimumCoordinateY}\nMin Z:{genProps.MinimumElevation}" +
                         $"\nMin X:{genProps.MaximumCoordinateX}" +
                         $"\nMin Y:{genProps.MaximumCoordinateY}\nMin Z:{genProps.MaximumElevation}" +
                         $"\n---------------------";
            ed.WriteMessage(propsMsg);
            if (surface is TinSurface)
            {
                var tinProps = ((TinSurface) surface).GetTinProperties();
                propsMsg = $"\nTin Surface properties for {surface.Name}";
                propsMsg += "\n-------------------";
                propsMsg += "\nMin Triangle Area: " + tinProps.MinimumTriangleArea;
                propsMsg += "\nMin Triangle Length: " + tinProps.MinimumTriangleLength;
                propsMsg += "\nMax Triangle Area: " + tinProps.MaximumTriangleArea;
                propsMsg += "\nMax Triangle Length: " + tinProps.MaximumTriangleLength;
                propsMsg += "\nNumber of Triangles: " + tinProps.NumberOfTriangles;
                propsMsg += "\n-------------------";
                ed.WriteMessage(propsMsg);
            }
            else if(surface is GridSurface)
            {
                var gridProps = ((GridSurface) surface).GetGridProperties();
                propsMsg = $"\nGrid Surface properties for {surface.Name}";
                propsMsg += "\n-------------------";
                propsMsg += "\n X Spacing: " + gridProps.SpacingX;
                propsMsg += "\n Y Spacing: " + gridProps.SpacingY;
                propsMsg += "\n Orientation: " + gridProps.Orientation;
                propsMsg += "\n-------------------";

                ed.WriteMessage(propsMsg);
            }
        }
        catch (Exception e)
        {
            ed.WriteMessage(e.Message);
        }
    }
}

2. 创建曲面

Note: Import from LandXML data is not supported in the .NET API at this time. You can use the COM API to import or export surface data to or from LandXML.

注意:目前 .NET API 暂不支持LandXML,可以通过COM API导入或导出XML。

public void CreateFromTin()
{
    using (var tr = db.TransactionManager.StartTransaction())
    {
        var tinFile = "";
        OpenFileDialog ofd = new OpenFileDialog();
        ofd.Filter = "TIN文件|*.tin";
        ofd.Title = "请选择TIN文件";
        ofd.Multiselect = false;//不能多选
        //设置默认路径
        ofd.InitialDirectory = @"C:\Program Files\Autodesk\AutoCAD 2018\C3D\Help\Civil Tutorials\";
        if (ofd.ShowDialog() == DialogResult.OK)
        {
            tinFile = ofd.FileName;
        }
        else
        {
            return;
        }
        ed.WriteMessage(tinFile+"\n路径\n");
        try
        {
            ObjectId tinSurfaceId = TinSurface.CreateFromTin(db, tinFile);
            ed.WriteMessage($"Import succeeded:{tinSurfaceId.ToString()}\n{db.Filename}\n");
        }
        catch (Exception e)
        {
            ed.WriteMessage($"Import failed:{e.Message}\n");
        }
        tr.Commit();
    }
}
public void CreateTinSurface()
{
    using (var tr = db.TransactionManager.StartTransaction())
    {
        string surfaceName = "ExampleTinSurface";
        ObjectId surfaceStyleId = civilDoc.Styles.SurfaceStyles[2];
        ObjectId surfaceId = TinSurface.Create(surfaceName, surfaceStyleId);
        var surface = surfaceId.GetObject(OpenMode.ForWrite) as TinSurface;
        Point3dCollection pts=new Point3dCollection();
        Random rand=new Random();
        for (int i = 0; i < 10; i++)
        {
            var x = rand.NextDouble() * 250;
            var y = rand.NextDouble() * 250;
            var z = rand.NextDouble() * 100;
            pts.Add(new Point3d(x, y, z));
        }

        surface.AddVertices(pts);
        tr.Commit();
    }
}
public void CreateFromDTM()
{
    using (var tr = db.TransactionManager.StartTransaction())
    {
        var DTMFile = "";
        OpenFileDialog ofd = new OpenFileDialog();
        ofd.Filter = "DTM文件|*.DTM";
        ofd.Title = "请选择DTM文件";
        ofd.Multiselect = false;//不能多选
        //设置默认路径
        ofd.InitialDirectory = @"C:\Program Files\Autodesk\AutoCAD 2018\C3D\Help\Civil Tutorials\";
        if (ofd.ShowDialog() == DialogResult.OK)
        {
            DTMFile = ofd.FileName;
        }
        else
        {
            return;
        }
        ed.WriteMessage(DTMFile + "\n路径\n");
        try
        {
            ObjectId DTMSurfaceId = GridSurface.CreateFromDEM(db, DTMFile);
            ed.WriteMessage($"Import succeeded:{DTMSurfaceId.ToString()}\n{db.Filename}\n");
        }
        catch (Exception e)
        {
            ed.WriteMessage($"Import failed:{e.Message}\n");
        }
        tr.Commit();
    }
}
public void CreateGridSurface()
{
    using (var tr=db.TransactionManager.TopTransaction)
    {
        var surfaceName = "ExGridSurface";
        ObjectId surfaceStyleId = civilDoc.Styles.SurfaceStyles["Slope Banding (2D)"];
        ObjectId surfaceId = GridSurface.Create(surfaceName, 25, 25, 0.0, surfaceStyleId);
        var surface = surfaceId.GetObject(OpenMode.ForWrite) as GridSurface;
        Random rand=new Random();
        for (int i = 0; i < 10; i++)
        {
            for (int j = 0; j < 10; j++)
            {
                var z = rand.NextDouble() * 10;
                GridLocation loc=new GridLocation(j,i);
                surface.AddPoint(loc, z);
            }
        }
        tr.Commit();
    }
}
public void CreateTinVolumeSurface()
{
    using (var tr=db.TransactionManager.TopTransaction)
    {
        var surfaceName = "ExampleVolumeSurface";
        ObjectId baseId = PromptFromTinSurface("选择基准曲面");
        ObjectId comparisonId = PromptFromTinSurface("选择对比曲面");
        try
        {
            ObjectId surfaceId = TinVolumeSurface.Create(surfaceName, baseId, comparisonId);
            var surface = surfaceId.GetObject(OpenMode.ForWrite) as TinVolumeSurface;
        }
        catch (Exception e)
        {
            ed.WriteMessage($"体积曲面创建失败:{e.Message}");
        }
    }
}