Transaction Class |
The Transaction type exposes the following members.
Name | Description | |
---|---|---|
![]() | Transaction(Document) | Instantiates a transaction object. |
![]() | Transaction(Document, String) | Instantiates a transaction object |
Name | Description | |
---|---|---|
![]() | IsValidObject | Specifies whether the .NET object represents a valid Revit entity. |
Name | Description | |
---|---|---|
![]() ![]() | Commit | Commits all changes made to the model during the transaction. |
![]() ![]() | Commit(FailureHandlingOptions) | Commits all changes made to the model during the transaction. |
![]() | Dispose | Releases all resources used by the Transaction |
![]() | Equals | Determines whether the specified object is equal to the current object. (Inherited from Object) |
![]() | GetFailureHandlingOptions | Returns the current failure handling options. |
![]() | GetHashCode | Serves as the default hash function. (Inherited from Object) |
![]() | GetName | Returns the transaction's name. |
![]() | GetStatus | Returns the current status of the transaction. |
![]() | GetType | Gets the Type of the current instance. (Inherited from Object) |
![]() | HasEnded | Determines whether the transaction has ended already. |
![]() | HasStarted | Determines whether the transaction has been started yet. |
![]() ![]() | RollBack | Rolls back all changes made to the model during the transaction. |
![]() ![]() | RollBack(FailureHandlingOptions) | Rolls back all changes made to the model during the transaction. |
![]() ![]() | SetFailureHandlingOptions | Sets options for handling failures to be used when the transaction is being committed or rolled back. |
![]() ![]() | SetName | Sets the transaction's name. |
![]() ![]() | Start | Starts the transaction. |
![]() ![]() | Start(String) | Starts the transaction with an assigned name. |
![]() | ToString | Returns a string that represents the current object. (Inherited from Object) |
Any change to a document can only be made while there is an active transaction open for that document. Changes do not become part of the document until the active transaction is committed. Consequently, all changes made in a transaction can be rolled back either explicitly or implicitly by the transaction's destructor.
A document can have only one transaction open at any given time.
Transactions cannot be started when the document is in read-only mode, either permanently or temporarily. See the Document class methods IsReadOnly and IsModifiable for more details.
Transactions in linked documents are not permitted, for linked documents are not allowed to be modified.
If a transaction was started and not finished yet by the time the Transaction object is about to be disposed, the default destructor will roll it back automatically, thus all changes made to the document while this transaction was open will be discarded. It is not recommended to rely on this default behavior though. Instead, it is advised to always call either Commit or RollBack explicitly before the transaction object gets disposed. Please note that unless invoked explicitly the actual destruction of an object in managed code might not happen until the object is collected by the garbage collector.
public void CreatingSketch(UIApplication uiApplication) { Autodesk.Revit.DB.Document document = uiApplication.ActiveUIDocument.Document; Autodesk.Revit.ApplicationServices.Application application = uiApplication.Application; // Create a few geometry lines. These lines are transaction (not in the model), // therefore they do not need to be created inside a document transaction. 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); // This geometry plane is also transaction and does not need a transaction XYZ origin = XYZ.Zero; XYZ normal = new XYZ(0, 0, 1); Plane geomPlane = Plane.CreateByNormalAndOrigin(normal, origin); // In order to a sketch plane with model curves in it, we need // to start a transaction because such operations modify the model. // All and any transaction should be enclosed in a 'using' // block or guarded within a try-catch-finally blocks // to guarantee that a transaction does not out-live its scope. using (Transaction transaction = new Transaction(document)) { if (transaction.Start("Create model curves") == TransactionStatus.Started) { // Create a sketch plane in current document SketchPlane sketch = SketchPlane.Create(document,geomPlane); // Create a ModelLine elements using the geometry lines and sketch plane 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; // Ask the end user whether the changes are to be committed or not 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()) { // For many various reasons, a transaction may not be committed // if the changes made during the transaction do not result a valid model. // If committing a transaction fails or is canceled by the end user, // the resulting status would be RolledBack instead of Committed. if (TransactionStatus.Committed != transaction.Commit()) { TaskDialog.Show("Failure", "Transaction could not be committed"); } } else { transaction.RollBack(); } } } }