Actually, i am duplicating Python code into C#. It works but the view always inverting the 3d views.
here is the pyRevit Python 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.')
and here my C#
using System;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
namespace MyPlugin
{
[Transaction(TransactionMode.Manual)]
public class OrientView : IExternalCommand
{
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
UIDocument uiDoc = commandData.Application.ActiveUIDocument;
Document doc = uiDoc.Document;
// Ensure the active view is a 3D view
View3D currentView = uiDoc.ActiveView as View3D;
if (currentView == null)
{
TaskDialog.Show("Error", "You must be in a 3D view to use this tool.");
return Result.Failed;
}
try
{
// Prompt the user to select a face
Reference faceReference = uiDoc.Selection.PickObject(ObjectType.Face, "Select a face to orient the view");
if (faceReference == null) return Result.Cancelled;
// Get the selected face
GeometryObject geometryObject = doc.GetElement(faceReference).GetGeometryObjectFromReference(faceReference);
PlanarFace selectedFace = geometryObject as PlanarFace;
if (selectedFace == null)
{
TaskDialog.Show("Error", "The selected face is not planar.");
return Result.Failed;
}
// Compute the normal vector and create a plane
XYZ faceNormal = selectedFace.ComputeNormal(new UV(0, 0));
XYZ faceOrigin = selectedFace.Origin;
Plane basePlane = Plane.CreateByNormalAndOrigin(faceNormal, faceOrigin);
using (Transaction trans = new Transaction(doc, "Orient to Selected Face"))
{
trans.Start();
// Create a SketchPlane
SketchPlane sketchPlane = SketchPlane.Create(doc, basePlane);
// Orient the view to the face
OrientViewToNormal(currentView, faceNormal, faceOrigin);
// Set the SketchPlane to the active view
currentView.SketchPlane = sketchPlane;
trans.Commit();
}
// Refresh the view
uiDoc.RefreshActiveView();
return Result.Succeeded;
}
catch (OperationCanceledException)
{
return Result.Cancelled;
}
catch (Exception ex)
{
message = ex.Message;
return Result.Failed;
}
}
/// <summary>
/// Orients the active 3D view to the given normal vector and origin.
/// </summary>
private void OrientViewToNormal(View3D view, XYZ normal, XYZ origin)
{
// Reverse the normal to align with the face
XYZ viewDirection = normal.Negate();
// Calculate a valid up direction for the view
XYZ upDirection = viewDirection.CrossProduct(XYZ.BasisX);
if (upDirection.IsZeroLength())
{
upDirection = viewDirection.CrossProduct(XYZ.BasisY);
}
// Set the view orientation
ViewOrientation3D orientation = new ViewOrientation3D(origin, upDirection.Normalize(), viewDirection.Normalize());
view.SetOrientation(orientation);
}
}
}
any help is much appreciated.