using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
using Autodesk.Civil.ApplicationServices;
using Autodesk.Civil.DatabaseServices;
using Autodesk.Civil.DatabaseServices.Styles;
#pragma warning disable 0618
namespace CreateNetworkTest
{
public class CmdClass
{
/// <summary>
/// Этот метод работает
/// </summary>
[CommandMethod("CreateNetworkTestTrans")]
public void CreateNetCmdTrans()
{
CreateNetAndGetParamsForPipe
(out ObjectId netId,
out ObjectId pipeFamId,
out ObjectId pipeSizeId);
// Добавляем трубу в сеть, открытую с помощью транзакции
using (Transaction tr = netId.Database.TransactionManager.StartTransaction())
{
Network net = tr.GetObject(netId, OpenMode.ForWrite) as Network;
AddPipe(net, pipeFamId, pipeSizeId);
tr.Commit();
}
}
/// <summary>
/// Этот метод не работает
/// </summary>
[CommandMethod("CreateNetworkTestNoTrans")]
public void CreateNetCmdNoTrans()
{
CreateNetAndGetParamsForPipe
(out ObjectId netId,
out ObjectId pipeFamId,
out ObjectId pipeSizeId);
// Добавляем трубу в сеть, открытую без транзакции
using (Network net = netId.Open(OpenMode.ForWrite) as Network)
{
AddPipe(net, pipeFamId, pipeSizeId);
}
}
static void AddPipe(Network net, ObjectId pipeFamId, ObjectId pipeSizeId)
{
LineSegment3d line = new LineSegment3d
(new Point3d(0, 0, 0), new Point3d(100, 0, 0));
ObjectId pipeId = ObjectId.Null;
net.AddLinePipe
(pipeFamId, pipeSizeId, line, ref pipeId, false);
}
static void CreateNetAndGetParamsForPipe
(out ObjectId netId, out ObjectId pipeFamId,
out ObjectId pipeSizeId)
{
CivilDocument cdoc = CivilApplication.ActiveDocument;
string newNetName = "NewNetwork";
netId = Network.Create(cdoc, ref newNetName);
ObjectId partsListId = cdoc.Styles.PartsListSet[0];
using (Network net
= netId.Open(OpenMode.ForWrite) as Network)
{
net.PartsListId = partsListId;
}
using (PartsList partsList
= partsListId.Open(OpenMode.ForRead) as PartsList)
{
ObjectIdCollection pipeFamIds
= partsList.GetPartFamilyIdsByDomain(DomainType.Pipe);
pipeFamId = pipeFamIds[0];
}
using (PartFamily pipeFam
= pipeFamId.Open(OpenMode.ForRead) as PartFamily)
{
pipeSizeId = pipeFam[0];
}
}
}
}