项目完整代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.Attributes;
namespace Trans
{
[TransactionAttribute(TransactionMode.Manual)]//注:Revit2017以上版本已取消Automatic,现推荐使用Manual
[RegenerationAttribute(RegenerationOption.Manual)]
public class Class1 : IExternalCommand
{
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
UIDocument uidoc = commandData.Application.ActiveUIDocument;
Document doc = uidoc.Document;
TransactionGroup tg = new TransactionGroup(doc, "TG");//名字"TG"可以随便取,用于Revit撤销菜单中可见
tg.Start();
Transaction t1 = new Transaction(doc, "T1"); //在TransactionGroup嵌入Transaction
t1.Start();
Wall.Create(doc, Line.CreateBound(new XYZ(), new XYZ(0, 10, 0)), Level.Create(doc, 0).Id, false);//创建第一道墙,false表示创建的是结构墙
t1.Commit();
TaskDialog.Show("提示", "已生成第一道墙!");
Transaction t2 = new Transaction(doc, "T2");
t2.Start();
Wall.Create(doc, Line.CreateBound(new XYZ(), new XYZ(0, 10, 0)), Level.Create(doc, 0).Id, false);//创建第二道墙,与第一道墙相同
t2.Commit();
//tg.Assimilate(); //同化两道相同的墙,警告消失
//SubTransaction的使用
Transaction tt = new Transaction(doc, "Transact");
tt.Start();
SubTransaction st1 = new SubTransaction(doc);
st1.Start();
SubTransaction st2 = new SubTransaction(doc);
st2.Start();
st2.Commit();
TaskDialog.Show("提示", "t2已提交");
st1.Commit();
TaskDialog.Show("提示", "t1已提交");
tt.Commit();
return Result.Succeeded;
}
}
}
效果:
参考资料:
周婧祎《Autodesk Revit 2016二次开发入门教程》