Copy Entities in code

Copy Entities in code

Anonymous
Not applicable
4,165 Views
4 Replies
Message 1 of 5

Copy Entities in code

Anonymous
Not applicable

Hi can someone please assist me here. I'm trying to copy a entity to a new location in the drawing but I want to do this all in code. Im currently playing around with PromptSelectionResult and can successfully select the entity but i want to be able to select the location in the current model space to copy this entity to.

Can someone provide me some example code on how to do this.

 

Thanks in advance

Shaban.

 

0 Likes
Accepted solutions (2)
4,166 Views
4 Replies
Replies (4)
Message 2 of 5

_gile
Consultant
Consultant

Hi,

 

You'll find some examples on how to mimic the cOPY command with code reading this reply and the following ones.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 3 of 5

Anonymous
Not applicable
Accepted solution

Thank you for the links they are helpful. But i am struggling to put it all together.

I can select the entity or entities to copy but I'm failing on executing the destination location to copy to.

Can you please provide a simple code snippet of selecting the entity then copying to a new location that i select.

I'm coding in vb.net

 

Thanks in advance.

Shaban

 

0 Likes
Message 4 of 5

_gile
Consultant
Consultant
Accepted solution

Here's a simple example.

It's C# but you should be able to convert it to VB or, better, learn C# instead of VB.

 

        [CommandMethod("CMD")]
        public static void Cmd()
        {
            var doc = Application.DocumentManager.MdiActiveDocument;
            var db = doc.Database;
            var ed = doc.Editor;

            // prompt the user to select objects
            var psr = ed.GetSelection();
            if (psr.Status != PromptStatus.OK)
                return;

            // prompt the user to specify the base point
            var ppo = new PromptPointOptions("\nSpecify base point: ");
            var ppr = ed.GetPoint(ppo);
            if (ppr.Status != PromptStatus.OK)
                return;
            var basePt = ppr.Value;

            // prompt the user to specify the desination point
            ppo.Message = "\nSpecify destination point: ";
            ppo.BasePoint = basePt;
            ppo.UseBasePoint = true;
            ppr = ed.GetPoint(ppo);
            if (ppr.Status != PromptStatus.OK)
                return;
            var destPt = ppr.Value;

            // compute the displacement matrix
            var vect = basePt.GetVectorTo(destPt).TransformBy(ed.CurrentUserCoordinateSystem);
            var disp = Matrix3d.Displacement(vect);

            using (var tr = db.TransactionManager.StartTransaction())
            {
                // copy the selected entities
                var ids = new ObjectIdCollection(psr.Value.GetObjectIds());
                var idMap = new IdMapping(); ;
                db.DeepCloneObjects(ids, db.CurrentSpaceId, idMap, false);

                // displace the copied entities
                foreach (IdPair pair in idMap)
                {
                    if (pair.IsPrimary && pair.IsCloned)
                    {
                        Entity ent = (Entity)tr.GetObject(pair.Value, OpenMode.ForWrite);
                        ent.TransformBy(disp);
                    }
                }
                tr.Commit();
            }
        }


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 5 of 5

Anonymous
Not applicable

Love ya work thank you.

0 Likes