Here is an extension method for a BaseLineRegion that returns the Extents3D bounding box (at 0 elevation, it wouldn't take too much to convert to a true 3d box if you need that). Note that the Subassemblies used must have a "Top" link for this to work.
///
/// Created by Jeff Mishler
/// Quux Software LLC
/// http://www.quuxsoft.com
/// All rights reserved.
///
using System;
using System.Collections.Generic;
using System.Linq;
using Autodesk.AutoCAD.DatabaseServices;
using AcDb = Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.Civil.DatabaseServices;
using Quux.AcadUtilities;
namespace Quux.C3DUtilities.Extensions
{
public static class BaselineRegionExtensions
{
public static Extents3d BoundingBox(this BaselineRegion bsregion)
{
List<OffsetPoints> pts = new List<OffsetPoints>();
Baseline bsline = null;
using (Transaction tr = HostApplicationServices.WorkingDatabase.TransactionManager.StartTransaction())
{
var corr = (Corridor)tr.GetObject(bsregion.CorridorId, OpenMode.ForRead);
foreach (Baseline bsl in corr.Baselines)
{
foreach (BaselineRegion bsr in bsl.BaselineRegions)
{
if (bsr.Name== bsregion.Name && bsr.StartStation == bsregion.StartStation && bsr.EndStation==bsregion.EndStation)
{
bsline = bsl;
break;
}
}
if (bsline != null)
break;
}
tr.Commit();
}
foreach (AppliedAssembly assy in bsregion.AppliedAssemblies)
{
CalculatedLinkCollection links = assy.GetLinksByCode("Top");
double maxLeftOffset = 0, maxRightOffset = 0;
OffsetPoints offpts = new OffsetPoints();
bool firstpt = true;
foreach (CalculatedLink link in links)
{
foreach (CalculatedPoint cpt in link.CalculatedPoints)
{
Point3d offPt = cpt.StationOffsetElevationToBaseline;
double offset = offPt.Y;
if (firstpt)
{
maxLeftOffset = offset;
maxRightOffset = offset;
offpts.LeftOffset = bsline.StationOffsetElevationToXYZ(offPt);
offpts.RightOffset = bsline.StationOffsetElevationToXYZ(offPt);
firstpt = false;
}
else
{
if (offset < maxLeftOffset)
{
maxLeftOffset = offset;
offpts.LeftOffset = bsline.StationOffsetElevationToXYZ(offPt);
continue;
}
else if (offset > maxRightOffset)
{
maxRightOffset = offset;
offpts.RightOffset = bsline.StationOffsetElevationToXYZ(offPt);
}
}
}
}
pts.Add(offpts);
}
Point2dCollection bndypts = new Point2dCollection();
for (int i = 0; i < pts.Count; i++)
{
bndypts.Add(new Point2d(pts[i].LeftOffset.X, pts[i].LeftOffset.Y));
}
for (int i = pts.Count - 1; i > -1; i--)
{
bndypts.Add(new Point2d(pts[i].RightOffset.X, pts[i].RightOffset.Y));
}
Polyline pline = new Polyline(bndypts.Count);
for (int i = 0; i < bndypts.Count; i++)
{
pline.AddVertexAt(i, bndypts[i], 0, 2, 2);
}
pline.Closed = true;
pline.Elevation = 0;
var retval = pline.GeometricExtents;
pline.Dispose();
return retval;
}
}
}