Guest User

Untitled

a guest
May 27th, 2025
17
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.16 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Autodesk.Revit.Attributes;
  5. using Autodesk.Revit.DB;
  6. using Autodesk.Revit.UI;
  7. using Autodesk.Revit.UI.Selection;
  8.  
  9. namespace RevitCommands
  10. {
  11. [Transaction(TransactionMode.Manual)]
  12. [Regeneration(RegenerationOption.Manual)]
  13. public class HighlightDoorsInLinkedModel : IExternalCommand
  14. {
  15. public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
  16. {
  17. var uiDoc = commandData.Application.ActiveUIDocument;
  18. var doc = uiDoc.Document;
  19.  
  20. try
  21. {
  22. // Get the currently selected element
  23. var selectedIds = uiDoc.Selection.GetElementIds();
  24.  
  25. if (selectedIds.Count == 0)
  26. {
  27. TaskDialog.Show("Error", "Please select a linked Revit model first.");
  28. return Result.Failed;
  29. }
  30.  
  31. // Find the first RevitLinkInstance in the selection
  32. RevitLinkInstance linkInstance = null;
  33. foreach (var id in selectedIds)
  34. {
  35. var elem = doc.GetElement(id);
  36. if (elem is RevitLinkInstance)
  37. {
  38. linkInstance = elem as RevitLinkInstance;
  39. break;
  40. }
  41. }
  42.  
  43. if (linkInstance == null)
  44. {
  45. TaskDialog.Show("Error", "Selected element is not a linked Revit model.");
  46. return Result.Failed;
  47. }
  48.  
  49. // Get the linked document
  50. var linkedDoc = linkInstance.GetLinkDocument();
  51. if (linkedDoc == null)
  52. {
  53. TaskDialog.Show("Error", "Cannot access the linked document. Make sure it's loaded.");
  54. return Result.Failed;
  55. }
  56.  
  57. // Find all doors in the linked document
  58. var doorCollector = new FilteredElementCollector(linkedDoc)
  59. .OfCategory(BuiltInCategory.OST_Doors)
  60. .WhereElementIsNotElementType();
  61.  
  62. var doors = doorCollector.ToElements();
  63.  
  64. if (!doors.Any())
  65. {
  66. TaskDialog.Show("Info", "No doors found in the linked model.");
  67. return Result.Succeeded;
  68. }
  69.  
  70. // Create references for all doors
  71. var doorReferences = new List<Reference>();
  72.  
  73. foreach (var door in doors)
  74. {
  75. try
  76. {
  77. // Create a reference for the door element
  78. var doorRef = new Reference(door);
  79.  
  80. // Convert the reference to be readable from the current document
  81. var linkedRef = doorRef.CreateLinkReference(linkInstance);
  82.  
  83. if (linkedRef != null)
  84. {
  85. doorReferences.Add(linkedRef);
  86. }
  87. }
  88. catch (Exception ex)
  89. {
  90. // Some elements might not be able to create references
  91. // Continue with the next door
  92. continue;
  93. }
  94. }
  95.  
  96. if (doorReferences.Count > 0)
  97. {
  98. // Highlight all door references
  99. uiDoc.Selection.SetReferences(doorReferences);
  100.  
  101. TaskDialog.Show("Success",
  102. $"Highlighted {doorReferences.Count} doors in the linked model.");
  103. }
  104. else
  105. {
  106. TaskDialog.Show("Warning",
  107. "Found doors but couldn't create references for highlighting.");
  108. }
  109.  
  110. return Result.Succeeded;
  111. }
  112. catch (Exception ex)
  113. {
  114. message = ex.Message;
  115. return Result.Failed;
  116. }
  117. }
  118. }
  119.  
  120. // Alternative version that prompts user to pick a linked model
  121. [Transaction(TransactionMode.Manual)]
  122. [Regeneration(RegenerationOption.Manual)]
  123. public class HighlightDoorsInPickedLinkedModel : IExternalCommand
  124. {
  125. public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
  126. {
  127. var uiDoc = commandData.Application.ActiveUIDocument;
  128. var doc = uiDoc.Document;
  129.  
  130. try
  131. {
  132. // Create a filter for RevitLinkInstance
  133. var linkFilter = new LinkInstanceSelectionFilter();
  134.  
  135. // Prompt user to select a linked model
  136. var pickedRef = uiDoc.Selection.PickObject(
  137. ObjectType.Element,
  138. linkFilter,
  139. "Select a linked Revit model");
  140.  
  141. if (pickedRef == null)
  142. {
  143. return Result.Cancelled;
  144. }
  145.  
  146. // Get the RevitLinkInstance
  147. var linkInstance = doc.GetElement(pickedRef) as RevitLinkInstance;
  148.  
  149. if (linkInstance == null)
  150. {
  151. TaskDialog.Show("Error", "Failed to get the linked model.");
  152. return Result.Failed;
  153. }
  154.  
  155. // Get the linked document
  156. var linkedDoc = linkInstance.GetLinkDocument();
  157. if (linkedDoc == null)
  158. {
  159. TaskDialog.Show("Error", "Cannot access the linked document. Make sure it's loaded.");
  160. return Result.Failed;
  161. }
  162.  
  163. // Find all doors in the linked document
  164. var doorCollector = new FilteredElementCollector(linkedDoc)
  165. .OfCategory(BuiltInCategory.OST_Doors)
  166. .WhereElementIsNotElementType();
  167.  
  168. var doors = doorCollector.ToElements();
  169.  
  170. if (!doors.Any())
  171. {
  172. TaskDialog.Show("Info", "No doors found in the linked model.");
  173. return Result.Succeeded;
  174. }
  175.  
  176. // Create references for all doors
  177. var doorReferences = new List<Reference>();
  178.  
  179. foreach (var door in doors)
  180. {
  181. try
  182. {
  183. // Create a reference for the door element
  184. var doorRef = new Reference(door);
  185.  
  186. // Convert the reference to be readable from the current document
  187. var linkedRef = doorRef.CreateLinkReference(linkInstance);
  188.  
  189. if (linkedRef != null)
  190. {
  191. doorReferences.Add(linkedRef);
  192. }
  193. }
  194. catch (Exception ex)
  195. {
  196. // Some elements might not be able to create references
  197. // Continue with the next door
  198. continue;
  199. }
  200. }
  201.  
  202. if (doorReferences.Count > 0)
  203. {
  204. // Highlight all door references
  205. uiDoc.Selection.SetReferences(doorReferences);
  206.  
  207. TaskDialog.Show("Success",
  208. $"Highlighted {doorReferences.Count} doors in '{linkInstance.Name}'.");
  209. }
  210. else
  211. {
  212. TaskDialog.Show("Warning",
  213. "Found doors but couldn't create references for highlighting.");
  214. }
  215.  
  216. return Result.Succeeded;
  217. }
  218. catch (Autodesk.Revit.Exceptions.OperationCanceledException)
  219. {
  220. return Result.Cancelled;
  221. }
  222. catch (Exception ex)
  223. {
  224. message = ex.Message;
  225. return Result.Failed;
  226. }
  227. }
  228.  
  229. // Selection filter for RevitLinkInstance
  230. private class LinkInstanceSelectionFilter : ISelectionFilter
  231. {
  232. public bool AllowElement(Element elem)
  233. {
  234. return elem is RevitLinkInstance;
  235. }
  236.  
  237. public bool AllowReference(Reference reference, XYZ position)
  238. {
  239. return false;
  240. }
  241. }
  242. }
  243. }
  244.  
Advertisement
Add Comment
Please, Sign In to add comment