I took on a project which required replacing Profile View Band label text components, but I had a problem. The problem was that I couldn’t figure out a way to find out what the label was labeling. The Civil 3D API doesn’t provide access to what it is labeling, in this case. This caused the program to sometimes not replace the contents of the correct band label. The solution is to use the AchorInfo property of the label. Using this property I may find the station and elevation location of the label. This will return a nearly correct station value of what the label is labeling, even if the label is adjusted so they don’t overlap. The code that I was using is below.
private static ObjectId ReplaceBandLabelTextComponent(ObjectId vgpLabelObjId, ProfileDataBandLabelGroup profDataBandLabelGroup, List<PVIPointInfo> pviPtInfo, ProfileView profView)
{
double station = double.NaN;
double elev = double.NaN;
foreach (LabelGroupSubEntity label in profDataBandLabelGroup.SubEntities)
{
if (label.StyleId == vgpLabelObjId)
{
profView.FindStationAndElevationAtXY(label.AnchorInfo.Location.X, label.AnchorInfo.Location.Y, ref station, ref elev);
PVIPointInfo pviPtToUse = GetPviAtStation(pviPtInfo, station);
if (pviPtToUse != null)
{
foreach (ObjectId textObjId in label.GetTextComponentIds())
{
label.SetTextComponentOverride(textObjId, pviPtToUse.PointDescription);
}
}
}
}
return vgpLabelObjId;
}
Some of the objects are custom objects that I use to make it easier to pass information between the methods. I said almost correct in that with floating points computers use there is some rounding difficulties which may be encountered. The PVI might be reported at 23.32233412121 and the AnchorInfo at 23.32233418354. Most of the digits match, except for the ending digits. This will make any equality checks fail. To get around this we have to use a technique to see if it is approximately correct.
To do this I created an extension for double types. An extension makes the method like it always belongs to the object type. This way it may be used where ever I need it. The code is below.
public static class DoubleExtensions
{
public static bool AreEqual(this double number, double number2)
{
double difference = Math.Abs(number * .0001);
// Compare the value
if (Math.Abs(number - number2) <= difference)
return true;
else
return false;
}
}
Source: http://msdn.microsoft.com/en-us/library/sy5fbaw6(v=vs.95).aspx
Now I put the code together and I can now replace the text in the correct label.