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 move a few elements using the ElementTransformUtils.MoveElements method.
Here is the code:
public void SelectAndMoveElements()
{
IList<Reference> pickedObjs = CachedUiApp.ActiveUIDocument.Selection.PickObjects(ObjectType.Element, "Please select some elements to move.");
List<ElementId> ids = (from Reference r in pickedObjs
select r.ElementId).ToList();
if (pickedObjs != null && pickedObjs.Count > 0)
{
string translatin = string.Empty;
CollectDataInput1("Translation in the format X,Y,Z", out translatin);
string[] arr = translatin.Split(new char[] { ',' });
using (Transaction trans = new Transaction(CachedDoc, "SelectAndMoveElements"))
{
trans.Start();
Line lineAsAxis = CachedApp.Create.NewLineBound(new XYZ(0, 0, 0), new XYZ(0, 0, 1));
XYZ xyz = new XYZ(double.Parse(arr[0]), double.Parse(arr[1]), double.Parse(arr[2]));
ElementTransformUtils.MoveElements(CachedDoc, ids, xyz);
trans.Commit();
}
}
}
public static bool CollectDataInput1(string title, out string 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();
ret = tb.Text;
return true;
}
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 CollectDataInput1 method along with the dynamic form creation code was created automatically by the Data Collector of the Revit Addin Wizard.
If the SelectAndMoveElements method is placed into an external command created automatically by the RevitAddinWizard, we are ready to go.
Inside Revit, if we have two walls with some attachments such as a door and windows like the following:
The command is triggered, the above walls are selected, and the translation 0,5,0 has been input in the Data Collector dialog:
The two walls along with their attachments will be copied to the new location:
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