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

a way of the editing of the Group table

7 REPLIES 7
SOLVED
Reply
Message 1 of 8
NKCAUTOCAD01
991 Views, 7 Replies

a way of the editing of the Group table

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.

7 REPLIES 7
Message 2 of 8
khoa.ho
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

Message 3 of 8
NKCAUTOCAD01
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)

 

Message 4 of 8
khoa.ho
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

 

Message 5 of 8
NKCAUTOCAD01
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)
Message 6 of 8
khoa.ho
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

Message 7 of 8
NKCAUTOCAD01
in reply to: khoa.ho

Dear khoa.ho
Thank you for repeated advice.
It was saved very much.
Yours sincerely,
NKCAUTOCAD01 (M.Tsukamoto)
Message 8 of 8

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

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