/// <summary>
/// Repeatedly inserts hangers into model space until the user cancels the routine through the jig.
/// Once the hanger is inserted it will be anchored to the pipe and a threaded rod will be drawn to
/// the desired elevation
/// </summary>
/// <param name="manufacturer">The manufacturer of the hanger</param>
/// <param name="model">The model number of the hanger</param>
/// <param name="partSize">The size of the hanger</param>
internal static void AddHanger(string manufacturer, string model, string partSize)
{
Document document = Application.DocumentManager.MdiActiveDocument;
Database database = document.Database;
Editor editor = document.Editor;
const double tolerance = 0.25;
while (true)
{
try
{
Member hanger = GetHangerFromCatalog(manufacturer, model, partSize);
if (hanger != null)
{
Member jigHanger = hanger;
hanger = jigHanger as MultiViewPart;
jigHanger.Normal = Vector3d.ZAxis.TransformBy(editor.CurrentUserCoordinateSystem);
jigHanger.Direction = Vector3d.XAxis.TransformBy(editor.CurrentUserCoordinateSystem);
jigHanger.Location = Point3d.Origin;
jigHanger.SetDefaultLayer();
var jig
= new HangerJig
(ref jigHanger
);
// The user can cancel out of the routine by normal means during the jigging process
if (jig.StartJig() != PromptStatus.OK)
{
break;
}
using (Transaction transaction = document.TransactionManager.StartOpenCloseTransaction())
{
var blockTable = transaction.GetObject(database.BlockTableId, OpenMode.ForWrite, false) as BlockTable;
var blockTableRecord = transaction.GetObject(blockTable[BlockTableRecord.ModelSpace], OpenMode.ForWrite, false) as BlockTableRecord;
blockTableRecord.AppendEntity((hanger));
transaction.AddNewlyCreatedDBObject(hanger, true);
// Determine if the hanger was located on a pipe. If so then the hanger will be
// anchored to the pipe using an entity hanger. First get all objects in model space
// and then if an object is a pipe check to see if the insertion point of the hanger
// lies on the pipe curve.
foreach (ObjectId objectId in blockTableRecord)
{
var pipe = transaction.GetObject(objectId, OpenMode.ForRead) as Pipe;
if (pipe != null)
{
Point3d point = hanger.Location;
var lineSegment3D
= new LineSegment3d
(pipe
.StartPoint, pipe
.EndPoint); var pointOnLine = lineSegment3D.GetClosestPointTo(point);
if (point.DistanceTo(pointOnLine.Point) <= tolerance)
{
// At this point the hanger should either be on the pipe or be
// within the tolerance.
pipe.UpgradeOpen();
transaction.GetObject(hanger.ObjectId, OpenMode.ForWrite);
var anchor
= new AnchorEntityToEntity
(); anchor.SetToStandard(database);
anchor.SubSetDatabaseDefaults(database);
anchor.SetReferencedEntity(pipe);
anchor.SetReferencedEntityOldEcs(pipe.Ecs);
hanger.SetAnchor(anchor);
hanger.UpdateGeoEcs(true);
break;
}
pipe.Dispose();
}
}
transaction.Commit();
}
}
}
catch (Exception e)
{
Console.WriteMessage("\n**Error** Unable to create/insert hanger.");
}
}
return;
}