# Get Bounding Box of Titleblock with Python

**URL:** <https://forum.dynamobim.com/t/get-bounding-box-of-titleblock-with-python/104648>\
**Category:** Revit\
**Tags:** python, dynamo\
**Created:** [September 25, 2024, 5:38pm UTC](https://forum.dynamobim.com/t/get-bounding-box-of-titleblock-with-python/104648 "2024-09-25T17:38:00Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![tom123](https://avatars.discourse-cdn.com/v4/letter/t/d78d45/32.png) [@tom123](https://forum.dynamobim.com/u/tom123)\
**Post date:** [September 25, 2024, 5:38pm UTC](https://forum.dynamobim.com/t/get-bounding-box-of-titleblock-with-python/104648/1 "2024-09-25T17:38:00Z")

</div>

Hi All,

I am trying to get the bounding box of a titleblock instance that I have selected with the Select Model Element node. The OOTB node Element.BoundingBox can get the bounding box as shown below, but I can’t get the correct python equivalent. I used ChatGPT for the python code. The python code can get the bounding box of say a duct when I test it but it outputs null when trying to get a titleblock’s bounding box. I want to be able to make this into a [pyRevit](https://discourse.pyrevitlabs.io/) script which is why I want to write this in python and not just use the OOTB node. Any help on the python code is appreciated.

 ![TitleBlockBoundingBox](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/7/2/72bc18026ab293e3e0a1f214351a1da57d0d6c83.png)  
 ![PythonBoundingBoxAttempt](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/c/7/c7f7de3964ce4caec0854506568b4670b83e6626.png)  
Here is the current python code, thanks to chatGPT.

> Blockquote

> **Code**
>
> # Import necessary Revit API and Dynamo services
> 
> from RevitServices.Persistence import DocumentManager  
> from Autodesk.Revit.DB import BoundingBoxXYZ
> 
> # Get the active document
> 
> doc = DocumentManager.Instance.CurrentDBDocument
> 
> # Input: the element from Dynamo
> 
> element = UnwrapElement(IN[0]) # Unwrap the input to get the Revit element
> 
> # Initialize the bounding box variable
> 
> bounding\_box = None
> 
> # Check if the element is valid and get its bounding box
> 
> if element is not None:  
> bounding\_box = element.get\_BoundingBox(None) # None gets the bounding box in the active view
> 
> # Prepare the output
> 
> if bounding\_box:  
> output = [  
> bounding\_box.Min.X, bounding\_box.Min.Y, bounding\_box.Min.Z, # Minimum point  
> bounding\_box.Max.X, bounding\_box.Max.Y, bounding\_box.Max.Z # Maximum point  
> ]  
> else:  
> output = None # Return None if no bounding box is found
> 
> # Output to Dynamo
> 
> OUT = output

---

<div class="post-metadata">

**Author:** ![Nick\_Boyts](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/nick_boyts/32/76313_2.png) [@Nick\_Boyts](https://forum.dynamobim.com/u/Nick_Boyts)\
**Post date:** [September 25, 2024, 5:50pm UTC](https://forum.dynamobim.com/t/get-bounding-box-of-titleblock-with-python/104648/2 "2024-09-25T17:50:16Z")

</div>

Have you confirmed _where_ the code is failing? You have all these checks for missing data but that means you don’t get any exceptions or errors to tell you what’s happening. The first step in troubleshooting this would be to find out where you’re actually getting a null.

If you do that, you should see that everything “works” but your element is returning no bounding box. That’s because you provided no view. The `None` option is specifically for model elements that aren’t bound by a view. A titleblock is view specific and doesn’t have any model geometry. Instead of providing a `None` input, give it the active view (`doc.ActiveView`) if you have the sheet open or get the sheet view and provide that if the active view is unknown.

Also, make sure you’re posting formatted code so we know you’re not having formatting issues and other users can easily copy and reuse your code.

---

<div class="post-metadata">

**Author:** ![tom123](https://avatars.discourse-cdn.com/v4/letter/t/d78d45/32.png) [@tom123](https://forum.dynamobim.com/u/tom123)\
**Post date:** [September 25, 2024, 6:21pm UTC](https://forum.dynamobim.com/t/get-bounding-box-of-titleblock-with-python/104648/3 "2024-09-25T18:21:05Z")

</div>

Thanks Nick, chatGPT likes to gloss over those errors sometimes and iterating with chatGPT can get circular/frustrating haha.

That was the solution, I needed to get the current active view of the selected titleblock. Here is the final code that outputs the bounding box min and max points. Which is good enough for the rest of the main script I’m working on.

```auto
# Import necessary Revit API and Dynamo services
import clr
clr.AddReference('RevitAPI')
clr.AddReference('RevitServices')
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager
from Autodesk.Revit.DB import View, BoundingBoxXYZ

# Get the active document and active view
doc = DocumentManager.Instance.CurrentDBDocument
active_view = doc.ActiveView # Get the active view

# Input: the element from Dynamo
element = UnwrapElement(IN[0]) # Unwrap the input to get the Revit element

# Initialize the output for detailed error messages
output = []

try:
    # Check if the element is valid
    if element is None:
        raise ValueError("The input element is None.")
    
    # Check if the active view is valid
    if active_view is None:
        raise ValueError("The active view is not set.")

    # Try to get the bounding box in the active view
    bounding_box = element.get_BoundingBox(active_view)
    
    # If bounding box is found, prepare output
    if bounding_box is None:
        raise Exception(f"Bounding box not found for element ID: {element.Id} in view ID: {active_view.Id}")
    
    output = [
        bounding_box.Min.X, bounding_box.Min.Y, bounding_box.Min.Z, # Minimum point
        bounding_box.Max.X, bounding_box.Max.Y, bounding_box.Max.Z # Maximum point
    ]

except Exception as e:
    # Append the error message to the output list for detailed feedback
    output = f"Error: {str(e)}"

# Output to Dynamo
OUT = output

```

---

<div class="post-metadata">

**Author:** ![Nick\_Boyts](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/nick_boyts/32/76313_2.png) [@Nick\_Boyts](https://forum.dynamobim.com/u/Nick_Boyts)\
**Post date:** [September 25, 2024, 6:32pm UTC](https://forum.dynamobim.com/t/get-bounding-box-of-titleblock-with-python/104648/4 "2024-09-25T18:32:32Z")

</div>

ChatGPT is great for a lot of things, but it can make Dynamo and Revit integration a lot harder if you’re not careful. Use it to generate a generic process, but _always_ double check the methods it uses. It’s usually really good with python, but it will make up methods for Revit or Dynamo all the time - and they’ll be convincing.
