PickElementsByRectangle() throw System.NullReferenceException

PickElementsByRectangle() throw System.NullReferenceException

denisyukJ
Advocate Advocate
826 Views
10 Replies
Message 1 of 11

PickElementsByRectangle() throw System.NullReferenceException

denisyukJ
Advocate
Advocate

 

Hello,

 

I would like to follow Revit API developer's guide, but MassSelectionFilter throws me System.NullReferenceException and even doesn't give me chance to select elements with rectangle shape. If I get rid of selFilter as PickElementsByRectangle agrument, selection working fine, but obviously don't filter anything.

 

public static IList<Element> GetManyRefByRectangle(UIDocument doc)
{
        ReferenceArray ra = new ReferenceArray();
        ISelectionFilter selFilter = new MassSelectionFilter();
        IList<Element> eList = doc.Selection.PickElementsByRectangle(selFilter, 
                "Select multiple faces") as IList<Element>;
        return eList;
}

public class MassSelectionFilter : ISelectionFilter
{
        public bool AllowElement(Element element)
        {
        if (element.Category.Name == "Mass")
        {
                return true;
        }
        return false;
        }

        public bool AllowReference(Reference refer, XYZ point)
        {
                return false;
        }
}

 

Can some of you give me a hint how to proceed?

 

BR,

Evgeniy Denisyuk

 

0 Likes
827 Views
10 Replies
Replies (10)
Message 2 of 11

jeremy_tammik
Alumni
Alumni

Yes, sure. Welcome to the Revit API. Please start by working through the Revit API getting started material, e.g., the My First Revit Plug-in tutorial:

  

  

Then, all will become clear.

  

Jeremy Tammik Developer Advocacy and Support + The Building Coder + Autodesk Developer Network + ADN Open
0 Likes
Message 3 of 11

rvtquestions
Advocate
Advocate

Depending on the API version you are working to, you can try testing against the Element Id. Note 2024+ ElementId is no longer just a simple int but a 64bit long format.

 

public bool AllowElement(Element e)
{
    return e.Category.Id.IntegerValue.Equals((int)BuiltInCategory.OST_Mass);
    // Again note in 2024+ you will have to handle the Id differently and convert it to an integer on your own since IntegerValue is depreciated. 
}
0 Likes
Message 4 of 11

denisyukJ
Advocate
Advocate

 

Hello, @jeremy_tammik 

 

Thank you for your answer. I already have done that, but unfortunately haven't found any.

 

 

0 Likes
Message 5 of 11

Moustafa_K
Collaborator
Collaborator

the null exception might occur for elements that has no category defined.

In short you need to consider there is a probability the category variable might be null

 

another important aspect. Try as mush as possible to avoid hard coding names literally such as "name== "Mass", 

always verify category by its built in id / Category

 

see the below modified version of your code, hope this helps

public class MassSelectionFilter : ISelectionFilter
{
    public bool AllowElement(Element element)
    {
        if (element.CanBeHidden == null)
            return false;
        if (element.Category.BuiltInCategory == BuiltInCategory.OST_Mass)
        {
            return true;
        }
        return false;
    }

    public bool AllowReference(Reference refer, XYZ point)
    {
        return false;
    }
}

.

 

Moustafa Khalil
Cropped-Sharp-Bim-500x125-Autodesk-1
0 Likes
Message 6 of 11

denisyukJ
Advocate
Advocate

 

Hi, 

 

Thanks for all answers, but non of them is not working for me. I guess that problem is null category as @Moustafa_K suggested, but for some reasons I can not even select elements by rectangle properly because program allows me to pick only one point and throw exception right after that.

 

@Moustafa_K , how I can modify this piece of code because CanBeHidden method contains arg and can not be equal to the null value?

 

        if (element.CanBeHidden == null)
            return false;

 

0 Likes
Message 7 of 11

Moustafa_K
Collaborator
Collaborator

CanbeHidden needs a view as per the documentation, it is a function that requires a a view, to return a bool 

 

thus the statement needs a bit of alter for example

 

 

 

if(element.CanbeHidden(element.Document.ActiveView))
{
    // do something
}

 

 

 

Moustafa Khalil
Cropped-Sharp-Bim-500x125-Autodesk-1
0 Likes
Message 8 of 11

denisyukJ
Advocate
Advocate

 

Can bool value be null?

 

0 Likes
Message 9 of 11

Moustafa_K
Collaborator
Collaborator

no it can't be null, I mean for this function it is no. so you need to check the equality if  equals true or false;

Moustafa Khalil
Cropped-Sharp-Bim-500x125-Autodesk-1
0 Likes
Message 10 of 11

denisyukJ
Advocate
Advocate

Hello, 

 

I healed this by adding small null protection in the beginning, what is still strange for me because exeption throws BEFORE selection.

Any way current filter implementation part looks like this:

 

 

public class MassSelectionFilter : ISelectionFilter
{
        public bool AllowElement(Element element)
        {
        if (element.Category.Name == null)
        {
                return false;
        }
        if (element.Category.Name == "Mass")
        {
                return true;
        }
        return false;
        }

        public bool AllowReference(Reference refer, XYZ point)
        {
                return false;
        }
}

 

 

0 Likes
Message 11 of 11

BKSpurgeon
Collaborator
Collaborator

Check out the attachment / docs of the labs which covers selections which @jeremy_tammik  (is probably) referring to. If you want the source code, check out the labs as Jeremy suggests.

 

But if you want a "quick and dirty" answer check out what I hacked together (untested and no guarantees) - where I want to select family instances.:

 

 

private List<FamilyInstance> selectFamilyInstances()
{
    ISelectionFilter familyInstanceFilter = new SelectionFilterFamilyInstance();

    return app.ActiveUIDocument.Selection
              .PickElementsByRectangle(familyInstanceFilter, "Select all items you want to Dimension")
              .Cast<FamilyInstance>()
              .ToList();
}

class SelectionFilterFamilyInstance : ISelectionFilter
{
    public bool AllowElement(Element e)
    {
        return e is FamilyInstance;
    }

    public bool AllowReference(Reference reference, XYZ position)
    {
        return true;
    }
}

 

 

 

 

 

0 Likes