I get an eNullObjectId error. I thought I found the answer here
http://www.theswamp.org/index.php?topic=47128.msg521607#msg521607, but I couldn't get it to work. I also tried to lock the drawing but that didn't help. The file it bombs on has locked viewports in it. For some reason the code edits the viewport size and placement. Bassed on the error message could it be the locked viewports are causing the error?
Original code:
Public Function FindReplace(ldate As String, fn As String, strFind As List(Of String), strReplace As List(Of String))
Dim ed As Autodesk.AutoCAD.EditorInput.Editor = Core.Application.DocumentManager.MdiActiveDocument.Editor,
db As Database = New Database(False, True),
dwgDir As String = IO.Path.GetDirectoryName(fn),
dwgName As String = IO.Path.GetFileName(fn),
dwgfullname As String = dwgDir & "\" & dwgName,
log As New List(Of String)
Using db
Try 'FileOpenMode.OpenForReadAndAllShare
db.ReadDwgFile(fn, FileOpenMode.OpenForReadAndAllShare, False, Nothing)
Catch __unusedException1__ As System.Exception
ed.WriteMessage(vbLf & "Unable to read drawing file.")
End Try
Dim lg As New List(Of String())(),
txtClass = RXObject.GetClass(GetType(DBText)),
mtxtClass = RXObject.GetClass(GetType(MText)),
blkrefClass = RXObject.GetClass(GetType(BlockReference))
Using tr = db.TransactionManager.StartTransaction
Dim loDic As DBDictionary = CType(tr.GetObject(db.LayoutDictionaryId, OpenMode.ForRead, False), DBDictionary)
For Each entry As DBDictionaryEntry In loDic
Dim layout = CType(tr.GetObject(entry.Value, OpenMode.ForRead), Layout)
Dim btr As BlockTableRecord = CType(tr.GetObject(layout.BlockTableRecordId, OpenMode.ForRead), BlockTableRecord)
For Each objID As ObjectId In btr
If objID.ObjectClass.IsDerivedFrom(blkrefClass) Then
Dim blkRef As BlockReference = CType(tr.GetObject(objID, OpenMode.ForRead), BlockReference),
acblkName As String = blkRef.Name, ' Error is here
nbtr As BlockTableRecord = CType(tr.GetObject(blkRef.BlockTableRecord, OpenMode.ForRead), BlockTableRecord),
attCol As AttributeCollection = blkRef.AttributeCollection
If attCol IsNot Nothing Then
For Each attId As ObjectId In attCol
Dim att As AttributeReference = CType(tr.GetObject(attId, OpenMode.ForWrite), AttributeReference),
wksp As String = att.BlockName.ToString,
hand As String = att.Handle.ToString,
val As String = att.TextString,
locX As String = att.Position.X.ToString,
locY As String = att.Position.Y.ToString,
locZ As String = att.Position.Z.ToString
For i = 0 To strFind.Count - 1
If val.Contains(strFind(i)) = True Then
att.TextString = val.Replace(strFind(i), strReplace(i))
att.RecordGraphicsModified(True)
att.AdjustAlignment(db)
log.Add(ldate & "," & att.TextString & "," & val & "," & "Attribute" & "," & dwgName & "," & wksp & "," & hand & "," & locX & "," & locY & "," & locZ)
Dim wbd As Database = HostApplicationServices.WorkingDatabase
HostApplicationServices.WorkingDatabase = db
att.AdjustAlignment(db)
End If
Next
Next
End If
End If
Next
Next
tr.Commit()
End Using
db.SaveAs(fn, DwgVersion.AC1027)
End Using
Return log
End Function
Attempt to fix it:
Public Function FindReplace(ldate As String, fn As String, strFind As List(Of String), strReplace As List(Of String))
' Using docLock As DocumentLock = Core.Application.DocumentManager.MdiActiveDocument.LockDocument()
Dim ed As Autodesk.AutoCAD.EditorInput.Editor = Core.Application.DocumentManager.MdiActiveDocument.Editor,
db As Database = New Database(False, True),
dwgDir As String = IO.Path.GetDirectoryName(fn),
dwgName As String = IO.Path.GetFileName(fn),
dwgfullname As String = dwgDir & "\" & dwgName,
log As New List(Of String)
Using db
Try 'FileOpenMode.OpenForReadAndAllShare
db.ReadDwgFile(fn, FileOpenMode.OpenForReadAndAllShare, False, Nothing)
Catch __unusedException1__ As System.Exception
ed.WriteMessage(vbLf & "Unable to read drawing file.")
End Try
Dim lg As New List(Of String())(),
txtClass = RXObject.GetClass(GetType(DBText)),
mtxtClass = RXObject.GetClass(GetType(MText)),
blkrefClass = RXObject.GetClass(GetType(BlockReference))
Using tr = db.TransactionManager.StartTransaction
Dim loDic As DBDictionary = CType(tr.GetObject(db.LayoutDictionaryId, OpenMode.ForRead, False), DBDictionary)
For Each entry As DBDictionaryEntry In loDic
Dim layout = CType(tr.GetObject(entry.Value, OpenMode.ForRead), Layout)
Dim btr As BlockTableRecord = CType(tr.GetObject(layout.BlockTableRecordId, OpenMode.ForRead), BlockTableRecord)
For Each objID As ObjectId In btr
If objID.ObjectClass.IsDerivedFrom(txtClass) Then
Dim txt As DBText = CType(tr.GetObject(objID, OpenMode.ForWrite), DBText),
wksp As String = txt.BlockName.ToString,
hand As String = txt.Handle.ToString,
val As String = txt.TextString,
locX As String = txt.Position.X.ToString,
locY As String = txt.Position.Y.ToString,
locZ As String = txt.Position.Z.ToString
For i = 0 To strFind.Count - 1
If val.Contains(strFind(i)) Then
txt.TextString = val.Replace(strFind(i), strReplace(i))
txt.RecordGraphicsModified(True)
log.Add(ldate & "," & txt.TextString & "," & val & "," & "DBText" & "," & dwgName & "," & wksp & "," & hand & "," & locX & "," & locY & "," & locZ)
' Need this code in order to insure that the attributes alignment remains
txt.AdjustAlignment(db)
End If
Next
End If
If objID.ObjectClass.IsDerivedFrom(mtxtClass) Then
Dim mtxt As MText = CType(tr.GetObject(objID, OpenMode.ForWrite), MText),
wksp As String = mtxt.BlockName.ToString,
hand As String = mtxt.Handle.ToString,
val As String = mtxt.Contents.ToString,
locX As String = mtxt.Location.X.ToString,
locY As String = mtxt.Location.Y.ToString,
locZ As String = mtxt.Location.Z.ToString
For i = 0 To strFind.Count - 1
If val.Contains(strFind(i)) = True Then
mtxt.Contents = val.Replace(strFind(i), strReplace(i))
mtxt.RecordGraphicsModified(True)
log.Add(ldate & "," & mtxt.Contents & "," & val & "," & "MText" & "," & dwgName & "," & wksp & "," & hand & "," & locX & "," & locY & "," & locZ)
End If
Next
End If
If objID.ObjectClass.IsDerivedFrom(blkrefClass) Then
Dim blkRef As BlockReference = CType(tr.GetObject(objID, OpenMode.ForRead), BlockReference)
If blkRef IsNot Nothing Then
Dim nbtr As BlockTableRecord = CType(tr.GetObject(blkRef.BlockTableRecord, OpenMode.ForRead), BlockTableRecord), ' This is line 430
acblkName As String = blkRef.Name,
attCol As AttributeCollection = blkRef.AttributeCollection
If attCol IsNot Nothing Then
For Each attId As ObjectId In attCol
Dim att As AttributeReference = CType(tr.GetObject(attId, OpenMode.ForWrite), AttributeReference),
wksp As String = att.BlockName.ToString,
hand As String = att.Handle.ToString,
val As String = att.TextString,
locX As String = att.Position.X.ToString,
locY As String = att.Position.Y.ToString,
locZ As String = att.Position.Z.ToString
For i = 0 To strFind.Count - 1
If val.Contains(strFind(i)) = True Then
att.TextString = val.Replace(strFind(i), strReplace(i))
att.RecordGraphicsModified(True)
att.AdjustAlignment(db)
log.Add(ldate & "," & att.TextString & "," & val & "," & "Attribute" & "," & dwgName & "," & wksp & "," & hand & "," & locX & "," & locY & "," & locZ)
Dim wbd As Database = HostApplicationServices.WorkingDatabase
HostApplicationServices.WorkingDatabase = db
att.AdjustAlignment(db)
End If
Next
Next
End If
End If
End If
Next
Next
tr.Commit()
End Using
db.SaveAs(fn, DwgVersion.AC1027)
'db.SaveAs(fn, Autodesk.AutoCAD.DatabaseServices.DwgVersion.Current)
End Using
Return log
' End Using
End Function
Error message:
Application does not support just-in-time (JIT)
debugging. See the end of this message for details.
************** Exception Text **************
Autodesk.AutoCAD.Runtime.Exception: eNullObjectId
at Autodesk.AutoCAD.DatabaseServices.TransactionManager.GetObjectInternal(AcDbTransactionManager* pTM, ObjectId id, OpenMode mode, Boolean openErased, Boolean forceOpenOnLockedLayer)
at Autodesk.AutoCAD.DatabaseServices.Transaction.GetObject(ObjectId id, OpenMode mode)
at Joshua.frmPermianBasinWellSiteFacilityGenerator.FindReplace(String ldate, String fn, List`1 strFind, List`1 strReplace) in E:\Joshua\16APR18\Joshua\frmPermianBasinWellSiteFacilityGenerator.vb:line 430
at Joshua.frmPermianBasinWellSiteFacilityGenerator.cmdGenerate_Click(Object sender, EventArgs e) in E:\Joshua\16APR18\Joshua\frmPermianBasinWellSiteFacilityGenerator.vb:line 231
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)