.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Get the groupname/groupid which an entity belongs to

3 REPLIES 3
Reply
Message 1 of 4
mabe2k11
686 Views, 3 Replies

Get the groupname/groupid which an entity belongs to

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.???????
}
3 REPLIES 3
Message 2 of 4
Anonymous
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
Message 3 of 4
mabe2k11
in reply to: mabe2k11

Thank you!

It's exactly what I need.
Message 4 of 4
Anonymous
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]

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost