Revit Ironpython shell code - Orient to face and active selection Box

Revit Ironpython shell code - Orient to face and active selection Box

Khashayar.Saffarifardkhouzani
Explorer Explorer
691 Views
5 Replies
Message 1 of 6

Revit Ironpython shell code - Orient to face and active selection Box

Khashayar.Saffarifardkhouzani
Explorer
Explorer

Any one can help me how to activate selection box of associated item when I click on its face and also make the target view 100mm in 3D view .

below is the orient to face code :

 

from pyrevit import HOST_APP
from pyrevit import revit, DB, UI
from pyrevit import forms


def reorient():
    face = revit.pick_face()

    if face:
        with revit.Transaction('Orient to Selected Face'😞
            # calculate normal
            if HOST_APP.is_newer_than(2015😞
                normal_vec = face.ComputeNormal(DB.UV(0, 0))
            else:
                normal_vec = face.Normal

            # create base plane for sketchplane
            if HOST_APP.is_newer_than(2016😞
                base_plane = \
                    DB.Plane.CreateByNormalAndOrigin(normal_vec, face.Origin)
            else:
                base_plane = DB.Plane(normal_vec, face.Origin)

            # now that we have the base_plane and normal_vec
            # let's create the sketchplane
            sp = DB.SketchPlane.Create(revit.doc, base_plane)

            # orient the 3D view looking at the sketchplane
            revit.active_view.OrientTo(normal_vec.Negate())
            # set the sketchplane to active
            revit.uidoc.ActiveView.SketchPlane = sp

        revit.uidoc.RefreshActiveView()


curview = revit.active_view

if isinstance(curview, DB.View3D):
    reorient()
else:
    forms.alert('You must be on a 3D view for this tool to work.')
0 Likes
692 Views
5 Replies
Replies (5)
Message 2 of 6

Mohamed_Arshad
Advisor
Advisor

HI @Khashayar.Saffarifardkhouzani 
Can you please confirm, you need to orient the face and Activate the section Box of that Element ?


Mohamed Arshad K
Software Developer (CAD & BIM)

0 Likes
Message 3 of 6

Yes. I need after clicking on face, 3D view orient toward that face, and then activate selection box.( I don't want to see any item rather one I clicked on its face. maybe eye view distance and far clipping distance to be around 100mm  is  an good idea as well to add to the code .)

0 Likes
Message 4 of 6

I know C#, Can proceed with C# So you can convert that into Python


Mohamed Arshad K
Software Developer (CAD & BIM)

0 Likes
Message 5 of 6

khashayar.saffari
Observer
Observer

Sounds good idea. I can get help from Chat GPT to convert that to Python.

0 Likes
Message 6 of 6

Mohamed_Arshad
Advisor
Advisor

HI @Khashayar.Saffarifardkhouzani 
Kindly refer to the below code and covert to Python

 

Orient To Face

           //Revit Database Access
            UIDocument uiDoc = commandData.Application.ActiveUIDocument;
            Document doc = uiDoc.Document;

            //Get 3D View
            View3D view = doc.ActiveView as View3D;

            //Face Selection 
            Reference selRef = uiDoc.Selection.PickObject(ObjectType.Face);

            //Convert Reference to Planar Face
            Element e = doc.GetElement(selRef);
            PlanarFace pFace = e.GetGeometryObjectFromReference(selRef) as PlanarFace;

            //Forward Direction
            XYZ forwardDirection = pFace.FaceNormal.Negate();

            //Orient To Face
            view.OrientTo(forwardDirection);

            //Document Regen and Refresh
            uiDoc.RefreshActiveView();


Activate Section Box

  BoundingBoxXYZ elementBoundingBox = e.get_BoundingBox(view);

            XYZ center = elementBoundingBox.Min
                .Add(elementBoundingBox.Max)
                .Multiply(0.5);

            XYZ vector = elementBoundingBox.Max.Subtract(center).Normalize();

            XYZ maxOffsetDir = vector
                .Multiply(UnitUtils
                .ConvertToInternalUnits(1000, UnitTypeId.Millimeters));

            XYZ minOffserDir = vector
                .Negate()
                .Multiply(UnitUtils
                .ConvertToInternalUnits(1000, UnitTypeId.Millimeters));

            //Extend Bounding Box to 1000mm
            XYZ minExtend = elementBoundingBox.Min.Add(minOffserDir);
            XYZ maxExtend = elementBoundingBox.Max.Add(maxOffsetDir);

            //New Bounding Box Creation
            BoundingBoxXYZ newBox = new BoundingBoxXYZ();
            newBox.Min = minExtend;
            newBox.Max = maxExtend;

            using (Transaction enableSectionBox = new Transaction(doc, "Enable Section Box"))
            {
                enableSectionBox.Start();

                view.SetSectionBox(newBox);

                view.get_Parameter(BuiltInParameter.VIEWER_MODEL_CLIP_BOX_ACTIVE).Set(1);

                enableSectionBox.Commit();
            }

Kindly understand the code and convert to python.

Hope this will help 🙂


 


Mohamed Arshad K
Software Developer (CAD & BIM)