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.
Solved! Go to Solution.
Solved by khoa.ho. Go to Solution.
Solved by khoa.ho. Go to Solution.
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
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
Sincerely,
NKCAUTOCAD01(M.Tsukamoto)
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
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
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