Hey aravinth,
thanks for your comment!
i tried running the script you have provided, and i saw that the
ObjectIdCollection getpipeNetworks = Cdoc.GetPipeNetworkIds();
has 0 elements in it so i needed to loop on something else. so i looped over the partlist.
thanks to your general direction, i could create the following script that loops over mock data of manholes and create structures:
using System;
using System.Collections.Generic;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
using Autodesk.Civil.ApplicationServices;
using Autodesk.Civil.DatabaseServices;
using Autodesk.Civil.DatabaseServices.Styles;
using ModifyStructure;
[assembly: CommandClass(typeof(Civil3DCommands.PipeNetworkCreator))]
namespace Civil3DCommands
{
public class PipeNetworkCreator
{
private string NETWORK_NAME = "MyPipeNetwork";
[CommandMethod("makeStructure")]
public void CreatePipeNetworkWithStructure()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
CivilDocument civilDoc = CivilDocument.GetCivilDocument(db);
using (Transaction tr = db.TransactionManager.StartTransaction())
{
try
{
ObjectId networkId = Network.Create(civilDoc, ref NETWORK_NAME);
Network network = tr.GetObject(networkId, OpenMode.ForWrite) as Network;
ObjectId partsListId = civilDoc.Styles.PartsListSet[0]; // Automatically selects the first parts list
PartsList partsList = tr.GetObject(partsListId, OpenMode.ForRead) as PartsList;
ed.WriteMessage($"\nUsing Parts List: {partsList.Name}");
// Get all families in the Parts List
int partFamilyCount = partsList.PartFamilyCount;
ObjectId structureFamilyId = ObjectId.Null;
PartFamily structureFamily = null;
// mock data- will be replaced by function that loops over the json and fill up the list->
List<Manhole> manholes = new List<Manhole>
{
new Manhole(89, "Circular", "Sewer", 0.12, 0.0, 0.0, "Concrete", 0.07, "Cylinder", 0.0, 0.0, 270, false),
new Manhole(1, "Ractangular", "storm", 0.0, 1.36, 0.48, "Steel", 0.61, "Ractangular", 1.42, 1.52, 90, false)
};
foreach (var manhole in manholes)
{
for (int i = 0; i < partFamilyCount; i++)
{
ObjectId familyId = partsList[i]; // Get the PartFamily ObjectId
PartFamily family = tr.GetObject(familyId, OpenMode.ForRead) as PartFamily;
if (family != null && family.PartType == PartType.StructJunction) // Ensure it's a Structure
{
structureFamilyId = familyId;
ed.WriteMessage($"\nUsing Structure Family: {family.Name}");
if (structureFamilyId == ObjectId.Null)
{
ed.WriteMessage("\nError: No Structure Family found in Parts List.");
return;
}
structureFamily = tr.GetObject(structureFamilyId, OpenMode.ForWrite) as PartFamily;
if (structureFamily != null)
{
if (manhole.coverShape == "Ractangular" && structureFamily.BoundingShape.ToString() == "Box")
{
break;
}
else if (manhole.coverShape == "Circular" && structureFamily.BoundingShape.ToString() == "Cylinder")
{
break;
}
}
}
}
ObjectId structureSizeId = structureFamily[0];
Point3d location = new Point3d(50 + manhole.id * 10, 50, 0); // Example location logic
double rotation = 0;
ObjectId newStructureId = ObjectId.Null;
network.AddStructure(structureFamilyId, structureSizeId, location, rotation, ref newStructureId, true);
}
ed.WriteMessage("\nPipe Network and Structures Created Successfully.");
tr.Commit();
}
catch (System.Exception ex)
{
ed.WriteMessage($"\nError: {ex.Message}");
}
}
}
}
}