Change surface style with Civil 3D API

By Daniel Du

I was asked how to edit the style of surface in Civil 3D, for example, we need to change the color of contours. We can do it with Civil 3D UI by right clicking the surface object and choose “Edit surface style…” and switching to “Display” tab to show the dialogue as below:

image

This is code snippet demonstrating how to do it with 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;

            //contours 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";

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

            //Minor contour, layer 0
            surfaceStyle.GetDisplayStylePlan(
                SurfaceDisplayStyleType.MinorContour).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();
        }
    }
}

This is how it looks like before editing:

image

And here is how it looks like after editing the surface contour style with major contour color to red and minor contour color to green.

image

Hope this helps.


Comments

Leave a Reply

Discover more from Autodesk Developer Blog

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

Continue reading