ObjectOverrule with a custom DeepClone and change BlockTableRecord

ObjectOverrule with a custom DeepClone and change BlockTableRecord

dix
Participant Participant
996 Views
1 Reply
Message 1 of 2

ObjectOverrule with a custom DeepClone and change BlockTableRecord

dix
Participant
Participant

I'm trying to create a routine that
change BlockTableRecord in BlockReference
when copying
use ObjectOverrule with a custom DeepClone

source block with attributes

everything turns out well

but then when you manual change the new block of a fatal error occurs
new block - rotated, scaled, change attributes, and copy to clipboard - fatal error

please correct my code

 

look at the line
bref.BlockTableRecord = newbtr.Id;

 

using System;
using System.Text;
using System.ComponentModel;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;

using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Windows;
using Autodesk.AutoCAD.GraphicsInterface;

using MgdAcApplication = Autodesk.AutoCAD.ApplicationServices.Application;
using DSe = Autodesk.AutoCAD.DatabaseServices;


[assembly: ExtensionApplication(typeof(test20_Namespace.Init))]

[assembly: CommandClass(typeof(test20_Namespace.Commands))]


namespace test20_Namespace
{
    public class Init : Autodesk.AutoCAD.Runtime.IExtensionApplication
    {
        public void Initialize()
        {
            ObjectOverrule.AddOverrule(RXClass.GetClass(typeof(Autodesk.AutoCAD.DatabaseServices.BlockReference)),
                CopyOverrule.theOverrule, true);

            DrawableOverrule.Overruling = true;

        }

        public void Terminate()
        {
        }
    }


    public class CopyOverrule : ObjectOverrule
    {
        static public CopyOverrule theOverrule = new CopyOverrule();

        public CopyOverrule()
        {
        }

        public override DBObject DeepClone(
          DBObject dbObject, DBObject ownerObject,
          IdMapping idMap, bool isPrimary
        )
        {
            // First we deep clone the object via the parent
            DBObject res = base.DeepClone(dbObject, ownerObject, idMap, isPrimary);


            // Check if its our block
            if (res != null)
            {
                Database db = HostApplicationServices.WorkingDatabase;

                using (Transaction tr = db.TransactionManager.StartTransaction())
                {
                    BlockTable blockTable = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);

                    // Get BlockRefrence object
                    BlockReference bref = (BlockReference)res;
                    // find a back reference
                    BlockTableRecord btrec = (BlockTableRecord)tr.GetObject(bref.BlockTableRecord, OpenMode.ForRead);

                    BlockTableRecord newbtr = new BlockTableRecord();
                    blockTable.UpgradeOpen();
                    newbtr.Name = "*U";
                    ObjectId newBtrId = blockTable.Add(newbtr);
                    tr.AddNewlyCreatedDBObject(newbtr, true);

                    ObjectIdCollection ids = new ObjectIdCollection();
                    foreach (ObjectId id in btrec)
                    {
                        ids.Add(id);
                    }
                    IdMapping idM = new IdMapping();
                    db.DeepCloneObjects(ids, newBtrId, idM, true);


                    bref.BlockTableRecord = newbtr.Id;

                    tr.Commit();
                }
            }

            return res;
        }
    }


    public class Commands
    {
        static bool overruling = true;

        [CommandMethod("GOO2")]
        public static void GripOverruleOnOff()
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Editor ed = doc.Editor;

            if (overruling)
            {
                ObjectOverrule.RemoveOverrule(RXClass.GetClass(typeof(Autodesk.AutoCAD.DatabaseServices.BlockReference)),
                    CopyOverrule.theOverrule);
            }
            else
            {
                ObjectOverrule.AddOverrule(RXClass.GetClass(typeof(Autodesk.AutoCAD.DatabaseServices.BlockReference)),
                    CopyOverrule.theOverrule, true);
            }

            DrawableOverrule.Overruling = !overruling;
            overruling = !overruling;

            ed.WriteMessage("\nEnable {0}.", overruling ? "on" : "off");
        }
    }
}

 

0 Likes
Accepted solutions (1)
997 Views
1 Reply
Reply (1)
Message 2 of 2

dix
Participant
Participant
Accepted solution

sorry

move line in the two lines below 

after the transaction

 

bref.BlockTableRecord = newbtr.Id;

thanx

0 Likes