利用Civil 3D API更改曲面的樣式

峻祁連發表於2014-12-30

如果你需要更改曲面的樣式,比如更改等高線的顏色等等,在Civil 3D中,你可以通過在toolspace中選中曲面,然後點右鍵選擇“Edit surface style…”然後切換到“Display” tab 來更改,:

image

下面的通過程式碼的方式使用API來實現同樣的效果:

[CommandMethod("MyGroup", "SurfaceStyleExample", 
    "SurfaceStyleExample", CommandFlags.Modal)]
public void MyCommand() // This method can have any name
{
 
    Document doc = Application.DocumentManager.MdiActiveDocument;
   
    if (doc != null)
    {
        Editor ed = Application.DocumentManager
            .MdiActiveDocument.Editor;

       

        // select a tin surface

        PromptEntityOptions peo = new PromptEntityOptions(
          "\nSelect a tin surface: ");
        peo.SetRejectMessage("\nOnly Tin surface is accepted");
        peo.AddAllowedClass(typeof(TinSurface), true);
        PromptEntityResult per = ed.GetEntity(peo);
        if (per.Status != PromptStatus.OK) return;

        CivilDocument civilDoc = CivilApplication.ActiveDocument;

        using (Transaction trans = doc.TransactionManager
            .StartTransaction())
        {
            TinSurface surface = trans.GetObject(per.ObjectId,
                OpenMode.ForRead) as TinSurface;

            //exclude invalid points, 附贈功能,排除異常點

            surface.BuildOptions.ExecludeMaximumElevation = true;
            surface.BuildOptions.MaximumElevation = 5000;
            surface.BuildOptions.ExecludeMinimumElevation = true;
            surface.BuildOptions.MinimumElevation = 200;

            //set the Maximum Triangle Length,設定三角形最大邊長

            surface.BuildOptions.MaximumTriangleLength = 200;

            //change the style, 下面開始更改樣式了
            ObjectId styleId;
            if (civilDoc.Styles.SurfaceStyles.Contains("Standard"))
            {
                styleId = civilDoc.Styles.SurfaceStyles["Standard"];
            }
            else
            {
                // create a new style called 'example style':
                styleId = civilDoc.Styles.SurfaceStyles
                    .Add("example style");

            }
           
           

            // modify the style

            SurfaceStyle surfaceStyle = styleId.GetObject(
                OpenMode.ForWrite) as SurfaceStyle;

            //countours smoothing

            surfaceStyle.ContourStyle.SmoothContours = true;
            surfaceStyle.ContourStyle.SmoothingType
                = ContourSmoothingType.AddVertices;
            surfaceStyle.ContourStyle.SmoothingFactor = 10;

            surfaceStyle.ContourStyle.MajorContourColorScheme
                = ColorSchemeType.Rainbow;

            //Major contour, red

            surfaceStyle.GetDisplayStylePlan(
                SurfaceDisplayStyleType.MajorContour).Color
                = Autodesk.AutoCAD.Colors.Color.FromRgb(255, 0, 0);

            //Major contour, layer 0

            surfaceStyle.GetDisplayStylePlan(
                SurfaceDisplayStyleType.MajorContour).Layer = "0";

            //Mainor contour, gree

            surfaceStyle.GetDisplayStylePlan(
                SurfaceDisplayStyleType.MinorContour).Color
                = Autodesk.AutoCAD.Colors.Color.FromRgb(0, 255, 0);

            //Mainor contour, layer 0, 如果你要放在其他圖層,要確保該圖層存在

            surfaceStyle.GetDisplayStylePlan(
                SurfaceDisplayStyleType.MajorContour).Layer = "0";

            // display major contours:

            surfaceStyle.GetDisplayStylePlan(SurfaceDisplayStyleType
                .MajorContour).Visible = true;
            surfaceStyle.GetDisplayStylePlan(SurfaceDisplayStyleType
                .MinorContour).Visible = true;

            // turn off display of other items:
           
            surfaceStyle.GetDisplayStylePlan(SurfaceDisplayStyleType
                .UserContours).Visible = false;
            surfaceStyle.GetDisplayStylePlan(SurfaceDisplayStyleType
                .Directions).Visible = false;
            surfaceStyle.GetDisplayStylePlan(SurfaceDisplayStyleType
                .Elevations).Visible = false;
            surfaceStyle.GetDisplayStylePlan(SurfaceDisplayStyleType
                .Slopes).Visible = false;
            surfaceStyle.GetDisplayStylePlan(SurfaceDisplayStyleType
                .SlopeArrows).Visible = false;
            surfaceStyle.GetDisplayStylePlan(SurfaceDisplayStyleType
                .Watersheds).Visible = false;

            //TODO: do the same for all model display settings as well
            //


            // assign the style to the first surface in the document:
          
            surface.StyleId = styleId;

            // commit the transaction

            trans.Commit();

            //rebuild the surface
            surface.Rebuild();
        }

    }



}

 

改之前的樣子:

image

執行之後,等高線的顏色已經改變了.

image

 

 

Hope this helps.

相關文章