Hello
I am trying to copy selected objects to a new DWG file and move them to the origin point. I have the user selection and copying to a new DWG sorted and all works fine, i just can't seem to get them to move to the origin point.
I thought of copying to a new drawing, making that the active document and using the editor to move the entities. I have already calculated the amount needed to move during the selection. I literally just need to move by XY when it is in the new database, but cannot figure it out!!
any help would be appreciated 🙂
Solved! Go to Solution.
Solved by Keith.Brown. Go to Solution.
Solved by Keith.Brown. Go to Solution.
You need transform the object in order to get it to move.
Vector3d moveVector = basePoint.GetVectorTo(Point3d.Origin); objectToMove.TransformBy(Matrix3d.Displacement(moveVector));
This link should help.
Hi Keith
Thanks a lot for the reply. I have tried to implement this but can only seem to get it to work in the current active document. I only want to move them in the document i have copied the geometry to (not where the original selection took place) if that makes sense?
There should be no difference in moving an entity in an active database vs moving an entity in a side database. I suspect the issue that you are having is that you have not reselected the newly pasted entities in the new database. You will need to iterate through the block table record in which you placed them, and open each entity for write and them move it to the origin. Even though you are in a side database there should not be any differences as long as this is all you are doing.
If you need any more help please post some code showing what you have so far and what is not working.
Keith, thank you very much for your prompt replies.. I'm almost there!
I am itterating through all objects now and am trying to move just the copied opjects. However, as this is working from a template, it seems to move everything. I tried to get around that by storing the documents object ids into a list prior to adding the copied components, then once they have been copied, only move the new ids:
using (Database newdb = new Database(false, true)) { newdb.ReadDwgFile(newFileLoc, System.IO.FileShare.ReadWrite, true, ""); using (Transaction newtr = newdb.TransactionManager.StartTransaction()) { BlockTable newbt = (BlockTable)newtr.GetObject(newdb.BlockTableId, OpenMode.ForRead); BlockTableRecord newbtr = (BlockTableRecord)newtr.GetObject(newbt[BlockTableRecord.ModelSpace], OpenMode.ForWrite); List<ObjectId> toIgnore = new List<ObjectId>(); foreach (ObjectId i in newbtr) { toIgnore.Add(i); } if (acObjIdColl.Count != 0) { IdMapping iMap = new IdMapping(); newdb.WblockCloneObjects(acObjIdColl, newbtr.ObjectId, iMap, DuplicateRecordCloning.MangleName, false); } foreach(ObjectId i in newbtr) { foreach(ObjectId c in toIgnore) { if (i != c) { Entity g = newtr.GetObject(i, OpenMode.ForWrite) as Entity; Point3d acPt3d = new Point3d(minX, minY, 0); Vector3d acVec3d = acPt3d.GetVectorTo(new Point3d(0, 0, 0)); g.TransformBy(Matrix3d.Displacement(acVec3d)); } } } newdb.SaveAs(newFileLoc, DwgVersion.Current); newtr.Commit(); } }
The copied objects are now positioning correctly on the template.. However, it seems to shift every entity miles away from the origin. Can you see something i have done wrong there?
EDIT. My bad, think i have found the issue!!! Here is the new code for the section i got wrong... :
foreach(ObjectId i in newbtr) { bool toMove = true; foreach(ObjectId c in toIgnore) { if (i == c) { toMove = false; } } if (toMove) { Entity g = newtr.GetObject(i, OpenMode.ForWrite) as Entity; Point3d acPt3d = new Point3d(minX, minY, 0); Vector3d acVec3d = acPt3d.GetVectorTo(new Point3d(0, 0, 0)); g.TransformBy(Matrix3d.Displacement(acVec3d)); } }