Revit API Forum
Welcome to Autodesk’s Revit API Forums. Share your knowledge, ask questions, and explore popular Revit API topics.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

C# How to select bar of Rebar

3 REPLIES 3
Reply
Message 1 of 4
huynguyen96str
316 Views, 3 Replies

C# How to select bar of Rebar

Hi everyone! How to select bar of Rebar using C#.

Thank a lot.

Untitled.png

Labels (1)
3 REPLIES 3
Message 2 of 4

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
Message 3 of 4

Thank you very much!

You misunderstood my point.I don't want the user to pick the bar object in the rebar; instead, I want to use code to select that object.

Message 4 of 4

Hi @huynguyen96str 

 

   You have to use FilteredElementCollector. Kindly check the below code snippet for additional reference 

 

 

public void FilterRebarAndSetSelection(UIDocument uidoc)
{
    // Get the Revit Document
    Document doc = uidoc.Document;

    // Create a FilteredElementCollector to collect all Rebar elements
    FilteredElementCollector collector = new FilteredElementCollector(doc);
    
    // Filter only Rebar elements
    ICollection<Element> rebarElements = collector
        .OfClass(typeof(Rebar))
        .ToElements();

    // Convert the rebar elements to a list of ElementIds
    List<ElementId> rebarElementIds = rebarElements.Select(e => e.Id).ToList();

    // Set the current selection to the Rebar elements
    uidoc.Selection.SetElementIds(rebarElementIds);
}

 

call the method inside the Execute Function.

 

Hope this will helps 🙂

 

Thanks & Regards,
Mohamed Arshad K

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report