.NET

Reply
Mentor
188 Posts
11 Kudos
Registered: ‎01-06-2003
Post 1 of 4

Groups - Explode/Purge

223 Views, 3 Replies
05-27-2012 10:03 PM

Hi All, I have had great support from these groups over the years, with many questions answered. I had some trouble finding a solution to exploding groups, so I thought I would post my code for comment and to help others (maybe). Ignore the error message utility. Dale

<code>

// Groups explode and purge uses a different logic
// iterate groups, iterate objects in group, remove objects, erase group
public static void ExplodeGroups()
{
Document doc = AcadApp.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
// store drawing name for error report
string lstrDWGFileName = db.Filename.ToString();
// start a transaction
using(Transaction tr = db.TransactionManager.StartTransaction())
{
try
{
DBDictionary NOD = (DBDictionary)tr.GetObject(db.NamedObjectsDictionaryId, OpenMode.ForRead, false, false);
if(NOD.Contains("ACAD_GROUP"))
{
DBDictionary gd = tr.GetObject(NOD.GetAt("ACAD_GROUP"), OpenMode.ForWrite, false, false) as DBDictionary;
foreach(DictionaryEntry de in gd)
{
ObjectId Did = gd.GetAt(de.Key.ToString());
Group gp = (Group)tr.GetObject(Did, OpenMode.ForWrite, false, false);

// testing>>
// string gName = gp.Name;
// MessageBox.Show(gName);

// iterate entities and remove from group
ObjectIdCollection ids = new ObjectIdCollection(gp.GetAllEntityIds());
foreach(ObjectId oid in ids)
{
gp.Remove(oid);
}
// now delete/purge empty group
gp.Erase();
}
}
else
{
ed.WriteMessage("No Groups found");
}
}
catch(Autodesk.AutoCAD.Runtime.Exception ex)
{
MessageBox.Show(UtilityLibrary.ErrorMessage(ex, MethodBase.GetCurrentMethod().Name.ToString(), lstrDWGFileName), "Something Happened...");
tr.Abort();
}
finally
{
// Save the new objects to the database
tr.Commit();
}
}
}

<code>

*Expert Elite*
1,569 Posts
171 Kudos
Registered: ‎10-08-2008
Post 2 of 4

Re: Groups - Explode/Purge

05-27-2012 11:37 PM in reply to: Dale.Bartlett

Hi,

Just on the quick glance, maybe you need to add functionality

to unlock / reset layers

Secondly, do not use tr.Abort(); inside the Using...End Using code block

just remove it from there,

Regards,

 

~'J'~

 

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Mentor
188 Posts
11 Kudos
Registered: ‎01-06-2003
Post 3 of 4

Re: Groups - Explode/Purge

05-28-2012 02:03 AM in reply to: Hallex

Hi, Thanks for feedback. I'll remove tr.abort as suggested. The layer reset as you commented is already taken care of in an earlier function. Regards, Dale

Mentor
188 Posts
11 Kudos
Registered: ‎01-06-2003
Post 4 of 4

Re: Groups - Explode/Purge

05-30-2012 04:08 AM in reply to: Dale.Bartlett

Correction:
Use:
ObjectId[] ids = gp.GetAllEntityIds();
instead of:
ObjectIdCollection ids = new ObjectIdCollection(gp.GetAllEntityIds());

Dale

Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Are you interested in helping shape the future of the Autodesk Community? To participate in this brief usability study, please click here. Your time and input is greatly appreciated!