Hi @huynguyen96str
Welcome to Revit API Forum Community, I think you're new to Revit API Programming, So kindly check the below link which help you in understanding the basics of Revit API Programming and general procedure.
Revit API for Beginners.
https://thebuildingcoder.typepad.com/blog/2022/02/getting-started-once-again.html
https://www.autodesk.com/support/technical/article/caas/tsarticles/ts/7I2bC1zUr4VjJ3U31uM66K.html
You will get an Idea after seeing this, Then I have added a code snippet for Rebar Selection. You can Implement snippet and check the results.
Rebar Selection Code Snippet
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
#region Revit API Namesapce
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.UI.Selection;
using Autodesk.Revit.DB.Structure;
#endregion
namespace RebarSelection
{
[Transaction(TransactionMode.Manual)]
public class RebarSelection : IExternalCommand
{
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
UIDocument uiDoc = commandData.Application.ActiveUIDocument;
Document doc = uiDoc.Document;
try
{
///Rebar Selection
///Use ObjectType.Element for Complete rebar set Selection
///Use ObjectType.SubElement for single Rebar selection
Reference selRef = uiDoc.Selection.PickObject(ObjectType.Subelement, new SelectionHelper(), "Select Rebar");
if (selRef != null)
{
}
return Result.Succeeded;
}
catch (Exception ex)
{
return Result.Cancelled;
}
}
}
public class SelectionHelper : ISelectionFilter
{
public bool AllowElement(Element elem)
{
if (elem is Rebar)
{
return true;
}
return false;
}
public bool AllowReference(Reference reference, XYZ position)
{
return true;
}
}
}
Hope this will Helps 🙂
Thanks & Regards,
Mohamed Arshad K