Collect the visible rebars in the current view

Hi Friends,
is the any python code or node to collect the visible rebars in the current view which located inside the crop reigon.

Any help we be appreciated.
Thanks in advance

@hanywillim

just use a Collector, active view

# 🎯 get items
elements = FilteredElementCollector(doc, doc.ActiveView.Id).OfCategory(BuiltInCategory.OST_Walls).WhereElementIsNotElementType().ToElements()
1 Like

With this example, I have (4) pieces of rebar in the project. (3) are in the active view with only (2) of them visible in the view.

With the code block you have to make the view active before executing the script.

The “All Elements of Category In View” node will pull only the visible rebar in the inputted view. The view does not have to be active. And as shown, if you don’t provide a view, it pulls all of the rebar in the entire project.

@staylor , Thanks for your response.
I tried your way but the isuue is it collect all the visible rebars in the view even the ones outside the crop region of the view , So I am looking to collect only the visible rebars inside the crop region.

Thanks in advace

this method will collect all the rebars even the ones outside the crop region

That’s weird, because as my example shows and when I tested, the code block and the node (with the view specified) only collected the bars that were visible and inside the crop region. (1) bar is outside the crop region. (3) bars are inside the crop region, but only (2) are visible. Not sure why it pulled all rebar with your project. I will have to defer to others for insight. Sorry

this is what i got
i only have 2 rebars (set of rebars) within the crop region as shown but the code block give me the outside rebars also:

1 Like

@hanywillim

a work around can be make a selection…


#đź”· PickElemnetsbyRectangle
selected_elements = selection.PickElementsByRectangle('visible rebars')
OUT = selected_elements

i got this error

I just want to collect the rebars without any selection from the user

1 Like

@hanywillim

yes ! you have to set up your modules,

look


from Autodesk.Revit.UI.Selection import Selection

selection = uidoc.Selection #type: Selection

1 Like

@Draxl_Andreas I got your point,
but your way force the user to make a selection when i am looking for collecting all the visible rebars inside the crop region automatically.
Do you have any suggestions.

Thanks in advance

make sure “crop view” is on

@rajeshjamariya16 , Yes i am pretty sure that the “Crop View” is On.

Hi,

here a workaround if the cropshape of view is set

get_elem_in_view


import clr
import sys
import System
#
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *
import Autodesk.DesignScript.Geometry as DS

#import Revit API
clr.AddReference('RevitAPI')
import Autodesk
from Autodesk.Revit.DB import *
import Autodesk.Revit.DB as DB
from Autodesk.Revit.DB.Structure import *

#import net library
from System import Array
from System.Collections.Generic import List, IList, Dictionary

#import transactionManager and DocumentManager (RevitServices is specific to Dynamo)
clr.AddReference('RevitServices')
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager

doc = DocumentManager.Instance.CurrentDBDocument

def get_elem_in_view(view_plan, lst_BuiltInCategories = []):
    catFilter = ElementMulticategoryFilter(List[BuiltInCategory](lst_BuiltInCategories))
    elements_in_view = FilteredElementCollector(doc, view_plan.Id).WherePasses(catFilter).WhereElementIsNotElementType().ToElements()
    # filter and fix list by cropRegionManager.Shape if is set 
    cropRegionManager = view_plan.GetCropRegionShapeManager()
    if  cropRegionManager.ShapeSet and view_plan.CropBoxActive :
        temp = []
        lst_CurveLoop_shape=  cropRegionManager.GetCropShape()
        solid_view_crop = DB.GeometryCreationUtilities.CreateExtrusionGeometry(lst_CurveLoop_shape, XYZ(0,0,1), 0.1)
        topFace = next((f for f in solid_view_crop.Faces if isinstance(f, DB.PlanarFace) and f.FaceNormal.IsAlmostEqualTo(XYZ(0,0,1))), None)
        if topFace is not None:
            for elem_instance in elements_in_view:
                if isinstance(elem_instance.Location, LocationPoint):
                    all_End_xyz = [elem_instance.Location.Point] 
                elif isinstance(elem_instance.Location, LocationCurve):
                    all_End_xyz = [elem_instance.Location.Curve.GetEndPoint(0), elem_instance.Location.Curve.GetEndPoint(1)] 
                else:
                    bbx = elem_instance.get_BoundingBox(None)
                    all_End_xyz = [(bbx.Min + bbx.Max) * 0.5] 
                # compute ray fro each point and check the Intersection with TopFace
                if all(topFace.Intersect(DB.Line.CreateUnbound(pt, XYZ(0,0,1))) == SetComparisonResult.Disjoint for pt in all_End_xyz):
                    pass
                else:
                    temp.append(elem_instance)
        # replace init list
        elements_in_view = temp[:]
        # release geometry
        solid_view_crop.Dispose()
    return elements_in_view

view_plan = doc.ActiveView

OUT = get_elem_in_view(view_plan, [BuiltInCategory.OST_StructuralColumns])
1 Like

@c.poupin , You always amaze me, As Usual
Many Thanks, Boss
Hany William.

1 Like