I just ran into another .NET API bug (Acad2006).
The bug is with
Autodesk.AutoCAD.DatabaseServices.DatabaseSummaryInfo.CustomProperties.
According to .NET API, DatabaseSummaryInfo.CustomProperties is
an IDictionaryEnumerator object, so, one can call Reset(), MoveNext() to loop
through each element in the collection, that is (make sure you use a drawing
that has a few custom properties):
Document
doc=Application.DocumentManager.MdiActiveDocument;
//Loop through drawing's custom properties
doc.SummaryInfo.CustomProperties.Reset();
while
(doc.SummaryInfo.CustomProperties.MoveNext())
{
MessageBox.Show ("KEY: " +
doc.SummaryInfo.CustomProperties.Key + "\nVALUE: " +
doc.SummaryInfo.CustomProperties.Value)
//Or, more clearly
//MessageBox.Show ("KEY: " +
doc.SummaryInfo.CustomProperties.Current.Key + "\nVALUE: " +
doc.SummaryInfo.CustomProperties.Current.Value)
}
However, above code will generate exception.
The error occurs on the first line of code in the loop when
you try to access
doc.SummaryInfo.CustomProperties.Current
The exception indicates that there is no element in the
collection. If you remove all code in the loop, you will be trapped in an
endless loop. You have to kill Acad session to stop it.
It cost whole morning to find the workaround:
You do not loop through the "doc.SummaryInfo.CustomProperties"
object, you do:
System.Collection.IDictionaryEnumerator
de=doc.SummaryInfo.CustomProperties
de.Reset();
while(de.MoveNext())
{
MessageBox.Show ("KEY: " + de.Key +
"\nVALUE: " + de.Value)
}
Magically, explitly assign
DatabaseSummaryInfo.CustomProperties to a IDictionaryEnumerator variable
ridicularly solve the problem.
It is obviously that the implementation of
DatabaseSummaryInfo.CustomProperties to AutoCAD .NET API does not do it
correctly.
Can someone else verify this on Acad2006, 2007?
Here is the whole test code (note: you need to open a drawing
with a few custom properties to run the test):
using System;
using Autodesk.AutoCAD.Runtime;
using
Autodesk.AutoCAD.ApplicationServices;
using
Autodesk.AutoCAD.DatabaseServices;
using System.Windows.Forms;
[assembly:
CommandClass(typeof(ClassLibrary.WDSGClass))]
namespace ClassLibrary
{
///
<summary>
/// Summary description for
WDSGClass.
/// </summary>
public class WDSGClass
{
private
Autodesk.AutoCAD.ApplicationServices.Document
mDoc;
public
WDSGClass()
{
}
[CommandMethod("DwgTest")]
public
void test() // This method can have any
name
{
mDoc =
Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
Database db = mDoc.Database;
size=2> int
propCount = 0;
size=2>
/*==This part of code generates exception, although it should
work
db.SummaryInfo.CustomProperties.Reset();
while
(db.SummaryInfo.CustomProperties.MoveNext())
{
propCount++;
//if you comment out this line of code, you get endless loop,
until propCount overflows or you kill Acad
session
MessageBox.Show(db.SummaryInfo.CustomProperties.Key + "\n" +
db.SummaryInfo.CustomProperties.Value);
}
*/
//This part code is OK
size=2>
System.Collections.IDictionaryEnumerator de =
db.SummaryInfo.CustomProperties;
de.Reset();
while
(de.MoveNext())
{
propCount++;
MessageBox.Show(de.Key + "\n" +
de.Value);
}
size=2>
MessageBox.Show(propCount + " custom
properties.");
}
}
}