Revit API Forum
Welcome to Autodesk’s Revit API Forums. Share your knowledge, ask questions, and explore popular Revit API topics.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Rotating Elements on their axis

2 REPLIES 2
SOLVED
Reply
Message 1 of 3
recepagah12
4251 Views, 2 Replies

Rotating Elements on their axis

I am trying to create an add-in. Rotate multiple elements with their own axis. 

But element rotates angle increasing within foreach loop. I even tried constant value by ignoring user input.

The rotation angle increasing by applied elements.

double  RotateAngle;

            using (Rotating_Form thisForm = new Rotating_Form())
            {
                // Determine the position of form
                thisForm.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
                // Show the form 
                thisForm.ShowDialog();

                // Convert Radian to Degree
                RotateAngle = thisForm.Get_Angle()*Math.PI/180;

                if (thisForm.DialogResult == System.Windows.Forms.DialogResult.Cancel)
                {
                    return Result.Cancelled;
                }
                using (Transaction t = new Transaction(doc))
                {
                    //Provide access to make change in document
                    t.Start("Rotate Element");
                    // Collect selected elements
                    ElementSet selection = new ElementSet();
                    //  Select element references
                    ICollection<ElementId> selectedIds = uidoc.Selection.GetElementIds();

                    foreach (ElementId elementId in selectedIds)
                    {
                        selection.Insert(uidoc.Document.GetElement(elementId));
                        foreach (Element e in selection)
                        {
                            //Get the location curve
                            Location location = e.Location;
                            LocationCurve locationCurve = location as LocationCurve;
                            Curve curve = locationCurve.Curve;
// Get the end point of curve XYZ point1 = curve.GetEndPoint(0); XYZ point2 = curve.GetEndPoint(1); // Get the mid point of curve XYZ midPoint = (point1 + point2) / 2; XYZ midHigh = midPoint.Add(XYZ.BasisZ); Line axisLine = Line.CreateBound(midPoint, midHigh); //Rotate elements on their axis bool rotated = locationCurve.Rotate(axisLine, RotateAngle); rotated = true; } } // Nothings selected if (selection.IsEmpty) { message = "Please select at least one element"; return Result.Failed; } t.Commit(); } }

 

2 REPLIES 2
Message 2 of 3
BardiaJahan
in reply to: recepagah12

I think the inner Foreach loop is redundant. For each selected element, you would need to retrieve the location curve and rotate it - the is only one Foreach. Your current snippet keeps adding elements to ElementSet selection, which means the first element is rotated once, the second element is rotated twice, etc.

Try:

 

 

foreach (ElementId elementId in selectedIds)
                {
                    Element e = doc.GetElement(elementId);

                    //Get the location curve
                    Location location = e.Location;
                    LocationCurve locationCurve = location as LocationCurve;
                    Curve curve = locationCurve.Curve;

                    // Get the end point of curve
                    XYZ point1 = curve.GetEndPoint(0);
                    XYZ point2 = curve.GetEndPoint(1);
                    // Get the mid point of curve
                    XYZ midPoint = (point1 + point2) / 2;
                    XYZ midHigh = midPoint.Add(XYZ.BasisZ);
                    Line axisLine = Line.CreateBound(midPoint, midHigh);

                    //Rotate elements on their axis
                    bool rotated = locationCurve.Rotate(axisLine, RotateAngle);
                    rotated = true;
                }
Message 3 of 3
recepagah12
in reply to: BardiaJahan

Thank you, I am appreciated to your help. 

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Rail Community