コード例 #1
0
 protected void BeginTransaction(DB.Document document)
 {
     CurrentTransaction = new DB.Transaction(document, Name);
     if (CurrentTransaction.Start() != DB.TransactionStatus.Started)
     {
         CurrentTransaction.Dispose();
         CurrentTransaction = null;
         throw new InvalidOperationException($"Unable to start Transaction '{Name}'");
     }
 }
コード例 #2
0
        /// <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 virtual Result Execute(ExternalCommandData commandData
                                      , ref string message, ElementSet elements)
        {
            // Get the document from external command data.
            UIDocument activeDoc = commandData.Application.ActiveUIDocument;

            Autodesk.Revit.DB.Document doc = activeDoc.Document;
            if (null == doc)
            {
                return(Result.Failed);
            }

            // The transaction and its status, using Revit's Transaction class
            Autodesk.Revit.DB.Transaction trans = new Autodesk.Revit.DB.Transaction(doc, "Delete structural connection");
            TransactionStatus             ts    = TransactionStatus.Uninitialized;

            try
            {
                // We get the connection to be deleted. We use Revit's StructuralConnectionHandler class
                // for more details, please consult http://www.autodesk.com/adv-steel-api-walkthroughs-2019-enu
                StructuralConnectionHandler conn = Utilities.Functions.SelectConnection(activeDoc);
                if (null == conn)
                {
                    return(Result.Failed);
                }
                // Start the transaction.
                trans.Start();
                // Delete selected structural connection.
                doc.Delete(conn.Id);
                // Commit the transaction
                ts = trans.Commit();
                if (ts != TransactionStatus.Committed)
                {
                    message = "Failed to commit the current transaction !";
                    trans.RollBack();
                    return(Result.Failed);
                }
            }

            catch (Autodesk.Revit.Exceptions.OperationCanceledException)
            {
                if (ts != TransactionStatus.Uninitialized)
                {
                    trans.RollBack();
                }
                trans.Dispose();
                return(Result.Cancelled);
            }
            return(Result.Succeeded);
        }
コード例 #3
0
        protected void BeginTransaction(DB.Document document)
        {
            if (CurrentTransactions?.ContainsKey(document) != true)
            {
                var transaction = new DB.Transaction(document, Name);
                if (transaction.Start() != DB.TransactionStatus.Started)
                {
                    transaction.Dispose();
                    throw new InvalidOperationException($"Unable to start Transaction '{Name}'");
                }

                if (CurrentTransactions is null)
                {
                    CurrentTransactions = new Dictionary <DB.Document, DB.Transaction>();
                }

                CurrentTransactions.Add(document, transaction);
            }
        }
コード例 #4
0
        // Step 2.
        //protected override void OnAfterStart(Document document, string strTransactionName) { }

        // Step 3.
        //protected override void TrySolveInstance(IGH_DataAccess DA) { }

        // Step 4.
        //protected override void OnBeforeCommit(Document document, string strTransactionName) { }

        // Step 5.
        protected override void AfterSolveInstance()
        {
            if (TransactionalStrategy != TransactionStrategy.PerComponent)
            {
                return;
            }

            try
            {
                if (RunCount <= 0)
                {
                    return;
                }

                if (TransactionStatus == DB.TransactionStatus.Uninitialized)
                {
                    return;
                }

                if (Phase != GH_SolutionPhase.Failed)
                {
                    CommitTransaction();
                }
            }
            finally
            {
                switch (TransactionStatus)
                {
                case DB.TransactionStatus.Uninitialized:
                case DB.TransactionStatus.Started:
                case DB.TransactionStatus.Committed:
                    break;

                default:
                    AddRuntimeMessage(GH_RuntimeMessageLevel.Error, $"Transaction {TransactionStatus} and aborted.");
                    break;
                }

                CurrentTransaction?.Dispose();
                CurrentTransaction = null;
            }
        }
コード例 #5
0
        /// <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 virtual Result Execute(ExternalCommandData commandData
                                      , ref string message, ElementSet elements)
        {
            // Get the document from external command data.
            UIDocument activeDoc = commandData.Application.ActiveUIDocument;

            Autodesk.Revit.DB.Document doc = activeDoc.Document;

            if (null == doc)
            {
                return(Result.Failed);
            }

            // The transaction and its status, using Revit's Transaction class
            Autodesk.Revit.DB.Transaction trans = new Autodesk.Revit.DB.Transaction(doc, "Remove subelements from custom connection");
            TransactionStatus             ts    = TransactionStatus.Uninitialized;

            try
            {
                // Selecting the custom connection, using Revit's StructuralConnectionHandler class
                // for more details, please consult http://www.autodesk.com/adv-steel-api-walkthroughs-2019-enu
                StructuralConnectionHandler conn = Utilities.Functions.SelectConnection(activeDoc);

                // If the connection is not a custom one
                if (!(conn.IsCustom()))
                {
                    return(Result.Failed);
                }

                IList <Subelement> ide = new List <Subelement>();
                // Prompt to select subelements
                IList <Reference> refs = activeDoc.Selection.PickObjects(ObjectType.Subelement, "Select subelements:").ToList();
                // Populate the reference list
                foreach (Reference eRef in refs)
                {
                    ide.Add(doc.GetSubelement(eRef));
                }

                if (ide.Count <= 0)
                {
                    return(Result.Failed);
                }
                // Start the transaction
                trans.Start();
                // Removing the subelements from the custom connection
                StructuralConnectionHandlerType.RemoveMainSubelementsFromCustomConnection(conn, ide);
                // Committing the transaction
                ts = trans.Commit();

                if (ts != TransactionStatus.Committed)
                {
                    message = "Failed to commit the current transaction !";
                    trans.RollBack();
                    return(Result.Failed);
                }
            }

            catch (Autodesk.Revit.Exceptions.OperationCanceledException)
            {
                if (ts != TransactionStatus.Uninitialized)
                {
                    trans.RollBack();
                }
                trans.Dispose();
                return(Result.Cancelled);
            }
            catch (Autodesk.Revit.Exceptions.ArgumentException)
            {
                if (ts != TransactionStatus.Uninitialized)
                {
                    trans.RollBack();
                }
                trans.Dispose();
                message = "Custom connection already contains the selected element(s)! / Can't delete all subelements!";
                return(Result.Failed);
            }
            return(Result.Succeeded);
        }
コード例 #6
0
        /// <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 virtual Result Execute(ExternalCommandData commandData
                                      , ref string message, ElementSet elements)
        {
            // Get the document from external command data.
            UIDocument activeDoc = commandData.Application.ActiveUIDocument;

            Autodesk.Revit.DB.Document doc = activeDoc.Document;

            if (null == doc)
            {
                return(Result.Failed);
            }
            // The transaction and its status, using Revit's Transaction class
            Autodesk.Revit.DB.Transaction trans = new Autodesk.Revit.DB.Transaction(doc, "Remove element(s) from structural connection");
            TransactionStatus             ts    = TransactionStatus.Uninitialized;

            try
            {
                // Select the connection we need to remove elements from.
                // We use Revit's StructuralConnectionHandler class for the connection.
                // for more details, please consult http://www.autodesk.com/adv-steel-api-walkthroughs-2019-enu
                StructuralConnectionHandler conn = Utilities.Functions.SelectConnection(activeDoc);

                if (null == conn)
                {
                    return(Result.Failed);
                }
                // Select elements to remove from connection.
                IList <ElementId> ids = Utilities.Functions.SelectConnectionElements(activeDoc, "Select elements to remove from connection :");

                if (ids.Count() <= 0)
                {
                    return(Result.Failed);
                }

                // Starting the transaction
                trans.Start();
                // Removing the elements from the connection
                conn.RemoveElementIds(ids);
                // Committing the transaction
                ts = trans.Commit();

                if (ts != TransactionStatus.Committed)
                {
                    message = "Failed to commit the current transaction !";
                    if (ts != TransactionStatus.Uninitialized)
                    {
                        trans.RollBack();
                    }
                    return(Result.Failed);
                }
            }

            catch (Autodesk.Revit.Exceptions.OperationCanceledException)
            {
                if (ts != TransactionStatus.Uninitialized)
                {
                    trans.RollBack();
                }
                trans.Dispose();
                return(Result.Cancelled);
            }

            catch (Autodesk.Revit.Exceptions.ArgumentException)
            {
                if (ts != TransactionStatus.Uninitialized)
                {
                    trans.RollBack();
                }
                trans.Dispose();
                message = "Invalid elements selected!";
                return(Result.Failed);
            }
            return(Result.Succeeded);
        }
コード例 #7
0
        /// <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 virtual Result Execute(ExternalCommandData commandData
                                      , ref string message, ElementSet elements)
        {
            // Get the document from external command data.
            UIDocument activeDoc = commandData.Application.ActiveUIDocument;

            Autodesk.Revit.DB.Document doc = activeDoc.Document;

            if (null == doc)
            {
                return(Result.Failed);
            }
            // The transaction and its status. We use Revit's Transaction class for this purpose
            Autodesk.Revit.DB.Transaction trans = new Autodesk.Revit.DB.Transaction(doc, "Add ranges of applicability");
            TransactionStatus             ts    = TransactionStatus.Uninitialized;

            try
            {
                // Select the connection to add ranges, using Revit's StructuralConnectionHandler class
                StructuralConnectionHandler conn = Utilities.Functions.SelectConnection(activeDoc);

                if (null == conn)
                {
                    return(Result.Failed);
                }

                StructuralConnectionHandlerType connectionType = doc.GetElement(conn.GetTypeId()) as StructuralConnectionHandlerType;

                if (null == connectionType)
                {
                    return(Result.Failed);
                }

                RuleApplicabilityRangeTable rangeTable = ApplicabilityRangesAccess.GetRanges(connectionType);

                // Create the rows and add the conditions to them
                RuleApplicabilityRangeRow rangeRow1 = new RuleApplicabilityRangeRow();
                rangeRow1.Key    = "My new range 1";
                rangeRow1.Ranges = CreateConditionsForRow1();

                RuleApplicabilityRangeRow rangeRow2 = new RuleApplicabilityRangeRow();
                rangeRow2.Key    = "My new range 2";
                rangeRow2.Ranges = CreateConditionsForRow2();

                // get existing rows
                RuleApplicabilityRangeRow[] rows    = rangeTable.Rows;
                RuleApplicabilityRangeRow[] newRows = new RuleApplicabilityRangeRow[] { rangeRow1, rangeRow2 };

                // set back the rows
                rangeTable.Rows = rows.Concat(newRows).ToArray();

                // we can also verify if the conditions added in the ranges are met. If the result is false it means that the input elements are out of the defined conditions
                bool validate = ApplicabilityRangeValidator.Validate(conn, rangeTable, "Revit", "");

                // Start the transaction
                trans.Start();

                // Save the ranges
                ApplicabilityRangesAccess.SaveRanges(connectionType, rangeTable);

                // Commit the transaction
                ts = trans.Commit();

                if (ts != TransactionStatus.Committed)
                {
                    message = "Failed to commit the current transaction !";
                    trans.RollBack();
                    return(Result.Failed);
                }
            }

            catch (Autodesk.Revit.Exceptions.OperationCanceledException)
            {
                if (ts != TransactionStatus.Uninitialized)
                {
                    trans.RollBack();
                }
                trans.Dispose();
                return(Result.Cancelled);
            }
            catch (Autodesk.Revit.Exceptions.ArgumentException)
            {
                if (ts != TransactionStatus.Uninitialized)
                {
                    trans.RollBack();
                }
                trans.Dispose();
                message = "Failed to add ranges";
                return(Result.Failed);
            }
            return(Result.Succeeded);
        }
コード例 #8
0
        /// <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 virtual Result Execute(ExternalCommandData commandData
                                      , ref string message, ElementSet elements)
        {
            // Get the document from external command data.
            UIDocument activeDoc = commandData.Application.ActiveUIDocument;

            Autodesk.Revit.DB.Document doc = activeDoc.Document;
            if (null == doc)
            {
                return(Result.Failed);
            }

            // The transaction and its status. We use Revit's Transaction class for this purpose
            Autodesk.Revit.DB.Transaction trans = new Autodesk.Revit.DB.Transaction(doc, "Add element(s) to custom connection");
            TransactionStatus             ts    = TransactionStatus.Uninitialized;

            try
            {
                // Selecting the custom connection, using Revit's StructuralConnectionHandler class
                // for more details, please consult http://www.autodesk.com/adv-steel-api-walkthroughs-2019-enu
                StructuralConnectionHandler conn = Utilities.Functions.SelectConnection(activeDoc);

                if (null == conn)
                {
                    return(Result.Failed);
                }

                if (!(conn.IsCustom()))
                {
                    return(Result.Failed);
                }
                // Select elements to add to connection.
                IList <Reference> refs = Utilities.Functions.SelectConnectionElementsCustom(activeDoc);

                if (refs.Count() <= 0)
                {
                    return(Result.Failed);
                }

                // Start transaction
                trans.Start();
                // Adding the elements to the custom connection, using Revit's StructuralConnectionHandlerType class
                StructuralConnectionHandlerType.AddElementsToCustomConnection(conn, refs);
                // Commit the transaction
                ts = trans.Commit();

                if (ts != TransactionStatus.Committed)
                {
                    message = "Failed to commit the current transaction !";
                    trans.RollBack();
                    return(Result.Failed);
                }
            }

            catch (Autodesk.Revit.Exceptions.OperationCanceledException)
            {
                if (ts != TransactionStatus.Uninitialized)
                {
                    trans.RollBack();
                }
                trans.Dispose();
                return(Result.Cancelled);
            }
            catch (Autodesk.Revit.Exceptions.ArgumentException)
            {
                if (ts != TransactionStatus.Uninitialized)
                {
                    trans.RollBack();
                }
                trans.Dispose();
                message = "Custom connection already contains the selected element(s)!";
                return(Result.Failed);
            }
            return(Result.Succeeded);
        }