/// <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); }
/// <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); }