Civil3D开发者指南系列之三——创建曲面

1. 访问曲面

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

获取曲面属性

  1. public void SurfaceProperties()
  2. {
  3. using (var tr = db.TransactionManager.StartTransaction())
  4. {
  5. try
  6. {
  7. ObjectId surfaceId = civilDoc.GetSurfaceIds()[0];
  8. Surface surface = surfaceId.GetObject(OpenMode.ForRead) as Surface;
  9. GeneralSurfaceProperties genProps = surface.GetGeneralProperties();
  10. var propsMsg=$"\n{surface.Name}的通用属性\n----------------------\nMin X:{genProps.MinimumCoordinateX}" +
  11. $"\nMin Y:{genProps.MinimumCoordinateY}\nMin Z:{genProps.MinimumElevation}" +
  12. $"\nMin X:{genProps.MaximumCoordinateX}" +
  13. $"\nMin Y:{genProps.MaximumCoordinateY}\nMin Z:{genProps.MaximumElevation}" +
  14. $"\n---------------------";
  15. ed.WriteMessage(propsMsg);
  16. if (surface is TinSurface)
  17. {
  18. var tinProps = ((TinSurface) surface).GetTinProperties();
  19. propsMsg = $"\nTin Surface properties for {surface.Name}";
  20. propsMsg += "\n-------------------";
  21. propsMsg += "\nMin Triangle Area: " + tinProps.MinimumTriangleArea;
  22. propsMsg += "\nMin Triangle Length: " + tinProps.MinimumTriangleLength;
  23. propsMsg += "\nMax Triangle Area: " + tinProps.MaximumTriangleArea;
  24. propsMsg += "\nMax Triangle Length: " + tinProps.MaximumTriangleLength;
  25. propsMsg += "\nNumber of Triangles: " + tinProps.NumberOfTriangles;
  26. propsMsg += "\n-------------------";
  27. ed.WriteMessage(propsMsg);
  28. }
  29. else if(surface is GridSurface)
  30. {
  31. var gridProps = ((GridSurface) surface).GetGridProperties();
  32. propsMsg = $"\nGrid Surface properties for {surface.Name}";
  33. propsMsg += "\n-------------------";
  34. propsMsg += "\n X Spacing: " + gridProps.SpacingX;
  35. propsMsg += "\n Y Spacing: " + gridProps.SpacingY;
  36. propsMsg += "\n Orientation: " + gridProps.Orientation;
  37. propsMsg += "\n-------------------";
  38. ed.WriteMessage(propsMsg);
  39. }
  40. }
  41. catch (Exception e)
  42. {
  43. ed.WriteMessage(e.Message);
  44. }
  45. }
  46. }

Civil3D开发者指南系列之三——创建曲面 - 图1

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。

  • 从TIN文件创建三角网曲面
  1. public void CreateFromTin()
  2. {
  3. using (var tr = db.TransactionManager.StartTransaction())
  4. {
  5. var tinFile = "";
  6. OpenFileDialog ofd = new OpenFileDialog();
  7. ofd.Filter = "TIN文件|*.tin";
  8. ofd.Title = "请选择TIN文件";
  9. ofd.Multiselect = false;//不能多选
  10. //设置默认路径
  11. ofd.InitialDirectory = @"C:\Program Files\Autodesk\AutoCAD 2018\C3D\Help\Civil Tutorials\";
  12. if (ofd.ShowDialog() == DialogResult.OK)
  13. {
  14. tinFile = ofd.FileName;
  15. }
  16. else
  17. {
  18. return;
  19. }
  20. ed.WriteMessage(tinFile+"\n路径\n");
  21. try
  22. {
  23. ObjectId tinSurfaceId = TinSurface.CreateFromTin(db, tinFile);
  24. ed.WriteMessage($"Import succeeded:{tinSurfaceId.ToString()}\n{db.Filename}\n");
  25. }
  26. catch (Exception e)
  27. {
  28. ed.WriteMessage($"Import failed:{e.Message}\n");
  29. }
  30. tr.Commit();
  31. }
  32. }
  • 使用TinSurface.Create()创建曲面
  1. public void CreateTinSurface()
  2. {
  3. using (var tr = db.TransactionManager.StartTransaction())
  4. {
  5. string surfaceName = "ExampleTinSurface";
  6. ObjectId surfaceStyleId = civilDoc.Styles.SurfaceStyles[2];
  7. ObjectId surfaceId = TinSurface.Create(surfaceName, surfaceStyleId);
  8. var surface = surfaceId.GetObject(OpenMode.ForWrite) as TinSurface;
  9. Point3dCollection pts=new Point3dCollection();
  10. Random rand=new Random();
  11. for (int i = 0; i < 10; i++)
  12. {
  13. var x = rand.NextDouble() * 250;
  14. var y = rand.NextDouble() * 250;
  15. var z = rand.NextDouble() * 100;
  16. pts.Add(new Point3d(x, y, z));
  17. }
  18. surface.AddVertices(pts);
  19. tr.Commit();
  20. }
  21. }
  • 从DTM文件创建三角网曲面
  1. public void CreateFromDTM()
  2. {
  3. using (var tr = db.TransactionManager.StartTransaction())
  4. {
  5. var DTMFile = "";
  6. OpenFileDialog ofd = new OpenFileDialog();
  7. ofd.Filter = "DTM文件|*.DTM";
  8. ofd.Title = "请选择DTM文件";
  9. ofd.Multiselect = false;//不能多选
  10. //设置默认路径
  11. ofd.InitialDirectory = @"C:\Program Files\Autodesk\AutoCAD 2018\C3D\Help\Civil Tutorials\";
  12. if (ofd.ShowDialog() == DialogResult.OK)
  13. {
  14. DTMFile = ofd.FileName;
  15. }
  16. else
  17. {
  18. return;
  19. }
  20. ed.WriteMessage(DTMFile + "\n路径\n");
  21. try
  22. {
  23. ObjectId DTMSurfaceId = GridSurface.CreateFromDEM(db, DTMFile);
  24. ed.WriteMessage($"Import succeeded:{DTMSurfaceId.ToString()}\n{db.Filename}\n");
  25. }
  26. catch (Exception e)
  27. {
  28. ed.WriteMessage($"Import failed:{e.Message}\n");
  29. }
  30. tr.Commit();
  31. }
  32. }
  • 创建栅格曲面
  1. public void CreateGridSurface()
  2. {
  3. using (var tr=db.TransactionManager.TopTransaction)
  4. {
  5. var surfaceName = "ExGridSurface";
  6. ObjectId surfaceStyleId = civilDoc.Styles.SurfaceStyles["Slope Banding (2D)"];
  7. ObjectId surfaceId = GridSurface.Create(surfaceName, 25, 25, 0.0, surfaceStyleId);
  8. var surface = surfaceId.GetObject(OpenMode.ForWrite) as GridSurface;
  9. Random rand=new Random();
  10. for (int i = 0; i < 10; i++)
  11. {
  12. for (int j = 0; j < 10; j++)
  13. {
  14. var z = rand.NextDouble() * 10;
  15. GridLocation loc=new GridLocation(j,i);
  16. surface.AddPoint(loc, z);
  17. }
  18. }
  19. tr.Commit();
  20. }
  21. }
  • 创建体积曲面
  1. public void CreateTinVolumeSurface()
  2. {
  3. using (var tr=db.TransactionManager.TopTransaction)
  4. {
  5. var surfaceName = "ExampleVolumeSurface";
  6. ObjectId baseId = PromptFromTinSurface("选择基准曲面");
  7. ObjectId comparisonId = PromptFromTinSurface("选择对比曲面");
  8. try
  9. {
  10. ObjectId surfaceId = TinVolumeSurface.Create(surfaceName, baseId, comparisonId);
  11. var surface = surfaceId.GetObject(OpenMode.ForWrite) as TinVolumeSurface;
  12. }
  13. catch (Exception e)
  14. {
  15. ed.WriteMessage($"体积曲面创建失败:{e.Message}");
  16. }
  17. }
  18. }