retrieve connected elements on a circuit ... advice please.

retrieve connected elements on a circuit ... advice please.

MuirEng
Collaborator Collaborator
674 Views
6 Replies
Message 1 of 7

retrieve connected elements on a circuit ... advice please.

MuirEng
Collaborator
Collaborator

 

Hello and thanks if anyone can take a few minutes and look at this. 

What I am trying to do is retrieve all elements connected to a circuit, but the result also contains elements on other circuits. What am I doing wrong please? 

 

public string GetCircuitElementInfo(Document doc)
{
// Filter for electrical circuits
  FilteredElementCollector collector = new FilteredElementCollector(doc);
collector.OfCategory(BuiltInCategory.OST_ElectricalCircuit);

// StringBuilder to build the results string
StringBuilder messageBuilder = new StringBuilder();
messageBuilder.AppendLine("Circuit Element Information:");

// Iterate through the list of circuits
foreach (ElectricalSystem circuit in collector.OfClass(typeof(ElectricalSystem)))
{
    messageBuilder.AppendLine($"Circuit ID: {circuit.Id}");

   // List all elements on the circuit
   foreach (Element element in circuit.Elements)
{
   string elementType = element.GetType().Name;
   string elementName = element.Name;
   string elementId = element.Id.ToString();

messageBuilder.AppendLine($" - Type: {elementType}, Name: {elementName}, ID: {elementId}");
}
}

return messageBuilder.ToString();

Brian Muir, P.Eng, Muir Engineering
0 Likes
675 Views
6 Replies
Replies (6)
Message 2 of 7

ctm_mka
Collaborator
Collaborator

You're not doing anything wrong per se, rather your code is too broad in scope. Your filtered element collector is grabbing all circuits in the projects. Perhaps what you want is to select and element (I suggest PickObject with a selection filter) and then work your way back to circuit, then proceed as you are. Unless of course your can use PickObject on the circuit i

0 Likes
Message 3 of 7

MuirEng
Collaborator
Collaborator

Hi and thanks for taking the time to reply.
But I'm a bit confused.

I'm using this line which as I understand it should take advantage of the .Elements method of the ElectricalSystem class which as I understood it returns a collection of the elements connected.  

 

Foreach (Element element in circuit.Elements)

 

But I went into the revit documentation and do not see ElectricalSystem.Elements as a method so I'm wondering now if I've chased some bad information. If this is a bad method shouldn't I get an error during compile or execution here? 

It seems that I may need to traverse in the other direction, from elements back to circuit number if there is no API method to obtain a collection of connected elements as I may have misunderstood. This will not be too difficult but a slower to execute especially on models containing lots of circuits. 


Brian Muir, P.Eng, Muir Engineering
0 Likes
Message 4 of 7

ctm_mka
Collaborator
Collaborator

Okay lets walk through your code in broad terms.

First, your collector, grabs all objects of the electrical circuit category. Next, your first foreach loop, iterates through all circuits collected, of the electrical system class. As a side note, you can move ".OfClass(typeof(ElectricalSystem)" to your collector, after the "OfCategory" statement. Last, the inner foreach loop iterates over each element in the current electrical system. So going back to your original post, which I understand to mean you only want the elements in one particular circuit at a time. Nothing in your code will return only one circuit, it will always return each element of each circuit, in the entire model.  As for you second question, I see "ElectricalSystems.Elements" in the 2024 API, what version are you using? (Always important to specify this) I dont think thats an issue, as you were getting the info you wanted, just to much correct?  Which brings me back to Pickobject with a selection filter. I dont do MEP, so I can't verify, but you should be able to filter for the Electrical System, assuming you can manually select it in Revit>


0 Likes
Message 5 of 7

MuirEng
Collaborator
Collaborator

Hello, ok, get it now, thank you. 

I'm using Revit 2024, and yes, I see elements as a property when I revit the documentation. Of course this must be the case or I'd see a compile error. Duh.

My problem is that this line:

       foreach (Element element in circuit.Elements)

retrieves a collection of ElementID objects and I'm trying to iterate over elements and not element IDs. 
This line works: 

      foreach (ElementId elementId in circuit.Elements)

I'm not sure I have a 100% understanding of this but at least I have it working now. Thanks again. 

 

Brian Muir, P.Eng, Muir Engineering
0 Likes
Message 6 of 7

ctm_mka
Collaborator
Collaborator

Well since your getting Element Id's, then all you should need to do in that loop is add something like this:

Element element = doc.GetElement(elementid);

then get the rest of your information using that.

0 Likes
Message 7 of 7

MuirEng
Collaborator
Collaborator
Thank you, I have this working now. I appreciate your help.
Brian Muir, P.Eng, Muir Engineering
0 Likes