Author Topic: Editing Block Attributes  (Read 2617 times)

0 Members and 1 Guest are viewing this topic.

TJK44

  • Guest
Editing Block Attributes
« on: September 10, 2012, 02:53:39 PM »
Hi,

I'm trying to develop a routine for editing attributes. I want to select a block reference and pull its attributes into a dialog where they can be edited. Then click a save button and the attributes are updated. I'm having an issue with updating the attributes when using an external database (Database.ReadDwgFile). This is how I am cycling through the attributes and assigning a new value to attRef.TextString. The command completes without any errors, but the attributes have not changed... am I missing something?


Code: [Select]
                                Dim db2 As Database = New Database(False, False)
                                If File.Exists(btr.PathName) Then
                                    db2.ReadDwgFile(btr.PathName, FileShare.ReadWrite, False, "")
                                    Using trx2 As Transaction = db2.TransactionManager.StartTransaction()
                                        'HostApplicationServices.WorkingDatabase = db2
                                        Dim xrefBt As BlockTable = trx2.GetObject(db2.BlockTableId, OpenMode.ForWrite)
                                        Dim btrMs2 As BlockTableRecord = trx2.GetObject(xrefBt(BlockTableRecord.ModelSpace), OpenMode.ForWrite)
                                        Dim blkRef2 As BlockReference
                                        Dim bID As ObjectId
                                        For Each id2 As ObjectId In btrMs2
                                            If id2.ObjectClass = RXClass.GetClass(GetType(BlockReference)) Then
                                                bID = id2
                                                blkRef2 = TryCast(trx2.GetObject(id2, OpenMode.ForRead, False), BlockReference)
                                                attRefIds = blkRef2.AttributeCollection
                                                For Each attrefid As ObjectId In attRefIds
                                                    Dim attref As AttributeReference = trx2.GetObject(attrefid, OpenMode.ForRead, False)
                                                    Select Case attref.Tag
                                                        Case "PartDescription"
                                                            partDesc = attref.TextString
                                                        Case "PartNumber"
                                                            partNum = attref.TextString
                                                       
                                                    End Select

                                                Next

                                            End If
                                        Next
                                        Dim frmEditAtts As New frmEditAttributes
                                        If frmEditAtts.ShowDialog() = Windows.Forms.DialogResult.OK Then

                                            Dim bref As BlockReference = TryCast(trx2.GetObject(bID, OpenMode.ForWrite), BlockReference)
                                            For Each attrefid As ObjectId In bref.AttributeCollection
                                                Dim attref As AttributeReference = trx2.GetObject(attrefid, OpenMode.ForWrite)
                                                Select Case attref.Tag
                                                    Case "PartDescription"
                                                        attref.TextString = txtPartDesc.Text
                                                    Case "PartNumber"
                                                        attref.TextString = txtPartNum.Text
                                                End Select
                                            Next
                                            trx2.Commit()
                                        Else
                                            frmEditAtts.Dispose()
                                        End If
                                    End Using
                                End If
                                db2.Dispose()

Thanks,

-Ted

mgreven

  • Guest
Re: Editing Block Attributes
« Reply #1 on: September 10, 2012, 04:52:33 PM »
Hi Ted,

I had a simular problem a while ago.
Are you sure you are getting a valid dialogresult back from the form?


Renards,

Marco

elliottpd

  • Guest
Re: Editing Block Attributes
« Reply #2 on: September 10, 2012, 06:29:20 PM »
Does this code lie within a trx1?  If so, is it committed?  I had an issue like that awhile back. :-D

TJK44

  • Guest
Re: Editing Block Attributes
« Reply #3 on: September 11, 2012, 07:56:18 AM »
Are you sure you are getting a valid dialogresult back from the form?

Yes, I am getting the correct DialogResult from the form.

Does this code lie within a trx1?  If so, is it committed?  I had an issue like that awhile back. :-D

Yes, trx2 is nested within another transaction using statement. This will cause an issue?


MexicanCustard

  • Swamp Rat
  • Posts: 705
Re: Editing Block Attributes
« Reply #4 on: September 11, 2012, 12:33:51 PM »

Yes, trx2 is nested within another transaction using statement. This will cause an issue?

Is that a Transaction from db2? Are you committing that transaction too? You need to be.  Nested transaction only commit if they are committed through the top level transaction.
Revit 2019, AMEP 2019 64bit Win 10

TJK44

  • Guest
Re: Editing Block Attributes
« Reply #5 on: September 11, 2012, 01:04:58 PM »
trx2 is a transaction from db2. I am committing trx2 after I changed all the attref.TextString properties... how would I commit changes for the external database(db2) using a transaction from a different database? Or am I misunderstanding you?

Jeff H

  • Needs a day job
  • Posts: 6151
Re: Editing Block Attributes
« Reply #6 on: September 11, 2012, 01:13:07 PM »
If I understand you correctly are you saving the changes to the database?

TJK44

  • Guest
Re: Editing Block Attributes
« Reply #7 on: September 12, 2012, 08:07:27 AM »
Jeff,

Do you mean performing db2.SaveAs after committing the changes to db2?

MexicanCustard

  • Swamp Rat
  • Posts: 705
Re: Editing Block Attributes
« Reply #8 on: September 12, 2012, 04:15:24 PM »
Jeff,

Do you mean performing db2.SaveAs after committing the changes to db2?

Yes!

You are committing the changes, trx2, but you're calling Dispose on the database before you ever save the changes in the code shown above.
Revit 2019, AMEP 2019 64bit Win 10