Autodesk.Revit.DB.ReferenceWithContext类是Revit API提供的一个类,用于表示被选中构件并提供上下文环境,以进行与此构件相关的操作。
public void AddWallPaint(Autodesk.Revit.UI.UIDocument uidoc)
{
Autodesk.Revit.DB.ReferenceWithContext rwc = uidoc.Selection.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Element, "Pick a wall");
if (rwc != null)
{
Autodesk.Revit.DB.Element elem = uidoc.Document.GetElement(rwc.OptionalContext.Id);
if (elem != null && elem is Wall)
{
XYZ wallMidpoint = ((Wall)elem).Location.PointAtParameter(0.5);
XYZ paintLocation = wallMidpoint - new XYZ(0, 0, 1); //paint 1 foot below wall midpoint
Autodesk.Revit.DB.Reference paintRef = rwc.Reference; //use the selected wall's Reference
using (Transaction trans = new Transaction(uidoc.Document, "Paint Wall"))
{
trans.Start();
uidoc.Document.Create.NewPaint(paintLocation, paintRef, "Red");
trans.Commit();
}
}
}
}
在上面的示例中,用户选中一堵墙,并将该墙传递给AddWallPaint方法中的ReferenceWithContext对象。在此墙的上下文环境中,该方法创建了一个新的油漆标记,其参照是选定的墙Reference。