how to create structures ( pipe network) using .net (C#)

how to create structures ( pipe network) using .net (C#)

or_levi5LSMC
Participant Participant
862 Views
4 Replies
Message 1 of 5

how to create structures ( pipe network) using .net (C#)

or_levi5LSMC
Participant
Participant

hey, 

im pretty new to the civil3d automation, and i need to find a way to create new structures from data that i got from survey.

i looked in the api documentation, and saw only one example of how to create a pipe network using VB. (was not very clear) 

 

if i understand correctly, to create structures , i need to first create new pipe network, and then i should use one of 2 overloaded methods of Netowork.AddStructure: which is specific to add new structure.. 

AddStructure(ObjectId, ObjectId, Point3d, Double, ObjectId, Boolean)

can someone help me with code example of how to start? 

0 Likes
Accepted solutions (1)
863 Views
4 Replies
Replies (4)
Message 2 of 5

Aravinth.Pichamani
Enthusiast
Enthusiast
Accepted solution
// add structure by cogo points 
[CommandMethod("AddStrbyPoint")]

public void AddStrPoints()
{
    Document doc = Application.DocumentManager.MdiActiveDocument;
    Database db = doc.Database;
    CivilDocument Cdoc = CivilApplication.ActiveDocument;
    Editor editor = doc.Editor;
    try
    {
        using (Transaction tr = db.TransactionManager.StartTransaction())
        {
            ObjectIdCollection getpipeNetworks = Cdoc.GetPipeNetworkIds();
            foreach (ObjectId id in getpipeNetworks)
            {
                Network network = tr.GetObject(id, OpenMode.ForWrite) as Network;

                // Get the Partlist from civil 3d dwg
                Var partlistColl = Cdoc.Styles.PartsListSet.Cast<ObjectId>().Select(p => p.GetObject(OpenMode.ForRead) as PartsList).ToList();

                // select part list from the listed partlist 
                PartsList Selectpartlist = partlistColl.Where(p => p.Name).First();

                // Get the part Partfamily from selected partlist
                List<PartFamily> partfamilyCollecStr = Selectpartlist.GetPartFamilyIdsByDomain(DomainType.Structure).Cast<ObjectId>().Select(p => p.GetObject(OpenMode.ForRead) as PartFamily).ToList();
                PartFamily SelectedPartFamliyStr = partfamilyCollecStr.Where(p => p.Name).First();
                foreach (var partfamily in partfamilyCollecStr)
                {
                    Structure strFamliySizes = tr.GetObject(partfamily, OpenMode.ForWrite) as Structure;
                    // if u have multiple point create list point 3d 
                    Point3d point3D = new Point3d()

                // create str by point 

                ObjectId Addstrbypoit = network.AddStructure(point3D, SelectedPartFamliyStr, strFamliySizes)
                }
            }

            tr.Commit();
        }
    }
    catch (System.Exception ex)
    {
        editor.WriteMessage("Error {}", ex.Message);
    }
}
Message 3 of 5

Aravinth.Pichamani
Enthusiast
Enthusiast

Note: This is just an example. You can modify it further.

 

0 Likes
Message 4 of 5

or_levi5LSMC
Participant
Participant

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}");
                }
            }
        }
    }



}
0 Likes
Message 5 of 5

norman.yuan
Mentor
Mentor

It looks to me that you missed one step: after creating the new Network and before adding Structures/Pipes to the it, you should assign a PartsList to the network. 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes