By Wayne Brill
There is not an API to convert an AutoCAD object to a Plant 3D pipe support like the PlantPipeSupportConvert command does. You can run the command from your code however. This c# example creates a command that has the user select the pipe and an AutoCAD entity to be used as the support. The location of the support is the mid point of the selected pipe. The Plant 3D API is used to get the length of the pipe from the database and it is used to calculate the mid point. The PlantPipeSuportConvert command takes a point for the pipe. (It uses an an input point monitor to get the pipe at that point).
Download Plant_3D_PipeSupportConvert
Note: Put the Plant_3D_Test_wB.dll in the PLNT3D folder to avoid errors about missing references. (this issue has been reported to Plant 3D engineering)
C:\Program Files\Autodesk\AutoCAD 2014\PLNT3D
Here is the command:
[CommandMethod("PipeSupConvert")]
public void myPlantPipeSuppportConvert()
{
Document docDocument =
AcadApp.DocumentManager.MdiActiveDocument;
Database dbDatabase = docDocument.Database;
Editor ed = docDocument.Editor;
PromptEntityOptions pmtEntOpts =
new PromptEntityOptions
("\nSelect a Pipe to connect to new Support : ");
PromptEntityResult pmtEntRes =
ed.GetEntity(pmtEntOpts);
if (pmtEntRes.Status == PromptStatus.OK)
{
Autodesk.AutoCAD.DatabaseServices.
TransactionManager tmTranMan =
dbDatabase.TransactionManager;
Transaction trans = null;
ObjectId oidPipeId = pmtEntRes.ObjectId;
try
{
Project currentProject =
PlantApp.CurrentProject.
ProjectParts["Piping"];
DataLinksManager dlm =
currentProject.DataLinksManager;
PnPDatabase pnpdb =
dlm.GetPnPDatabase();
// will use this with acedCmd to
// send the command
ResultBuffer rb = new ResultBuffer();
trans = tmTranMan.StartTransaction();
Pipe supP3DPipe = trans.GetObject
(oidPipeId, OpenMode.ForRead) as Pipe;
if (supP3DPipe == null)
{
ed.WriteMessage
("\n Selected entity is not a Plant 3D Pipe");
}
else
{
// Output the ObjectId of the
//Pipe for check purposes
// ed.WriteMessage
//("\n ObjectId of the Pipe : {0}", oidPipeId);
// Get Matrix3d of the selected
// pipe to extract its Xaxis
Matrix3d suppM3D =
((Entity)supP3DPipe).Ecs;
// Get the RowId of the Pipe in
// the Plant 3D project database
int iPipeRowId =
dlm.FindAcPpRowId(oidPipeId);
// Get the corresponding PnPRow
// of the Pipe
PnPRow ppRow =
pnpdb.GetRow(iPipeRowId);
// Get the insertion point of
// the selected Pipe
Point3d p3dPipePosition =
new Point3d(
Convert.ToDouble(ppRow["Position X"]),
Convert.ToDouble(ppRow["Position Y"]),
Convert.ToDouble(ppRow["Position Z"]));
// Get the Pipe length from the
// Plant 3D project database
double dPipeLength =
Convert.ToDouble(ppRow["Length"]);
// Get the position of the
// middle of the selected Pipe
Point3d p3dSupportInsertionPoint =
new Point3d(
p3dPipePosition.X + dPipeLength *
suppM3D.CoordinateSystem3d.Xaxis.X / 2,
p3dPipePosition.Y + dPipeLength *
suppM3D.CoordinateSystem3d.Xaxis.Y / 2,
p3dPipePosition.Z + dPipeLength *
suppM3D.CoordinateSystem3d.Xaxis.Z / 2);
// Commit the transaction have
// all the data now
trans.Commit();
pmtEntOpts = new PromptEntityOptions
("\nSelect AutoCad entity to be converted : ");
pmtEntRes = ed.GetEntity(pmtEntOpts);
if (pmtEntRes.Status ==
PromptStatus.OK)
{
ObjectId oidEntityId =
pmtEntRes.ObjectId;
// Add the command to the ResBuf
rb.Add(new TypedValue
(5005, "_PlantPipeSupportConvert"));
// Add the selected
// entity ObjectId
rb.Add(new TypedValue
(5006, oidEntityId));
// return on the command line
rb.Add(new TypedValue(5005, ""));
// mid point of the selected pipe
rb.Add(new TypedValue
(5009, p3dSupportInsertionPoint));
acedCmd(rb.UnmanagedObject);
}
}
}
catch
{
if (trans != null)
{
trans.Abort();
trans.Dispose();
}
}
}
}