public void Test()
{
Document doc = AcApSrvApp.DocumentManager.MdiActiveDocument;
Editor ed = doc.Editor;
Matrix3d ucs = ed.CurrentUserCoordinateSystem;
PromptSelectionResult psr = ed.GetSelection();
if (psr.Status != PromptStatus.OK)
return;
PromptPointResult ppr = ed.GetPoint("\nBase point: ");
if (ppr.Status != PromptStatus.OK)
return;
Point3d basePoint = ppr.Value;
PromptPointOptions ppo = new PromptPointOptions("\nSecond point: ");
ppo.BasePoint = basePoint;
ppo.UseBasePoint = true;
ppr = ed.GetPoint(ppo);
if (ppr.Status != PromptStatus.OK)
return;
Vector3d disp = basePoint.TransformBy(ucs).GetVectorTo(ppr.Value.TransformBy(ucs));
ObjectId[] ids = psr.Value.GetObjectIds();
try
{
CopyMove(ids, disp);
}
catch (System.Exception ex)
{
ed.WriteMessage("Error: " + ex.Message);
}
}
private void CopyMove(ObjectId[] ids, Vector3d disp)
{
Document doc = AcApSrvApp.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
using (Transaction tr = db.TransactionManager.StartTransaction())
{
foreach (ObjectId Id in ids)
{
Entity ent = (Entity)tr.GetObject(Id, OpenMode.ForRead);
ObjectId ownerId = ent.OwnerId;
IdMapping IdMap = new IdMapping();
ObjectIdCollection idsColl = new ObjectIdCollection(new ObjectId[] { Id });
db.DeepCloneObjects(idsColl, db.CurrentSpaceId, IdMap, false);
foreach (IdPair pair in IdMap)
{
if (pair.IsPrimary)
{
BlockReference btr = (BlockReference)tr.GetObject(pair.Value, OpenMode.ForWrite);
if (btr != null) btr.TransformBy(Matrix3d.Displacement(disp));
}
}
}
tr.Commit();
}
//Document doc = AcApSrvApp.DocumentManager.MdiActiveDocument;
//Database db = doc.Database;
//using (Transaction tr = db.TransactionManager.StartTransaction())
//{
// foreach (ObjectId id in ids)
// {
// Entity ent = (Entity)tr.GetObject(id, OpenMode.ForWrite, false);
// BlockTableRecord btr = (BlockTableRecord)tr.GetObject(ent.OwnerId, OpenMode.ForWrite);
// IdMapping mapping = new IdMapping();
// Entity copiedEnt = (Entity)ent.DeepClone(btr, mapping, false);
// tr.AddNewlyCreatedDBObject(copiedEnt, true);
// ent.TransformBy(Matrix3d.Displacement(disp));
// }
// tr.Commit();
//}
//Document doc = AcApSrvApp.DocumentManager.MdiActiveDocument;
//Database db = doc.Database;
//using (Transaction tr = db.TransactionManager.StartTransaction())
//{
// BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
// foreach (ObjectId id in ids)
// {
// Entity ent = (Entity)tr.GetObject(id, OpenMode.ForWrite, false);
// Entity copiedEnt = (Entity)ent.Clone();
// btr.AppendEntity(copiedEnt);
// tr.AddNewlyCreatedDBObject(copiedEnt, true);
// ent.TransformBy(Matrix3d.Displacement(disp));
// }
// tr.Commit();
//}
}