try this and see if it works
Okay, now that I take a better look at it, this does simplify what I was doing.
I never would've thought to sort the handle values directly, then get the objectId of the group from the value.
This also eliminates the sorted list, which I understand is the same as using 2 arrays, so I'll assume for now that the List<int64> is a more efficient way to handle things, not to mention the reverse method which the sorted list doesn't have.
I chose to not check the group for number of entities as I list them even if empty
and to obtain the Object id using the GetAt method (don't know if it's better than opening for read or not, I'm thinking it is
).
The DBDictionaryEntry = DictionaryEntry on my platform.
Oh, one question, why do I see some using:
Group grp = (Group)tr.GetObject(db.GetObjectId(false, new Handle(handleVals[0]), 0), OpenMode.ForRead) as Group;
When it works without using the "as Group":
Group grp = (Group)tr.GetObject(db.GetObjectId(false, new Handle(handleVals[0]), 0), OpenMode.ForRead);
Thanks for the tips!
Here's what I end up with, any critique appreciated, just don't get too carried away!
using System;
using System.Collections;
using System.Collections.Generic;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.ApplicationServices;
using AcadApp = Autodesk.AutoCAD.ApplicationServices.Application;
namespace PartGroupsMonitor
{
public class LastGroupInDrawing
{
public string FindLastGroup()
{
Document doc = AcadApp.DocumentManager.MdiActiveDocument;
Database db = HostApplicationServices.WorkingDatabase;
List<Int64> handleVals = new List<Int64>();
using (DocumentLock dloc = doc.LockDocument())
{
using (Transaction tr = db.TransactionManager.StartTransaction())
{
DBDictionary groups = (DBDictionary)tr.GetObject(db.GroupDictionaryId, OpenMode.ForRead);
ObjectIdCollection objIDs = new ObjectIdCollection();
foreach (DictionaryEntry DicEnt in groups)
{
ObjectId Did = groups.GetAt(DicEnt.Key.ToString());
handleVals.Add(Did.Handle.Value);
}
if (handleVals.Count > 0)
{
handleVals.Sort();
handleVals.Reverse();
Group grp = (Group)tr.GetObject(db.GetObjectId(false, new Handle(handleVals[0]), 0), OpenMode.ForRead);
return grp.Name;
}
else
{
return "No";
}
}
}
}
}
}