应用程序:TransactionControl

Revit平台:所有

Revit版本:2011.0

最初发布于:2008.0

编程语言:C#

技能水平:中级

类别:基础知识

类型:ExternalCommand

主题:如何处理事务组,事务和子事务。

相关类:

Autodesk.Revit.Document

Autodesk.Revit.DB.TransactionGroup

Autodesk.Revit.DB.Transaction

Autodesk.Revit.DB.SubTransaction

项目文件:

Command.cs

它包含实现接口IExternalCommand的类Command。这是此外部命令的入口。

TransactionForm.cs

它包含用于开始、回滚或提交事务组和事务的选项的类TransactionForm。该表单还允许用户在交易中创建、移动或删除墙壁。树状视图用于显示所有操作、上面的交易/事务组和它们之间的关系。

CreateWallForm.cs

它包含用于创建墙壁的CreateWallForm类。该表单中有一个组合框和几个文本框,用于设置用于创建墙壁的参数。

描述:

功能:

- 事务组的组框中的三个按钮提供以下操作:开始交易组、回滚交易组、提交交易组。

- 事务的组框中的三个按钮提供以下操作:开始事务、回滚事务、提交事务。

- 另一个操作组框中的三个按钮提供以下操作:创建墙壁、移动墙壁、删除墙壁。

- 提供树状视图以显示事务组、事务、操作及其之间的关系。

- 允许用户使用嵌套事务组来管理其事务组和事务。交易不能嵌套,可以在交易组内外创建。子事务必须在事务内创建。用户可以在交易或子交易内执行上面列出的一个或多个操作。

- 当用户单击“确定”或“取消”并且有一些尚未完成的事务组或事务时,显示一个消息框询问用户是否要结束所有事务。

实现:

- 通过类TransactionGroupStart/Rollback/Commit方法可以启动/回滚/提交事务组。

- 可以通过类TransactionStart/Rollback/Commit方法启动/回滚/提交事务。

源代码:
完整的源代码请加入QQ群649037449,在群文件中下载RevitSDK.exe,解压后在文件夹中搜索本文中应用程序名称即可获得完整源码

Command.cs

//
// (C) Copyright 2003-2019 by Autodesk, Inc.
//
// Permission to use, copy, modify, and distribute this software in
// object code form for any purpose and without fee is hereby granted,
// provided that the above copyright notify appears in all copies and
// that both that copyright notify and the limited warranty and
// restricted rights notify below appear in all supporting
// documentation.
//
// AUTODESK PROVIDES THIS PROGRAM "AS IS" AND WITH ALL FAULTS.
// AUTODESK SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTY OF
// MERCHANTABILITY OR FITNESS FOR A PARTICULAR USE. AUTODESK, INC.
// DOES NOT WARRANT THAT THE OPERATION OF THE PROGRAM WILL BE
// UNINTERRUPTED OR ERROR FREE.
//
// Use, duplication, or disclosure by the U.S. Government is subject to
// restrictions set forth in FAR 52.227-19 (Commercial Computer
// Software - Restricted Rights) and DFAR 252.227-7013(c)(1)(ii)
// (Rights in Technical Data and Computer Software), as applicable.
//
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using Autodesk.Revit;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
namespace Revit.SDK.Samples.TransactionControl.CS
{
    /// <summary>
    /// Implements the Revit add-in interface IExternalCommand
    /// </summary>
    [Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]
    [Autodesk.Revit.Attributes.Regeneration(Autodesk.Revit.Attributes.RegenerationOption.Manual)]
    [Autodesk.Revit.Attributes.Journaling(Autodesk.Revit.Attributes.JournalingMode.NoCommandData)]
    public class Command : IExternalCommand
    {
        /// <summary>
        /// Implement this method as an external command for Revit.
        /// </summary>
        /// <param name="commandData">An object that is passed to the external application 
        /// which contains data related to the command, 
        /// such as the application object and active view.</param>
        /// <param name="message">A message that can be set by the external application 
        /// which will be displayed if a failure or cancellation is returned by 
        /// the external command.</param>
        /// <param name="elements">A set of elements to which the external application 
        /// can add elements that are to be highlighted in case of failure or cancellation.</param>
        /// <returns>Return the status of the external command. 
        /// A result of Succeeded means that the API external method functioned as expected. 
        /// Cancelled can be used to signify that the user cancelled the external operation 
        /// at some point. Failure should be returned if the application is unable to proceed with 
        /// the operation.</returns>
        public Autodesk.Revit.UI.Result Execute(ExternalCommandData commandData,
        ref string message, Autodesk.Revit.DB.ElementSet elements)
        {
            try
            {
                // process data from Revit and show dialog
                using (TransactionForm transactionFrm = new TransactionForm(commandData))
                {
                    if (transactionFrm.ShowDialog() == DialogResult.OK)
                    {
                        return Autodesk.Revit.UI.Result.Succeeded;
                    }
                }
            }
            catch (Exception ex)
            {
                message = ex.Message;
                return Autodesk.Revit.UI.Result.Failed;
            }
            return Autodesk.Revit.UI.Result.Cancelled;
        }
    }
}