Can not select any linked element in revit using revit api

Can not select any linked element in revit using revit api

jackmrdoctor
Contributor Contributor
492 Views
2 Replies
Message 1 of 3

Can not select any linked element in revit using revit api

jackmrdoctor
Contributor
Contributor

using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using System;
using System.Collections.Generic;
using System.IO;

[Transaction(TransactionMode.Manual)]
public class FixtureHandOrientationAndLocationCommand : IExternalCommand
{
public Result Execute(
ExternalCommandData commandData,
ref string message,
ElementSet elements)
{
try
{
// Get the active document
Document doc = commandData.Application.ActiveUIDocument.Document;
UIDocument uiDoc = commandData.Application.ActiveUIDocument;

if (uiDoc == null || uiDoc.Selection == null)
{
message = "No active selection available.";
return Result.Failed;
}

// Prompt the user to select linked instances
IList<Reference> pickedRefs = uiDoc.Selection.PickObjects(ObjectType.LinkedElement, new LightingFixtureSelectionFilter(), "Select linked lighting fixture instances");
if (pickedRefs.Count > 0)
{
foreach (Reference pickedRef in pickedRefs)
{
// Get the linked instance
RevitLinkInstance linkInstance = doc.GetElement(pickedRef) as RevitLinkInstance;
if (linkInstance == null)
{
message = "Selected element is not a linked instance.";
return Result.Failed;
}

Document linkedDoc = linkInstance.GetLinkDocument();
if (linkedDoc == null)
{
message = "Failed to access linked document.";
return Result.Failed;
}

ElementId linkedElementId = pickedRef.LinkedElementId;
Element linkedElement = linkedDoc.GetElement(linkedElementId);
if (linkedElement == null || !(linkedElement is FamilyInstance fixture))
{
message = "Selected element is not a linked family instance.";
return Result.Failed;
}

// Check if the fixture has a valid location
LocationPoint fixtureLocation = fixture.Location as LocationPoint;
if (fixtureLocation != null)
{
// Get the center of the fixture
XYZ center = fixtureLocation.Point;



TaskDialog.Show("Fixture Location",

$"Location (XYZ): {center.X:F2}, {center.Y:F2}, {center.Z:F2}");
}
else
{
message = "One or more selected fixtures do not have valid locations.";
return Result.Failed;
}
}
return Result.Succeeded;
}
else
{
message = "No elements selected.";
return Result.Failed;
}
}
catch (Exception ex)
{
message = $"Error: {ex.Message}";
return Result.Failed;
}
}

}
public class LightingFixtureSelectionFilter : ISelectionFilter
{
[Obsolete]
public bool AllowElement(Element elem)
{
if (elem.Category.Name == "Lighting Fixtures")
{

return true;

}
return false;
}


public bool AllowReference(Reference reference, XYZ position)
{
return true;
}
}
i can not select revitlinkinstance that in category "Lighting Fixtures"
if i delete LightingFixtureSelectionFilter() in
IList<Reference> pickedRefs = uiDoc.Selection.PickObjects(ObjectType.LinkedElement, new LightingFixtureSelectionFilter(), "Select linked lighting fixture instances");
it will work find but not what i want. so hard to work with revit link. 
the problem is from here

if (elem.Category.Name == "Lighting Fixtures")
{

return true;

}
seem to me revit can not get inside the if statement. even elem have bunch of element in category "Lighting Fixtures"



0 Likes
Accepted solutions (1)
493 Views
2 Replies
Replies (2)
Message 2 of 3

scgq425
Advocate
Advocate
Accepted solution

Hi jackmrdoctor:

just implention ISelectionFilter , the input element is revitlinkinstance ,  u selection target category u need to in linkdoc not active document , so u can change u code to this  and run again :

```

public bool AllowElement(Element elem)
{
Instance = elem as RevitLinkInstance;
if (Instance != null) return true;

return false;
}

public bool AllowReference(Reference reference, XYZ position)
{
if (Instance == null)
{
return false;
}

var linkdoc = Instance.GetLinkDocument();
var familyInstance = linkdoc.GetElement(reference.LinkedElementId);
if (familyInstance.GetType() == typeof(Floor) || familyInstance.Category.Name == "Beams") return true;

return false;
}

```

LanHui Xu 徐兰辉
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

Blog
LinkedIn
Revit/CAD Development | Steel Product Manager

EESignature

0 Likes
Message 3 of 3

jackmrdoctor
Contributor
Contributor

Thank you so much it works.

0 Likes