I'm trying to insert an element and rotate it 90 degrees but I'm having problems to get the ElementId in order to use ElementTransformUtils.RotateElement, I've tried follow https://thebuildingcoder.typepad.com/blog/2010/06/place-family-instance.html in order to set an Event Handler before I insert the element and get Element Id through GetAddedElementIds but I can't make it work for me :(
I would appreciate any help in this regard.
Below is my code, I start a transaction to place the family (symbol) and I make a for loop to insert 5 times, I would like to rotate the elements inserted 90 degrees, I tried with the RotateElement inside the for loop to rotate each element as they are inserted, but maybe would be better to rotate all the inserted in just 1 instruction?
using (Transaction trans = new Transaction(doc, "Place Family"))
{
trans.Start();
if (!symbol.IsActive)
{
symbol.Activate();
}
for (int x = 0; x < 5; x++)
{
doc.Create.NewFamilyInstance(new XYZ(x, 0, 0), symbol, Autodesk.Revit.DB.Structure.StructuralType.NonStructural);
}
trans.Commit();
}
Thanks very much.
--EDIT--
I did suppose that the code to work would be the below, however, when I try to execute it it states: "Error: Sequence contains no elements"
code:
if (!symbol4.IsActive)
{
symbol4.Activate();
}
for (int x = 0; x < 4; x++)
{
double xloc = x * BayLength / 304.8;
_added_element_ids.Clear();
app.DocumentChanged += new EventHandler<DocumentChangedEventArgs>(OnDocumentChanged);
doc.Create.NewFamilyInstance(new XYZ(xloc, 0, 1.021), symbol4, Autodesk.Revit.DB.Structure.StructuralType.NonStructural);
app.DocumentChanged -= new EventHandler<DocumentChangedEventArgs>(OnDocumentChanged);
ElementId LastModifiedElementId = _added_element_ids.Last();
XYZ p1= new XYZ(xloc, 0.0, 1.021);
XYZ p2 = new XYZ(xloc, 0.0, 2.021);
Line Axis = Line.CreateBound(p1, p2);
double angle = -90 * Math.PI / 180;
ElementTransformUtils.RotateElement(doc, LastModifiedElementId, Axis, angle);
}
void OnDocumentChanged(object sender, DocumentChangedEventArgs e)
{
_added_element_ids.AddRange(e.GetAddedElementIds());
}
I would appreciate any help, thanks