版权声明:此文由黑夜の骑士创作,转载请注明出处,交流qq1056291511 https://blog.csdn.net/birdfly2015/article/details/90298908
1背景
小伙伴们在做revit二次开发的时候,可能需要通过程序去打开指定的族文档,样板文件,或者项目文件。
2思路
1.这三者其实都是一样的(参见代码中的注释1)
2.为了对打开的文档进行操作,必须将Document 设置为打开的文档(参见注释2)
3.如果对文档进行了修改,那么需要保存(参见注释3、4)
4.如果需要关闭这个打开的文档(参见注释5)
3程序
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autodesk.Revit.UI;
using Autodesk.Revit.DB;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.Attributes;
namespace heiyedeqishi
{
[Transaction(TransactionMode.Manual)]
class Revit_API_Executable1 : IExternalCommand
{
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
UIDocument uidoc = commandData.Application.ActiveUIDocument;
Document document = uidoc.Document;
//1文件地址,可以是rvt,rfa,rte
string filePath = @"D:\黑夜的骑士\山海关大桥.rvt";
//2这样,我们就能打开文档,并且获得这个文档的document
Document doc = document.Application.OpenDocumentFile(filePath);
try
{
//3接下来,把对整个doc的操作放到这儿就行了
//....
return Result.Succeeded;
//4对文档进行保存
doc.Save();
//5关闭打开的这个文档
doc.Close();
}
catch (Exception ex)
{
message = ex.Message;
return Result.Failed;
}
}
}
}
4注意事项
拓展一下,我们通过不停的开打关闭文档,就可以实现批量操作了