如何将”Family”保存到一个新文件中。

huangapple go评论96阅读模式
英文:

How to save Family to a new file

问题

使用Revit API并且不使用Revit UI,我想将一个加载的族保存为一个新文件。

让我们看一下以下代码:

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

    if (document.LoadFamily(familyFullPath, out Family family))
    {
        Console.Writeline("加载成功");
    }

    transaction.Commit();

    ModelPath newPath = ModelPathUtils.ConvertUserVisiblePathToModelPath("result.rfa");
    SaveAsOptions saveAsOptions = new SaveAsOptions();

    family.Document.SaveAs(newPath, saveAsOptions);
}

这段代码的问题在于它保存了加载族的默认文档,因为family.Document返回的是“元素所在的文档”。

我想要做的是仅将加载的Family保存为一个新的.rfa文档。

为了更好地解释,我将演示在使用Revit UI时的操作:

如何将”Family”保存到一个新文件中。

如何用纯粹的Autodesk.Revit.DB实现相同的效果呢?

英文:

Using Revit API and without using Revit UI, I want to save a loaded family into a new file.

Let's take a look at the following code:

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


            if (document.LoadFamily(familyFullPath, out Family family))
            {
                Console.Writeline("Load successful")
            }

            transaction.Commit();

            ModelPath newPath = ModelPathUtils.ConvertUserVisiblePathToModelPath("result.rfa");
            SaveAsOptions saveAsOptions = new SaveAsOptions();
         
            family.Document.SaveAs(newPath, saveAsOptions);
            
        }

The problem with this code is that it saves the default document in which we load the family, because family.Document is returning "the document in which the element resides".

What I want to do is to save only the loaded Family as a new .rfa document.

To explain better, I will demonstrate what it looks like when using Revit UI:

如何将”Family”保存到一个新文件中。

How can I achieve the same thing but purely with Autodesk.Revit.DB?

答案1

得分: 3

好的,解决方案是使用 EditFamily
因此,以下代码将把族保存到一个新的独立文档中:

using (Transaction transaction = new Transaction(document))
{
    transaction.Start("加载并保存族");


    if (document.LoadFamily(familyFullPath, out Family family))
    {
        Console.Writeline("加载成功");
    }

    transaction.Commit();

    // 通过编辑族创建新的族文档
    var famDoc = document.EditFamily(family);

    ModelPath newPath = ModelPathUtils.ConvertUserVisiblePathToModelPath("result.rfa");
    SaveAsOptions saveAsOptions = new SaveAsOptions();

    // 保存新文档
    famDoc.SaveAs(newPath, saveAsOptions);

}

解释一下:EditFamily "创建一个独立的族的副本,供编辑使用",在我们拥有这个独立副本时,我们可以保存它。

英文:

Ok, the solution is to use EditFamily.
So the following code will save the family to a new, separate document:

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


        if (document.LoadFamily(familyFullPath, out Family family))
        {
            Console.Writeline("Load successful")
        }

        transaction.Commit();

        // create new family document by editing family
        var famDoc = document.EditFamily(family);

        ModelPath newPath = ModelPathUtils.ConvertUserVisiblePathToModelPath("result.rfa");
        SaveAsOptions saveAsOptions = new SaveAsOptions();

        // save the new document
        famDoc.SaveAs(newPath, saveAsOptions);
        
    }

And for explanation: EditFamily "creates an independent copy of the family for editing", and while we have the independent copy, we can save it.

huangapple
  • 本文由 发表于 2023年8月10日 22:26:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/76876684.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定