英文:
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时的操作:
如何用纯粹的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:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论