How to Add a Window to a Legend View Using Revit API?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hi everyone,
I'm working on a Revit add-in where I need to add a window family instance to a legend view. I have the following code, which successfully finds the legend view and the window family symbol. However, when I try to place the window in the legend, I encounter the following error:
Family cannot be placed as hosted on an input face reference, because its FamilyPlacementType is not ViewBased
Parameter name: symbol
Here is my current implementation:
[Transaction(TransactionMode.Manual)]
public class CreateLegendCommand : IExternalCommand
{
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
UIApplication uiApp = commandData.Application;
Document doc = uiApp.ActiveUIDocument.Document;
try
{
using (Transaction trans = new Transaction(doc, "Modificar Leyenda"))
{
trans.Start();
View legendView = new FilteredElementCollector(doc)
.OfClass(typeof(View))
.Cast<View>()
.FirstOrDefault(v => v.ViewType == ViewType.Legend);
if (legendView == null)
{
message = "No se encontró una vista de leyenda en el documento.";
return Result.Failed;
}
AddTextToLegend(doc, legendView);
AddWindowToLegend(doc, legendView);
trans.Commit();
}
return Result.Succeeded;
}
catch (System.Exception ex)
{
message = ex.Message;
return Result.Failed;
}
}
private void AddWindowToLegend(Document doc, View legendView)
{
FamilySymbol windowSymbol = new FilteredElementCollector(doc)
.OfClass(typeof(FamilySymbol))
.Cast<FamilySymbol>()
.FirstOrDefault(s => s.Family.Name == "M_Fixed" && s.Name == "0406 x 0610mm");
if (windowSymbol == null)
{
throw new System.Exception("No se encontró el símbolo de ventana 'M_Fixed' con las dimensiones 0406 x 0610mm en el documento.");
}
if (!windowSymbol.IsActive)
{
windowSymbol.Activate();
}
XYZ windowPosition = new XYZ(0, 0, 0); // Cambia esta posición según lo necesites
doc.Create.NewFamilyInstance(windowPosition, windowSymbol, legendView);
}
I believe this error occurs because the family placement type is not compatible with the legend view. Is there a way to place family instances in a legend view, or is this operation not supported by the Revit API?
Any guidance, suggestions, or workarounds would be greatly appreciated. Thank you!