.NET

Reply
Valued Contributor
NKCAUTOCAD01
Posts: 86
Registered: ‎08-31-2011
Message 1 of 8 (402 Views)
Accepted Solution

a way of the editing of the Group table

402 Views, 7 Replies
11-05-2012 08:02 PM

In AutoCAD.NET API, please tell me.
a way of the editing of the Group table of the collection object
Are there not the following operation samples?
 ①Crate of the new group
 ②The group name acquisition
 ③Element addition to the group
 ④Element deletion from the group
 ⑤The group acquisition from an element
 ⑥Deletion of the group
 ⑦I add the expansion data to the group,
and, please acquire it.

Mentor
khoa.ho
Posts: 209
Registered: ‎09-15-2011
Message 2 of 8 (367 Views)

Re: a way of the editing of the Group table

11-07-2012 08:20 AM in reply to: NKCAUTOCAD01

Hi,

Group collection is stored as a Group Dictionary on AutoCAD database (db.GroupDictionaryId). So it works the same way like any other dictionary-type objects (LayoutDictionary, ExtensionDictionary,...).

 

You can use the following code to write more code for your need:

 

public static void GroupAdd(Database db, string groupName, string groupDescription, bool groupSelected = true)
{
    using (Transaction trans = db.TransactionManager.StartTransaction())
    {
        var groupDict = (DBDictionary)trans.GetObject(db.GroupDictionaryId, OpenMode.ForWrite);
        if (groupDict.Contains(groupName))
            return;
        var group = new Group(groupDescription, groupSelected);
        groupDict.SetAt(groupName, group);
        groupDict.DowngradeOpen();
        trans.AddNewlyCreatedDBObject(group, true);
        trans.Commit();
    }
}

public static void GroupDelete(Database db, string groupName)
{
    using (Transaction trans = db.TransactionManager.StartTransaction())
    {
        var groupDict = (DBDictionary)trans.GetObject(db.GroupDictionaryId, OpenMode.ForWrite);
        if (!groupDict.Contains(groupName))
            return;
        var group = (DBObject)groupDict[groupName];
        group.Erase();
        groupDict.DowngradeOpen();
        trans.Commit();
    }
}

 

-Khoa

Valued Contributor
NKCAUTOCAD01
Posts: 86
Registered: ‎08-31-2011
Message 3 of 8 (353 Views)

Re: a way of the editing of the Group table

11-07-2012 04:40 PM in reply to: khoa.ho

 Dear Mr. khoa.ho

Thank you very much for your precise advice.

Please tell me. approximately another two points

  1.Element addition to the group

  2.I add the expansion data to the group

Thanking you in advance.

 

Sincerely,

NKCAUTOCAD01(M.Tsukamoto)

 

Mentor
khoa.ho
Posts: 209
Registered: ‎09-15-2011
Message 4 of 8 (336 Views)

Re: a way of the editing of the Group table

11-08-2012 09:01 AM in reply to: NKCAUTOCAD01

Hi M.Tsukamoto,

 

Here is the basic code for group and a test command. You can expand more to suite your need.

 

Group is a dictionary entry with its name is the key. So the group name must be unique and not have spaces inside.

 

[CommandMethod("GroupNew")]
public static void GroupNew()
{
    Document doc = Application.DocumentManager.MdiActiveDocument;
    Editor ed = doc.Editor;
    Database db = doc.Database;
    using (Transaction trans = db.TransactionManager.StartTransaction())
    {
        // Get group name
        PromptResult resultString = ed.GetString("Specify new group name: ");
        string groupName = resultString.StringResult;
        if (string.IsNullOrEmpty(groupName))
            return;
        // Get group entities
        PromptSelectionResult selectionResult = ed.GetSelection();
        if (selectionResult.Status != PromptStatus.OK)
            return;
        if (selectionResult.Value != null)
        {
            var objectIdCollection = new ObjectIdCollection(selectionResult.Value.GetObjectIds());
            GroupAddEntities(db, groupName, objectIdCollection);
        }
        trans.Commit();
    }
}

// Add a new empty group
public static Group GroupNew(Database db, string groupName, string groupDescription, bool groupSelected = true)
{
    try
    {
        using (Transaction trans = db.TransactionManager.TopTransaction)
        {
            var groupDict = (DBDictionary)trans.GetObject(db.GroupDictionaryId, OpenMode.ForRead);
            if (groupDict.Contains(groupName))
                return (Group)groupDict[groupName];
            var group = new Group(groupDescription, groupSelected);
            groupDict.UpgradeOpen();
            groupDict.SetAt(groupName, group);
            groupDict.DowngradeOpen();
            trans.AddNewlyCreatedDBObject(group, true);
            return group;
        }
    }
    catch (Exception ex)
    {
        return null;
    }
}

// Delete an existing group
public static void GroupDelete(Database db, string groupName)
{
    using (Transaction trans = db.TransactionManager.TopTransaction)
    {
        var groupDict = (DBDictionary)trans.GetObject(db.GroupDictionaryId, OpenMode.ForRead);
        if (!groupDict.Contains(groupName))
            return;
        var group = (DBObject)groupDict[groupName];
        group.Erase();
    }
}

// Add all entities in a collection to a group
public static void GroupAddEntities(Database db, string groupName, ObjectIdCollection entityIdCollection)
{
    using (Transaction trans = db.TransactionManager.TopTransaction)
    {
        var groupDict = (DBDictionary)trans.GetObject(db.GroupDictionaryId, OpenMode.ForRead);
        Group group = groupDict.Contains(groupName)
            ? (Group)groupDict[groupName]
            : GroupNew(db, groupName, groupName);
        group.UpgradeOpen();
        foreach (ObjectId entityId in entityIdCollection)
        {
            group.Append(entityId);
        }
        group.DowngradeOpen();
    }
}

// Add extension XData to a group
public static void GroupAddXData(Database db, string groupName, ResultBuffer xdata)
{
    using (Transaction trans = db.TransactionManager.TopTransaction)
    {
        var groupDict = (DBDictionary)trans.GetObject(db.GroupDictionaryId, OpenMode.ForRead);
        if (!groupDict.Contains(groupName))
            return;
        var group = (Group)groupDict[groupName];
        group.UpgradeOpen();
        group.XData = xdata;
        group.DowngradeOpen();
    }
}

 

 -Khoa

 

Valued Contributor
NKCAUTOCAD01
Posts: 86
Registered: ‎08-31-2011
Message 5 of 8 (318 Views)

Re: a way of the editing of the Group table

11-08-2012 10:04 PM in reply to: khoa.ho

 

Dear khoa.ho
Thank you very much for your accurate advice.
I thank for your advice heartily.
Yours sincerely,
NKCAUTOCAD01 (M.Tsukamoto)
Mentor
khoa.ho
Posts: 209
Registered: ‎09-15-2011
Message 6 of 8 (312 Views)

Re: a way of the editing of the Group table

11-08-2012 10:48 PM in reply to: NKCAUTOCAD01

Hi Tsukamoto,

 

You are very welcome. I hope you can add more functionalities to your code.

 

Here is another extra code to add an extension xrecord to a group, so you can use it as an enhancement over xdata.

 

// Add extension XRecord to a group
public static void GroupAddXRecord(Database db, string groupName, string xkey, ResultBuffer xdata)
{
    using (Transaction trans = db.TransactionManager.TopTransaction)
    {
        var groupDict = (DBDictionary)trans.GetObject(db.GroupDictionaryId, OpenMode.ForRead);
        if (!groupDict.Contains(groupName))
            return;
        var group = (Group)groupDict[groupName];
        group.UpgradeOpen();
        // Create a new extension dictionary of the group
        group.CreateExtensionDictionary();
        var extDictionary = (DBDictionary)trans.GetObject(group.ExtensionDictionary, OpenMode.ForWrite);
        var xrecord = new Xrecord
            {
                Data = xdata
            };
        extDictionary.SetAt(xkey, xrecord);
        extDictionary.DowngradeOpen();
        group.DowngradeOpen();
        // Let the transaction know about the new xrecord
        trans.AddNewlyCreatedDBObject(xrecord, true);
    }
}

 

-Khoa

Valued Contributor
NKCAUTOCAD01
Posts: 86
Registered: ‎08-31-2011
Message 7 of 8 (253 Views)

Re: a way of the editing of the Group table

11-28-2012 04:34 PM in reply to: khoa.ho
Dear khoa.ho
Thank you for repeated advice.
It was saved very much.
Yours sincerely,
NKCAUTOCAD01 (M.Tsukamoto)
Valued Contributor
cjacobi
Posts: 54
Registered: ‎11-12-2012
Message 8 of 8 (173 Views)

Re: a way of the editing of the Group table

10-30-2013 08:10 AM in reply to: NKCAUTOCAD01

I know this is an older post, but I found it very usefull, but have a problem with the GroupAddEntities. I am still a novice programmer with .NET and usually stick to VB. I converted the above code to vb, but it breaks when there is an existing group already defined in the drawing. It is breaking at the "directcast" line. I used an automated conversion tool to convert to vb, so maybe it buggered up somehow. Anyway I have never used "directcast" before so I'm not sure what the issue is.

 

What I'm trying to do is an additional entities to an already existing group.

 

Public Shared Sub GroupAddEntities(db As Database, groupName As String, entityIdCollection As ObjectIdCollection)
        Using trans As Transaction = db.TransactionManager.TopTransaction
            Dim groupDict = DirectCast(trans.GetObject(db.GroupDictionaryId, OpenMode.ForRead), DBDictionary)
            Dim group As Group = If(groupDict.Contains(groupName), DirectCast(groupDict(groupName), Group), GroupNew(db, groupName, groupName))
            group.UpgradeOpen()
            For Each entityId As ObjectId In entityIdCollection
                group.Append(entityId)
            Next
            group.DowngradeOpen()
        End Using
    End Sub

 Any help would be greatly appreciated.

 

thanks

 

cj

Announcements
Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.
Need installation help?

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