On a profile view, after add a pipe network part (structure or pipe), Civil 3D creates a new entity: ProfileViewPart. This object don’t have many methods, but it’s a database resident object (with ID and geometry).
So the question is: how get the part station and elevation of the profile view?
Using the part bounding box, we’ll access the XY coordinate on the model space. Then it’s possible to get the average point (or any other point, like minimum or maximum Y). Using that XY, we can go back to the profile view and ask what’s the station & elevation on that point.
The code sample below demonstrate the idea:
// select a part on the profile view
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
PromptEntityOptions peo = new PromptEntityOptions(
"Select a part on the profile view: ");
peo.SetRejectMessage("\nOnly parts on profile view");
peo.AddAllowedClass(typeof(ProfileViewPart), true);
PromptEntityResult per = ed.GetEntity(peo);
if (per.Status != PromptStatus.OK) return;
Database db = Application.DocumentManager.MdiActiveDocument.Database;
using (Transaction trans = db.TransactionManager.StartTransaction())
{
// get the part showing on the view
ProfileViewPart partPV = trans.GetObject(per.ObjectId,
OpenMode.ForRead) as ProfileViewPart;
Part part = trans.GetObject(partPV.ModelPartId,
OpenMode.ForRead) as Part;
// may have several PVs showing this part...
ObjectIdCollection pvIds = part.GetProfileViewsDisplayingMe();
foreach (ObjectId pvId in pvIds)
{
// get the average point of this structure on the profile view
// you may change this X,Y to the max or min...
Extents3d ext = partPV.GeometricExtents;
double x = (ext.MaxPoint.X - ext.MinPoint.X) / 2 + ext.MinPoint.X;
double y = (ext.MaxPoint.Y - ext.MinPoint.Y) / 2 + ext.MinPoint.Y;
// get the profile view
ProfileView pv = trans.GetObject(pvId, OpenMode.ForRead) as ProfileView;
// and the station/elevation on the profile view
double station = 0.0;
double elevation = 0.0;
pv.FindStationAndElevationAtXY(x, y, ref station, ref elevation);
ed.WriteMessage(
"\n{0} showing on {1} at station: {2:0.00} | Elevation: {3:0.00}",
part.Name, pv.Name, station, elevation);
}
}