• Industries
  • Products
  • Buy
  • Services & Support
  • Communities
  • Discussion Groups

    .NET

    Reply
    Mentor
    Posts: 258
    Registered: ‎01-27-2010

    ReloadXrefs Error eWasOpenForRead exception

    122 Views, 4 Replies
    03-14-2012 02:12 AM
    Public Class Start
        <CommandMethod("ProgAR", CommandFlags.Session)> _
        Public Sub ProgAR()
            'initialisation
    .....
    
    
    
    
     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 & "-->"
            Else
                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
    
            Try
                '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
                            CollId.Add(btid)
                            CollIdXref.Add(btid)
                            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
                            Else
                                '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
                        Else
                            OkErreur = True
                            GestionAUTOCAD.EcrireSurLigneDeCommande(strIndex & DefBloc.Name & "-> Impossible à trouver !")
                        End If
                        'Else
                        '    CollIdXref.Add(btid)
                        'End If
                    End If
                Next
                myTrans.Commit()
            Catch ex As Exception
                MsgBox(ex.Message, vbOKOnly & vbCritical, "ee")
            Finally
                myTrans.Dispose()
            End Try
            DocLock.Dispose()
            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é
                        CollIdXref.Remove(Id)
                        'effacement
                        Dim obj As Object = myTrans.GetObject(Id, OpenMode.ForWrite)
                        obj.Erase(True)
                    Next
                    myTrans.Commit()
                    myTrans.Dispose()
                End If
            End If
    
    
            DocLock.Dispose()
            DocLock = Nothing
    
            Return OkRetour
        End Function

     

    i have sometime a exception "eWasOpenForRead" on

    mydb.ReloadXrefs(CollId)

     

    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 ?

     

    Thanks

     

    Please use plain text.
    Distinguished Contributor
    Posts: 372
    Registered: ‎06-27-2005

    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.

     

    -Mark

    Please use plain text.
    Employee
    artc2
    Posts: 120
    Registered: ‎06-08-2010

    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.

    Please use plain text.
    Distinguished Contributor
    Posts: 372
    Registered: ‎06-27-2005

    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.

     

    -Mark

    Please use plain text.
    Mentor
    Posts: 258
    Registered: ‎01-27-2010

    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.

    Please use plain text.