.NET

Reply
*Expert Elite*
Hallex
Posts: 1,555
Registered: ‎10-08-2008
Message 11 of 15 (114 Views)

Re: Copy of BlockTableRecord (save as)

09-04-2012 12:11 AM in reply to: RichardCammeray

Richard, try first to create 2 similar blocks

with different names manually

and set color property for all subentities

in the first block to byblock and for the second one

to bylayer, then see what happens

 

~'J'~

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Active Contributor
RichardCammeray
Posts: 43
Registered: ‎12-08-2010
Message 12 of 15 (112 Views)

Re: Copy of BlockTableRecord (save as)

09-04-2012 12:49 AM in reply to: Hallex

I have tried to create manually two blocks as you advised and inserted them into drawing (two per each one).

Then I run my version of  CopyXBref command. Unfortunately original block had change colour as well.

 

Thank you for your patience

 

Richard

*Expert Elite*
Hallex
Posts: 1,555
Registered: ‎10-08-2008
Message 13 of 15 (104 Views)

Re: Copy of BlockTableRecord (save as)

09-04-2012 02:07 AM in reply to: RichardCammeray

No problems,

Now you try this code on your manually created block<

but create layer before, see comments at the end of code

 

        [CommandMethod("CopyXB")]
        public void CopyBlockDef()
        {
            Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
                PromptEntityOptions peo = new PromptEntityOptions("\nSelect parent block instance to copy: ");
                peo.SetRejectMessage("\nMust be a type of the BlockReference!");
                peo.AddAllowedClass(typeof(BlockReference), true);
                PromptEntityResult per = ed.GetEntity(peo);

                if (per.Status != PromptStatus.OK) return;

                BlockReference bref = (BlockReference)tr.GetObject(per.ObjectId, OpenMode.ForWrite);
                BlockTableRecord btr = (BlockTableRecord)tr.GetObject(bref.BlockTableRecord, OpenMode.ForRead);
                ed.WriteMessage("\nBlock Selected with name: {0}", btr.Name);
                PromptStringOptions pso = new PromptStringOptions("\nEnter new block name: ");
                pso.AllowSpaces = true;
                PromptResult sres = ed.GetString(pso);
                if (sres.Status != PromptStatus.OK) return;
                string newname = sres.StringResult;
                if (bt.Has(newname))
                {
                    ed.WriteMessage("\nBlock with name: {0} already exist, try again", newname);
                    return;
                }

                BlockTableRecord newbtr = new BlockTableRecord();
                bt.UpgradeOpen();

                newbtr = (BlockTableRecord)btr.DeepClone(bt, new IdMapping(), true);

                newbtr.Name = newname;
                bt.Add(newbtr);
                ed.Regen();
                //----------------------------------------------------------------//

                foreach (ObjectId id in newbtr)
                {
                    Entity ent = tr.GetObject(id, OpenMode.ForRead) as Entity;
                    if (ent == null) continue;
                    ent.UpgradeOpen();
                    ent.ColorIndex = 256;
                }


                tr.AddNewlyCreatedDBObject(newbtr, true);

                //Change BlockReference
                bref.BlockTableRecord = newbtr.ObjectId;
                // change layer, "RedColoredLayer" must be created before
                bref.Layer = "RedColoredLayer";
 
                tr.Commit();

            }
        }

 

~'J'~

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Active Contributor
RichardCammeray
Posts: 43
Registered: ‎12-08-2010
Message 14 of 15 (83 Views)

Re: Copy of BlockTableRecord (save as)

09-04-2012 09:38 PM in reply to: Hallex

Did not work either.

I set subentities in original block to color 3 but after running this command subentities were assigned to color ByLayer (ColorIndex = 256) in new and original block.

As I said at the beginning of the thread, ObjectIds of subentities in original and new blocks are the same and that is an issue.

 

I decided to create new empty Block and use DeepCloneObjects instead. Example below.

Hallex, Thank you for your help and very good examples.

 

Richard

 

        [CommandMethod("CopyXC")]
        public void testCopyBlockReference()
        {
            Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
                PromptEntityOptions peo = new PromptEntityOptions("\nSelect main block instance to copy: ");
                peo.SetRejectMessage("\nMust be a type of the BlockReference!");
                peo.AddAllowedClass(typeof(BlockReference), true);
                PromptEntityResult per = ed.GetEntity(peo);

                if (per.Status != PromptStatus.OK) return;

                BlockReference bref = (BlockReference)tr.GetObject(per.ObjectId, OpenMode.ForWrite);
                BlockTableRecord btr = (BlockTableRecord)tr.GetObject(bref.BlockTableRecord, OpenMode.ForRead);
                ed.WriteMessage("\nBlock Selected with name: {0}", btr.Name);
                PromptStringOptions pso = new PromptStringOptions("\nEnter new block name: ");
                pso.AllowSpaces = true;
                PromptResult sres = ed.GetString(pso);
                if (sres.Status != PromptStatus.OK) return;
                string newname = sres.StringResult;
                if (bt.Has(newname))
                {
                    ed.WriteMessage("\nBlock with name: {0} already exist, try again", newname);
                    return;
                }

                ObjectIdCollection ids = new ObjectIdCollection();
                foreach (ObjectId id in btr)
                {
                    ids.Add(id);
                }

                BlockTableRecord newbtr = new BlockTableRecord();
                bt.UpgradeOpen();

                newbtr.Name = newname;
                ObjectId newBtrId = bt.Add(newbtr);
                tr.AddNewlyCreatedDBObject(newbtr, true);
                //----------------------------------------------------------------//


                IdMapping idMap = new IdMapping();
                db.DeepCloneObjects(ids, newBtrId, idMap, true);

                foreach (ObjectId id in newbtr)
                {
                    Entity ent = tr.GetObject(id, OpenMode.ForRead) as Entity;
                    if (ent == null) continue;
                    ent.UpgradeOpen();
                    ent.ColorIndex = 1;
                }

                //Change BlockReference
                bref.BlockTableRecord = newbtr.Id;

                tr.Commit();

            }
        }

*Expert Elite*
Hallex
Posts: 1,555
Registered: ‎10-08-2008
Message 15 of 15 (74 Views)

Re: Copy of BlockTableRecord (save as)

09-05-2012 01:23 AM in reply to: RichardCammeray

Glad you've solved it,

Cheers :smileyhappy:

 

~'J'~

_____________________________________
C6309D9E0751D165D0934D0621DFF27919

You are not logged in.

Log into access your profile, ask and answer questions, share ideas and more. Haven't signed up yet? Register

Announcements
Welcome to the new Autodesk Community!
If this is your first visit, click here to get started and make the most of the Community. Let us know what you think of the new experience in the Community Feedback Forum.

Need installation help?

Start with some of our most frequented solutions to get help installing your software.

Ask the Community