.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

clone Dynamic anonymous block

1 REPLY 1
SOLVED
Reply
Message 1 of 2
Amremad
648 Views, 1 Reply

clone Dynamic anonymous block

Hello all 

iam using this code for copy any entity in autocad 

 

        public static Entity Copy(Entity entity, Point3d BasePoint, Point3d SecondPoint)
        {
            Matrix3d UCS = Application.DocumentManager.MdiActiveDocument.Editor.CurrentUserCoordinateSystem;
            Entity ent;
            BasePoint = BasePoint.TransformBy(UCS.Inverse());
            SecondPoint = SecondPoint.TransformBy(UCS.Inverse());
          
            using (Transaction acTrans = Application.DocumentManager.MdiActiveDocument.TransactionManager.StartTransaction())
            {
                BlockTableRecord btr = (BlockTableRecord)acTrans.GetObject(Application.DocumentManager.MdiActiveDocument.Database.CurrentSpaceId, OpenMode.ForWrite);
                ent = (Entity)entity.Clone();
                btr.AppendEntity(ent);
                acTrans.AddNewlyCreatedDBObject(ent, true);

                Vector3d disp = BasePoint.TransformBy(UCS).GetVectorTo(SecondPoint.TransformBy(UCS));
                ent.TransformBy(Matrix3d.Displacement(disp));

                acTrans.Commit();
            }
            return ent;
        }

but i have a problem with copy anonymous block . because this code copy the block name with starting *U.

i need any function copy the effective block with anonymous block properties that i selected to copy.

 

or how can i insert block but has the anonymous block properties (attributes and parametric dimensions ....)

 

1 REPLY 1
Message 2 of 2
_gile
in reply to: Amremad

Hi,

 

You should not use the Clone() method which returns a shallow copy, you should use DeepCloneobjects instead.

 

One other thing, when you use DBOjects as arguments in methods, you should use the top transaction instead of starting a new one.

 

        public static Entity Copy(Transaction tr, Entity source, Point3d basePoint, Point3d destinationPoint)
        {
            var id = source.ObjectId;
            var ids = new ObjectIdCollection();
            ids.Add(id);
            var mapping = new IdMapping();
            source.Database.DeepCloneObjects(ids, source.OwnerId, mapping, false);
            if (mapping[id].IsCloned)
            {
                var clone = (Entity)tr.GetObject(mapping[id].Value, OpenMode.ForWrite);
                clone.TransformBy(Matrix3d.Displacement(basePoint.GetVectorTo(destinationPoint)));
                return clone;
            }
            return null;
        }

 

 

Another (safer) practice is to use only ObjectId as arguments and return values. In case of deep cloning you do not need any nested transaction. In your case, you can open the cloned entity in the callin method transaction to transform it.

 

        public static ObjectId Copy(ObjectId sourceId, ObjectId ownerId)
        {
            var ids = new ObjectIdCollection();
            ids.Add(sourceId);
            var mapping = new IdMapping();
            sourceId.Database.DeepCloneObjects(ids, ownerId, mapping, false);
            return mapping[sourceId].Value;
        }

 

 



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Forma Design Contest


AutoCAD Beta