如何在 Revit 文档中覆盖/替换 FamilySymbol

问题描述 投票:0回答:0

我们正在开发一个项目,其中使用 Autodesk 的 Forge(或平台服务),更具体地说,是设计自动化,我们在云中的文件上运行脚本。这意味着 Revit.UI 不可用。

话虽如此,我们找到了一种方法来加载 Revit Family 文件及其“类型目录”,而且效果很好。但是,下一步是使用我们使用

document.LoadFamily()
函数加载的族来更新放置在打开的文档中的默认族。

我们尝试使用以下代码:

private static int LoadAndUpdateFamily(Document document)
    {
        var count = 0;

        var familyFullPath = (path);

        var docFamilyManager = document.FamilyManager;

        FilteredElementCollector windows = new FilteredElementCollector(document);
        windows.OfClass(typeof(FamilySymbol))
            .OfCategory(BuiltInCategory.OST_GenericModel);

        IList<Element> windowTypes = windows.OfCategory(BuiltInCategory.OST_GenericModel).WhereElementIsElementType().ToElements();

        for (int i = 0; i < windowTypes.Count; i++)
        {
            Console.WriteLine("Window types name = " + windowTypes[i].Name);
        }
        
        // this is where we get the default FS which needs to be updated
        FamilySymbol defaultFamSymbol = windowTypes.ElementAt(0) as FamilySymbol;

        Console.WriteLine("defaultFamSymbol NAME = " + defaultFamSymbol.Name);

        defaultFamSymbol.Activate();


        using (Transaction transaction = new Transaction(document))
        {
            transaction.Start("Load Family");

            if (document.LoadFamily(familyFullPath, out Family family))
            {
                var symbolIds = family.GetFamilySymbolIds();
                count = symbolIds.Count;
                Console.WriteLine($"{family.Name}: {symbolIds.Count}");

                var symbols = symbolIds.Select(document.GetElement).OfType<FamilySymbol>();

                foreach (var symbol in symbols)
                {
                    Console.WriteLine($"[{symbol.Id}]: {symbol.Name}");
                    symbol.Activate();
                    // --- we try to update the default family symbol with the new one here
                    defaultFamSymbol = symbol;
                    
                    // --- we print the defaulFamSymbol's name here and it has the new symbol's name, however in the final file nothing is changed. 
                    Console.WriteLine("defaultFamSymbol NAME = " + defaultFamSymbol.Name);

                    document.Regenerate();
                }

            } // end of Load family

            transaction.Commit();
        }

        return count;
    } // -- end of LoadFamilyCount function

该函数的整体用法是:

            var documentTest = data.RevitApp.OpenDocumentFile(path);
            Document docT = documentTest;
            
            var loads = LoadAndUpdateFamily(docT);
            Console.WriteLine("Loads count = " + loads );
            Console.WriteLine("Loads size = " + docT.FamilyManager.Types.Size);


            ModelPath pathT = ModelPathUtils.ConvertUserVisiblePathToModelPath("result.rfa");
            docT.SaveAs(pathT, new SaveAsOptions());

有人知道这种方法有什么问题吗?看起来原来的家庭总是保持不变,我们找不到一种方法来更新它与加载的家庭..

c# autodesk-forge revit-api autodesk-designautomation
© www.soinside.com 2019 - 2024. All rights reserved.