Hi, I have been experimenting with using the dictionaries collection as a means of storing data in a drawing. The following code is creating an error. Dim oDict As AcadDictionary For Each oDict in ThisDrawing.Dictionaries If oDict.Name = "MyDictionary" Then DoSomething End If Next It seems that the Dictionaries collection contains a couple of Items which don't have a name.or even show up as dictionaries. The code below runs without error. For i = 5 to ThisDrawing.Dictionaries.Count If ThisDrawing.Dictionaries.Item(i).Name = "MyDictionary" Then Do Something End If Next At the moment I'm working in Land Desktop R3 and my drawing has 26 dictionaries. Is this normal? Roughly how many default dictionaries do you find in a standard AutoCAD drawing. The following code works in respect of finding my dictionary: On Error Resume Next Set oDict = ThisDrawing.Dictionaries.Item("MyDictionary") If Err <> 0 Then MsgBox "You haven't created this dictionary yet" End If I found this in the help file: "Dictionaries: The return value type for this method is IAcadObject. This allows you to retrieve named objects from the dictionaries collection that are not of the type AcadDictionary. " Does this mean that the first code segment could be made to work by using a TypeOf test along the lines below: Dim oDict As Object For Each oDict in ThisDrawing.Dictionaries If TypeOf oDict Is AcadDictionary Then If oDict.Name = "MyDictionary" Then DoSomething End If End If Next -- Laurie Comerford CADApps www.cadapps.com.au
Hi Laurie, See my comments in-line [JM]Yep, things like Groups, raster_variables, layouts, and other obvious things. [JM] Yuck.... [JM] I just checked my current drawing and there are 38 in it, currently using LDD3 but I think that this drawing was started in S8 [JM] Yes, the above code works quite well. Of course I would modify it slightly though: Dim oDictTest As Object Dim oDict As AcadDictionary For Each oDictTest In ThisDrawing.Dictionaries If TypeOf oDictTest Is AcadDictionary Then If oDictTest.Name = "MyDictionary" Then Set oDict = oDictTest 'Do stuff with oDict End If End If Next Jeff
Right. A dictionary (which is what the dictionaries collection itself is), can contain any object derived from AcadObject, so you can't assume that everything in the collection is an AcadDictionary. You can either use typeof, or just try to assign each item to a variable dim'd as AcadDictionary, and trap the error that occurs if its not an AcadDictionary.
Hi Tony, I'd be interested to know if you have a preference for either of the two ways which both "work". From the standpoint of a criteria of professionalism in programming, - or just doing it the 'right' way - would there be a reason to prefer one over the other? 1) the error trapping method or 2) the type-testing method Mark
I won't give you a direct answer, but here are some points to to consider: 1. Do you want to process only AcadDictionary objects, or any object that supports the IAcadDictionary interface? To do the latter, you have to use 'typeof IAcadDictionary', which means that any type of object that supports that interface will pass the test. I'm not sure if that also works if you use 'typeof AcadDictionary' (note that you're using a class type in this case, not an interface type). 2. Code clarity. Obviously, the error trapping approach does not as clearly or explictily convey the intent, so that is a ligitimate concern. 3. Unanticipated errors. With the error trapping approach, you can either test the error condition explicitly to see if it is the one you expect to occur when you try to assign an object that doesn't support the AcadDictionary interface to a variable dim'd as AcadDictionary, or you can assume it was the expected error (as most do). However, if there is a remote chance that another error could also occur when you do the assignment, you would need to explicitly test the error, rather than just assume it was the one you expected. Make up your own mind