Exemplo n.º 1
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);
        }
Exemplo n.º 2
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);
        }
        /// <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);
        }