Get CogoPoint label text

By Augusto Goncalves

The CogoPoint object on Civil 3D can hold labels with more information than the regular Description, which contains only part of the information (and sometimes not the same showed on the point).

point_description

One alternative could be the CogoPoint.GetLabelTextComponentOverride method, passing the IDs obtained from. Unfortunately this will output the format of the text, as shown on the Text Component Editor, not the actual value.

point_label_component

So one workaround, although not very elegant, can be explode the CogoPoint in memory, then explode again and again all the containing BlockReference and MText objects, to end up with DBText, which is the information we seek. The following code show the idea.

[CommandMethod("getPointText")]    public void CmdGetPointText()    {      Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;      PromptEntityResult resPoint = ed.GetEntity("Select point: ");      if (resPoint.Status != PromptStatus.OK) return;      ed.WriteMessage(GetText(resPoint.ObjectId));    }         private string GetText(ObjectId id)    {      // store the DBTexts      StringBuilder entityText = new StringBuilder();           Database db = Application.DocumentManager.        MdiActiveDocument.Database;      using (Transaction trans = db.        TransactionManager.StartTransaction())      {        // open the entity        Entity point = trans.GetObject(id,           OpenMode.ForRead) as Entity;             // do a full explode (considering explode again        // all BlockReferences and MText)        List objs = FullExplode(point);        foreach (Entity ent in objs)        {          // now get the text of each DBText          if (ent.GetType() == typeof(DBText))          {            DBText text = ent as DBText;            entityText.AppendLine(text.TextString);          }        }        trans.Commit();      }           return entityText.ToString();    }         private List FullExplode(Entity ent)    {      // final result      List fullList = new List();           // explode the entity      DBObjectCollection explodedObjects = new DBObjectCollection();      ent.Explode(explodedObjects);      foreach (Entity explodedObj in explodedObjects)      {        // if the exploded entity is a blockref or mtext        // then explode again        if (explodedObj.GetType() == typeof(BlockReference) ||            explodedObj.GetType() == typeof(MText))        {          fullList.AddRange(FullExplode(explodedObj));        }    &#
160;   else          fullList.Add(explodedObj);      }      return fullList;    }

Comments

4 responses to “Get CogoPoint label text”

  1. Hi Augusto,
    Only commenting on the conceptual aspect of this, would it not instead be simpler for the API Author to include a GetDisplayedTextString() Method?
    Think similar to extracting the TextString Property of a Text entity which includes a Field reference… The evaluated result is returned, and not the FieldCode.
    This Method (and it’s overload(s)) would need to account for multiple components given the myriad ways a Point Label Style can be configured, of course, so it stands to reason that perhaps returning a List, etc. would be required, and only apply to the COGO Point’s current Style(s).
    Again, just a quick, upper level thought… Obviously there would be a need to dig further to ensure no deal breakers were the API Author to consider the suggestion.
    Cheers

  2. Hi,
    Thanks, sure that make sense and we’re investigating it… the approach suggested here is a workaround if you need this feature right now.
    Regards,
    Augusto Goncalves

  3. Maybe need call Dispose() for new objects after Explode()?

  4. That’s a good point: indeed you should call .Dispose() if the Entity is not appended to the database. Thanks for pointing that out.

Leave a Reply to BlackBoxCancel reply

Discover more from Autodesk Developer Blog

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

Continue reading