If you try read the Pipe or Structure Description using the API, it will return the size information, which is not what we need. In fact that information is on the database, but we need to get it from the part family. The code below show how it can be done. Note the method signature for Extensions.
Here is the code:
public static string GetPartDescription(this Part p)
{
using (Transaction trans = p.Database.TransactionManager.StartTransaction())
{
// access the part (pipe or structure) network
Network pipenetwork = trans.GetObject(p.NetworkId, OpenMode.ForRead) as Network;
// get the parts list of the network
PartsList partsList = trans.GetObject(pipenetwork.PartsListId, OpenMode.ForRead) as PartsList;
// for each family on the parts list...
for (int f = 0; f < partsList.PartFamilyCount; f++)
{
// ... get the information and...
PartFamily partFam = trans.GetObject(partsList[f], OpenMode.ForRead) as PartFamily;
// ... get all sizes and...
for (int s = 0; s < partFam.PartSizeCount; s++)
{
// for each size, check if the name match
PartSize partSize = trans.GetObject(partFam[s], OpenMode.ForRead) as PartSize;
// if the name match, return the part family name
// which is the information we're looking for
if (partSize.Name.Equals(p.PartDescription)) return partFam.Name;
}
}
}
return string.Empty;
}
That can be used like a property (thanks to .NET Extension mechanism):
[CommandMethod("showpartdesc")]
public static void CmdShowPartDescription()
{
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
PromptEntityOptions peo = new PromptEntityOptions("\nSelect a pipe or structure: ");
peo.SetRejectMessage("\nOnly pipes or structures");
peo.AddAllowedClass(typeof(Part), false);
PromptEntityResult per = ed.GetEntity(peo);
if (per.Status != PromptStatus.OK) return;
Database db = Application.DocumentManager.MdiActiveDocument.Database;
using (Transaction trans = db.TransactionManager.StartTransaction())
{
Part p = trans.GetObject(per.ObjectId, OpenMode.ForRead) as Part;
ed.WriteMessage("\nPipe description: {0}", p.GetPartDescription());
}
}