Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using Autodesk.Revit.Attributes;
- using Autodesk.Revit.DB;
- using Autodesk.Revit.UI;
- using Autodesk.Revit.UI.Selection;
- namespace RevitCommands
- {
- [Transaction(TransactionMode.Manual)]
- [Regeneration(RegenerationOption.Manual)]
- public class HighlightDoorsInLinkedModel : IExternalCommand
- {
- public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
- {
- var uiDoc = commandData.Application.ActiveUIDocument;
- var doc = uiDoc.Document;
- try
- {
- // Get the currently selected element
- var selectedIds = uiDoc.Selection.GetElementIds();
- if (selectedIds.Count == 0)
- {
- TaskDialog.Show("Error", "Please select a linked Revit model first.");
- return Result.Failed;
- }
- // Find the first RevitLinkInstance in the selection
- RevitLinkInstance linkInstance = null;
- foreach (var id in selectedIds)
- {
- var elem = doc.GetElement(id);
- if (elem is RevitLinkInstance)
- {
- linkInstance = elem as RevitLinkInstance;
- break;
- }
- }
- if (linkInstance == null)
- {
- TaskDialog.Show("Error", "Selected element is not a linked Revit model.");
- return Result.Failed;
- }
- // Get the linked document
- var linkedDoc = linkInstance.GetLinkDocument();
- if (linkedDoc == null)
- {
- TaskDialog.Show("Error", "Cannot access the linked document. Make sure it's loaded.");
- return Result.Failed;
- }
- // Find all doors in the linked document
- var doorCollector = new FilteredElementCollector(linkedDoc)
- .OfCategory(BuiltInCategory.OST_Doors)
- .WhereElementIsNotElementType();
- var doors = doorCollector.ToElements();
- if (!doors.Any())
- {
- TaskDialog.Show("Info", "No doors found in the linked model.");
- return Result.Succeeded;
- }
- // Create references for all doors
- var doorReferences = new List<Reference>();
- foreach (var door in doors)
- {
- try
- {
- // Create a reference for the door element
- var doorRef = new Reference(door);
- // Convert the reference to be readable from the current document
- var linkedRef = doorRef.CreateLinkReference(linkInstance);
- if (linkedRef != null)
- {
- doorReferences.Add(linkedRef);
- }
- }
- catch (Exception ex)
- {
- // Some elements might not be able to create references
- // Continue with the next door
- continue;
- }
- }
- if (doorReferences.Count > 0)
- {
- // Highlight all door references
- uiDoc.Selection.SetReferences(doorReferences);
- TaskDialog.Show("Success",
- $"Highlighted {doorReferences.Count} doors in the linked model.");
- }
- else
- {
- TaskDialog.Show("Warning",
- "Found doors but couldn't create references for highlighting.");
- }
- return Result.Succeeded;
- }
- catch (Exception ex)
- {
- message = ex.Message;
- return Result.Failed;
- }
- }
- }
- // Alternative version that prompts user to pick a linked model
- [Transaction(TransactionMode.Manual)]
- [Regeneration(RegenerationOption.Manual)]
- public class HighlightDoorsInPickedLinkedModel : IExternalCommand
- {
- public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
- {
- var uiDoc = commandData.Application.ActiveUIDocument;
- var doc = uiDoc.Document;
- try
- {
- // Create a filter for RevitLinkInstance
- var linkFilter = new LinkInstanceSelectionFilter();
- // Prompt user to select a linked model
- var pickedRef = uiDoc.Selection.PickObject(
- ObjectType.Element,
- linkFilter,
- "Select a linked Revit model");
- if (pickedRef == null)
- {
- return Result.Cancelled;
- }
- // Get the RevitLinkInstance
- var linkInstance = doc.GetElement(pickedRef) as RevitLinkInstance;
- if (linkInstance == null)
- {
- TaskDialog.Show("Error", "Failed to get the linked model.");
- return Result.Failed;
- }
- // Get the linked document
- var linkedDoc = linkInstance.GetLinkDocument();
- if (linkedDoc == null)
- {
- TaskDialog.Show("Error", "Cannot access the linked document. Make sure it's loaded.");
- return Result.Failed;
- }
- // Find all doors in the linked document
- var doorCollector = new FilteredElementCollector(linkedDoc)
- .OfCategory(BuiltInCategory.OST_Doors)
- .WhereElementIsNotElementType();
- var doors = doorCollector.ToElements();
- if (!doors.Any())
- {
- TaskDialog.Show("Info", "No doors found in the linked model.");
- return Result.Succeeded;
- }
- // Create references for all doors
- var doorReferences = new List<Reference>();
- foreach (var door in doors)
- {
- try
- {
- // Create a reference for the door element
- var doorRef = new Reference(door);
- // Convert the reference to be readable from the current document
- var linkedRef = doorRef.CreateLinkReference(linkInstance);
- if (linkedRef != null)
- {
- doorReferences.Add(linkedRef);
- }
- }
- catch (Exception ex)
- {
- // Some elements might not be able to create references
- // Continue with the next door
- continue;
- }
- }
- if (doorReferences.Count > 0)
- {
- // Highlight all door references
- uiDoc.Selection.SetReferences(doorReferences);
- TaskDialog.Show("Success",
- $"Highlighted {doorReferences.Count} doors in '{linkInstance.Name}'.");
- }
- else
- {
- TaskDialog.Show("Warning",
- "Found doors but couldn't create references for highlighting.");
- }
- return Result.Succeeded;
- }
- catch (Autodesk.Revit.Exceptions.OperationCanceledException)
- {
- return Result.Cancelled;
- }
- catch (Exception ex)
- {
- message = ex.Message;
- return Result.Failed;
- }
- }
- // Selection filter for RevitLinkInstance
- private class LinkInstanceSelectionFilter : ISelectionFilter
- {
- public bool AllowElement(Element elem)
- {
- return elem is RevitLinkInstance;
- }
- public bool AllowReference(Reference reference, XYZ position)
- {
- return false;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment