.NET

Reply
Contributor
mabe2k11
Posts: 24
Registered: ‎03-14-2006
Message 1 of 4 (199 Views)

Get the groupname/groupid which an entity belongs to

199 Views, 3 Replies
05-07-2006 03:02 AM
Hello!

Is it possible to get the groupname or groupid which an entity belongs to?

In the VBA-group I found something that it is not possible and I have to search in the group-dictionary and in all its groups for the entity I need. So I wrote the following code. But I don't know how to get the groupname (or ID) in the GroupDictionaryId.

DBDictionary dbDict = (DBDictionary)trans.GetObject(db.GroupDictionaryId, OpenMode.ForRead);
for (int i = 0; i < dbDict.Count; i++)
{
ObjectId groupId = dbDic.???????
}
Distinguished Contributor
Alexander Rivilis
Posts: 303
Registered: ‎09-26-2004
Message 2 of 4 (199 Views)

Re: Get the groupname/groupid which an entity belongs to

05-07-2006 04:02 AM in reply to: mabe2k11
...
ObjectId groupId = dbDic.Value;
...

[code]
static public void GetGroupNames()
{
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
Database db = HostApplicationServices.WorkingDatabase;
Transaction trans = db.TransactionManager.StartTransaction();
try
{
DBDictionary lt = (DBDictionary) trans.GetObject(db.GroupDictionaryId, OpenMode.ForRead);
foreach (DictionaryEntry ide in lt)
{
Group ltr = (Group) trans.GetObject((ObjectId)ide.Value, OpenMode.ForRead);
ed.WriteMessage("\nGroup Name: {0}",ltr.Name);
}
trans.Commit();
}
finally
{
trans.Dispose();
}
}
[/code]

Next code also can be useful for you:
[code]
//
// Return ArrayList of ObjectId for all groups
//
static private ArrayList GetAllGroupsIds()
{
ArrayList ids = new ArrayList();
Database db = HostApplicationServices.WorkingDatabase;
Transaction trans = db.TransactionManager.StartTransaction();
try
{
DBDictionary lt = (DBDictionary) trans.GetObject(db.GroupDictionaryId, OpenMode.ForRead);
foreach (DictionaryEntry ide in lt) ids.Add(ide.Value);
trans.Commit();
}
finally
{
trans.Dispose();
}
return ids;
}
//
// Print names of all groups of selected entity
//
[CommandMethod("PrintGroupNamesOfEntity")]
static public void PrintGroupNamesOfEntity()
{
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
Database db = HostApplicationServices.WorkingDatabase;
Transaction trans = db.TransactionManager.StartTransaction();
try
{
ArrayList groupIds = GetAllGroupsIds();
if (groupIds.Count == 0) {
ed.WriteMessage("\nDrawing has not any group!");
return;
}

PromptEntityResult entres = ed.GetEntity("\nSelect entity: ");
if (entres.Status == PromptStatus.OK)
{
Entity en = (Entity) trans.GetObject(entres.ObjectId, OpenMode.ForRead);
foreach (ObjectId id in groupIds)
{
if (en.HasPersistentReactor(id))
{
Group gr = (Group) trans.GetObject(id, OpenMode.ForRead);
ed.WriteMessage("\nGroup Name: {0}",gr.Name);
}
}
}
trans.Commit();
}
finally
{
trans.Dispose();
}
}
[/code] Message was edited by: Alexander Rivilis
Contributor
mabe2k11
Posts: 24
Registered: ‎03-14-2006
Message 3 of 4 (199 Views)

Re: Get the groupname/groupid which an entity belongs to

05-08-2006 06:48 AM in reply to: mabe2k11
Thank you!

It's exactly what I need.
Distinguished Contributor
Alexander Rivilis
Posts: 303
Registered: ‎09-26-2004
Message 4 of 4 (199 Views)

Re: Get the groupname/groupid which an entity belongs to

05-08-2006 08:14 AM in reply to: mabe2k11
For AutoCAD 2007 there is a better and more quick solution:
[code]
[CommandMethod("GetEntGrps")]
static public void GetEntGrps()
{
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
Database db = HostApplicationServices.WorkingDatabase;
Transaction trans = db.TransactionManager.StartTransaction();
try
{
PromptEntityResult entres = ed.GetEntity("\nSelect entity: ");
if (entres.Status == PromptStatus.OK) {
Entity en = (Entity)trans.GetObject(entres.ObjectId, OpenMode.ForRead);
ObjectIdCollection col = en.GetPersistentReactorIds();
if (col != null) {
foreach (ObjectId id in col) {
DBObject obj = (DBObject)trans.GetObject(id, OpenMode.ForRead);
if (obj is Group) {
Group gr = (Group)obj;
ed.WriteMessage("\nGroup Name: {0}", gr.Name);
}
}
}
}
trans.Commit();
}
finally
{
trans.Dispose();
}
}
[/code]
Need installation help?

Start with some of our most frequented solutions or visit the Installation and Licensing Forum to get help installing your software.