Accessing ProfileView from PartProfileLabel using AutoCAD Civil 3D .NET API

By Partha Sarkar

In AutoCAD
Civil 3D using UI tools, if we select a Structure Profile Label in the Profile
View and look into the Property Window you will see the name of the Profile
View as shown in the screenshot below –

 

ProfileView_From_StructureProfileLabel

 

If you want
to access the same using AutoCAD Civil 3D .NET API, here is a C# .NET code
sample –

//get editor and database 
Editor ed = AcadApp.DocumentManager.MdiActiveDocument.Editor;
Database db = AcadApp.DocumentManager.MdiActiveDocument.Database;
CivilDocument civilDoc = CivilApplication.ActiveDocument;
 
PromptEntityOptions entOpt = new PromptEntityOptions("nSelect ProfileViewPart:");
entOpt.SetRejectMessage("nThis is not ProfileViewPart!");
entOpt.AddAllowedClass(typeof(ProfileViewPart), true);
PromptEntityResult entRes = ed.GetEntity(entOpt);
if (entRes.Status == PromptStatus.OK)
{
  ObjectId pViewPartId = entRes.ObjectId;
  using (Transaction tr = db.TransactionManager.StartTransaction())
  {
    ObjectIdCollection pViewPartLabelsIds;
    using (ProfileViewPart pViewPart = tr.GetObject(pViewPartId, OpenMode.ForRead) as ProfileViewPart)
    {
      pViewPartLabelsIds = pViewPart.GetPartProfileLabelIds();
    }
    foreach (ObjectId labelId in pViewPartLabelsIds)
    {
      using (PartProfileLabel label = tr.GetObject(labelId, OpenMode.ForRead) as PartProfileLabel)
      {
        if (label is StructureProfileLabel)
        {
          StructureProfileLabel spLabel = label as StructureProfileLabel;
 
          //Accessing a ProfileView from PartProfileLabel
          ObjectId profileViewId = spLabel.ViewId;
          ProfileView profileView = tr.GetObject(profileViewId, OpenMode.ForRead) as ProfileView;
          ed.WriteMessage("nProfileView Name : " + profileView.Name.ToString());                  
        }
      }
    }
  } 
}

 

 

Hope this is
useful to you!


Comments

Leave a Reply

Discover more from Autodesk Developer Blog

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

Continue reading