Ejemplo n.º 1
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);
        }
Ejemplo n.º 2
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);;
        }
Ejemplo n.º 3
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);
        }
Ejemplo n.º 4
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);
        }
Ejemplo n.º 5
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);
        }
Ejemplo n.º 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 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 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);
        }
        /// <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);
        }