Author Topic: How can you copy an entity from Model Space to Paper Space  (Read 763 times)

0 Members and 1 Guest are viewing this topic.

Offline fixo

  • Full Member
  • ***
  • Posts: 135
  • Karma: +4/-0
  • Gender: Male
    • prefered language: C
    • Prog expertise: Good
    • View Profile
How can you copy an entity from Model Space to Paper Space
« on: January 03, 2013, 10:07:28 AM »
         Translated on C# from this web page:
         http://adndevblog.typepad.com/autocad/2013/01/how-can-you-copy-an-entity-from-model-space-to-paper-space-in-objectarx.html
         Citation:
        How can you copy an entity from Model Space to Paper Space in ObjectARX?
        By Gopinath Taget

        To copy entities from Model Space to Paper Space in ObjectARX is actually quite straightforward.
        You can utilize the services of deepCloneObject() function to copy your select entities from Model Space into Paper Space.
        This is useful in situations where for example an older drawing contains a title block and you want to copy that title block into Paper Space.
        The following code defines command called "MS2PS". Simply select entities you want to copy over using a "window" style of selection:


        // This is command 'MS2PS'
Code: [Select]
        [CommandMethod("MS2PS")]
        public void ms2ps()
        {
            Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            Editor ed = doc.Editor;
            Database db = doc.Database;
            Transaction tr = doc.TransactionManager.StartTransaction();

            using (tr)
            {

                PromptPointOptions ppo = new PromptPointOptions("\nFirst corner: ");
                PromptPointResult ppr = ed.GetPoint(ppo);
                if (ppr.Status != PromptStatus.OK) return;
                PromptCornerOptions pco = new PromptCornerOptions("\nOther corner: ", ppr.Value);
                PromptPointResult pcr = ed.GetCorner(pco);
                if (pcr.Status != PromptStatus.OK) return;
                Point3d p1 = ppr.Value;
                Point3d p2 = pcr.Value;
                if (p1.X == p2.X || p1.Y == p2.Y)
                {
                    ed.WriteMessage("\nInvalid coordinate specification");
                    return;
                }//end getcorner
                PromptSelectionResult res = ed.SelectCrossingWindow(p1, p2);
                if (res.Status != PromptStatus.OK) return;
                SelectionSet sset = res.Value;
                if (sset.Count == 0) return;
                ObjectIdCollection idps = new ObjectIdCollection();
                foreach (SelectedObject obj in sset)
                    idps.Add(obj.ObjectId);
                // Get the BlockTable
                BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
                BlockTableRecord btr = (BlockTableRecord)tr.GetObject(bt[BlockTableRecord.PaperSpace], OpenMode.ForWrite);
                db.DeepCloneObjects(idps, btr.ObjectId, new IdMapping(), true);

                tr.Commit();
            }
        }