Add boundary Line to Surface by using API


Today, I saw a post from Dynamo forum that mentioned how to add boundaryline to the surface. Just make a note for future use. The original post link is listed in the Reference.

.NETAPI-C#

[CommandMethod("AddSurfaceBoundary")]
public void AddSurfaceBoundary()
{
    using (Transaction ts = Application.DocumentManager.MdiActiveDocument.Database.TransactionManager.StartTransaction())
    {
        // Prompt the user to select a surface and a polyline
        ObjectId surfaceId = promptForEntity("Select the surface to add a boundary to", typeof(TinSurface));
        ObjectId polyId = promptForEntity("Select the object to use as a boundary", typeof(Polyline));

        // The boundary or boundaries must be added to an ObjectIdCollection for the AddBoundaries method:
        ObjectId[] boundaries = { polyId };
        TinSurface oSurface = surfaceId.GetObject(OpenMode.ForWrite) as TinSurface;

        try
        {
            oSurface.BoundariesDefinition.AddBoundaries(new ObjectIdCollection(boundaries), 100, Autodesk.Civil.Land.SurfaceBoundaryType.Outer, true);
            oSurface.Rebuild();
        }

        catch (System.Exception e)
        {
            editor.WriteMessage("Failed to add the boundary: {0}", e.Message);
        }

        // commit the transaction
        ts.Commit();
    }
}

private ObjectId promptForTinSurface(String prompt)
{
    PromptEntityOptions options = new PromptEntityOptions(
        String.Format("\n{0}: ", prompt));
    options.SetRejectMessage(
        "\nThe selected object is not a TIN Surface.");
    options.AddAllowedClass(typeof(TinSurface), true);

    PromptEntityResult result = editor.GetEntity(options);
    if (result.Status == PromptStatus.OK)
    {
        // We have the correct object type
        return result.ObjectId;
    }
    return ObjectId.Null;   // Indicating error.
}

IronPython in Dynamo

surfaceId = promptForEntity("Select the surface to add a boundary to", clr.GetClrType(TinSurface))
polyId = promptForEntity("Select the object to use as a boundary", clr.GetClrType(Polyline))

coll = ObjectIdCollection()
coll.Add(plineId)

oSurface = surfaceId.GetObject(OpenMode.ForWrite)

osurface.BoundariesDefinition.AddBoundaries(coll, 100, SurfaceBoundaryType.Outer, True)
osurface.Rebuild()

Reference:

http://help.autodesk.com/view/CIV3D/2020/ENU/?guid=GUID-7C32E309-9B54-42A0-A4DC-C6FD592071F2
https://forum.dynamobim.com/t/create-object-id-collection/49472


Author: Ford Yang
Reprint policy: All articles in this blog are used except for special statements CC BY 4.0 reprint polocy. If reproduced, please indicate source Ford Yang !
  TOC