.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

how to change block name

4 REPLIES 4
SOLVED
Reply
Message 1 of 5
imagination_s
846 Views, 4 Replies

how to change block name

Hellow Everyone

I am trying to change a block name.

First a clone the block and then i want to change the block name from DOT to DOT2

 

Here is my part of code.I know is something with block record and new block record

 

 Dim acBlkTbl As BlockTable
        acBlkTbl = myT.GetObject(acCurDb.BlockTableId, OpenMode.ForRead)
        Dim acBlkTblRec As BlockTableRecord
        Dim blkname As String = "DOT2"
        acBlkTblRec = myT.GetObject(acBlkTbl(BlockTableRecord.ModelSpace), OpenMode.ForWrite)
        acBlkTblRec.Name = blkname
        acBlkTbl.UpgradeOpen()

 

I know that something is wrong.

Please Help.Thankyou for your help

4 REPLIES 4
Message 2 of 5
SENL1362
in reply to: imagination_s

You're changing the Block==ModelSpace. I'm pretty sure you are going to regret that. 🙂

I'll recommend(not tested):
acBlkTbl.UpgradeOpen()
acBlkTblRec = myT.GetObject(acBlkTbl("DOT"), OpenMode.ForWrite)
acBlkTblRec.Name = blkname

Message 3 of 5
SENL1362
in reply to: SENL1362

or in C#:

        [CommandMethod("TestRenameBlock")]
        public void TestRenameBlock()
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;

            string oldBlockName = "DOT";
            string newBlockName = "DOT2";


            try
            {
                using (Transaction tr = db.TransactionManager.StartTransaction())
                {
                    BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
                    if (!bt.Has(oldBlockName))
                        throw new System.Exception(string.Format("Error: TestRenameBlock: Old Block not found: {0}", oldBlockName));
                    if (bt.Has(newBlockName))
                        throw new System.Exception(string.Format("Error: TestRenameBlock: New Block already exists: {0}", newBlockName));

                    BlockTableRecord oldBlock = (BlockTableRecord)tr.GetObject(bt[oldBlockName], OpenMode.ForRead);
                    oldBlock.UpgradeOpen();
                    oldBlock.Name = newBlockName;

                    tr.Commit();
                }
            }
            catch (System.Exception ex)
            {
                ed.WriteMessage(ex.Message);
            }
        }

 

Message 4 of 5
imagination_s
in reply to: SENL1362

Thankyou so much for your help 🙂
Nice code.It works
Apreciated
Thankyou again
Message 5 of 5
SENL1362
in reply to: imagination_s

My pleasure.

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

AutoCAD Inside the Factory


Autodesk Design & Make Report