.NET

Reply
Valued Contributor
87 Posts
0 Kudos
Registered: ‎05-03-2013
Post 1 of 6
Accepted Solution

How to keep the old group

214 Views, 5 Replies
04-15-2014 10:05 AM

I have a command, it can group the selected objects, but it has  some problem.

 

For example:

Select A B C entities, the first execution of the command,  so these entities are gouped together after the first execution, then Select D E F entities and execute the command again, the result is D E F will be a group, but the A B C won't still be a group, I need the A B C  keep the group still.

 

How should I modify the code so that  it will maintain the original grouping after each time run this command?

 

There is a piece of code as follows

  private void Group(Database acurrentDataBase, ObjectIdCollection entityObjectIdCollection)
        {
            //Grouping them
            using (Transaction acTrans = acurrentDataBase.TransactionManager.StartTransaction())
            {
                DBDictionary DBDictionaryDataBase = (DBDictionary)acTrans.GetObject(acurrentDataBase.GroupDictionaryId, OpenMode.ForWrite);
                Group groupObj = new Group("Test group", true);
                DBDictionaryDataBase.SetAt("myGroupName", groupObj);
                acTrans.AddNewlyCreatedDBObject(groupObj, true);
                groupObj.InsertAt(0, entityObjectIdCollection);
                acTrans.Commit();
            }
        }

 

Any help much appreciate, very thanks.

Hi,

 

If you want your method creates several groups, don't use the same name for the group. The name is a key to retrieve instances in dictionaries, it have to be unic.

 

From documentation for the DBDictionary.SetAt() method:

"This function adds a new entry specified by newValue into the dictionary, if searchKey does not already exist in the dictionary. If the entry with key searchKey already exists, the existing entry is erased, the dictionary is removed from its reactor list, and it is replaced by the newValue object is added to the dictionary in its place."

Hi,

 

If you don't care about the group name, you can create anonymous groups (*A1, *A2, *A3, ...).

 

 private void MakeAnonymousGroup(Database db, ObjectIdCollection ids)
{
using (Transaction tr = db.TransactionManager.StartTransaction())
{
DBDictionary NOD =
(DBDictionary)tr.GetObject(db.NamedObjectsDictionaryId, OpenMode.ForRead);
DBDictionary groups =
(DBDictionary)tr.GetObject(NOD.GetAt("ACAD_GROUP"), OpenMode.ForWrite);
using (Group grp = new Group())
{
grp.Append(ids);
groups.SetAt("*", grp);
}
tr.Commit();
}
}

 

Otherwise, you can increment a prefixed name (foo1, foo2, foo3, ...)

 

 private void MakeNamedGroup(Database db, ObjectIdCollection ids, string prefix, string description)
{
using (Transaction tr = db.TransactionManager.StartTransaction())
{
DBDictionary NOD =
(DBDictionary)tr.GetObject(db.NamedObjectsDictionaryId, OpenMode.ForRead);
DBDictionary groups =
(DBDictionary)tr.GetObject(NOD.GetAt("ACAD_GROUP"), OpenMode.ForWrite);
int index = 1;
while (groups.Contains(prefix + index)) index++;
using (Group grp = new Group(description, true))
{
grp.Append(ids);
groups.SetAt(prefix + index, grp);
}
tr.Commit();
}
}

 

*Expert Elite*
2,142 Posts
255 Kudos
Registered: ‎04-29-2006
Post 2 of 6

Re : How to keep the old group

04-16-2014 12:58 AM in reply to: HelloWorlddd

Hi,

 

If you want your method creates several groups, don't use the same name for the group. The name is a key to retrieve instances in dictionaries, it have to be unic.

 

From documentation for the DBDictionary.SetAt() method:

"This function adds a new entry specified by newValue into the dictionary, if searchKey does not already exist in the dictionary. If the entry with key searchKey already exists, the existing entry is erased, the dictionary is removed from its reactor list, and it is replaced by the newValue object is added to the dictionary in its place."

Gilles Chanteau
Valued Contributor
61 Posts
0 Kudos
Registered: ‎11-19-2010
Post 3 of 6

Re : How to keep the old group

04-16-2014 06:39 AM in reply to: _gile

Yes , you are right. this method acts as if the Append mode of writing a file.

if groupname exist, it just add the new objects to the exist group, if it is not exist, then create one and make them a group.

So when we create a group, we can ask user to input a name, but this may still cause identical names, so what is the best practice of create a group and keep their hierarchy?

Valued Contributor
87 Posts
0 Kudos
Registered: ‎05-03-2013
Post 4 of 6

Re : How to keep the old group

04-16-2014 07:17 AM in reply to: _gile

Thank you very much : )

I take the DateTime.Now.Ticks.ToString() as new name for each one. it will work ok.

*Expert Elite*
2,142 Posts
255 Kudos
Registered: ‎04-29-2006
Post 5 of 6

Re : How to keep the old group

04-16-2014 12:00 PM in reply to: HelloWorlddd

Hi,

 

If you don't care about the group name, you can create anonymous groups (*A1, *A2, *A3, ...).

 

        private void MakeAnonymousGroup(Database db, ObjectIdCollection ids)
        {
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                DBDictionary NOD =
                    (DBDictionary)tr.GetObject(db.NamedObjectsDictionaryId, OpenMode.ForRead);
                DBDictionary groups =
                    (DBDictionary)tr.GetObject(NOD.GetAt("ACAD_GROUP"), OpenMode.ForWrite);
                using (Group grp = new Group())
                {
                    grp.Append(ids);
                    groups.SetAt("*", grp);
                }
                tr.Commit();
            }
        }

 

Otherwise, you can increment a prefixed name (foo1, foo2, foo3, ...)

 

        private void MakeNamedGroup(Database db, ObjectIdCollection ids, string prefix, string description)
        {
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                DBDictionary NOD =
                    (DBDictionary)tr.GetObject(db.NamedObjectsDictionaryId, OpenMode.ForRead);
                DBDictionary groups =
                    (DBDictionary)tr.GetObject(NOD.GetAt("ACAD_GROUP"), OpenMode.ForWrite);
                int index = 1;
                while (groups.Contains(prefix + index)) index++;
                using (Group grp = new Group(description, true))
                {
                    grp.Append(ids);
                    groups.SetAt(prefix + index, grp);
                }
                tr.Commit();
            }
        }

 

Gilles Chanteau
Valued Contributor
61 Posts
0 Kudos
Registered: ‎11-19-2010
Post 6 of 6

Re : How to keep the old group

04-17-2014 12:17 AM in reply to: _gile

anonymous groupname is fine, this looks like anonymous block~

Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Are you interested in helping shape the future of the Autodesk Community? To participate in this brief usability study, please click here. Your time and input is greatly appreciated!