我想插入一个元素并将其旋转90度,但在使用ElementTransformUtils.RotateElement时,我在获取ElementId时遇到了问题,我尝试了以下方法 https:/thebuildingcoder.typepad.comlog201006place-family-instance.html。 为了在插入元素之前设置一个事件处理程序,并通过GetAddedElementIds获取元素Id,但我不能让它为我工作 :(
在这方面,我将感谢任何帮助。
下面是我的代码,我启动了一个事务来放置家族(符号),我做了一个for循环来插入5次,我想把插入的元素旋转90度,我试过在for循环中使用RotateElement来旋转每个元素,但也许在1个指令中旋转所有插入的元素会更好?
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();
}
非常感谢。
--EDIT--
我本以为工作的代码会是下面的,但是,当我尝试执行它时,它却说:"Error: Error:Implementation of your work: "Error: Sequence contains no elements "Error: Sequence contains no elements"
代码。
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());
}
我将感激任何帮助,谢谢
有两种方法可以解决这个问题,而且都不需要使用DocumentChanged Event。 这其中的难点是要想好用什么来做旋转轴.第一种方法在创建后就使用RotateElement。
public void PlaceAndRotateFamilyMethod1()
{
Document doc = this.ActiveUIDocument.Document;
FamilySymbol symbol = GetFamilySymbolToPlace(doc);
using (Transaction trans = new Transaction(doc, "Place Family"))
{
trans.Start();
if (!symbol.IsActive)
{
symbol.Activate();
}
for (int x = 0; x < 5; x++)
{
FamilyInstance fi = doc.Create.NewFamilyInstance(new XYZ(x, 0, 0), symbol, Autodesk.Revit.DB.Structure.StructuralType.NonStructural);
XYZ point1 = new XYZ(0, 10, 0);
XYZ point2 = new XYZ(0, 10, 10);
Line axis = Line.CreateBound(point1, point2);
double angleToRotate = 45;
ElementTransformUtils.RotateElement(doc, fi.Id, axis, ConvertToRadians(angleToRotate));
}
trans.Commit();
}
}
第二种方法使用第二个事务来旋转元素,因为在这个方法中我使用了元素的BoundingBox来获得它的中心。 创建的事务必须先完成,才能得到它的BoundingBox。
public void PlaceAndRotateFamilyMethod2()
{
Document doc = this.ActiveUIDocument.Document;
FamilySymbol symbol = GetFamilySymbolToPlace(doc);
List<FamilyInstance> newFamInstToRotate = new List<FamilyInstance>();
using (Transaction trans = new Transaction(doc, "Place Family"))
{
trans.Start();
if (!symbol.IsActive)
{
symbol.Activate();
}
for (int x = 0; x < 5; x++)
{
FamilyInstance fi = doc.Create.NewFamilyInstance(new XYZ(x, 0, 0), symbol, Autodesk.Revit.DB.Structure.StructuralType.NonStructural);
newFamInstToRotate.Add(fi);
}
trans.Commit();
}
using (Transaction trans = new Transaction(doc, "Rotate Families"))
{
trans.Start();
foreach (FamilyInstance fi in newFamInstToRotate)
{
XYZ center = (fi.get_BoundingBox(doc.ActiveView).Max + fi.get_BoundingBox(doc.ActiveView).Min) * 0.5;
Line axis = Line.CreateBound(center, center + XYZ.BasisZ);
double angleToRotate = 45;
ElementTransformUtils.RotateElement(doc, fi.Id, axis, ConvertToRadians(angleToRotate));
}
trans.Commit();
}
}
助手方法。
private FamilySymbol GetFamilySymbolToPlace(Document doc)
{
FamilySymbol symbol = null;
foreach (FamilySymbol fSym in new FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_Furniture))
{
if (fSym.FamilyName == "Desk" && fSym.Name == "60\" x 30\"")
{
symbol = fSym;
break;
}
}
return symbol;
}
private double ConvertToRadians(double angle)
{
return (Math.PI / 180) * angle;
}