• Industries
  • Products
  • Buy
  • Services & Support
  • Communities
  • Discussion Groups

    .NET

    Reply
    Distinguished Contributor
    Posts: 118
    Registered: ‎01-06-2003

    Groups - Explode/Purge

    130 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>

    Please use plain text.
    *Expert Elite*
    Hallex
    Posts: 1,334
    Registered: ‎10-08-2008

    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
    Please use plain text.
    Distinguished Contributor
    Posts: 118
    Registered: ‎01-06-2003

    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

    Please use plain text.
    Distinguished Contributor
    Posts: 118
    Registered: ‎01-06-2003

    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

    Please use plain text.