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.
Solved! Go to Solution.
Solved by _gile. Go to Solution.
Solved by _gile. Go to Solution.
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."
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?
Thank you very much : )
I take the DateTime.Now.Ticks.ToString() as new name for each one. it will work ok.
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(); } }