I think I figured out the issue ... It seems to be working now..
I changed this line
Dim acDict As DBDictionary = DirectCast(acTrans.GetObject(acDB.LayoutDictionaryId, OpenMode.ForRead), DBDictionary)
to this
Dim acDict As DBDictionary = DirectCast(acTrans.GetObject(acDB.NamedObjectsDictionaryId, OpenMode.ForRead), DBDictionary)
and this line
Dim acDict As DBDictionary = DirectCast(acTrans.GetObject(acDB.LayoutDictionaryId, OpenMode.ForWrite), DBDictionary)
to this
Dim acDict As DBDictionary = DirectCast(acTrans.GetObject(acDB.NamedObjectsDictionaryId, OpenMode.ForWrite), DBDictionary)
LayoutDictionary is a vastly different thing than NamedObjectsDictionary.
Keith,
Looks to me that you are trying to cast a Layout to an Xrecord.
lData = DirectCast(acTrans.GetObject(dataID, OpenMode.ForWrite), Xrecord)
I'm pretty sure this can't be done.
added:
personally I wouldn't use try/catch as you have for checking data ... it's designed for catching exceptions
It's not casting a layout ... if you look at the code a little closer, you will see that dataID is supposed to be the ObjectId of an xRecord ... Here is what was happening and how that came to be as it is with the Try/Catch wrapper.
acDict.GetAt(layout) should return the ObjectID of what is supposed to be an xRecord with a name that is passed by the calling function .. the name of a layout in the drawing ... if there is no xRecord existing by that name, acDict.GetAt throws an exception so I put the wrapper in there to create an xRecord by the name that doesn't exist (see rValue = "No Notes for layout: " & layout).
Anyway, it was a bandaid at the time until I read the post by Atook ...
Keith, I can't see the problem directly, but maybe the try/catch is hiding an error. I use something like this to edit/add xrecords. key is a string, rBuff is a ResultBuffer.
if(myDbDictionary.Contains(key))
{
// edit the existing xrecord
var myXRec = (Xrecord) tr.GetObject(myDbDictionary.GetAt(key), OpenMode.ForWrite);
myXRec.Data = rBuff;
}
else
{
// add the xrecord
Xrecord newXRec
= new Xrecord
() {Data
= rBuff
}; myDbDictionary.SetAt(key, newXRec);
tr.AddNewlyCreatedDBObject(newXRec, true);
}
tr.Commit();
I think I put this code together after reading this: http://aucache.autodesk.com/au2012/sessionsFiles/2146/2646/handout_2146_CP2146.pdf
Contains resolves my Try/Catch issue ... much cleaner. I've implemented that now that I have figured out the underlying problem!
Its working like a champ .. except I do have one thing that I am attempting to resolve ...
User creates a drawing with a few layouts ... they add an xRecord to the layout (stored as the layout name) .. then later renames the layout. I can no longer filter for the layout name because it will not find the xRecord even though it exists ...
I moved to xRecords because they are more flexible than xData and you can store larger amounts of information .. but the downside is that I can't attach it to a specific layout tab because xRecords live at the document level. I have currently resorted to saving the xRecords with a name that is the handle of a layout. That should prevent any issues with not being able to find xRecords BUT ... if a layout is deleted I need the xRecord to go away too .. don't want to be polluting drawings with useless crap. I'll leave that up to users AND I want to be sure that if a user renames a layout I'll still be able to pair it up with the xRecord.
Thanks for the help!