How to Add a Window to a Legend View Using Revit API?

How to Add a Window to a Legend View Using Revit API?

emaguilera
Contributor Contributor
234 Views
1 Reply
Message 1 of 2

How to Add a Window to a Legend View Using Revit API?

emaguilera
Contributor
Contributor

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!

 

0 Likes
235 Views
1 Reply
Reply (1)
Message 2 of 2

reylorente1
Collaborator
Collaborator
Primero debes de cambiar la construcción del NewFamilyInstance,ya que el que seleccionaste solo se cumple para 2D(lo puedes ver en el intelleset(,creo que se escribe asi),en el VS.
 
doc.Create.NewFamilyInstance(windowPosition, windowSymbol, legendView);
 
 
  doc.Create.NewFamilyInstance(windowPosition, windowSymbol, Autodesk.Revit.DB.Structure.StructuralType.NonStructural); 
 
después tiene que converstir a elementType
 
ElementType elementType = doc.GetElement(famInstance.GetTypeId()) as ElementType;
 
 
y dentro de 
UIDocument Class ,
 existe este metodo:
 
public void PromptToPlaceElementTypeOnLegendView(
ElementType elementType
).que esel que te va adicionar el elemento a la vista,
 
Algo asi:
 
 FamilyInstance famInstance;
 
 using (Transaction trans = new(doc, "Adicionar Componente"))
 {
     trans.Start();
 
     famInstance = doc.Create.NewFamilyInstance(windowPosition, windowSymbol, Autodesk.Revit.DB.Structure.StructuralType.NonStructural); ;
 
     trans.Commit();
 }
 
 ElementType elementType = doc.GetElement(famInstance.GetTypeId()) as ElementType;
 
 uidoc.PromptToPlaceElementTypeOnLegendView(elementType);
 
Si,la vista no esta activa,, la puedes llamar aasi:
//Activar la ventana
uidoc.ActiveView = Tu vista;
 
espero que te ayude.
 
First you must change the construction of the NewFamilyInstance, since the one you selected is only true for 2D (you can see it in the intelleset(, I think it is written like this), in the VS.
 
Doc. Create.NewFamilyInstance(windowPosition, windowSymbol, legendView);
 
to 
 
Doc. Create.NewFamilyInstance(windowPosition, windowSymbol, Autodesk.Revit.DB.Structure.StructuralType.NonStructural); 
 
then you have to convert to elementType
 
ElementType elementType = doc. GetElement(famInstance.GetTypeId()) as ElementType;
 
and within 
UIDocument Class ,
 There is this method:
 
public void PromptToPlaceElementTypeOnLegendView(
ElementType elementType
).which is the one that will add the element to your sight,
 
Kind of:
 
FamilyInstance famInstance;
 
using (Transaction trans = new(doc, "Adicionar Componente"))
 {
     trans. Start();
 
famInstance = doc. Create.NewFamilyInstance(windowPosition, windowSymbol, Autodesk.Revit.DB.Structure.StructuralType.NonStructural); ;
 
trans. Commit();
 }
 
0 Likes