// // (C) Copyright 2003-2021 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 Autodesk.Revit.DB; using Autodesk.Revit.UI.Events; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Revit.SDK.Samples.SelectionChanged.CS { /// /// This class is used to extend SelectionChangedEventArgs with GetInfo method /// public static class SelectionChangedEventArgsExtension { /// /// Get SelectionChangedEvent information /// /// Event arguments that contains the event data. /// document in which the event occurs. public static string GetInfo(this SelectionChangedEventArgs args, bool showId = false) { StringBuilder sb = new StringBuilder(); sb.AppendLine(); Document doc = args.GetDocument(); sb.AppendLine("[Event] " + GetEventName(args.GetType()) + ": " + TitleNoExt(doc.Title)); IList refs = args.GetReferences(); sb.AppendLine("Selection Count:" + refs.Count); foreach (var aRef in refs) { //foreach (PropertyInfo pi in aRef.GetType().GetProperties()) //{ // Trace.WriteLine(pi.Name + ":" + pi.GetValue(aRef, null)?.ToString()); //} switch (aRef.ElementReferenceType) { case ElementReferenceType.REFERENCE_TYPE_NONE: { sb.Append("The reference is to an element."); if (showId) { sb.AppendFormat(" ElementId:{0}.", aRef.ElementId); if (aRef.LinkedElementId != ElementId.InvalidElementId) { sb.AppendFormat("LinkedElementId:{0}", aRef.LinkedElementId); } } break; } case ElementReferenceType.REFERENCE_TYPE_LINEAR: { sb.Append("The reference is to a curve or edge."); if (showId) { sb.AppendFormat(" ElementId:{0}.", aRef.ElementId); } break; } case ElementReferenceType.REFERENCE_TYPE_SURFACE: { sb.Append("The reference is to a face or face region."); if (showId) { sb.AppendFormat(" ElementId:{0}.", aRef.ElementId); } break; } case ElementReferenceType.REFERENCE_TYPE_FOREIGN: { sb.Append("The reference is to geometry or elements in linked Revit file."); if (showId) { sb.AppendFormat(" LinkedElementId:{0}.", aRef.LinkedElementId); } break; } case ElementReferenceType.REFERENCE_TYPE_INSTANCE: { sb.Append("The reference is an instance of a symbol."); if (showId) { sb.AppendFormat(" ElementId:{0}.", aRef.ElementId); } break; } case ElementReferenceType.REFERENCE_TYPE_CUT_EDGE: { sb.Append("The reference is to a face that was cut."); if (showId) { sb.AppendFormat(" ElementId:{0}.", aRef.ElementId); } break; } case ElementReferenceType.REFERENCE_TYPE_MESH: { sb.Append("The reference is to a mesh."); if (showId) { sb.AppendFormat(" ElementId:{0}.", aRef.ElementId); } break; } case ElementReferenceType.REFERENCE_TYPE_SUBELEMENT: { sb.Append("The reference is to a subelement."); if (showId) { sb.AppendFormat(" ElementId:{0}.", aRef.ElementId); } break; } default: { sb.Append("Unknown reference type."); break; } } Element elem = doc.GetElement(aRef.ElementId); if (elem != null) { sb.AppendFormat(" Name:{0}.", elem.Name); } if (aRef.LinkedElementId != ElementId.InvalidElementId && elem is RevitLinkInstance) { Element linkedElem = ((RevitLinkInstance)elem).GetLinkDocument().GetElement(aRef.LinkedElementId); if (linkedElem != null) { sb.AppendFormat("Linked Element Name:{0}.", linkedElem.Name); } } sb.AppendLine(); } return sb.ToString(); } /// /// Get event name from its EventArgs, without namespace prefix /// /// Generic event arguments type. /// the event name private static string GetEventName(Type type) { String argName = type.ToString(); String tail = "EventArgs"; String head = "Autodesk.Revit.UI.Events."; int firstIndex = head.Length; int length = argName.Length - head.Length - tail.Length; String eventName = argName.Substring(firstIndex, length); return eventName; } /// /// This method will remove the extension of the file name (if it has an extension). /// /// Document.Title will return title of project depends on OS setting: /// If we choose show extension name by IE:Tools\Folder Options, then the title will end with accordingly extension name. /// If we don't show extension, the Document.Title will only return file name without extension. /// /// Origin file name to be revised. /// New file name without extension name. private static string TitleNoExt(String orgTitle) { // return null directly if it's null if (String.IsNullOrEmpty(orgTitle)) { return ""; } // Remove the extension int pos = orgTitle.LastIndexOf('.'); if (-1 != pos) { return orgTitle.Remove(pos); } else { return orgTitle; } } } }