// // (C) Copyright 2003-2015 by Autodesk, Inc. // // Permission to use, copy, modify, and distribute this software in // object code form for any purpose and without fee is hereby granted, // provided that the above copyright notice appears in all copies and // that both that copyright notice and the limited warranty and // restricted rights notice below appear in all supporting // documentation. // // AUTODESK PROVIDES THIS PROGRAM "AS IS" AND WITH ALL FAULTS. // AUTODESK SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTY OF // MERCHANTABILITY OR FITNESS FOR A PARTICULAR USE. AUTODESK, INC. // DOES NOT WARRANT THAT THE OPERATION OF THE PROGRAM WILL BE // UNINTERRUPTED OR ERROR FREE. // // Use, duplication, or disclosure by the U.S. Government is subject to // restrictions set forth in FAR 52.227-19 (Commercial Computer // Software - Restricted Rights) and DFAR 252.227-7013(c)(1)(ii) // (Rights in Technical Data and Computer Software), as applicable. // using System; using System.Collections.Generic; using System.Text; using System.Linq; using Autodesk.Revit; using Autodesk.Revit.ApplicationServices; using Autodesk.Revit.DB; using Autodesk.Revit.DB.Structure; using Autodesk.Revit.UI; using Autodesk.Revit.UI.Selection; using Autodesk.Revit.DB.ExtensibleStorage; namespace Revit.SDK.Samples.GenericStructuralConnection.CS { /// /// Performs basic operations on detailed structural connections. /// public class DetailedStructuralConnectionOps { /// /// Create detailed structural connection. /// /// The active document. /// Set message on failure. /// Returns the status of the operation. public static Result CreateDetailedStructuralConnection(UIDocument activeDoc, ref string message) { Result ret = Result.Succeeded; // Selected the elements to be connected. List 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; } /// /// Change detailed structural connection. /// /// The active document. /// Set message on failure. /// Returns the status of the operation. 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; } /// /// Copy detailed structural connection. /// /// The active document. /// Set message on failure. /// Returns the status of the operation. public static Result CopyDetailedStructuralConnection(UIDocument activeDoc, ref string message) { Result ret = Result.Succeeded; // Select a connection and the connected elements. List ids = activeDoc.Selection.GetElementIds().ToList(); if (ids.Count() > 0) { // Create transform Transform transform = Transform.CreateTranslation(new XYZ(0, 20, 0)); // Copy selection using (Transaction tran = new Transaction(activeDoc.Document, "Copy elements")) { tran.Start(); ICollection copyResult = ElementTransformUtils.CopyElements(activeDoc.Document, ids, activeDoc.Document, transform, null); 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; } /// /// Match properties for detailed structural connections. /// /// The active document. /// Set message on failure. /// Returns the status of the operation. 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 primarySchema = GetSchema(activeDoc.Document, srcConn); Entity primaryEnt = srcConn.GetEntity(primarySchema); // You could also access and modify the connection parameters. IList fields = primarySchema.ListFields(); foreach (Field field in fields) { if (field.ValueType == typeof(string)) { IList parameters = primaryEnt.Get>(field); foreach (string str in parameters) { // Do something. } } } destConn.SetEntity(primaryEnt); 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; } /// /// Reset detailed structural connection type to generic. /// /// The active document. /// Set message on failure. /// Returns the status of the operation. 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; ; } /// /// Get the Extensible storage schema /// private static Schema GetSchema(Document doc, StructuralConnectionHandler connection) { Schema schema = null; Guid guid = GetConnectionHandlerTypeGuid(connection, doc); if (guid != Guid.Empty) schema = Schema.ListSchemas().Where(x => x.GUID == guid).FirstOrDefault(); return schema; } /// /// Get the unique identifier of the structural steel connection type /// /// structural connection /// current document /// returns the unique identifier of the input connection type 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) return Guid.Empty; return connType.ConnectionGuid; } } }