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

Copy and move to origin

5 REPLIES 5
SOLVED
Reply
Message 1 of 6
Anonymous
832 Views, 5 Replies

Copy and move to origin

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 🙂

5 REPLIES 5
Message 2 of 6
Keith.Brown
in reply to: Anonymous

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.

 

Move Object (.NET)

Message 3 of 6
Anonymous
in reply to: Keith.Brown

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?

Message 4 of 6
Keith.Brown
in reply to: Anonymous

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.

Message 5 of 6
Anonymous
in reply to: Keith.Brown

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));
                                    }
                                }
Message 6 of 6
ActivistInvestor
in reply to: Anonymous

The IDMapping that you pass to WBlockCloneObjects() holds the ObjectIds of all the copied objects after the method returns. Each IDPair in the map whose IsPrimary property is true will hold the ObjectId of a copied object in its Value property.

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

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost