PDA

View Full Version : eWasOpenForWrite



cadprog
2008-04-07, 05:27 AM
Hi,

I have some code that works fine. But after I run an AutoCAD command, the code will not work. I get the error 'eWasOpenForWrite'. The reason could be, that AutoCAD has opened the drawing database for write, but did not close. What can be done in my code to tackle this problem?

Thanks

Ed Jobe
2008-04-07, 02:11 PM
Not the database, but a single object. Show your code.

cadprog
2008-04-08, 04:56 AM
Public Sub BX()
Dim bTransMan As DatabaseServices.TransactionManager
Dim bTrans As DatabaseServices.Transaction
Dim bDwg As Document
Dim bBT As BlockTable

Dim bBTR As SymbolTableRecord
Dim bBTE As SymbolTableEnumerator
Dim bBTRr As BlockTableRecord

'Current Document
bDwg = Application.DocumentManager.MdiActiveDocument
bTransMan = bDwg.TransactionManager

bTrans = bTransMan.StartTransaction
Try
'Open the database for Read
bBT = bDwg.Database.BlockTableId.GetObject(OpenMode.ForRead)
bBTE = bBT.GetEnumerator

Dim XrefIds As New ObjectIdCollection
Dim DetXIds As New ObjectIdCollection
Dim DetXId As New ObjectId
Dim ReXIds As New ObjectIdCollection
Dim graphXref As XrefGraph
Dim nodeXref As XrefGraphNode
Dim btrId As Object

graphXref = bDwg.Database.GetHostDwgXrefGraph(True)
For idx As Integer = 1 To graphXref.NumNodes - 1
nodeXref = graphXref.GetXrefNode(idx)
If nodeXref.IsNested Then
Select Case nodeXref.XrefStatus
Case XrefStatus.FileNotFound
btrId = nodeXref.BlockTableRecordId
Dim BTR As BlockTableRecord = bTrans.GetObject(btrId, OpenMode.ForWrite)
BTR.PathName = "C:\1.dwg"
ReXIds.Add(BTR.Id)
Case XrefStatus.Unreferenced
btrId = nodeXref.BlockTableRecordId
Dim BTR As BlockTableRecord = bTrans.GetObject(btrId, OpenMode.ForWrite)
BTR.PathName = "C:\1.dwg"
ReXIds.Add(BTR.Id)
Case XrefStatus.Unloaded
btrId = nodeXref.BlockTableRecordId
Dim BTR As BlockTableRecord = bTrans.GetObject(btrId, OpenMode.ForWrite)
BTR.PathName = "C:\1.dwg"
ReXIds.Add(BTR.Id)
Case XrefStatus.Unresolved
btrId = nodeXref.BlockTableRecordId
Dim BTR As BlockTableRecord = bTrans.GetObject(btrId, OpenMode.ForWrite)
BTR.PathName = "C:\1.dwg"
ReXIds.Add(BTR.Id)
End Select
End If
Next

If ReXIds.Count > 0 Then
bDwg.Database.ReloadXrefs(ReXIds)
End If

While bBTE.MoveNext = True
bBTR = bBTE.Current.GetObject(OpenMode.ForRead)
bBTRr = CType(bBTR, BlockTableRecord)

'Check if block is xref and also resolved and then add its ID to the ObjectIdCollection
If bBTRr.IsFromExternalReference Then
Select Case bBTRr.XrefStatus
Case XrefStatus.Resolved
XrefIds.Add(bBTRr.Id)
Case XrefStatus.Unloaded
DetXIds.Add(bBTRr.Id)
Case XrefStatus.FileNotFound
DetXIds.Add(bBTRr.Id)
Case XrefStatus.Unreferenced
DetXIds.Add(bBTRr.Id)
Case XrefStatus.Unresolved
DetXIds.Add(bBTRr.Id)
End Select
End If

End While

'Bind xref of collection
Try
'We bind all xrefs of drawing
If XrefIds.Count > 0 Then
bDwg.Database.BindXrefs(XrefIds, False)
End If

For Each DetXId In DetXIds
bDwg.Database.DetachXref(DetXId)
Next

Catch ex As Exception
MsgBox("Error2: " & ex.Message) <-----------------The error displayed is from here.
End Try

bTrans.Commit()

Catch ex As Exception
MsgBox("Error1: " & ex.Message)
Finally
bTrans.Dispose()
bTransMan.Dispose()
End Try

End Sub
After I use the scalelist command to reset, it doesn't work. Otherwise, it's fine.

Maxence DELANNOY
2012-04-10, 08:51 AM
Little late, but I think it's because of the line:


Dim DetXId As New ObjectId

should be


Dim DetXId As ObjectId

You should never create an ObjectId.