Author Topic: Change the name of a certain block  (Read 2080 times)

0 Members and 1 Guest are viewing this topic.

babak56281

  • Guest
Change the name of a certain block
« on: July 10, 2015, 05:39:43 PM »
Hello to every one in theswamp.org
In advance, I should say thank you to all persons that share their knowledge here.
This is my first topic and I ask all friends to help me. I want to change the name of a certain block by VB .Net but I have not been successful yet. Can anyone help me to do this? Tnx

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Change the name of a certain block
« Reply #1 on: July 10, 2015, 09:43:03 PM »
Perhaps have a play with something like this.

Code - C#: [Select]
  1. // (C) CodeHimBelonga kdub 2015/07/11  
  2. //
  3.  
  4. using System;
  5. using Autodesk.AutoCAD.Runtime;
  6. using Autodesk.AutoCAD.ApplicationServices;
  7. using Autodesk.AutoCAD.DatabaseServices;
  8. using Autodesk.AutoCAD.Geometry;
  9. using Autodesk.AutoCAD.EditorInput;
  10. using AcadApp = Autodesk.AutoCAD.ApplicationServices.Core.Application;
  11.  
  12. [assembly: CommandClass(typeof (kdubRenameBlock.MyCommands))]
  13.  
  14. namespace kdubRenameBlock
  15. {
  16.     public class MyCommands
  17.     {
  18.         [CommandMethod("Test01", CommandFlags.Modal)]
  19.         public void Test01()
  20.         {
  21.             // get current document, database and editor.
  22.             var doc = AcadApp.DocumentManager.MdiActiveDocument;
  23.             var db = doc.Database;
  24.             var ed = doc.Editor;
  25.  
  26.             // establish variable
  27.             const string existingBlockName = "blockold";
  28.             const string renamedBlockName = "blocknew";
  29.  
  30.             //start a document transaction.
  31.             using (Transaction tr = db.TransactionManager.StartTransaction()) {
  32.                 // open the blocktable for reading.
  33.                 var bt = tr.GetObject(db.BlockTableId, OpenMode.ForWrite)
  34.                          as BlockTable;
  35.  
  36.                 // affirm that existingBlockName exists.
  37.                 if (!bt.Has(existingBlockName)) {
  38.                     ed.WriteMessage(
  39.                                     "\nBlock '{0}' is not available in the current drawing.",
  40.                                     existingBlockName);
  41.                     return;
  42.                 }
  43.  
  44.                 // affirm that renamedBlockName does NOT exist.
  45.                 if (bt.Has(renamedBlockName)) {
  46.                     ed.WriteMessage(
  47.                                     "\nBlock '{0}' already exists in the current drawing.",
  48.                                     renamedBlockName);
  49.                     return;
  50.                 }
  51.  
  52.                 // open the block tableRecord for the existingBlockName
  53.                 var blockTableRecordId = bt[existingBlockName];
  54.                 var btr = tr.GetObject(blockTableRecordId, OpenMode.ForWrite)
  55.                           as BlockTableRecord;
  56.  
  57.                 ed.WriteMessage("\nDXF name: " + btr.ObjectId.ObjectClass.DxfName);
  58.                 ed.WriteMessage("\nObjectID: " + btr.ObjectId.ToString());
  59.                 ed.WriteMessage("\nBlockName: " + btr.Name);
  60.  
  61.                 // rename the block
  62.                 btr.Name = renamedBlockName;
  63.  
  64.                 tr.Commit();
  65.  
  66.                 ed.WriteMessage("\nObjectID: " + btr.ObjectId.ToString());
  67.                 ed.WriteMessage("\nBlockName: " + btr.Name);
  68.             }
  69.         }
  70.     }
  71. }
  72.  

If you really want to use VB.net I'll leave the translation to you.

enjoy !


« Last Edit: July 10, 2015, 10:08:34 PM by Kerry »
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.

babak56281

  • Guest
Re: Change the name of a certain block
« Reply #2 on: July 11, 2015, 07:46:57 AM »
Dear Kerry

Thank you for your nice code and the results. Would you please to leave the VB code? tnx

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Change the name of a certain block
« Reply #3 on: July 11, 2015, 05:03:51 PM »
Untested and not reviewed
http://www.developerfusion.com/tools/convert/csharp-to-vb/
http://converter.telerik.com/
http://www.carlosag.net/tools/codetranslator/
http://lmgtfy.com/?q=translate+c%23+to+vb.net

Code - vb.net: [Select]
  1. ' (C) CodeHimBelonga kdub 2015/07/11  
  2. '
  3.  
  4. Imports Autodesk.AutoCAD.Runtime
  5. Imports Autodesk.AutoCAD.ApplicationServices
  6. Imports Autodesk.AutoCAD.DatabaseServices
  7. Imports Autodesk.AutoCAD.Geometry
  8. Imports Autodesk.AutoCAD.EditorInput
  9. Imports AcadApp = Autodesk.AutoCAD.ApplicationServices.Core.Application
  10.  
  11. <Assembly: CommandClass(GetType(kdubRenameBlock.MyCommands))>
  12.  
  13. Namespace kdubRenameBlock
  14.         Public Class MyCommands
  15.                 <CommandMethod("Test01", CommandFlags.Modal)> _
  16.                 Public Sub Test01()
  17.                         ' get current document, database and editor.
  18.                         Dim doc = AcadApp.DocumentManager.MdiActiveDocument
  19.                         Dim db = doc.Database
  20.                         Dim ed = doc.Editor
  21.  
  22.                         ' establish variable
  23.                         Const  existingBlockName As String = "blockold"
  24.                         Const  renamedBlockName As String = "blocknew"
  25.  
  26.                         'start a document transaction.
  27.                         Using tr As Transaction = db.TransactionManager.StartTransaction()
  28.                                 ' open the blocktable for reading.
  29.                                 Dim bt = TryCast(tr.GetObject(db.BlockTableId, OpenMode.ForWrite), BlockTable)
  30.  
  31.                                 ' affirm that existingBlockName exists.
  32.                                 If Not bt.Has(existingBlockName) Then
  33.                                         ed.WriteMessage(vbLf & "Block '{0}' is not available in the current drawing.", existingBlockName)
  34.                                         Return
  35.                                 End If
  36.  
  37.                                 ' affirm that renamedBlockName does NOT exist.
  38.                                 If bt.Has(renamedBlockName) Then
  39.                                         ed.WriteMessage(vbLf & "Block '{0}' already exists in the current drawing.", renamedBlockName)
  40.                                         Return
  41.                                 End If
  42.  
  43.                                 ' open the block tableRecord for the existingBlockName
  44.                                 Dim blockTableRecordId = bt(existingBlockName)
  45.                                 Dim btr = TryCast(tr.GetObject(blockTableRecordId, OpenMode.ForWrite), BlockTableRecord)
  46.  
  47.                                 ed.WriteMessage(vbLf & "DXF name: " & Convert.ToString(btr.ObjectId.ObjectClass.DxfName))
  48.                                 ed.WriteMessage(vbLf & "ObjectID: " & btr.ObjectId.ToString())
  49.                                 ed.WriteMessage(vbLf & "BlockName: " & Convert.ToString(btr.Name))
  50.  
  51.                                 ' rename the block
  52.                                 btr.Name = renamedBlockName
  53.  
  54.                                 tr.Commit()
  55.  
  56.                                 ed.WriteMessage(vbLf & "ObjectID: " & btr.ObjectId.ToString())
  57.                                 ed.WriteMessage(vbLf & "BlockName: " & Convert.ToString(btr.Name))
  58.                         End Using
  59.                 End Sub
  60.         End Class
  61. End Namespace
  62.  
  63.  
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.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Change the name of a certain block
« Reply #4 on: July 12, 2015, 10:29:27 PM »
non-breaking change;
Was doing some refactoring of this and realised that the comment

Code - C#: [Select]
  1. //start a document transaction.

should read

Code - C#: [Select]
  1. //start a database transaction.


I've made an appointment with my flagellator.

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.

owenwengerd

  • Bull Frog
  • Posts: 451
Re: Change the name of a certain block
« Reply #5 on: July 13, 2015, 12:44:17 PM »
I've made an appointment with my flagellator.

The flagellator will see you now.  :-D

Keith Brown

  • Swamp Rat
  • Posts: 601
Re: Change the name of a certain block
« Reply #6 on: July 13, 2015, 03:33:45 PM »
From Wikipedia

"The last person flogged in Australia was William John O'Meally in 1958 in Melbourne's Pentridge Prison."
Keith Brown | AutoCAD MEP Blog | RSS Feed
AutoCAD MEP 2014 / Revit MEP 2014 / EastCoast CAD/CAM addon / Visual Studio 2013