Author Topic: Changing Attributes & new errors  (Read 1848 times)

0 Members and 1 Guest are viewing this topic.

flowerrobot

  • Guest
Changing Attributes & new errors
« on: January 08, 2013, 05:30:54 PM »
Hey Guys,

I've been using this piece of code for a year or two, However I recently changed my hard drive, Thus re installed all programs since then I have received several new  errors.

I have since started a new project but have had several issues regarding me existing library features. I have loaded up some older programs & they too have the same issues.

The line "TryCast(blkAttID.GetObject(OpenMode.ForWrite)" does not work any more, Thus I have to implement code that's commented out.
Code: [Select]
<Extension()> Public Function GetAttribute(ByVal BlkRef As BlockReference) As List(Of AttributeReference)
        Dim BlkAttributeNames As New List(Of AttributeReference)
        'Using AcTrans As Transaction = BlkRef.Database.TransactionManager.StartTransaction
        '    For Each blkAttID As ObjectId In BlkRef.AttributeCollection
        '        Dim BlkAttribute As AttributeReference = TryCast(AcTrans.GetObject(blkAttID, OpenMode.Forread), AttributeReference)
        '        If BlkAttribute IsNot Nothing Then
        '            BlkAttributeNames.Add(BlkAttribute)
        '        End If
        '    Next
        '    Return BlkAttributeNames
        'End Using


        For Each blkAttID As ObjectId In BlkRef.AttributeCollection
            Dim BlkAttribute As AttributeReference = TryCast(blkAttID.GetObject(OpenMode.ForWrite), AttributeReference)
            If BlkAttribute IsNot Nothing Then
                BlkAttributeNames.Add(BlkAttribute)
            End If
        Next
        Return BlkAttributeNames

    End Function


This code will no longer "MyAttribute.UpgradeOpen()" will no longer open the entity for write & throws an error, nor does upgrading the parent & putting the value in "MyAttribute.TextString = Value"
Code: [Select]
<Extension()> Public Function Attribute(ByVal BlkRef As BlockReference, ByVal TagName As String, ByVal Value As String) As Boolean
        Using AcTrans As Transaction = BlkRef.Database.TransactionManager.StartTransaction
            Dim MyAttribute As AttributeReference = GetAttribute(BlkRef, TagName)
            If MyAttribute IsNot Nothing Then
                '   BlkRef.UpgradeOpen()
                MyAttribute.UpgradeOpen()
                MyAttribute.TextString = Value
                Return True
            End If
            AcTrans.Commit()
        End Using
        Return Nothing
    End Function

<Extension()> Public Function GetAttribute(ByVal BlkRef As BlockReference, ByVal TagName As String) As AttributeReference
        Dim lResults As List(Of AttributeReference) = GetAttribute(BlkRef)
        For Each BlkAttribute As AttributeReference In lResults
            If BlkAttribute.Tag = TagName Then
                Return BlkAttribute
            End If
        Next
        Return Nothing
    End Function


Do you have any idea's why this code is now failing & more importantly how I can solve the issues for the second part of the code?

Jeff H

  • Needs a day job
  • Posts: 6150
Re: Changing Attributes & new errors
« Reply #1 on: January 08, 2013, 05:53:50 PM »
Have you tried catching the error and outputting it or debbuging and stepping through?
 
 
First thought on throwing error on UpgradeOpen is on locked layer or if erased maybe?

flowerrobot

  • Guest
Re: Changing Attributes & new errors
« Reply #2 on: January 08, 2013, 06:00:28 PM »
Hi Jeff,

sorry should of included that, the error is "Object reference not set to an instance of an object." & "eInvalidOpenState" for the first code & second respectivly.
If I try to change the text of the attribute i get an error or "eNotOpenForWrite"
I was able to identify the line of code giving the error.

Yes, I agree however I am working in a test environment currently so the object is not locked & since I am retrieving it live from its parent it would not be erased (and I know its not)
« Last Edit: January 08, 2013, 06:07:12 PM by flowerrobot »

Jeff H

  • Needs a day job
  • Posts: 6150
Re: Changing Attributes & new errors
« Reply #3 on: January 08, 2013, 06:28:48 PM »
Looks like you were opening the object inside a transaction and now your not.
 
Do you have a outer transaction open so calling GetObject on an ObjectId does not throw an error?

flowerrobot

  • Guest
Re: Changing Attributes & new errors
« Reply #4 on: January 08, 2013, 06:33:12 PM »
Jeff,

The second code has a transaction, However it is not used, It should be the 'upper' transaction for the first code. However the transaction to get the blockreference would be closed.

However whilst I was waiting for a response I tried.
"  MyAttribute = DirectCast(AcTrans.GetObject(MyAttribute.ObjectId, OpenMode.ForWrite), AttributeReference)"

SO I believe you would be correct. However I was trying to keep all my code very modual (not relying on other code or transactions), So I may need to keep the piece of code.

Code: [Select]
<Extension()> Public Function Attribute(ByVal BlkRef As BlockReference, ByVal TagName As String, ByVal Value As String) As Boolean
        Dim doc As Document = Application.DocumentManager.MdiActiveDocument
        Using docloc As DocumentLock = doc.LockDocument

            Using AcTrans As Transaction = BlkRef.Database.TransactionManager.StartTransaction
                Dim MyAttribute As AttributeReference = GetAttribute(BlkRef, TagName)
                If MyAttribute IsNot Nothing Then
                    MyAttribute = DirectCast(AcTrans.GetObject(MyAttribute.ObjectId, OpenMode.ForWrite), AttributeReference)
                    '   BlkRef.UpgradeOpen()
                    'MyAttribute.UpgradeOpen()
                    MyAttribute.TextString = Value
                    Return True
                End If
                AcTrans.Commit()
            End Using
        End Using
        Return Nothing
    End Function

Thanks for your help!
« Last Edit: January 08, 2013, 06:37:10 PM by flowerrobot »