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

Groups - Explode/Purge

3 REPLIES 3
Reply
Message 1 of 4
Dale.Bartlett
496 Views, 3 Replies

Groups - Explode/Purge

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>




______________
Yes, I'm Satoshi.
3 REPLIES 3
Message 2 of 4
Hallex
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
Message 3 of 4
Dale.Bartlett
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




______________
Yes, I'm Satoshi.
Message 4 of 4

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

Dale




______________
Yes, I'm Satoshi.

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