Get all element in active View

Get all element in active View

jackmrdoctor
Contributor Contributor
460 Views
4 Replies
Message 1 of 5

Get all element in active View

jackmrdoctor
Contributor
Contributor

I think i missunderstand the definition of ActiveView in revit api. Because when i write this 
i was trying to get all instance in OST_ElectricalCircuit in the floor plan that is openning.
View activeView = doc.ActiveView;
IList<Element> electricalCircuits = new FilteredElementCollector(doc, activeView.Id)
.OfCategory(BuiltInCategory.OST_ElectricalCircuit)
.WhereElementIsNotElementType()
.ToList();

But somehow the result show that. above code is same with this code . even with active view above code shown all
OST_ElectricalCircuit instance in doc 
IList<Element> electricalCircuits = new FilteredElementCollector(doc)
.OfCategory(BuiltInCategory.OST_ElectricalCircuit)
.WhereElementIsNotElementType()
.ToList();

Please anyone know what wrong i just want to get the instance in the floorplan view that i open only.
thank you

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

scgq425
Advocate
Advocate

Hi @jackmrdoctor :

look up u instance elevation , usually the mep instance just visiably in this fllor plan but instance in next floor .  like : pipe level in 2F , but we can see this in 1F

LanHui Xu 徐兰辉
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

Blog
LinkedIn
Revit/CAD Development | Steel Product Manager

EESignature

0 Likes
Message 3 of 5

TripleM-Dev.net
Advisor
Advisor

Hi @jackmrdoctor,

 

The first collector with  "activeView.Id" should only retrieve the OST_ElectricalCircuit elements visible in the that view, the second one returns all used in the model.

 

If they return the same elements then the view passed in the first collector also has all elements visible.

Try hiding some in the view and run them again, they should then return different lists.

 

- Michel

0 Likes
Message 4 of 5

jeremy_tammik
Alumni
Alumni
Accepted solution

Whether there is a difference or not between the result of these two code snippets will obviously depend on your model. Also,I am not sure what the result will be when the view is still in the process of opening. After it has finished opening, the viewId argument to the filtered element collector should limit the elements returned to the ones visible in the given view:

  

  

Constructs a new FilteredElementCollector that will search and filter the visible elements in a view.
  

Elements that will be passed by the collector have graphics that may be visible in the input view. Some elements may still be hidden because they are obscured by other elements.

    

For elements which are outside of a crop region, they may still be passed by the collector because Revit relies on later processing to eliminate the elements hidden by the crop. This effect may more easily occur for non-rectangular crop regions, but may also happen even for rectangular crops. You can compare the boundary of the region with the element's boundary if more precise results are required.

 

Accessing these visible elements may require Revit to rebuild the geometry of the view. The first time your code constructs a collector for a given view, or the first time your code constructs a collector for a view whose display settings have just been changed, you may experience a significant performance degradation.

  

 

Jeremy Tammik Developer Advocacy and Support + The Building Coder + Autodesk Developer Network + ADN Open
0 Likes
Message 5 of 5

jackmrdoctor
Contributor
Contributor

this were my code


using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using System;
using System.Collections.Generic;
using System.Linq;

[Transaction(TransactionMode.Manual)]
public class GetAllElectricalCircuitsCommand : IExternalCommand
{
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
// Get the Revit document
Document doc = commandData.Application.ActiveUIDocument.Document;

try
{
// Get the active view
View activeView = doc.ActiveView;

// Filter electrical circuits visible in the active view
IList<Element> electricalCircuits = new FilteredElementCollector(doc, activeView.Id)
.OfCategory(BuiltInCategory.OST_ElectricalCircuit)
.WhereElementIsNotElementType()
.ToList();

// Create a list to store the information for each electrical circuit
List<string> circuitInfo = new List<string>();

// Append the name and ID of each electrical circuit, along with the name of the active view
foreach (var circuit in electricalCircuits)
{
circuitInfo.Add("ID: " + circuit.Id.IntegerValue + ", Name: " + circuit.Name + " - Active View: " + circuit.Document.ActiveView.Name);
}

// Display the count of electrical circuits and their information using TaskDialog
TaskDialog taskDialog = new TaskDialog("Electrical Circuits");
taskDialog.MainContent = "Total Electrical Circuits in " + activeView.Name + " view: " + electricalCircuits.Count + "\n\n";
taskDialog.MainContent += string.Join("\n", circuitInfo);
taskDialog.Show();

return Result.Succeeded;
}
catch (Exception ex)
{
TaskDialog.Show("Error", ex.Message);
return Result.Failed;
}
}
}
this were my code . The project i was testing on is actually the Snowdon tower Sample project in Revit 2024

jackmrdoctor_1-1713402796731.png

what i found interesting is ActiveView of all OST_ElectricalCircuit will automatically change to the level which is oppening. That's why it display all the circuit. in the code i was acctually display all the active view of all circuit, even they are at different level but they only show the current level.  I think this is error because Active view work fine with other but not the circuit. Thank you

0 Likes