miki969696

revit api 事务和事务组

使用事务创建元素
public void CreatingSketch(UIApplication uiApplication)
{
  Document document = uiApplication.ActiveUIDocument.Document;
  ApplicationServices.Application application = uiApplication.Application;


  // 创建一些几何线,这些线是临时的,所以不需要放在事务中
  XYZ Point1 = XYZ.Zero;
  XYZ Point2 = new XYZ(10, 0, 0);
  XYZ Point3 = new XYZ(10, 10, 0);
  XYZ Point4 = new XYZ(0, 10, 0);


  Line geomLine1 = Line.CreateBound(Point1, Point2);
  Line geomLine2 = Line.CreateBound(Point4, Point3);
  Line geomLine3 = Line.CreateBound(Point1, Point4);


  // 这个几何平面也是临时的,不需要事务
  XYZ origin = XYZ.Zero;
  XYZ normal = new XYZ(0, 0, 1);
  Plane geomPlane = application.Create.NewPlane(normal, origin);


  // 为了创建SketchPlane,我们需要一个事务,因为这个会修改Revit文档模型


  // 任何Transaction要放在 “using”中创建
  // 来保证它被正确的结束,而不会影响到其他地方
  using (Transaction transaction = new Transaction(document))
  {
     if (transaction.Start("Create model curves") == TransactionStatus.Started)
     {
        // 在当前文档中创建一个SketchPlane
        SketchPlane sketch = SketchPlane.Create(document, geomPlane);


        //使用SketchPlane和几何线来创建一个ModelLine
        ModelLine line1 = document.Create.NewModelCurve(geomLine1, sketch) as ModelLine;
        ModelLine line2 = document.Create.NewModelCurve(geomLine2, sketch) as ModelLine;
        ModelLine line3 = document.Create.NewModelCurve(geomLine3, sketch) as ModelLine;


        // 询问用户这个修改是否要提交
        TaskDialog taskDialog = new TaskDialog("Revit");
        taskDialog.MainContent = "Click either [OK] to Commit, or [Cancel] to Roll back the transaction.";
        TaskDialogCommonButtons buttons = TaskDialogCommonButtons.Ok | TaskDialogCommonButtons.Cancel;
        taskDialog.CommonButtons = buttons;


        if (TaskDialogResult.Ok == taskDialog.Show())
        {
           // 由于种种原因,如果修改或创建的模型不正确,
           // 这个Transaction可能不会被正确提交
           // 如果一个Transaction失败了或被用户取消了,
           // 那么返回的状态就是 RolledBack,而不是Committed。
           if (TransactionStatus.Committed != transaction.Commit())
           {
              TaskDialog.Show("Failure", "Transaction could not be committed");
           }
        }
        else
        {
           transaction.RollBack();
        }
     }
  }
}
使用Assimilate方法
public void CompoundOperation(Autodesk.Revit.DB.Document document)
{
  // 所有TransactionGroup要用“using”来创建来保证它的正确结束
  using (TransactionGroup transGroup = new TransactionGroup(document, "Level and Grid"))
  {
     if (transGroup.Start() == TransactionStatus.Started)
     {
        // 我们打算调用两个函数,每个都有一个独立的事务
        // 我们打算这个组合操作要么成功,要么失败
        // 只要其中有一个失败,我们就撤销所有操作


        if (CreateLevel(document, 25.0) && CreateGrid(document, new XYZ(0, 0, 0), new XYZ(10, 0, 0)))
        {
           // Assimilate函数会将这两个事务合并成一个,并只显示TransactionGroup的名字
           // 在Undo菜单里
           transGroup.Assimilate();
        }
        else
        {
           // 如果有一个操作失败了,我们撤销在这个事务组里的所有操作
           transGroup.RollBack();
        }
     }
  }
}


public bool CreateLevel(Autodesk.Revit.DB.Document document, double elevation)
{
  using (Transaction transaction = new Transaction(document, "Creating Level"))
  {
     // 必须启动事务来修改文档
     if (TransactionStatus.Started == transaction.Start())
     {
        if (null != document.Create.NewLevel(elevation))
        {
           return (TransactionStatus.Committed == transaction.Commit());
        }
        // 如果不能创建层,撤销这个事务
        transaction.RollBack();
     }
  }
  return false;
}


public bool CreateGrid(Autodesk.Revit.DB.Document document, XYZ p1, XYZ p2)
{
  using (Transaction transaction = new Transaction(document, "Creating Grid"))
  {
     if (TransactionStatus.Started == transaction.Start())
     {
        Line gridLine = Line.CreateBound(p1, p2);


        if ((null != gridLine) && (null != document.Create.NewGrid(gridLine)))
        {
           if (TransactionStatus.Committed == transaction.Commit())
           {
              return true;
           }
        }
        // 如果不能创建网格,撤销这个事务
        transaction.RollBack();
     }
  }
  return false;
}
 
 

posted on 2025-10-31 00:49  盛书伟  阅读(0)  评论(0)    收藏  举报

导航