1. 程式人生 > 其它 >Revit二次開發---專案檔案轉族檔案

Revit二次開發---專案檔案轉族檔案

技術標籤:Revit二次開發c#

將專案檔案(.rvt)轉換成族檔案(.rfa), 專案中的系統族用這種方式轉不了

1. 獲取到專案中例項

2.遍歷例項記錄屬性/座標/中心線等,並且將例項儲存成.rfa格式檔案

3. 載入一個空的族檔案,並且把剛剛匯出的例項全部載入到空的族檔案中設定其屬性/座標等

4. 將族檔案儲存

//獲取例項
FilteredElementCollector fec = new FilteredElementCollector(doc).ofClass(typeof(FamilyInstance));

fec.UnionWith(new FilteredElementCollector(doc).ofClass(typeof(HostObject))).ToElements();

//屬性記錄...
Dictionary<string,ParamaterSet> dicPara = new Dictionary<string,ParamaterSet>();
//匯出路徑
List<string> lstExportPath = new List<string>();

//把例項儲存.rfa檔案
if(elem is FamilyInstance)
{
    Familyinstance ins = elem as Familyinstance;
    Family family = ins.Symbol.Family;
    Document insDoc = ins.EditFamily(family);
    string sPath = "d:\\"+ins.Id+".rfa";
    lstExportPath.Add(sPath);
    insDoc.SaveAs("d:\\ins.rfa");
    insDoc.Close(false);
    dicPara.Add(sPath,ins.Paramters);
}

//載入一個空族並將例項載入 建立
Family fa = null;
using(Transaction trans = new Transaction(doc,"load"))
{
    trans.start();
    try
    {
        doc.LoadFamily("",familyLoadOption,out fa);
        trans.Commit();
    }
    catch
    {
        trans.RollBack();
    }
}

//遍歷lstExportPath 建立
Document fDoc = doc.EditFamily(fa);
foreach(string path in lstExportPath)
{
    Family loadFamily;
    FamilySymbol fs = null;
    fDoc.LoadFamily(path,familyLoadOption,out loadFamily);
    ISet<ElementId> symbolIds = loadFamily.GetFamilySymbolIds();
    foreach(Elementid symbolId in symbolIds)
    {
        fs = fDoc.GetElement(symbolId) as FamilySymbol;
        if(fs == null) Continue;
        if(!fs.IsActive) { fs.Activate(); break; }
    }
    if(fs == null) Comtinue;
    //這裡建立基於點的例項, 線/面 反正都是這個方法
    fDoc.FamilyCreate.NewFamilyInstance(記錄的座標,fs,StructuralType.NoStructural);
    //設定屬性,旋轉角度 Math.PI / 180 * 記錄的角度
    ...
    ...
    ...
}
//都建立完儲存就行了
fDoc.SaveAs("路徑");
fDoc.Close(false);

手打心好累, 功能不難,寫了一個大概。

希望能幫到大家。