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

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

How to save Family to a new file

问题

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

让我们看一下以下代码:

  1. using (Transaction transaction = new Transaction(document))
  2. {
  3. transaction.Start("Load and Save Family");
  4. if (document.LoadFamily(familyFullPath, out Family family))
  5. {
  6. Console.Writeline("加载成功");
  7. }
  8. transaction.Commit();
  9. ModelPath newPath = ModelPathUtils.ConvertUserVisiblePathToModelPath("result.rfa");
  10. SaveAsOptions saveAsOptions = new SaveAsOptions();
  11. family.Document.SaveAs(newPath, saveAsOptions);
  12. }

这段代码的问题在于它保存了加载族的默认文档,因为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:

  1. using (Transaction transaction = new Transaction(document))
  2. {
  3. transaction.Start("Load and Save Family");
  4. if (document.LoadFamily(familyFullPath, out Family family))
  5. {
  6. Console.Writeline("Load successful")
  7. }
  8. transaction.Commit();
  9. ModelPath newPath = ModelPathUtils.ConvertUserVisiblePathToModelPath("result.rfa");
  10. SaveAsOptions saveAsOptions = new SaveAsOptions();
  11. family.Document.SaveAs(newPath, saveAsOptions);
  12. }

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
因此,以下代码将把族保存到一个新的独立文档中:

  1. using (Transaction transaction = new Transaction(document))
  2. {
  3. transaction.Start("加载并保存族");
  4. if (document.LoadFamily(familyFullPath, out Family family))
  5. {
  6. Console.Writeline("加载成功");
  7. }
  8. transaction.Commit();
  9. // 通过编辑族创建新的族文档
  10. var famDoc = document.EditFamily(family);
  11. ModelPath newPath = ModelPathUtils.ConvertUserVisiblePathToModelPath("result.rfa");
  12. SaveAsOptions saveAsOptions = new SaveAsOptions();
  13. // 保存新文档
  14. famDoc.SaveAs(newPath, saveAsOptions);
  15. }

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

英文:

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

  1. using (Transaction transaction = new Transaction(document))
  2. {
  3. transaction.Start("Load and Save Family");
  4. if (document.LoadFamily(familyFullPath, out Family family))
  5. {
  6. Console.Writeline("Load successful")
  7. }
  8. transaction.Commit();
  9. // create new family document by editing family
  10. var famDoc = document.EditFamily(family);
  11. ModelPath newPath = ModelPathUtils.ConvertUserVisiblePathToModelPath("result.rfa");
  12. SaveAsOptions saveAsOptions = new SaveAsOptions();
  13. // save the new document
  14. famDoc.SaveAs(newPath, saveAsOptions);
  15. }

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:

确定