Get ElementId of all visible entities in a viewport

Get ElementId of all visible entities in a viewport

Anonymous
Not applicable
4,015 Views
8 Replies
Message 1 of 9

Get ElementId of all visible entities in a viewport

Anonymous
Not applicable

Hi,

 

I am looking for the code to get all ElementIds of all entities inside a viewport. A viewport is a region of a big view plan. How to get ElementId of all visible entities in a viewport?

 

Thank you.

 

0 Likes
Accepted solutions (3)
4,016 Views
8 Replies
Replies (8)
Message 2 of 9

Aaron.Lu
Autodesk
Autodesk
Accepted solution
Dear, you can take a look at this question:
http://forums.autodesk.com/t5/revit-api/elements-from-linked-document/m-p/5867049#M12287

in order to get visible elements regardless of linked elements, you can use FilteredElementCollector(Document document, ElementId viewId)

in order to include linked elements, you can use CustomExpoter, see examples here:
http://thebuildingcoder.typepad.com/blog/2013/07/graphics-pipeline-custom-exporter.html
when you call CustomExporter.Export, it will go through all the visible elements in the view you specified.


Aaron Lu
Developer Technical Services
Autodesk Developer Network
0 Likes
Message 3 of 9

Anonymous
Not applicable
Accepted solution

Hi Aaron,

 

I found the solution is to find the ViewPlan associated with the Viewport, then check BoundingBox of all elements in the same level of this ViewPlan to find ones that fit inside this ViewPlan.CropBox.

 

Thank you.

0 Likes
Message 4 of 9

Aaron.Lu
Autodesk
Autodesk
Cool, thanks for posting the solution!


Aaron Lu
Developer Technical Services
Autodesk Developer Network
0 Likes
Message 5 of 9

Anonymous
Not applicable

Dear Khoa, 

 

I am learning Revit API and a beginner. I am so desperate to learn what you have mentioned. Could you provide a code of how the method you have mentioned can be fleshed out? Please, please ... 

 

Minho

0 Likes
Message 6 of 9

jeremytammik
Autodesk
Autodesk
Accepted solution

Dear Minho,

 

Thank you for your query.

 

Welcome to Revit API programming!

 

I suggest that you first of all take a look at the getting started material and work through the step-by-step instructions provided by the DevTV and My First Revit Plugin video tutorials:

 

http://thebuildingcoder.typepad.com/blog/about-the-author.html#2

 

That will show you what other important material is available that you MUST be aware of, answer this question of yours, and many, many more besides.

 

Once you understand the basics of using filtered element collectors, the answers provided above will become self-explanatory.

 

They do already  answer your question in full.

 

The Building Coder discussed the related topic of determining all views displaying a specific element in depth, implementing an external command named CmdViewsShowingElements to try out some approaches:

 

- Determine Views Displaying Given Element -- http://thebuildingcoder.typepad.com/blog/2014/05/views-displaying-given-element-svg-and-nosql.html

- Determining Views Showing an Element -- http://thebuildingcoder.typepad.com/blog/2016/12/determining-views-showing-an-element.html

 

A similar question recently also arose in a StackOverflow thread on how to

 

- Determine whether a FamilyInstance is visible in a View -- http://stackoverflow.com/questions/44012630/determine-is-a-familyinstance-is-visible-in-a-view

 

Colin Stark answered that succinctly, saying:

 

I have found that the most reliable way of knowing whether an element is visible in a view is to use a FilteredElementCollector specific to that view. There are so many different ways of controlling the visibility of an element that it would be impractical to try to determine this any other way.

 

Below is the utility function I use to achieve this. Note this works for any element, and not just for family instances...

 

The category filter is used to eliminate any element not of the desired category before using the slower parameter filter to find the desired element. It is probably possible to speed this up further with clever usage of filters, but I have found that it is plenty fast enough for me in practice.

 

I added Colin's code to The Building Coder samples

 

https://github.com/jeremytammik/the_building_coder_samples

 

CmdViewsShowingElement

 

https://github.com/jeremytammik/the_building_coder_samples/blob/master/BuildingCoder/BuildingCoder/C...

 

I hope this helps.

 

Best regards,

 

Jeremy



Jeremy Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder

Message 7 of 9

jeremytammik
Autodesk
Autodesk

For future reference, I published a summary of this thread:

 

http://thebuildingcoder.typepad.com/blog/2017/05/retrieving-elements-visible-in-view.html

 

Cheers,

 

Jeremy



Jeremy Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder

0 Likes
Message 8 of 9

Anonymous
Not applicable

Jeremy is the big time saver. Thank you.

0 Likes
Message 9 of 9

Anonymous
Not applicable

 

Jeremy, thank you so much for the help! Can't thank you enough!

0 Likes