Author Topic: Clone Text  (Read 4033 times)

0 Members and 1 Guest are viewing this topic.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Clone Text
« on: December 20, 2008, 11:45:12 PM »

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 ...


Code: [Select]
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

Quote
Command: netload
Command: cmd01
Select a TEXT:
 MasterID is : (2130587720)
 slaveID is : (2130587832)
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8759
  • AKA Daniel
Re: Clone Text
« Reply #1 on: December 21, 2008, 12:28:09 AM »
Outstanding example!
Thanks

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Clone Text
« Reply #2 on: December 21, 2008, 12:32:07 AM »

There seemed to be a lot of casting used Dan ... but it does get the example result I wanted
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

TonyT

  • Guest
Re: Clone Text
« Reply #3 on: December 21, 2008, 02:23:01 AM »
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().

Code: [Select]

   /.......
   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 ...


Code: [Select]
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

Quote
Command: netload
Command: cmd01
Select a TEXT:
 MasterID is : (2130587720)
 slaveID is : (2130587832)

« Last Edit: December 21, 2008, 02:37:26 AM by TonyT »

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Clone Text
« Reply #4 on: December 21, 2008, 02:35:38 AM »
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().


Point taken and understood Tony.
The original poster was a little unclear as to his usage  and explicitly asked for a 'clone' sample.
... I made 'one of those assumptions' , again  :)
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

TonyT

  • Guest
Re: Clone Text
« Reply #5 on: December 21, 2008, 03:41:42 AM »
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().


Point taken and understood Tony.
The original poster was a little unclear as to his usage  and explicitly asked for a 'clone' sample.
... I made 'one of those assumptions' , again  :)


No problem. The reason I thought the intent might be to
copy the text was because the clone was being added to
the database.