I've managed to copy Entities from Model Space to Paper Space programmatically (without viewport). New copied objects appears far away from my paper list. So, I need to fit the whole set of copied Entities to paper boundaries and change the scale of Entities according to its extents and paper size. I couldnt find a correct way to do this in C#, hope for your help.
Here is my code:
[CommandMethod("test")]
public static void copytolist()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Editor ed = doc.Editor;
Database db = doc.Database;
//start transaction
Transaction tr = doc.TransactionManager.StartTransaction();
using (tr)
{
//get the crossing windows coordinates:
PromptPointOptions ppo = new PromptPointOptions("\nFirst corner: ");
PromptPointResult ppr = ed.GetPoint(ppo);
if (ppr.Status != PromptStatus.OK) return;
PromptCornerOptions pco = new PromptCornerOptions("\nOther corner: ", ppr.Value);
PromptPointResult pcr = ed.GetCorner(pco);
if (pcr.Status != PromptStatus.OK) return;
Point3d p1 = ppr.Value;
Point3d p2 = pcr.Value;
if (p1.X == p2.X || p1.Y == p2.Y)
{
ed.WriteMessage("\nInvalid coordinate specification");
return;
}
//crossing windows from coordinates:
PromptSelectionResult res = ed.SelectCrossingWindow(p1, p2);
if (res.Status != PromptStatus.OK)
return;
SelectionSet sset = res.Value;
if (sset.Count == 0)
return;
// take all the entities from the cross selection:
ObjectIdCollection ids = new ObjectIdCollection();
foreach (SelectedObject obj in sset)
ids.Add(obj.ObjectId);
//gettin and settin desired layout and clone objects
LayoutManager acLayoutMgr = LayoutManager.Current;
DBDictionary layoutDic = tr.GetObject(db.LayoutDictionaryId, OpenMode.ForWrite, false) as DBDictionary;
foreach (DBDictionaryEntry entry in layoutDic)
{
ObjectId layoutId = entry.Value;
Layout layout = tr.GetObject(layoutId, OpenMode.ForWrite) as Layout;
if (layout.LayoutName == "test")
{
acLayoutMgr.CurrentLayout=layout.LayoutName;
BlockTableRecord btr = (BlockTableRecord)tr.GetObject(layout.BlockTableRecordId, OpenMode.ForWrite);
IdMapping acIdMap = new IdMapping();
//verify if there are entities to be copied:
if (ids.Count > 0)
{
ed.WriteMessage("\nYES, there are entities to be copied: {0} entities!", ids.Count);
db.DeepCloneObjects(ids, btr.ObjectId, acIdMap, false);
}
else
ed.WriteMessage("\nNO, there are no entities to be copied!");
//loop through the newly created (clone) objects
foreach (ObjectId clone in ids)
{
IdPair pair1 = acIdMap[clone];
Entity ent = tr.GetObject(pair1.Value, OpenMode.ForWrite) as Entity;
//now i need to move new objects to fit my list and change scale of objects, depending from its size and list size
////////////////////////////////////////////////////////////////
}
}
}
tr.Commit();
}
}
Also, DWG file with drawing and list examples is attached.
Thanks in advance!