Revit API 2012 provides a new utility class ElementTransformUtils which has some utilty methods that can be used to rotate, move, copy and mirror elements. In this article, let us see how to select and rotate an element using the ElementTransformUtils.RotateElement method.
Here is the code:
public void SelectAndRotateElementAlongZAxis()
{
Reference pickedObj = CachedUiApp.ActiveUIDocument.Selection.PickObject(ObjectType.Element, "Please select an element to rotate.");
if (pickedObj != null && pickedObj.ElementId != ElementId.InvalidElementId)
{
double rotation = 0;
CollectDataInput("Rotation Angle in Degree:", out rotation);
rotation *= Math.PI / 180.0;
using (Transaction trans = new Transaction(CachedDoc, "RotateElementAlongZAxis"))
{
trans.Start();
Line lineAsAxis = CachedApp.Create.NewLineBound(new XYZ(0, 0, 0), new XYZ(0, 0, 1));
ElementTransformUtils.RotateElement(CachedDoc, pickedObj.ElementId, 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 SelectAndRotateElementAlongZAxis method is placed into an external command created automatically by the RevitAddinWizard, we are ready to go. Inside Revit, if we have a wall element and some attachments such as a door and two windows like the following:
The command is triggered, the wall is selected, and the rotation angle 30 degree has been input in the Data Collector dialog:
The wall along with its attachments will be rotated along the Z axis:
Revit Addin Wizard (RevitAddinWizard) also provides a Selection Filter wizard which can help implement the ISelectionFilter interface automatically at will. In case the PickObject() method needs to filter some elements specifically such as walls only, the Selection Filter wizard can save a lot of time.
Recent Comments