Creating a Profile object using Civil 3D .NET API

By Partha Sarkar

Profile object in Civil 3D is a 3D geometric object which joins the Surface elevations along a horizontal Alignment. Profiles are used to visualize the terrain along a route of interest, such as a proposed road, or simply to show how the elevation changes across a particular region.

Profile.CreateFromSurface() method creates a new profile and derives its elevation information from the specified surface along the alignment. Following C# code snippet demonstrates creating a Civil 3D Profile object using .NET API :

// Get the AutoCAD Editor
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
 
//select an Alignment which we will use to create a Profile
PromptEntityOptions selalignment = new PromptEntityOptions("nSelect an Alignment Object: ");
selalignment.SetRejectMessage("nOnly Alignment Object is allowed");
selalignment.AddAllowedClass(typeof(Alignment), true);
PromptEntityResult resalignment = ed.GetEntity(selalignment);
if (resalignment.Status != PromptStatus.OK) return;
ObjectId alignmentId = resalignment.ObjectId;
 
Database db = Application.DocumentManager.MdiActiveDocument.Database;
CivilDocument civilDoc = CivilApplication.ActiveDocument;
using (Transaction trans = db.TransactionManager.StartTransaction())
{
 
    try
    {
        Alignment alignment = trans.GetObject(alignmentId, OpenMode.ForRead) as Alignment;
 
        // Profile.CreateFromSurface() has 4 overloaded versions
        // In this code snippet we will use - public static ObjectId CreateFromSurface(string profileName,    
        // ObjectId alignmentId, ObjectId surfaceId, ObjectId layerId, ObjectId styleId, ObjectId labelSetId )
 
        // prepare the input parameters
        ObjectId layerId = alignment.LayerId;
 
        // let's get the 1st Surface object in the DWG file 
        ObjectId surfaceId = civilDoc.GetSurfaceIds()[0];
 
        // let's get the 1st Profile style object in the DWG file
        ObjectId styleId = civilDoc.Styles.ProfileStyles[0];
 
        // let's get the 1st ProfileLabelSetStyle object in the DWG file 
        ObjectId labelSetId = civilDoc.Styles.LabelSetStyles.ProfileLabelSetStyles[0];
 
        // Create the Profile Object
        ObjectId profileId = Profile.CreateFromSurface("Profile_Created_using_API", alignmentId, surfaceId, layerId, styleId, labelSetId); 
 
        trans.Commit();
    }
 
    catch (Autodesk.AutoCAD.Runtime.Exception ex)
    {
        ed.WriteMessage("/n Exception message :" + ex.Message);
    }
 
}

As a result of running the custom command containing above code snippet, you would see a new profile (like the following) being added to Civil 3D’s Profiles collection node :

Profile


Comments

6 responses to “Creating a Profile object using Civil 3D .NET API”

  1. Interesting article! Is the method for creating a QuickProfiles almost the same? I have tried this for some time but didn’t succeed. Maybe you can help?

  2. surajit biswas Avatar
    surajit biswas

    sir how i creat profile from file in autocad civil3d

  3. Hi Surajit,
    I don’t see any public API available at moment to create a Profile from a file which is equivalent of the UI command _AeccCreateProfileFromFile, sorry. I am logging a wish list to address this in future release of Civil 3D.
    Thanks,
    Partha Sarkar
    Autodesk Developer Network

  4. Hi Ulrikw,
    My apologies for missing your comment earlier :(
    If you are referring to the CREATEQUICKPROFILE command which draws temporary profiles along line, arc, polyline, lot line, feature line etc., then I find the direct or equivalent public API is missing in the current release. However, Profile.CreateFromFeatureLine() would help in creating a Profile from a corridor feature line.
    For a quick Profile, we should be able to work-around with few extra steps (though I haven’t tried yet), however, an equivalent API to UI command CREATEQUICKPROFILE or _AeccCreateQuickProfile would be a better option. I am logging a wish list for the same to expose a .NET API equivalent for CREATEQUICKPROFILE or _AeccCreateQuickProfile.
    Thanks,
    Partha Sarkar
    Autodesk Developer Network

  5. I’m using VB.net & would like to delete a profile with a given name so that I can create a new profile (from surface) with the same name.
    I’ve tried using the erase method of the profile object, but when it is time the create the new profile, I get an error due to the name already existing. I don’t want to get into any profile name management algorithms. The reason to delete it is because it is static and it should be replaced with a dynamic one.
    Ok, I just tried to make it dyanamic (instead of trying to delete it) via:
    TempProfile.UpdateMode = ProfileUpdateType.Dynamic
    that doesn’t work either (no errors)(transaction is committed). the profile in the drawing remains static.

  6. Hi Steve,
    Deleting a Profile using Erase() works fine. I tried the following code snippet which works fine in my testing in Civil 3D 2013 on Win7 64 bit.
    Profile profile = trans.GetObject(profileId, OpenMode.ForWrite) as Profile;
    profile.Erase(true);
    trans.Commit();
    Regarding Profile update using UpdateMode, it would work on Surface Profile only. That’s what mentioned in the Civil 3D API Ref document. I have published a blog post on the same –
    http://adndevblog.typepad.com/infrastructure/2013/01/updating-a-profile-mode-using-profileupdatemode.html
    Hope this helps.
    Thanks,
    Partha Sarkar
    ADN

Leave a Reply

Discover more from Autodesk Developer Blog

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

Continue reading