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 Copy an element using the ElementTransformUtils.CopyElement method.
Here is the code:
#region Select And Copy Element
public void SelectAndCopyElement()
{
Reference pickedObj = CachedUiApp.ActiveUIDocument.Selection.PickObject(ObjectType.Element, "Please select an element to copy.");
if (pickedObj != null && pickedObj.ElementId != ElementId.InvalidElementId)
{
string translatin = string.Empty;
CollectDataInput2("Translation in the format X,Y,Z", out translatin);
string[] arr = translatin.Split(new char[] { ',' });
using (Transaction trans = new Transaction(CachedDoc, "SelectAndCopyElement"))
{
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.CopyElement(CachedDoc, pickedObj.ElementId, xyz);
trans.Commit();
}
}
}
public static bool CollectDataInput2(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();
}
#endregion
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 SelectAndCopyElement 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 above wall is selected (the bottom one is a reference for the coming movement), and the translation 0,5,0 has been input in the Data Collector dialog:
The wall along with its 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 PickObject() method needs to filter some elements specifically such as walls only, the Selection Filter wizard can save a lot of time.
Recent Comments