Exemple #1
0
        public static void PlaceCustomConnection(Document doc)
        {
            StructuralConnectionHandlerType connectionType = new FilteredElementCollector(doc)
                                                             .OfCategory(BuiltInCategory.OST_StructConnections)
                                                             .WhereElementIsElementType()
                                                             .Where(e => e.Name == "Base Plate - Custom")
                                                             .Cast <StructuralConnectionHandlerType>()
                                                             .FirstOrDefault();

            FamilyInstance column = new FilteredElementCollector(doc)
                                    .OfCategory(BuiltInCategory.OST_StructuralColumns)
                                    .WhereElementIsNotElementType()
                                    .Cast <FamilyInstance>()
                                    .FirstOrDefault();

            if (column == null || connectionType == null)
            {
                return;
            }

            var connection = StructuralConnectionHandler.Create(doc, new List <ElementId>()
            {
                column.Id
            }, connectionType.Id);
        }
Exemple #2
0
        /// <summary>
        /// Create generic structural connection.
        /// </summary>
        /// <param name="activeDoc">The active document.</param>
        /// <param name="message">Set message on failure.</param>
        /// <returns>Returns the status of the operation.</returns>
        public static Result CreateGenericStructuralConnection(UIDocument activeDoc, ref string message)
        {
            Result ret = Result.Succeeded;

            List <ElementId> ids = StructuralConnectionSelectionUtils.SelectConnectionElements(activeDoc);

            if (ids.Count() > 0)
            {
                // Start a new transaction.
                using (Transaction tran = new Transaction(activeDoc.Document, "Create generic structural connection"))
                {
                    tran.Start();

                    StructuralConnectionHandler conn = StructuralConnectionHandler.Create(activeDoc.Document, ids);

                    TransactionStatus ts = tran.Commit();
                    if (ts != TransactionStatus.Committed)
                    {
                        message = "Failed to commit the current transaction !";
                        ret     = Result.Failed;
                    }
                }
            }
            else
            {
                message = "There is no element selected !";
                ret     = Result.Failed;
            }

            return(ret);
        }
Exemple #3
0
        /// <summary>
        /// Delete generic structural connection.
        /// </summary>
        /// <param name="activeDoc">The active document.</param>
        /// <param name="message">Set message on failure.</param>
        /// <returns>Returns the status of the operation.</returns>
        public static Result DeleteGenericStructuralConnection(UIDocument activeDoc, ref string message)
        {
            Result ret = Result.Succeeded;

            // Select a structural connection.
            StructuralConnectionHandler conn = StructuralConnectionSelectionUtils.SelectConnection(activeDoc);

            if (conn != null)
            {
                // Start a new transaction.
                using (Transaction tran = new Transaction(activeDoc.Document, "Delete generic structural connection"))
                {
                    tran.Start();

                    // Delete selected structural connection.
                    activeDoc.Document.Delete(conn.Id);

                    TransactionStatus ts = tran.Commit();
                    if (ts != TransactionStatus.Committed)
                    {
                        message = "Failed to commit the current transaction !";
                        return(Result.Failed);
                    }
                }
            }
            else
            {
                message = "There is no connection selected !";
                ret     = Result.Failed;
            }

            return(ret);
        }
        public virtual Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            UIDocument activeDoc = commandData.Application.ActiveUIDocument;

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

            try
            {
                // Start detailed steel modeling transaction
                using (FabricationTransaction trans = new FabricationTransaction(doc, false, "Update connection parameters"))
                {
                    // for more details, please consult http://www.autodesk.com/adv-steel-api-walkthroughs-2019-enu
                    Reference eRef = activeDoc.Selection.PickObject(ObjectType.Element, "Pick a base plate connection");

                    Element elem = doc.GetElement(eRef.ElementId);
                    if (null == elem || !(elem is StructuralConnectionHandler))
                    {
                        return(Result.Failed);
                    }

                    StructuralConnectionHandler rvtConnection = (StructuralConnectionHandler)elem;

                    FilerObject filerObj = Utilities.Functions.GetFilerObject(doc, eRef);

                    if (null == filerObj || !(filerObj is UserAutoConstructionObject))
                    {
                        return(Result.Failed);
                    }

                    UserAutoConstructionObject asConnection = (UserAutoConstructionObject)filerObj;
                    //
                    //read connection parameters
                    IFiler connectionFiler = asConnection.Save();

                    if (connectionFiler != null)
                    {
                        //I choose to modify thickess of the base plate
                        connectionFiler.WriteItem(Convert.ToDouble(50.0), "BaseThickness"); //units must be milimmeters;
                        asConnection.Load(connectionFiler);                                 //update connection parameters
                        asConnection.Update();
                        //
                        //if the connection parameters are modified, than we have to set this flag to true,
                        //meaning that this connection has different parameters than it's connection type.
                        rvtConnection.OverrideTypeParams = true;
                    }
                    trans.Commit();
                }
            }
            catch (Autodesk.Revit.Exceptions.OperationCanceledException)
            {
                return(Result.Cancelled);
            }
            return(Result.Succeeded);
        }
Exemple #5
0
        /// <summary>
        /// Get the Extensible storage schema
        /// </summary>
        private static Schema GetSchema(Document doc, StructuralConnectionHandler connection)
        {
            Schema schema = null;

            Guid guid = GetConnectionHandlerTypeGuid(connection, doc);

            if (guid != null && guid != Guid.Empty)
            {
                schema = Schema.ListSchemas().Where(x => x.GUID == guid).FirstOrDefault();
            }

            return(schema);
        }
Exemple #6
0
        /// <summary>
        /// Match properties for detailed structural connections.
        /// </summary>
        /// <param name="activeDoc">The active document.</param>
        /// <param name="message">Set message on failure.</param>
        /// <returns>Returns the status of the operation.</returns>
        public static Result MatchPropertiesDetailedStructuralConnection(UIDocument activeDoc, ref string message)
        {
            Result ret = Result.Succeeded;

            // Prompt to select a structural connection
            StructuralConnectionHandler srcConn  = StructuralConnectionSelectionUtils.SelectConnection(activeDoc);
            StructuralConnectionHandler destConn = StructuralConnectionSelectionUtils.SelectConnection(activeDoc);

            if (srcConn != null && destConn != null)
            {
                using (Transaction tran = new Transaction(activeDoc.Document, "Match properties"))
                {
                    tran.Start();

                    // Do the properties match.
                    Schema masterSchema = GetSchema(activeDoc.Document, srcConn);
                    Entity masterEnt    = srcConn.GetEntity(masterSchema);

                    // You could also access and modify the connection parameters.
                    IList <Field> fields = masterSchema.ListFields();
                    foreach (Field field in fields)
                    {
                        if (field.ValueType == typeof(string))
                        {
                            IList <string> parameters = masterEnt.Get <IList <string> >(field);
                            foreach (string str in parameters)
                            {
                                // Do something.
                            }
                        }
                    }

                    destConn.SetEntity(masterEnt);

                    TransactionStatus ts = tran.Commit();
                    if (ts != TransactionStatus.Committed)
                    {
                        message = "Failed to commit the current transaction !";
                        ret     = Result.Failed;
                    }
                }
            }
            else
            {
                message = "There must be two connections selected !";
                ret     = Result.Failed;
            }


            return(ret);
        }
Exemple #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, 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);
        }
Exemple #8
0
        /// <summary>
        /// Structural connection selection function. It uses a customizable filter, so the user can only select connections
        /// </summary>
        public static StructuralConnectionHandler SelectConnection(UIDocument document)
        {
            StructuralConnectionHandler conn = null;
            // Create a filter for structural connections.
            LogicalOrFilter types = new LogicalOrFilter(new List <ElementFilter> {
                new ElementCategoryFilter(BuiltInCategory.OST_StructConnections)
            });
            StructuralConnectionSelectionFilter filter = new StructuralConnectionSelectionFilter(types);
            Reference target = document.Selection.PickObject(ObjectType.Element, filter, "Select connection element :");

            if (target != null)
            {
                Element targetElement = document.Document.GetElement(target);
                if (targetElement != null)
                {
                    conn = targetElement as StructuralConnectionHandler;
                }
            }
            return(conn);
        }
Exemple #9
0
        /// <summary>
        /// Read information from generic structural connection.
        /// </summary>
        /// <param name="activeDoc">The active document.</param>
        /// <param name="message">Set message on failure.</param>
        /// <returns>Returns the status of the operation.</returns>
        public static Result ReadGenericStructuralConnection(UIDocument activeDoc, ref string message)
        {
            Result ret = Result.Succeeded;

            // Select structural connection.
            StructuralConnectionHandler conn = StructuralConnectionSelectionUtils.SelectConnection(activeDoc);

            if (conn != null)
            {
                // Get information from structural connection.
                StringBuilder msgBuilder = new StringBuilder();
                msgBuilder.AppendLine(string.Format("Connection id : {0}", conn.Id));

                StructuralConnectionHandlerType connType = activeDoc.Document.GetElement(conn.GetTypeId()) as StructuralConnectionHandlerType;
                if (connType != null)
                {
                    msgBuilder.AppendLine(string.Format("Type : {0}", connType.Name));
                }

                msgBuilder.Append("Connected elements ids : ");
                IList <ElementId> connectedElemIds = conn.GetConnectedElementIds();
                foreach (var connId in connectedElemIds)
                {
                    msgBuilder.Append(connId.ToString());
                    if (connId != connectedElemIds.Last())
                    {
                        msgBuilder.Append(", ");
                    }
                }
                TaskDialog.Show("Info", msgBuilder.ToString());
            }
            else
            {
                message = "There is no connection selected !";
                ret     = Result.Failed;
            }

            return(ret);
        }
Exemple #10
0
        /// <summary>
        /// Reset detailed structural connection type to generic.
        /// </summary>
        /// <param name="activeDoc">The active document.</param>
        /// <param name="message">Set message on failure.</param>
        /// <returns>Returns the status of the operation.</returns>
        public static Result ResetDetailedStructuralConnection(UIDocument activeDoc, ref string message)
        {
            Result ret = Result.Succeeded;

            // Prompt to select a structural connection.

            StructuralConnectionHandler conn = StructuralConnectionSelectionUtils.SelectConnection(activeDoc);

            if (conn != null)
            {
                using (Transaction tran = new Transaction(activeDoc.Document, "Change detailed connection type"))
                {
                    tran.Start();

                    ElementId genericTypeId = StructuralConnectionHandlerType.GetDefaultConnectionHandlerType(activeDoc.Document);
                    if (genericTypeId == ElementId.InvalidElementId)
                    {
                        genericTypeId = StructuralConnectionHandlerType.CreateDefaultStructuralConnectionHandlerType(activeDoc.Document);
                    }

                    conn.ChangeTypeId(genericTypeId);

                    TransactionStatus ts = tran.Commit();
                    if (ts != TransactionStatus.Committed)
                    {
                        message = "Failed to commit the current transaction !";
                        ret     = Result.Failed;
                    }
                }
            }
            else
            {
                message = "There is no connection selected !";
                ret     = Result.Failed;
            }

            return(ret);;
        }
Exemple #11
0
        /// <summary>
        /// Get the unique identifier of the structural steel connection type
        /// </summary>
        /// <param name="conn">structural connection</param>
        /// <param name="doc">current document</param>
        /// <returns>returns the unique identifier of the input connection type</returns>
        private static Guid GetConnectionHandlerTypeGuid(StructuralConnectionHandler conn, Document doc)
        {
            if (conn == null || doc == null)
            {
                return(Guid.Empty);
            }

            ElementId typeId = conn.GetTypeId();

            if (typeId == ElementId.InvalidElementId)
            {
                return(Guid.Empty);
            }

            StructuralConnectionHandlerType connType = (StructuralConnectionHandlerType)doc.GetElement(typeId);

            if (connType == null || connType.ConnectionGuid == null)
            {
                return(Guid.Empty);
            }

            return(connType.ConnectionGuid);
        }
Exemple #12
0
        /// <summary>
        /// Change detailed structural connection.
        /// </summary>
        /// <param name="activeDoc">The active document.</param>
        /// <param name="message">Set message on failure.</param>
        /// <returns>Returns the status of the operation.</returns>
        public static Result ChangeDetailedStructuralConnection(UIDocument activeDoc, ref string message)
        {
            Result ret = Result.Succeeded;

            // Prompt to select a structural connection.
            StructuralConnectionHandler conn = StructuralConnectionSelectionUtils.SelectConnection(activeDoc);

            if (conn != null)
            {
                using (Transaction tran = new Transaction(activeDoc.Document, "Change detailed connection type"))
                {
                    tran.Start();

                    // The type is from the SteelConnectionsData.xml file.
                    StructuralConnectionHandlerType connectionType = StructuralConnectionHandlerType.Create(activeDoc.Document, "shearplatenew", new Guid("B490A703-5B6D-4B7A-8471-752133527925"), "shearplatenew");
                    if (connectionType != null)
                    {
                        // The replacement type should be valid on the connected elements.
                        conn.ChangeTypeId(connectionType.Id);
                    }

                    TransactionStatus ts = tran.Commit();
                    if (ts != TransactionStatus.Committed)
                    {
                        message = "Failed to commit the current transaction !";
                        ret     = Result.Failed;
                    }
                }
            }
            else
            {
                message = "There is no connection selected!";
                ret     = Result.Failed;
            }

            return(ret);
        }
Exemple #13
0
        /// <summary>
        /// Create detailed structural connection.
        /// </summary>
        /// <param name="activeDoc">The active document.</param>
        /// <param name="message">Set message on failure.</param>
        /// <returns>Returns the status of the operation.</returns>
        public static Result CreateDetailedStructuralConnection(UIDocument activeDoc, ref string message)
        {
            Result ret = Result.Succeeded;

            // Selected the elements to be connected.
            List <ElementId> ids = StructuralConnectionSelectionUtils.SelectConnectionElements(activeDoc);

            if (ids.Count() > 0)
            {
                // Start a new transaction.
                using (Transaction transaction = new Transaction(activeDoc.Document, "Create detailed structural connection"))
                {
                    transaction.Start();

                    // The type is from the SteelConnectionsData.xml file.
                    StructuralConnectionHandlerType connectionType = StructuralConnectionHandlerType.Create(activeDoc.Document, "usclipangle", new Guid("A42C5CE5-91C5-47E4-B445-D053E5BD66DB"), "usclipangle");
                    if (connectionType != null)
                    {
                        StructuralConnectionHandler.Create(activeDoc.Document, ids, connectionType.Id);
                    }

                    TransactionStatus ts = transaction.Commit();
                    if (ts != TransactionStatus.Committed)
                    {
                        message = "Failed to commit the current transaction !";
                        ret     = Result.Failed;
                    }
                }
            }
            else
            {
                message = "There is no element selected!";
                ret     = Result.Failed;
            }

            return(ret);
        }
Exemple #14
0
        /// <summary>
        /// Update generic structural connection.
        /// </summary>
        /// <param name="activeDoc">The active document.</param>
        /// <param name="message">Set message on failure.</param>
        /// <returns>Returns the status of the operation.</returns>
        public static Result UpdateGenericStructuralConnection(UIDocument activeDoc, ref string message)
        {
            Result ret = Result.Succeeded;

            // Prompt to select a structural connection.
            StructuralConnectionHandler conn = StructuralConnectionSelectionUtils.SelectConnection(activeDoc);

            if (conn != null)
            {
                // Select elements to add to connection.
                List <ElementId> ids = StructuralConnectionSelectionUtils.SelectConnectionElements(activeDoc);
                if (ids.Count() > 0)
                {
                    // Start a new transaction.
                    using (Transaction transaction = new Transaction(activeDoc.Document, "Update generic structural connection"))
                    {
                        transaction.Start();

                        conn.AddElementIds(ids);

                        TransactionStatus ts = transaction.Commit();
                        if (ts != TransactionStatus.Committed)
                        {
                            message = "Failed to commit the current transaction !";
                            ret     = Result.Failed;
                        }
                    }
                }
                else
                {
                    message = "There are no connection input elements selected !";
                }
            }

            return(ret);
        }
        /// <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);
        }
Exemple #16
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);
        }
Exemple #17
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);
        }
        /// <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);
        }