Author Topic: Storing Dictionaries/Xrecords in Entities  (Read 3399 times)

0 Members and 1 Guest are viewing this topic.

murrdpirate

  • Guest
Storing Dictionaries/Xrecords in Entities
« on: June 25, 2010, 07:25:49 PM »
I got some sample code on Dictionaries and Xrecords (as well as Xdata) from here: http://au.autodesk.com/?nd=class&session_id=5418

The first command method works find, but the second one (adding child dictionaries) gives me problems.  It will add the child dictionary to the entity, and I can see it when I use a command to view it, but I can't save the drawing.  I get this error message:  "One or more objects in this drawing cannot be saved to the specified format.  The operation was not completed and no file was created."

Anyone have any ideas?

Code: [Select]
<CommandMethod("AddExtDictionaryToSel")> _
    Public Sub AddExtDictionaryToSel()
        Dim myDWG As Document = DocumentManager.MdiActiveDocument
        Dim selEntID As ObjectId = myDWG.Editor.GetEntity("Select:").ObjectId
        Dim myResBuff As New ResultBuffer(New TypedValue(DxfCode.Text, "testing"))
        myResBuff.Add(New TypedValue(DxfCode.Text, "testing2"))
        AddToExtDictionary(selEntID, "AU2009_Store", myResBuff)
    End Sub


    <CommandMethod("AddDictionaryToSel")> _
    Public Sub AddDictionaryToSel()
        Dim myDWG As Document = DocumentManager.MdiActiveDocument
        Dim selEntID As ObjectId = myDWG.Editor.GetEntity("Select:").ObjectId
        AddToExtDictionary(selEntID, "AU2009_Store", "AU2009_XRecord", New ResultBuffer(New TypedValue(DxfCode.Text, "cool"), New TypedValue(DxfCode.Text, "cool2")))
    End Sub
 
    Sub AddToExtDictionary(ByVal EntityID As ObjectId, ByVal EntryName As String, ByVal EntryData As ResultBuffer)
        Using myTrans As Transaction = EntityID.Database.TransactionManager.StartTransaction
            Dim myDBObject As DBObject = EntityID.GetObject(OpenMode.ForWrite)
            Dim myXRecord As New Xrecord
            myXRecord.Data = EntryData
            Try
                myDBObject.CreateExtensionDictionary()
            Catch
            End Try
            Dim myDBDictionary As DBDictionary = myDBObject.ExtensionDictionary.GetObject(OpenMode.ForWrite)
            myDBDictionary.SetAt(EntryName, myXRecord)
            myTrans.AddNewlyCreatedDBObject(myXRecord, True)
            myTrans.Commit()
        End Using
    End Sub

    Sub AddToExtDictionary(ByVal EntityID As ObjectId, ByVal DictionaryName As String, ByVal XRecordName As String, ByVal EntryData As ResultBuffer)
        Using myTrans As Transaction = EntityID.Database.TransactionManager.StartTransaction
            Dim myDBObject As DBObject = EntityID.GetObject(OpenMode.ForWrite)
            Try
                myDBObject.CreateExtensionDictionary()
            Catch
            End Try
            Dim myDBDictionary As DBDictionary = myDBObject.ExtensionDictionary.GetObject(OpenMode.ForWrite)

            Try
                myDBDictionary.CreateExtensionDictionary()
            Catch
            End Try

            Dim myChildDictionary As DBDictionary
            Try
                myChildDictionary = myDBDictionary.GetAt(DictionaryName).GetObject(OpenMode.ForWrite)
            Catch ex As System.Exception
                myChildDictionary = New DBDictionary
                myDBDictionary.SetAt(DictionaryName, myChildDictionary)
            End Try
            Try
                myChildDictionary.CreateExtensionDictionary()
            Catch
            End Try
            Dim myXRecord As New Xrecord
            myXRecord.Data = EntryData
            myChildDictionary.SetAt(XRecordName, myXRecord)
            myTrans.AddNewlyCreatedDBObject(myXRecord, True)
            myTrans.Commit()
        End Using
    End Sub

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Storing Dictionaries/Xrecords in Entities
« Reply #1 on: June 25, 2010, 09:32:25 PM »

Did the original code work ??

if so, what is different ?
if not ....
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

murrdpirate

  • Guest
Re: Storing Dictionaries/Xrecords in Entities
« Reply #2 on: June 26, 2010, 01:40:40 AM »
Oh, the code I posted is directly from the sample I linked to, just didn't want to force people to download it.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Storing Dictionaries/Xrecords in Entities
« Reply #3 on: June 26, 2010, 02:00:26 AM »
That code was presented at AU last year .. are you saying it's faulty  :|

have you asked the author ... he has a web site and an email address.

I'm not being nasty, just don't feel like looking at VB code ay the moment :)
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Storing Dictionaries/Xrecords in Entities
« Reply #4 on: June 26, 2010, 02:29:11 AM »
Just a couple of things someone will be sure to ask :)


What version is the original file ?
What version are you saving to ?
What Version and type (vertical) of AutoCAD are you using ?
Are you saving programmatically or manually ?
Are you saving to a Network ?
Are you saving to a USB stick ?

Have you Audited the file ?

kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

murrdpirate

  • Guest
Re: Storing Dictionaries/Xrecords in Entities
« Reply #5 on: June 26, 2010, 04:00:43 AM »
Haha, I'm definitely not accusing the presenter as giving faulty code.  If it's between me and any other person on who did something wrong with autocad/.net, the smart money is on me.  And you have shown that to be true...auditing the drawing does indeed allow me to save.  Is that a version thing, as you hinted?  Any way to correct that?

  • The original file says "created with AutoCAD 2010," but it started as a file that we got from an architect and got edited and saved again using AutoCAD 2010...so I suppose it could have originally been created with a difference version.
  • Saving as AutoCAD 2010 and using AutoCAD 2010, regular type.
  • Saving manually.
  • Saving to hard drive.

Thanks, you pointed me in the right direction without even looking at the code!  I'll do some googling in the morning and post what I find.  If you happen to know of something off hand, that's always appreciated.  :-)