need help finding bounding box for selected section view

need help finding bounding box for selected section view

rajadeva93
Explorer Explorer
512 Views
3 Replies
Message 1 of 4

need help finding bounding box for selected section view

rajadeva93
Explorer
Explorer

Here is the code i have using. 
even when i select the section view element it's not passing through following if statement. it

"

if ( element is View)
{

view = element as View;
}

"

i always get the else statement error message

can someone help me 

full code below 

 

using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using System;

namespace RotateElements
{
    [Transaction(TransactionMode.Manual)]
    public class RotateElementCommand : IExternalCommand
    {
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            UIDocument uidoc = commandData.Application.ActiveUIDocument;
            Document doc = uidoc.Document;

            // Get the currently selected view
            View view = null;
            try
            {
                Reference reference = uidoc.Selection.PickObject(ObjectType.Element, "Select a view");
                Element element = doc.GetElement(reference);
                if ( element is View)
                {

                    view = element as View;
                }

                else
                {
                    TaskDialog.Show("Error", "Selected element is not a section view");
                }

            }
            catch (Autodesk.Revit.Exceptions.OperationCanceledException)
            {
                // User canceled selection
                return Result.Cancelled;
            }

            if (view != null)
            {
                // Check if the selected view has a valid crop box
                BoundingBoxXYZ boundingBox = view.CropBox;
                if (boundingBox != null)
                {
                    // Process the bounding box
                    XYZ maxPoint = boundingBox.Max;
                    XYZ minPoint = boundingBox.Min;

                    // Print or use the bounding box coordinates as required
                    TaskDialog.Show("Bounding Box", $"Selected View: {view.Name}\nMax Point: {maxPoint}\nMin Point: {minPoint}");
                }
                else
                {
                    TaskDialog.Show("Bounding Box", "Selected view doesn't have a valid crop box.");
                }
            }
            else
            {
                TaskDialog.Show("Bounding Box", "No view selected or selected element is not a view.");
            }

            return Result.Succeeded;
        }
    }
}

 

 

0 Likes
513 Views
3 Replies
Replies (3)
Message 2 of 4

jeremy_tammik
Alumni
Alumni

You could use a selection filter to ensure that the object selected by the user is indeed a View, and nothing but a view:

  

  

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

ctm_mka
Collaborator
Collaborator

A few things, im surprised you are getting the else statement, as doc.GetElement (line 22) wants and ElementId, which you are not feeding it. Try changing "doc.GetElement(reference)" to "doc.GetElement(reference.Id)". Second, "View" is a very broad category, as Jeremy suggested, filtering may help, perhaps by Family, which would return, Detail View, Section View, etc., which i believe you are looking for. Lastly, there are References views to account for, which are also of the View category.

 

0 Likes
Message 4 of 4

mhannonQ65N2
Collaborator
Collaborator

Document.GetElement has an overload that takes a Reference.

0 Likes