.NET

Reply
Distinguished Contributor
Dale.Bartlett
Posts: 148
Registered: ‎01-06-2003
Message 1 of 4 (207 Views)

Groups - Explode/Purge

207 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*
Hallex
Posts: 1,569
Registered: ‎10-08-2008
Message 2 of 4 (199 Views)

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
Distinguished Contributor
Dale.Bartlett
Posts: 148
Registered: ‎01-06-2003
Message 3 of 4 (192 Views)

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

Distinguished Contributor
Dale.Bartlett
Posts: 148
Registered: ‎01-06-2003
Message 4 of 4 (157 Views)

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
Need installation help?

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