Valued Mentor
Posts: 394
Registered: ‎01-27-2010
Message 1 of 5 (243 Views)

ReloadXrefs Error eWasOpenForRead exception

243 Views, 4 Replies
03-14-2012 02:12 AM
Public Class Start
    <CommandMethod("ProgAR", CommandFlags.Session)> _
    Public Sub ProgAR()

 Public Shared Function MaJXRefDansTable(ByRef OkErreur As Boolean, _
                                            ByRef CollIdXref As ObjectIdCollection, _
                                            ByRef strIndex As String) As Boolean
        Dim OkRetour As Boolean = True
        If strIndex = "" Then
            strIndex = vbCrLf & "-->"
            strIndex = strIndex & "-->"
        End If
        'collection des ID pour vérifiation purge
        Dim CollId As New ObjectIdCollection
        'Recherche de tous les xref du desssin.
        Dim mydb As Database = Application.DocumentManager.MdiActiveDocument.Database '= HostApplicationServices.WorkingDatabase
        Dim myTrans As Transaction = mydb.TransactionManager.StartTransaction
        'verrouillage du doc car modification !
        Dim doc As Document = Application.DocumentManager.MdiActiveDocument
        Dim DocLock As DocumentLock = doc.LockDocument

            'acces à la base des blocs
            Dim BlocTable As BlockTable = CType(myTrans.GetObject(mydb.BlockTableId, OpenMode.ForRead), BlockTable)
            For Each btid As ObjectId In BlocTable
                Dim DefBloc As BlockTableRecord = CType(myTrans.GetObject(btid, OpenMode.ForWrite), BlockTableRecord)
                'Si c'est une Xref non résolu
                If DefBloc.IsFromExternalReference = True Then
                    'If DefBloc.IsResolved = False Then
                    'cherche un path
                    Dim Txt As String = GestionFichier.MiseAJourIndice(DefBloc.PathName, True)
                    If (Txt <> "") Then
                        If (Txt.ToUpper <> DefBloc.PathName.ToUpper) Then
                            GestionAUTOCAD.EcrireSurLigneDeCommande(strIndex & DefBloc.Name & "-> " & "MaJ !")
                            DefBloc.PathName = Txt
                            'si MaJ alors sortie et relancer la procedure car si Xref Remise à jour
                            'la base globale est remise à jour de plus si un Xref dans Xref est en Supperposer
                            '(DefBloc.IsFromOverlayReference) il sera toujours dans la base
                            'donc il faut purger la base !
                            OkRetour = False
                            'si path non changer mem de l'id pour vérifier si celui ci peut être purgé
                            'dans le cas ou l'Xref de l'Xref a été mis en Supperposé
                        End If
                        OkErreur = True
                        GestionAUTOCAD.EcrireSurLigneDeCommande(strIndex & DefBloc.Name & "-> Impossible à trouver !")
                    End If
                    '    CollIdXref.Add(btid)
                    'End If
                End If
        Catch ex As Exception
            MsgBox(ex.Message, vbOKOnly & vbCritical, "ee")
        End Try
        DocLock = Nothing

        DocLock = doc.LockDocument
        'regen du xref
        If CollId.Count > 0 Then
            mydb.ReloadXrefs(CollId) 'ne pas oublié le commit
            'la fonction purge permet de savoir si les ID restant dans la collection
            'peuvent être purgés
            mydb.Purge(CollId) 'ne pas oublié le commit
            If CollId.Count > 0 Then
                myTrans = mydb.TransactionManager.StartTransaction
                'purge des éléments restants
                For Each Id As ObjectId In CollId
                    'sup des id xref car il va être éffacé
                    Dim obj As Object = myTrans.GetObject(Id, OpenMode.ForWrite)
            End If
        End If

        DocLock = Nothing

        Return OkRetour
    End Function


i have sometime a exception "eWasOpenForRead" on



The Xref was not on layer locked

but on another dwg it work fine..

I have not find a solution on internet. Could you help me please ?




Posts: 375
Registered: ‎06-27-2005
Message 2 of 5 (226 Views)

Re: ReloadXrefs Error eWasOpenForRead exception

03-14-2012 07:56 AM in reply to: AubelecBE

At a glance, I would say the error is because you open your blockTable ForRead, but your BlockTableRecord ForWrite.  If you are going to perform database operations on your blocktablerecord (which resides in the blocktable), you need to open the blocktable ForWrite.



Posts: 225
Registered: ‎06-08-2010
Message 3 of 5 (224 Views)

Re: ReloadXrefs Error eWasOpenForRead exception

03-14-2012 08:30 AM in reply to: MarkPendergraft

That's not true.  You don't need the blockTable open for write unless you are actually going to do something that makes changes to the blockTable itself.

Posts: 375
Registered: ‎06-27-2005
Message 4 of 5 (217 Views)

Re: ReloadXrefs Error eWasOpenForRead exception

03-14-2012 08:59 AM in reply to: artc2

I stand corrected.   Thank you for pointing that out to me, I was under the impression that the table needed to be open forwrite.  Back to the problem at hand.  I ran the OP's code in about 10 drawings with xrefs, and couldn't reproduce the error.



Valued Mentor
Posts: 394
Registered: ‎01-27-2010
Message 5 of 5 (199 Views)

Re: ReloadXrefs Error eWasOpenForRead exception

03-15-2012 05:08 AM in reply to: AubelecBE

hi. so i trying with the open.forwrite i have the same error.


BUT !! i have added a Try Catch statement --> No more error.... the code dont use the catch ex as exception..i have added for debug a msgbox but the msgbox never show... I dont know but work fine no. funny. :smileytongue:



SO i dunno why.. !


Thanks for your help.

Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Are You Going To Be @ AU 2014? Feel free to drop by our AU topic post and share your plans, plug a class that you're teaching, or simply check out who else from the community might be in attendance. Ohh and don't forgot to stop by the Autodesk Help | Learn | Collaborate booths in the Exhibit Hall and meet our community team if you get a chance!