There is an interesting object available on the Civil 3D API: the ProfileViewPart. Actually it’s a database resident object, meaning we can select and get its ObjectId, but also acts as a ‘proxy’ for Part (Pipe and Structure) that are on a specific ProfileView.
More than that, from this ProfileViewPart, we can reach the original Part and get all the profile views showing it. Now the tricky part is: how get the station and elevation? In fact there is no direct API for it, but we can workaround this with the geometry of the part at the profile view getting its GeometryExtent and, from the XY, get the station and elevation.
The sample below demonstrate the idea.
[CommandMethod("getPartOnProfileView")]
public static void CmdGetPartOnProfileView()
{
// 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);
}
}
}