Revit API 2012 provides a new utility class ElementTransformUtils which has some utilty methods that can be used to rotate, move, copy and mirror a single element or multiple elements. In this article, let us see how to select and rotate a few elements using the ElementTransformUtils.RotateElements method.
Here is the code:
public void SelectAndRotateElementsAlongZAxis()
{
IList<Reference> pickedObjs = CachedUiApp.ActiveUIDocument.Selection.PickObjects(ObjectType.Element, "Please select some elements to rotate.");
List<ElementId> ids = (from Reference r in pickedObjs
select r.ElementId).ToList();
if (pickedObjs != null && pickedObjs.Count > 0)
{
double rotation = 0;
CollectDataInput("Rotation Angle in Degree:", out rotation);
rotation *= Math.PI / 180.0;
using (Transaction trans = new Transaction(CachedDoc, "RotateElementsAlongZAxis"))
{
trans.Start();
Line lineAsAxis = CachedApp.Create.NewLineBound(new XYZ(0, 0, 0), new XYZ(0, 0, 1));
ElementTransformUtils.RotateElements(CachedDoc, ids, lineAsAxis, rotation);
trans.Commit();
}
}
}
public static bool CollectDataInput(string title, out double ret)
{
System.Windows.Forms.Form dc = new System.Windows.Forms.Form();
dc.Text = title;
dc.HelpButton = dc.MinimizeBox = dc.MaximizeBox = false;
dc.ShowIcon = dc.ShowInTaskbar = false;
dc.TopMost = true;
dc.Height = 100;
dc.Width = 300;
dc.MinimumSize = new System.Drawing.Size(dc.Width, dc.Height);
int margin = 5;
System.Drawing.Size size = dc.ClientSize;
System.Windows.Forms.TextBox tb = new System.Windows.Forms.TextBox();
tb.TextAlign = HorizontalAlignment.Right;
tb.Height = 20;
tb.Width = size.Width - 2 * margin;
tb.Location = new System.Drawing.Point(margin, margin);
tb.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
dc.Controls.Add(tb);
System.Windows.Forms.Button ok = new System.Windows.Forms.Button();
ok.Text = "Ok";
ok.Click += new EventHandler(ok_Click);
ok.Height = 23;
ok.Width = 75;
ok.Location = new System.Drawing.Point(size.Width / 2 - ok.Width / 2, size.Height / 2);
ok.Anchor = AnchorStyles.Bottom;
dc.Controls.Add(ok);
dc.AcceptButton = ok;
dc.ShowDialog();
return double.TryParse(tb.Text, out ret);
}
private static void ok_Click(object sender, EventArgs e)
{
System.Windows.Forms.Form form = (sender as System.Windows.Forms.Control).Parent as System.Windows.Forms.Form;
form.DialogResult = DialogResult.OK;
form.Close();
}
By the way, the CollectDataInput method along with the dynamic form creation code was created automatically by the Data Collector of the Revit Addin Wizard.
If the SelectAndRotateElementsAlongZAxis method is placed into an external command created automatically by the RevitAddinWizard, we are ready to go.
Inside Revit, if we have some wall elements and some attachments such as a door and two windows like the following:
The command is triggered, the two walls are selected, and the rotation angle 30 degree has been input in the Data Collector dialog:
The two walls along with their attachments will be rotated along the Z axis for 30 degree:
Revit Addin Wizard (RevitAddinWizard) also provides a Selection Filter wizard which can help implement the ISelectionFilter interface automatically at will. In case the PickObjects() method needs to filter some elements specifically such as walls only, the Selection Filter wizard can save a lot of time.
Recent Comments