Clone() is generally not recommended for copying
objects, if that's the intent of your code.
Try it with text that has an extension dictionary (e.g.,
text with a field), and the reason why should be clear.
To do what the COPY command does, you need to use
Database.DeepCloneObjects().
/.......
using( Transaction tr = db.TransactionManager.StartTransaction() )
{
DBText master = tr.GetObject( prEntRes.ObjectId,
OpenMode.ForRead, true ) as DBText;
ObjectIdCollection ids = new ObjectIdCollection();
ids.Add( master.ObjectId );
using( IdMapping mapping = new IdMapping() )
{
db.DeepCloneObjects( ids, master.BlockId, mapping, false );
ObjectId cloneId = mapping[master.ObjectId].Value;
DBText cloned = tr.GetObject( cloneId, OpenMode.ForWrite ) as DBText;
cloned.Position += new Vector3d( 0.0, 5.0, 0.0 );
}
tr.Commit();
}
I've been trying for an hour (or more) to post this as an answer on another forum
... unfortunately my chinese is inadequate
So, 'cause it's still on my clipboard ...
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
using AcadApp = Autodesk.AutoCAD.ApplicationServices.Application;
[assembly: CommandClass(typeof(ACAD2008_VS2008_CSharp_kdub_testing.Copy_Move))]
namespace ACAD2008_VS2008_CSharp_kdub_testing
{
public class Copy_Move
{
[CommandMethod("Cmd01")]
static public void test01()
{
Database db = HostApplicationServices.WorkingDatabase;
Editor ed = AcadApp.DocumentManager.MdiActiveDocument.Editor;
PromptEntityOptions prEntOpts = new PromptEntityOptions("\nSelect a TEXT");
prEntOpts.SetRejectMessage("\nSelected entity must be of type TEXT");
prEntOpts.AddAllowedClass(typeof(DBText), false);
PromptEntityResult prEntRes = ed.GetEntity(prEntOpts);
if (prEntRes.Status != PromptStatus.OK)
return ;
ed.WriteMessage("\n MasterID is : " + prEntRes.ObjectId.ToString());
ObjectId slaveID = ObjectId.Null;
using (Transaction tr = db.TransactionManager.StartTransaction())
{
DBText master = tr.GetObject(prEntRes.ObjectId,
OpenMode.ForRead, true) as DBText;
DBText slave = master.Clone() as DBText;
BlockTable bt = tr.GetObject(db.BlockTableId,
OpenMode.ForRead, false) as BlockTable;
BlockTableRecord btrModelspace = tr.GetObject(bt[BlockTableRecord.ModelSpace],
OpenMode.ForWrite, false) as BlockTableRecord;
Point3d mpos = master.Position;
Vector3d offset = new Vector3d(0.0, 50.0, 0.0);
Point3d spos = mpos.Add(offset);
slave.Position = spos;
slaveID = btrModelspace.AppendEntity(slave);
tr.AddNewlyCreatedDBObject(slave, true);
tr.Commit();
}
ed.WriteMessage("\n slaveID is : " + slaveID.ToString());
}
}
}
///kdub
Command: netload
Command: cmd01
Select a TEXT:
MasterID is : (2130587720)
slaveID is : (2130587832)