InvalidOperationExcpetion when trying to get Space information from Family Instance using Revit API.

InvalidOperationExcpetion when trying to get Space information from Family Instance using Revit API.

Jyothi.Vemulapalli
Explorer Explorer
892 Views
4 Replies
Message 1 of 5

InvalidOperationExcpetion when trying to get Space information from Family Instance using Revit API.

Jyothi.Vemulapalli
Explorer
Explorer

Not all family instance has Space linked. There few family instance that has Space linked and others not.

Whenever code get family instance with space linked it works fine but when i get family instance which has no space linked that throws "Autodesk.Revit.Exceptions.InvalidOperationException: The target instance does not exist in the given phase". How to handle this?

 

Here is the code sample

 

List<BuiltInCategory> filteredCat = new List<BuiltInCategory> {
                    BuiltInCategory.OST_SpecialityEquipment
                   ,BuiltInCategory.OST_FurnitureSystems
                };
ElementMulticategoryFilter multiFilter = new ElementMulticategoryFilter(filteredCat);
FilteredElementCollector filterCollector = new FilteredElementCollector(doc)
                    .OfClass(typeof(FamilyInstance));
                filterCollector.WherePasses(multiFilter);
foreach (Element ele in filterCollector.ToElements())
{
  ElementId id = ele.GetTypeId();
  FamilySymbol symbol = doc.GetElement(id) as FamilySymbol;
  ParameterMap familyParamMap = symbol.ParametersMap;
  if (familyParamMap != null)
  {
    var familyInstance = ele as FamilyInstance;
    if (familyInstance != null)
    {
        Autodesk.Revit.DB.Mechanical.Space space = familyInstance.Space;
        if (space != null)
        {
          spaceMap = space.ParametersMap;
        }
    }
  }
}
      

 

 

 I am trying to get a value from Space parameter map. If Space is not linked to family instance will that not return null? Are we expected to see this error.

0 Likes
893 Views
4 Replies
Replies (4)
Message 2 of 5

jeremy_tammik
Alumni
Alumni

I do not understand what you code is intended to achieve.

  

However, if a family instance is not explicitly assigned to a specific space, you can use the family instance location point (or curve) or its bounding box to determine which space contains it.

  

Also, note the useful PointInSpace method in this context:

  

https://www.revitapidocs.com/2023/33c97031-a9ad-00d0-4d4a-42522201d2db.htm

  

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

Mohamed_Arshad
Advisor
Advisor

HI@Jyothi.Vemulapalli 

From my understanding, You need to retrieve the Space Information of a particular family instance.
In some cases, we can't able to find the get Family Instance space directly, The Space Property will be null in some cases. so we have to do some other approach to avoid this error.

If Space is not null, you will directly get space information from the space property. If the Space property is null then follow the below steps to avoid the mentioned error.

Kindly follow the steps:

01. Get the center point of the family Instance (Calculate Using Bounding Box)
02. Filter all spaces and check whether the point is in the space.


Refer To the below Code Additional Reference.

 

ParameterMap spaceMap = null;

            List<BuiltInCategory> filteredCat = new List<BuiltInCategory> {
                    BuiltInCategory.OST_ElectricalEquipment
                };
            ElementMulticategoryFilter multiFilter = new ElementMulticategoryFilter(filteredCat);
            FilteredElementCollector filterCollector = new FilteredElementCollector(doc)
                                .OfClass(typeof(FamilyInstance));
            filterCollector.WherePasses(multiFilter);


            foreach (Element ele in filterCollector.ToElements())
            {
                ElementId id = ele.GetTypeId();
                FamilySymbol symbol = doc.GetElement(id) as FamilySymbol;
                ParameterMap familyParamMap = symbol.ParametersMap;
                if (familyParamMap != null)
                {
                    var familyInstance = ele as FamilyInstance;

                    if (familyInstance != null)
                    {
                        Autodesk.Revit.DB.Mechanical.Space space = familyInstance.Space;

                        if (space == null)
                        {
                            //Get Center Point of the Family
                            XYZ centerPoint = GetCenterPoint(doc, familyInstance);

                            //Get Space Parameter of the Center Point
                            spaceMap = GetSpaceParameter(doc, centerPoint);
                        }
                        else
                        {
                            spaceMap = space.ParametersMap;
                        }
                    }
                }

            }


//Methods 

 public XYZ GetCenterPoint(Document doc, FamilyInstance instance)
        {
            BoundingBoxXYZ box = instance.get_BoundingBox(doc.ActiveView);

            XYZ center = (box.Min + box.Max) * 0.5;

            return center;
        }

        public ParameterMap GetSpaceParameter(Document doc, XYZ point)
        {
            ParameterMap parameterMap = null;

            List<Space> spaceList = new FilteredElementCollector(doc)
                .OfClass(typeof(SpatialElement))
                .OfCategory(BuiltInCategory.OST_MEPSpaces)
                .Cast<Space>()
                .ToList();

            foreach (Space space in spaceList)
            {
                if (space.IsPointInSpace(point))
                {
                    parameterMap = space.ParametersMap;
                    break;
                }
            }

            return parameterMap;
        }

 


Hope this will solve your issue



Mohamed Arshad K
Software Developer (CAD & BIM)

0 Likes
Message 4 of 5

Jyothi.Vemulapalli
Explorer
Explorer

Hi @Mohamed_Arshad,

 

Line 23 in my case is throwing error. As the Space attribute is not present for one family instance and is not returning null. The other thing i observed with the model is the familyInstance for which it throws is having different phase when compared to rest. This family instance is being demolished and thus our planner didn't associate space details to it.

 

Let me know if this makes sense.

 

Thanks,

Jyothi

0 Likes
Message 5 of 5

studio-a-int
Advocate
Advocate

You will need to make sure the object you are working with is valid in C#.
Use the .NET method to ensure it's a valid object.
Try something like this:

Autodesk.Revit.DB.Mechanical.Space space = familyInstance.Space;
if (space != null && space?.ParametersMap is object)
{
spaceMap = space.ParametersMap;
}

0 Likes