|
| 1 | +using Autodesk.AutoCAD.ApplicationServices.Core; |
| 2 | +using Autodesk.AutoCAD.DatabaseServices; |
| 3 | +using Autodesk.AutoCAD.EditorInput; |
| 4 | +using Autodesk.AutoCAD.Geometry; |
| 5 | +using Autodesk.AutoCAD.Runtime; |
| 6 | + |
| 7 | +namespace Test; |
| 8 | + |
| 9 | +public class CopySelection |
| 10 | +{ |
| 11 | + [CommandMethod("CopySel")] |
| 12 | + public static void CopySelectedEntities() |
| 13 | + { |
| 14 | + var doc = Application.DocumentManager.MdiActiveDocument; |
| 15 | + var db = doc.Database; |
| 16 | + var ed = doc.Editor; |
| 17 | + |
| 18 | + var sel = ed.GetSelection(); |
| 19 | + if (sel.Status != PromptStatus.OK) return; |
| 20 | + |
| 21 | + var fromRes = ed.GetPoint("\nBase point:"); |
| 22 | + if (fromRes.Status != PromptStatus.OK) return; |
| 23 | + var toRes = ed.GetPoint("\nSecond point:"); |
| 24 | + if (toRes.Status != PromptStatus.OK) return; |
| 25 | + |
| 26 | + var disp = toRes.Value - fromRes.Value; |
| 27 | + var mat = Matrix3d.Displacement(disp); |
| 28 | + |
| 29 | + using (var tr = db.TransactionManager.StartTransaction()) |
| 30 | + { |
| 31 | + var bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead); |
| 32 | + var ms = (BlockTableRecord)tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite); |
| 33 | + |
| 34 | + foreach (SelectedObject so in sel.Value) |
| 35 | + { |
| 36 | + var ent = tr.GetObject(so.ObjectId, OpenMode.ForRead) as Entity; |
| 37 | + if (ent == null) continue; |
| 38 | + var clone = ent.Clone() as Entity; |
| 39 | + if (clone == null) continue; |
| 40 | + clone.TransformBy(mat); |
| 41 | + ms.AppendEntity(clone); |
| 42 | + tr.AddNewlyCreatedDBObject(clone, true); |
| 43 | + } |
| 44 | + tr.Commit(); |
| 45 | + } |
| 46 | + } |
| 47 | +} |
0 commit comments