How can we extract the diameter or size of a pipe in AME using the .NET API?
The diameter or size of a given pipe (as seen from the AME user interface) can be calculated using two approaches. The first approach uses the profile definition from the style of a pipe and by accessing the NominalWidth property of the pipe. The second approach to extract the size information of a given pipe is to use its PartData (style). The following code illustrates both these approaches for extracting the pipe size.
[CommandMethod("GetPipeSize", CommandFlags.Modal)]
public static void GetPipeSize()
{
Editor ed = AcadApp.DocumentManager.MdiActiveDocument.Editor;
PromptEntityOptions peo = new PromptEntityOptions("\nPick a pipe");
peo.SetRejectMessage("\nMust select an Pipe");
peo.AddAllowedClass(typeof(Pipe), false);
PromptEntityResult per = AcadApp.DocumentManager.MdiActiveDocument
.Editor.GetEntity(peo);
if (per.Status != PromptStatus.OK)
{
return;
}
Database db = HostApplicationServices.WorkingDatabase;
using (Transaction trans = db.TransactionManager.StartTransaction())
{
Pipe pipe = trans.GetObject(per.ObjectId, OpenMode.ForRead) as Pipe;
// Approach 1 : Use the Profile Definition from pipe's style
Autodesk.AutoCAD.DatabaseServices.ObjectId styleOid = pipe.StyleId;
PipeStyle pipeStyle = (PipeStyle)trans.GetObject(pipe.StyleId,
OpenMode.ForRead);
if (pipeStyle != null)
{
Autodesk.Aec.Building.DatabaseServices.ProfileDefinition proDef =
(Autodesk.Aec.Building.DatabaseServices.ProfileDefinition)
trans.GetObject(pipeStyle.ProfileDefinitionId, OpenMode.ForRead);
if (proDef != null)
{
ed.WriteMessage("\nPipe size from profile definition: " +
proDef.NominalWidth.ToString());
}
}
// Approach 2 : Access the Part data of the pipe
DataRecord memRec = PartManager.GetPartData(pipe);
DataField df = memRec.DataFields.FindByContext(
Context.ConnectionSizeNominalDiameter);
ed.WriteMessage("\nPipe size from Part data: "
+ df.ValueDouble.ToString());
trans.Commit();
}
}