Here's the new CopyFroupFromExisting:
using System;
using System.Collections;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Interop;
using Autodesk.AutoCAD.Interop.Common;
using Autodesk.AutoCAD.Internal;
using AcadApp = Autodesk.AutoCAD.ApplicationServices.Application;
namespace PartGroupsMonitor
{
class CopyGroupFromExisting
{
public void CreateGroup()
{
Document document = AcadApp.DocumentManager.MdiActiveDocument;
Editor editor = document.Editor;
Database database = HostApplicationServices.WorkingDatabase;
using (document.LockDocument())
{
PromptEntityOptions options = new PromptEntityOptions("\nSelect part of Group: < pick > ");
options.SetRejectMessage("\nMust be Group related. ");
options.AddAllowedClass(typeof(Arc), false);
options.AddAllowedClass(typeof(Line), false);
options.AddAllowedClass(typeof(Circle), false);
options.AddAllowedClass(typeof(Polyline), false);
PaletteUtils.ActivateEditor();
PromptEntityResult selection = editor.GetEntity(options);
if (selection.Status == PromptStatus.OK)
{
ObjectId GrpId = GroupId(selection.ObjectId);
if (!GrpId.IsNull) //has reactors.
{
using (Transaction trans = database.TransactionManager.StartTransaction())
{
Group grp = trans.GetObject(GrpId, OpenMode.ForWrite, false) as Group; //is group.
if (grp != null)
{
DBDictionary GrpDic = (DBDictionary)trans.GetObject(database.GroupDictionaryId, OpenMode.ForWrite);
ObjectId[] EntIds = grp.GetAllEntityIds();
ObjectIdCollection IDCol = new ObjectIdCollection(EntIds);
HighlightSelection(IDCol);
PromptPointResult PntFrom = GroupBasePoint(); //basepoint for move.
if (PntFrom.Status == PromptStatus.OK)
{
UnHighlightSelection(IDCol);
Point3d basePt = (Point3d)PntFrom.Value;
EntityDragger dragger = new EntityDragger();
PromptResult dragResult = dragger.StartDrag(basePt, EntIds);
if (dragResult.Status == PromptStatus.OK)
{
grp.Remove(IDCol);
IdMapping IDMap = new IdMapping();
IDMap = database.DeepCloneObjects(IDCol, database.CurrentSpaceId, false);
grp.Append(IDCol);
MoveDeepClonedCopies(dragger.DisplacementMatrix, IDMap);
string str = grp.Name;
string desc = grp.Description;
string NewNam = GetNewName(str, "COPY");
Group NewGroup = new Group(NewNam, true);
GrpDic.SetAt(NewNam, NewGroup);
trans.AddNewlyCreatedDBObject(NewGroup, true);
NewGroup.Description = desc;
foreach (IdPair pair in IDMap)
{
if (pair.IsPrimary)
{
Entity en = (Entity)trans.GetObject(pair.Value, OpenMode.ForRead);
NewGroup.Append(en.ObjectId);
}
}
//AcadDocument ADoc = (AcadDocument)document.AcadDocument;
//AcadObject AO = ADoc.Dictionaries.Item("ACAD_GROUP");
//IAcadGroups iags = (IAcadGroups)AO;
//iags.Add(NewNam);
editor.WriteMessage("\nGroup < " + str + " > Copied.");
Utils.PostCommandPrompt();
}
if (dragResult.Status == PromptStatus.Cancel)
{
UnHighlightSelection(IDCol);
editor.WriteMessage("\n*Cancel*\n");
Utils.PostCommandPrompt();
}
}
if (PntFrom.Status == PromptStatus.Cancel)
{
editor.WriteMessage("\n*Cancel*\n");
Utils.PostCommandPrompt();
}
IDCol.Dispose();
}
if (grp == null)
{
editor.WriteMessage("\nNot a Group.\n");
Utils.PostCommandPrompt();
}
trans.Commit();
}
if (GrpId.IsNull)
{
editor.WriteMessage("\nInvalid entity.\n");
Utils.PostCommandPrompt();
}
}
} //using trans.
else
{
editor.WriteMessage("\n*Cancel*\n");
Utils.PostCommandPrompt();
}
} //using document lock
}
public ObjectId GroupId(ObjectId EntityId)
{
ResultBuffer rbf = SafeNativeMethods.EntGet(EntityId);
TypedValue[] XdataOut = rbf.AsArray();
ObjectId reactorId = ObjectId.Null;
if (XdataOut[3].Value.ToString() == "{ACAD_REACTORS") //part of as group.
{
reactorId = (ObjectId)XdataOut[4].Value;
}
return reactorId;
}
private void UnHighlightSelection(ObjectIdCollection Oid)
{
Document doc = AcadApp.DocumentManager.MdiActiveDocument;
Database db = HostApplicationServices.WorkingDatabase;
using (Transaction trans = db.TransactionManager.StartTransaction())
{
foreach (ObjectId entid in Oid)
{
Entity ent = (Entity)trans.GetObject(entid, OpenMode.ForRead);
ObjectId[] ids = new ObjectId[1];
ids[0] = entid;
SubentityId index = new SubentityId(SubentityType.Edge, 0);
FullSubentityPath path = new FullSubentityPath(ids, index);
ent.Unhighlight(path, true);
}
trans.Commit();
}
}
private void HighlightSelection(ObjectIdCollection Oid)
{
Document doc = AcadApp.DocumentManager.MdiActiveDocument;
Database db = HostApplicationServices.WorkingDatabase;
using (Transaction trans = db.TransactionManager.StartTransaction())
{
foreach (ObjectId entid in Oid)
{
Entity ent = (Entity)trans.GetObject(entid, OpenMode.ForRead);
ObjectId[] ids = new ObjectId[1];
ids[0] = entid;
SubentityId index = new SubentityId(SubentityType.Edge, 0);
FullSubentityPath path = new FullSubentityPath(ids, index);
ent.Highlight(path, true);
}
trans.Commit();
}
}
public PromptPointResult GroupBasePoint()
{
Document document = AcadApp.DocumentManager.MdiActiveDocument;
Editor editor = document.Editor;
PromptPointOptions options = new PromptPointOptions("\nPick base point: ");
options.AllowNone = false;
PromptPointResult pointRes = editor.GetPoint(options);
return pointRes;
}
private void MoveDeepClonedCopies(Matrix3d transformationMatrix, IdMapping idMap)
{
Document document = AcadApp.DocumentManager.MdiActiveDocument;
Editor currentEditor = document.Editor;
using (Transaction trans = currentEditor.Document.TransactionManager.StartTransaction())
{
foreach (IdPair pair in idMap)
{
if (pair.IsPrimary)
{
Entity clonedEntity = trans.GetObject(pair.Value, OpenMode.ForWrite) as Entity;
if (clonedEntity != null)
{
clonedEntity.TransformBy(transformationMatrix);
}
}
}
trans.Commit();
}
}
public static string GetNewName(string str, string cmd)
{
string NewNam = str;
if (str.Contains("-") && cmd.Equals("COPY"))
{
NewNam = str.Substring(0, str.IndexOf("-", 0, str.Length) + 3) + DateTime.Now.Millisecond.ToString();
}
if (str.Contains("_") && cmd.Equals("COPY"))
{
NewNam = str.Substring(0, str.IndexOf("_", 0, str.Length) + 1) + DateTime.Now.Millisecond.ToString();
}
if (!str.Contains("-") && !str.Contains("_") && !str.StartsWith("*") && cmd.Equals("COPY"))
{
NewNam = str + "_" + DateTime.Now.Millisecond.ToString();
}
if (str.StartsWith("*") && cmd.Equals("COPY"))
{
str = str.Substring(1);
NewNam = "~Copy" + str + "_" + DateTime.Now.Millisecond.ToString();
}
if (str.StartsWith("*") && cmd.Equals("MIRROR"))
{
str = str.Substring(1);
NewNam = "~Mirror" + str + "_" + DateTime.Now.Millisecond.ToString();
}
return NewNam;
}
}
}