Error "Object reference not set to an instance of an object"

Hi Everyone!!!

I try get List LevelName and List GridName from project.
Why sometime get that error, sometime not show.

image

# -*- coding: UTF-8 -*-
import clr
from Autodesk.Revit.DB import *
from Autodesk.Revit.UI import *

clr.AddReference("RevitNodes")
import Revit
clr.ImportExtensions(Revit.GeometryConversion)

clr.AddReference('System.Windows.Forms')
clr.AddReference('System.Drawing')
import System.Drawing
import System.Windows.Forms
from System.Drawing import *
from System.Windows.Forms import *
from System import IntPtr

doc = __revit__.ActiveUIDocument.Document
uidoc = __revit__.ActiveUIDocument

AllGrid = FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_Grids).WhereElementIsNotElementType().ToElements()
GridName = []
for i in AllGrid:
    GridName.append(i.Name)

setGridName = set(GridName)

AllLevel= FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_Levels).WhereElementIsNotElementType().ToElements()

LevelName = []
level_elevation = []

for i in AllLevel:
    LevelName.append(i.Name)
    level_elevation.append((i.Elevation)*304.8)

dic_level = {}

for x,y in zip(LevelName,level_elevation):
    dic_level[x] = y

items = AllGrid
curves = []

def Curve(dimension):
    curve = []
    if dimension.DimensionShape == DimensionShape.Linear:
        direction = dimension.Curve.Direction
        if dimension.NumberOfSegments == 0:
            start = dimension.Origin.Subtract(direction.Multiply(dimension.Value/2))
            end = dimension.Origin.Subtract(direction.Multiply(-dimension.Value/2))
            curve = Line.CreateBound(start,end).ToProtoType()
        else:
            for segment in dimension.Segments:
                start = segment.Origin.Subtract(direction.Multiply(segment.Value/2))
                end = segment.Origin.Subtract(direction.Multiply(-segment.Value/2))
                curve.append(Line.CreateBound(start,end).ToProtoType())
    return curve
    
def getCurve(section):
    #Get the section view from the Section Marker
    if ElementId(BuiltInCategory.OST_Viewers) == section.Category.Id :
        viewSection = doc.GetElement(ElementId(section.Id.IntegerValue + 1))
    elif isinstance(section,Autodesk.Revit.DB.ViewSection):
        viewSection=section
    shape = viewSection.GetCropRegionShapeManager().GetCropShape()
    #Last line of the rectangle
    l = [s for s in shape[0]][3]
    #Replace the Z value by 0
    curve = Line.CreateBound(XYZ(l.GetEndPoint(0).X,l.GetEndPoint(0).Y,0),XYZ(l.GetEndPoint(1).X,l.GetEndPoint(1).Y,0)).ToProtoType()
    return curve
try:
    for item in items:
        doc = item.Document
        view = doc.ActiveView
        #Railing
        # if hasattr(item, "GetPath"):
        #     curves.append([railpath.ToProtoType() for railpath in item.GetPath()])
        #Revision Cloud
        if hasattr(item, "GetSketchCurves"): 
            curves.append([sk.ToProtoType() for sk in item.GetSketchCurves()])
        elif hasattr(item, "LocationCurve"): 
            curves.append(item.LocationCurve.ToProtoType())
        elif hasattr(item, "FullCurve"): 
            curves.append(item.FullCurve.ToProtoType())
        elif hasattr(item, "ValueOverride"):
            curves.append(Curve(item))
        elif hasattr(item, "Curve"): 
            curves.append(item.Curve.ToProtoType())
        elif hasattr(item, "GeometryCurve"):        
            curves.append(item.GeometryCurve.ToProtoType())
        elif hasattr(item, "GetEndpoint"): 
            curves.append(item.ToProtoType())
        elif isinstance(item,Autodesk.Revit.DB.MultiSegmentGrid):
            curves.append([doc.GetElement(i).Curve.ToProtoType() for i in item.GetGridIds()])
        # elif version > 2019 and isinstance(item,Autodesk.Revit.DB.Analysis.PathOfTravel):
        #     curves.append([i.ToProtoType() for i in item.GetCurves()])
        # elif ElementId(BuiltInCategory.OST_StructuralColumns) == item.Category.Id and not item.IsSlantedColumn :
        #     curves.append(colCurve(item))
        elif hasattr(item, "GetCurvesInView"):
            for crv in item.GetCurvesInView(DatumExtentType.ViewSpecific,view):
                curves.append(crv.ToProtoType())
        #Get ViewSection or section marker curve
        elif ElementId(BuiltInCategory.OST_Viewers) == item.Category.Id or isinstance(item,Autodesk.Revit.DB.ViewSection):
            curves.append(getCurve(item))
        elif hasattr(item, "Location"):
            loc = item.Location
            if isinstance(loc,Autodesk.Revit.DB.LocationCurve):
                curves.append(loc.Curve.ToProtoType())
            else:
                curves.append(None)
        else: curves.append(None)
except Exception as e:
    MessageBox.Show(str(e))

Hi everyone

I tried run in Dynamo get this error.

How to fix Geometry.Intersect operation failed.

Thank Advance.

I tried use RevitAPI menthod instead convert to Line in ProtoGeometry
It work fine.

AllGrid = FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_Grids).WhereElementIsNotElementType().ToElements()

S_pointX = []
S_pointY = []

for i in AllGrid:
    S_pointX.append(((i.Curve.GetEndPoint(0))*304.8)[0])
    S_pointY.append(((i.Curve.GetEndPoint(0))*304.8)[1])

image

Thank you

Close this topic