Message 1 of 11
Civil3D API c# extract label text
Not applicable
04-01-2017
03:05 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
I want to extract the text in the label,
Its a civil 3D label (example pipe, structure, etc)
I am able to get the style name and the text. But the issue is text which i get using he current code is all text so I am not able to get component wise text.
Check the attached screen grab,
My label contains three components,
KN, Top and Bot, I want to extract the text within each of those component.
Is there a way to get that?
My Code as follows,
public static class LabelTextExtractor
{
public static string GetDisplayedLabelText(ObjectId labelId)
{
//if (labelId.ObjectClass.DxfName.ToUpper() != "AECC_GENERAL_SEGMENT_LABEL")
//{
// throw new ArgumentException(
// "argument mismatch: not an \"AECC_GENERAL_SEGMENT_LABEL\"");
//}
StringBuilder lblText = new StringBuilder();
using (var tran = labelId.Database.TransactionManager.StartTransaction())
{
var label = tran.GetObject(labelId, OpenMode.ForRead) as CivilDb.Label;
if (label != null)
{
bool changed = !label.Dragged && label.AllowsDragging;
try
{
if (changed)
{
label.UpgradeOpen();
double delta = label.StartPoint.DistanceTo(label.EndPoint);
label.LabelLocation =
new Point3d(label.LabelLocation.X +
delta, label.LabelLocation.Y +
delta, label.LabelLocation.Z);
}
var dbObjs = FullExplode(label);
foreach (var obj in dbObjs)
{
if (obj.GetType() == typeof(DBText))
{
lblText.Append(" " + (obj as DBText).TextString);
}
obj.Dispose();
}
}
finally
{
if (changed) label.ResetLocation();
}
}
tran.Commit();
}
return lblText.ToString().Trim();
}
#region private methods
private static List<CadDb.DBObject> FullExplode(CadDb.Entity ent)
{
// final result
List<CadDb.DBObject> fullList = new List<CadDb.DBObject>();
// explode the entity
DBObjectCollection explodedObjects = new DBObjectCollection();
ent.Explode(explodedObjects);
foreach (CadDb.Entity explodedObj in explodedObjects)
{
// if the exploded entity is a blockref or mtext
// then explode again
if (explodedObj.GetType() == typeof(CadDb.BlockReference) ||
explodedObj.GetType() == typeof(CadDb.MText))
{
fullList.AddRange(FullExplode(explodedObj));
}
else
fullList.Add(explodedObj);
}
return fullList;
}
#endregion
}