- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
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"
Solved! Go to Solution.