Unable to rotate grids (three-point rotation)

Unable to rotate grids (three-point rotation)

dwightduronidavy
Enthusiast Enthusiast
156 Views
0 Replies
Message 1 of 1

Unable to rotate grids (three-point rotation)

dwightduronidavy
Enthusiast
Enthusiast

Dear all,

I have created an application macro that rotates the model file using three-point rotation.
All element categories rotate except for grids.

Any help would be very much appreciated.

MACRO:

using System;
using Autodesk.Revit.UI;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI.Selection;
using System.Collections.Generic;
using System.Linq;

namespace DDA_CSharpModule6
{
    [Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]
    [Autodesk.Revit.DB.Macros.AddInId("2B2E4EAB-59F7-492A-A125-BA0981A77D99")]
    public partial class ThisApplication
    {
        private void Module_Startup(object sender, EventArgs e)
        {
        }

        private void Module_Shutdown(object sender, EventArgs e)
        {
        }

        #region Revit Macros generated code

        private void InternalStartup()
        {
            this.Startup += new System.EventHandler(Module_Startup);
            this.Shutdown += new System.EventHandler(Module_Shutdown);
        }

        #endregion

        public void RotateModelWithThreePoints()
        {
            UIDocument uidoc = this.ActiveUIDocument;
            Document doc = uidoc.Document;

            try
            {
                // Prompt user to select the center of rotation
                XYZ centerPoint = uidoc.Selection.PickPoint("Select the center of rotation");

                // Prompt user to select the end point to define the direction
                XYZ endPoint = uidoc.Selection.PickPoint("Select the end point to define the initial direction");
                endPoint = new XYZ(endPoint.X, endPoint.Y, centerPoint.Z); // Ensure end point is on the horizontal plane

                // Prompt user for the final point
                XYZ finalPoint = uidoc.Selection.PickPoint("Select the final point to define the angle of rotation");
                finalPoint = new XYZ(finalPoint.X, finalPoint.Y, centerPoint.Z); // Ensure final point is on the horizontal plane

                // Calculate the angle between the end point and final point around the center point
                double rotationAngle = CalculateRotationAngle(centerPoint, endPoint, finalPoint);

                // Define the rotation axis (Z-axis through centerPoint)
                Line rotationAxis = Line.CreateBound(centerPoint, centerPoint + XYZ.BasisZ);

                // Collect specified elements, including new categories
                FilteredElementCollector collector = new FilteredElementCollector(doc)
                    .WhereElementIsNotElementType()
                    .OfCategory(BuiltInCategory.OST_Grids) // Grids
                    .UnionWith(new FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_Levels))
                    .UnionWith(new FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_Walls))
                    .UnionWith(new FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_Doors))
                    .UnionWith(new FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_SpecialityEquipment))
                    .UnionWith(new FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_GenericModel))
                    .UnionWith(new FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_Tags))
                    .UnionWith(new FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_Rooms))
                    .UnionWith(new FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_Floors))
                    .UnionWith(new FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_Roofs))
                    .UnionWith(new FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_CurtainWallPanels))
                    .UnionWith(new FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_CurtainWallMullions))
                    .UnionWith(new FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_CurtainGrids))
                    .UnionWith(new FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_VolumeOfInterest)) // Scope Boxes
                    .UnionWith(new FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_RoomSeparationLines)) // Room Separation Lines
                    .UnionWith(new FilteredElementCollector(doc).OfClass(typeof(ReferencePlane))); // Reference Planes

                IList<ElementId> elementIds = collector.ToElementIds() as IList<ElementId>;

                // Start a transaction to unpin elements and rotate the model
                using (Transaction transaction = new Transaction(doc, "Rotate Model with Three Points on Horizontal Plane"))
                {
                    transaction.Start();

                    // Unpin all pinned elements
                    foreach (ElementId id in elementIds)
                    {
                        Element element = doc.GetElement(id);
                        if (element != null && element.Pinned)
                        {
                            element.Pinned = false;
                        }
                    }

                    // Rotate all collected elements around the internal origin
                    ElementTransformUtils.RotateElements(doc, elementIds, rotationAxis, rotationAngle);

                    transaction.Commit();
                    TaskDialog.Show("Success""The model has been successfully rotated using the specified three points on the horizontal plane.");
                }
            }
            catch (OperationCanceledException)
            {
                TaskDialog.Show("Info""Operation cancelled by user.");
            }
            catch (Exception e)
            {
                TaskDialog.Show("Error"string.Format("An error occurred while rotating the model: {0}", e.Message));
            }
        }

        private double CalculateRotationAngle(XYZ center, XYZ start, XYZ end)
        {
            XYZ vector1 = new XYZ(start.X - center.X, start.Y - center.Y, 0);
            XYZ vector2 = new XYZ(end.X - center.X, end.Y - center.Y, 0);

            double dotProduct = vector1.X * vector2.X + vector1.Y * vector2.Y;
            double magnitude1 = Math.Sqrt(vector1.X * vector1.X + vector1.Y * vector1.Y);
            double magnitude2 = Math.Sqrt(vector2.X * vector2.X + vector2.Y * vector2.Y);

            double cosTheta = dotProduct / (magnitude1 * magnitude2);
            double angle = Math.Acos(cosTheta);

            // Determine the direction of rotation using cross product (Z component)
            double crossZ = vector1.X * vector2.Y - vector1.Y * vector2.X;
            if (crossZ < 0)
            {
                angle = -angle;
            }

            return angle;
        }
    }
}

0 Likes
157 Views
0 Replies
Replies (0)