I'm still getting error in Revit 2023; "Object Reference not set to an instance of an object"
Please help me to fix the code.
<Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)>
Public Class AddDimToAssemblies
Implements Autodesk.Revit.UI.IExternalCommand
Private _uiApp As UIApplication
Private _uiDoc As UIDocument
Private _doc As Document
Public Function Execute(commandData As ExternalCommandData, ByRef message As String, elements As ElementSet) As Result Implements IExternalCommand.Execute
_uiApp = commandData.Application
_uiDoc = _uiApp.ActiveUIDocument
_doc = _uiDoc.Document
Using t As New Transaction(_doc, "Add dimensions to assemblies")
t.Start()
' Select an assembly instance in Revit
Dim assemblyInstance As AssemblyInstance = SelectAssemblyInstance(_uiDoc)
' Get the active view
Dim activeView As View = _doc.ActiveView
' Get the geometry of the assembly instance
Dim assemblyGeometry As GeometryElement = assemblyInstance.Geometry(New Options())
' Define the offset of the dimensions from the assembly geometry edge
Dim offset As Double = GetUserOffset()
' Add dimensions to the assembly in the active view
AddDimensionsToView(activeView, assemblyGeometry, offset)
t.Commit()
End Using
Return Result.Succeeded
End Function
Private Function SelectAssemblyInstance(_uiDoc As UIDocument) As AssemblyInstance
' Create a reference to pick an element in Revit
Dim ref As Reference = _uiDoc.Selection.PickObject(ObjectType.Element, "Select an assembly instance")
' Get the element from the reference
Dim elem As Element = _uiDoc.Document.GetElement(ref)
If elem Is Nothing Then
Throw New Exception("No assembly instance selected")
End If
' Check if the element is an assembly instance
If TypeOf elem Is AssemblyInstance Then
Return CType(elem, AssemblyInstance)
Else
Throw New Exception("Selected element is not an assembly instance")
End If
End Function
Private Function GetUserOffset() As Double
' Prompt the user for the offset distance
Dim offset As Double = 0
Dim result As Boolean = Double.TryParse(InputBox("Enter the offset distance for the dimensions:", "Offset Distance"), offset)
' Check if the user entered a valid number
If Not result Then
Throw New Exception("Invalid offset distance")
End If
Return offset
End Function
Private Sub AddDimensionsToView(view As View, geometry As GeometryElement, offset As Double)
' Create a dimension style for the dimensions
Dim dimensionStyle As DimensionType = New FilteredElementCollector(view.Document).OfClass(GetType(DimensionType)).Cast(Of DimensionType)().FirstOrDefault()
Try
If dimensionStyle Is Nothing Then
Throw New Exception("No dimension style found in document")
End If
MessageBox.Show(dimensionStyle.Name.ToString)
' Loop through the geometry of the assembly instance
For Each geomObj As GeometryObject In geometry
If TypeOf geomObj Is Solid Then
Dim solid As Solid = CType(geomObj, Solid)
' Loop through the edges of the solid
For Each edge As Edge In solid.Edges
Dim curve As Curve = edge.AsCurve()
'view = _doc.ActiveView
' Create a dimension line for the edge
Dim dimensionLine As Line = curve.CreateOffset(offset, view.ViewDirection)
Dim dimensionPlane As Plane = Plane.CreateByNormalAndOrigin(view.ViewDirection, dimensionLine.GetEndPoint(0))
' Create a dimension object between two reference points
Dim ref1 As Reference = edge.Reference
Dim ref2 As Reference = ref1 ' Use the same reference to create a linear dimension
Dim references As ReferenceArray = New ReferenceArray()
references.Append(ref1)
references.Append(ref2)
' Create a dimension curve between the two references
Dim dimensionCurve As Line = Line.CreateBound(ref1.GlobalPoint, ref2.GlobalPoint)
' Create the dimension
If _doc Is Nothing Then
Throw New Exception("Document is not initialized")
End If
If view Is Nothing Then
Throw New Exception("View is not initialized")
End If
Dim dimension As Dimension = _doc.Create.NewDimension(view, dimensionCurve, references, dimensionStyle)
' Lock the dimension and add it to the view
dimension.IsLocked = True
' view.AddElement(dimension) 'ERROR
Next
End If
Next
Catch e As Exception
MessageBox.Show(e.Message)
End Try
End Sub
End Class
Public Class AssemblyInstanceSelectionFilter
Implements ISelectionFilter
Public Function AllowElement(ByVal element As Element) As Boolean Implements ISelectionFilter.AllowElement
Return TypeOf element Is AssemblyInstance
End Function
Public Function AllowReference(ByVal reference As Reference, ByVal position As XYZ) As Boolean Implements ISelectionFilter.AllowReference
Return True
End Function
End Class