Hi im trying to create an add in in which the user input the element ID as a string and then if the element appears in the ActiveView - Revit will find it through out the view, select it and focus on that element.
Similar to the existing “section box (BX)” which is in the Modify tab only that I dont want to touch the section box at all - only focus on the element.
I started by taking the input from the user and tried using the get_boundingBox() method, but im pretty clueless from here.
Can anyone help please?
# collect element from user
element_id_str = forms.ask_for_string(
default='13192553',
prompt="Enter the Element ID:",
title='Go to Element By ID')
if not element_id_str:
forms.alert("No Element ID provided", title="Error")
sys.exit()
# validate input
try:
element_id = DB.ElementId(int(element_id_str))
except ValueError:
forms.alert("Invalid Element ID format", title="Error")
sys.exit()
active_view = doc.ActiveView
# Check if the element exists
element = doc.GetElement(element_id)
if not element:
forms.alert("Element not found in the document", title="Error")
sys.exit()
# Check if the element is in the active view
bounding_box = element.get_BoundingBox(active_view)
if bounding_box is None:
forms.alert("Element is not in the active view", title="Error")
sys.exit()
Hi and thanks for trying to help.
I went over these scripts and learned some new stuff.
However I am still not able to focus on the center of the element.
For now it only gets me near the element Im searching for and it also depended specifically on the angle im looking from at the project, meaning that the add-in “takes me” or my look at the project to a different place/different perspective each time.
maybe its something with the uiview and next() function which is new to me, or maybe the Transfrom…
I also tried to create an average point between the MIN and MAX - didnt work…
anyways, this is as far as Ive got for now:
# -*- coding: utf-8 -*-
__title__ = 'Go to Element By ID'
__doc__ = """
"""
#######################################
# IMPORTS
#######################################
import sys
from Autodesk.Revit.DB import *
from Autodesk.Revit.UI.Selection import *
# pyRevit
from pyrevit import forms, revit, DB
# .NET
import clr
clr.AddReference('System')
from System.Collections.Generic import List
List_example = List[ElementId]()
#######################################
# VARIABLES
#######################################
doc = __revit__.ActiveUIDocument.Document # type: Document
uidoc = __revit__.ActiveUIDocument # type: UIDocument
selection = uidoc.Selection # type: Selection
#######################################
# MAIN
#######################################
element_id_str = forms.ask_for_string(
default='639033',
prompt="Enter the Element ID:",
title='Go to Element By ID')
if not element_id_str:
forms.alert("No Element ID provided", title="Error")
sys.exit()
try:
element_id = DB.ElementId(int(element_id_str))
except ValueError:
forms.alert("Invalid Element ID format", title="Error")
sys.exit()
# Check if the element exists
element = doc.GetElement(element_id)
if not element:
forms.alert("Element not found in the document", title="Error")
sys.exit()
try:
element_transform = element.GetTotalTransform()
except Exception as e:
print("element {} does not have Transform class".format(element_id_str))
print(e)
sys.exit()
active_view = doc.ActiveView
uiviews = uidoc.GetOpenUIViews()
uiview = next((x for x in uiviews if x.ViewId == active_view.Id), None)
# Get the bounding box of the element
bbx = element.get_BoundingBox(active_view)
pt1 = XYZ(bbx.Min.X, bbx.Min.Y, bbx.Min.Z)
pt2 = XYZ(bbx.Max.X, bbx.Max.Y, bbx.Max.Z)
# pt_avg = XYZ((bbx.Min.X + bbx.Max.X)/2,(bbx.Min.X + bbx.Max.Y)/2,(bbx.Min.Z + bbx.Max.Z)/2)
if bbx is None:
print("Bounding box is None")
sys.exit()
try:
uiview.ZoomAndCenterRectangle(element_transform.OfPoint(pt2), element_transform.OfPoint(pt1))
selection.SetElementIds(List[ElementId]([element.Id]))
except Exception as e:
print("Failed to adjust the view: {}".forma
After digging a little more - I’ve succeed.
Thanks.
Just need to to replace these lines at the end of the script to these:
active_view = doc.ActiveView
uiviews = uidoc.GetOpenUIViews()
uiview = next((x for x in uiviews if x.ViewId == active_view.Id), None)
# Get the bounding box of the element
bbx = element.get_BoundingBox(active_view)
pt1 = XYZ(bbx.Min.X, bbx.Min.Y, bbx.Min.Z)
pt2 = XYZ(bbx.Max.X, bbx.Max.Y, bbx.Max.Z)
if bbx is None:
print("Bounding box is None")
sys.exit()
try:
uiview.ZoomAndCenterRectangle(pt2, pt1)
selection.SetElementIds(List[ElementId]([element.Id]))
except Exception as e:
print("Failed to adjust the view: {}".format(e))
Is the ‘Select by ID’ command under the Manage tab what you’re looking for, or is there something specific you’d like to do differently?
If your goal is to focus on a particular object, Revit’s API has a built-in method that might be exactly what you need, you can find more details here: ShowElements