After Mirroring Entity with XData, XData lost in new Entity?

After Mirroring Entity with XData, XData lost in new Entity?

swaywood
Collaborator Collaborator
1,866 Views
7 Replies
Message 1 of 8

After Mirroring Entity with XData, XData lost in new Entity?

swaywood
Collaborator
Collaborator

Hi,all:

I want to mirror an entity with XData,

But after mirroring, the new entity has no XData.

 

The following is the method, 

Could anyone tell me whether there is an easy way to reallize it?

		public static ObjectId Mirror(this ObjectId id, Point3d mirrorPt1, Point3d mirrorPt2, bool eraseSourceObject)
		{
			Line3d miLine = new Line3d(mirrorPt1, mirrorPt2);
			Matrix3d mt = Matrix3d.Mirroring(miLine);
			ObjectId mirrorId = id;
			Entity ent = (Entity)id.GetObject(OpenMode.ForWrite);
			if (eraseSourceObject == true)
				ent.TransformBy(mt);
			else
			{
				Entity entCopy = ent.GetTransformedCopy(mt);
				mirrorId = id.Database.AddToCurrentSpace(entCopy);
			}
			return mirrorId;
		}

 

0 Likes
Accepted solutions (2)
1,867 Views
7 Replies
Replies (7)
Message 2 of 8

_gile
Consultant
Consultant

Hi,

The code you show should work, the issue you have may come from elsewhere in your plugin.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 3 of 8

swaywood
Collaborator
Collaborator

Hi,Gile:

I tried with the following code, but failed.

    [CommandMethod("COOO")]
    public static void COOO()
    {
      Database db = HostApplicationServices.WorkingDatabase;
      Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
      using (Transaction trans = db.TransactionManager.StartTransaction())
      {
        Point3d ptMirr1 = new Point3d();
        Point3d ptMirr2 = ptMirr1.dPolar(90, 100);
				PromptEntityResult per= ed.GetEntity("Select a ref with xdata:");
				if (per.Status!=PromptStatus.OK)
				{
					return;
				}
        per.ObjectId.Mirror(ptMirr1, ptMirr2, false);
        trans.Commit();
      }
    }
0 Likes
Message 4 of 8

_gile
Consultant
Consultant

Works for me.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 5 of 8

Norman_Yuan
Mentor
Mentor
Accepted solution

Well, in your original post and this reply, your code uses custom extension method (Database.AddToCurrentSpace() and ObjectId.Mirror()). Since no one knows how the extension methods are implemented, the code would not help here.

 

According to the ARX/.NET API document, Entity.GetTransformedCopy() method is basically the same as doing 2 calls in one step:

 

var newEntity = TheEntity.Clone()/DeepClone();

newEntity.TransformBy();

 

You may want to consult the ARX API document for this method.

 

By default, entities derived from Entity would call Clone() method usually, but it is up to the derived object's implementation to decide to use Clone() or DeepClone(), or even does not do cloning at all. Since you did not say what type of entity you are working against, it is hard to say what is happening (you did left a vague hint when asking user to pick an entity by "Select a ref with XData", where "ref" implies the entity may not be a regular/simple entity (Line, Circle...).

 

I tested calling GetTransformedCopy() with simple entity (Circle, Line, Polyline), which has XData attached. And yes, the newly cloned and transformed entity does have the same XData, as its source entity.

 

Norman Yuan

Drive CAD With Code

EESignature

Message 6 of 8

swaywood
Collaborator
Collaborator

Hi,Giles,Yuan,

My ref. means block reference.

0 Likes
Message 7 of 8

_gile
Consultant
Consultant
Accepted solution

It looks like GetTransfomedCopy does not deep clone block references. Tou should use Database.DeepCloneObjects instead.

        public static ObjectId Mirror(this ObjectId id, Point3d mirrorPt1, Point3d mirrorPt2, bool eraseSourceObject)
        {
            Line3d miLine = new Line3d(mirrorPt1, mirrorPt2);
            Matrix3d mt = Matrix3d.Mirroring(miLine);
            ObjectId mirrorId = id;
            Entity ent = (Entity)id.GetObject(OpenMode.ForWrite);
            Database db = id.Database;
            if (!eraseSourceObject)
            {
                var ids = new ObjectIdCollection();
                ids.Add(id);
                var mapping = new IdMapping();
                db.DeepCloneObjects(ids, db.CurrentSpaceId, mapping, false);
                mirrorId = mapping[id].Value;
            }
            ent.TransformBy(mt);
            return mirrorId;
        }


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 8 of 8

swaywood
Collaborator
Collaborator

THANKS A LOT GILES AND YUAN.

0 Likes