Author Topic: Getting and setting the DimBlk ID used for Closed Filled Arrow  (Read 3224 times)

0 Members and 1 Guest are viewing this topic.

kdub_nz

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 2139
  • class keyThumper<T>:ILazy<T>
Getting and setting the DimBlk ID used for Closed Filled Arrow
« on: February 27, 2016, 02:56:35 AM »
this is based on a question here :
http://forums.autodesk.com/t5/net/dimblk-objectid-for-closed-filled-arrow/m-p/6052755#U6052755

I'm posting here because the code pane is "better"

The issue has several parts.
  • the dim arrow blocks may not be in the database
  • ... so add the block.
  • the default 'Closed filled' is named with an empty string and the ObjectId will be Null
  • look at db.Dimblk = ObjectId.Null;  for 'Closed filled'
  • The code is proof of concept because I'm unsure of the intended final usage.

If you're not using C#6+  in VS 2015 then the String interpolation used for WriteLine formatting will need to be modified back to the old formatting.
If you're using VB then http://converter.telerik.com/ will be your friend.

Code - C#: [Select]
  1. // Proof of concept code 2016-02-27
  2. // codehimbelonga kdub @ theSwamp
  3.  
  4. using System;
  5.  
  6. using Autodesk.AutoCAD.ApplicationServices;
  7. using Autodesk.AutoCAD.DatabaseServices;
  8. using Autodesk.AutoCAD.EditorInput;
  9. using Autodesk.AutoCAD.Geometry;
  10. using Autodesk.AutoCAD.Runtime;
  11.  
  12. using AcadApp = Autodesk.AutoCAD.ApplicationServices.Core.Application;
  13.  
  14. [assembly: CommandClass(typeof(DimBlocks.MyCommands))]
  15.  
  16. namespace DimBlocks {
  17.     public class MyCommands {
  18.         [CommandMethod("DoIt", CommandFlags.Modal)]
  19.         public void MyCommand() {
  20.             TM11();
  21.             AcadApp.DocumentManager.MdiActiveDocument.Editor.WriteMessage(
  22.                 "\n\n ====================================\n");
  23.             TM12();
  24.         }
  25.  
  26.         private static void TM11() {
  27.             var doc = AcadApp.DocumentManager.MdiActiveDocument;
  28.             var db = AcadApp.DocumentManager.MdiActiveDocument.Database;
  29.             var ed = doc.Editor;
  30.             var arrowName = string.Empty;
  31.  
  32.             // Get the current DIMBLK setting
  33.             arrowName = (string)AcadApp.GetSystemVariable("DIMBLK");
  34.             ed.WriteMessage($"\nDIMBLK is: {arrowName} \nID is: {db.Dimblk}\n");
  35.  
  36.             // Set to '_OPEN
  37.             AcadApp.SetSystemVariable("DIMBLK", "_OPEN");
  38.             arrowName = (string)AcadApp.GetSystemVariable("DIMBLK");
  39.             ed.WriteMessage($"\nDIMBLK is: {arrowName} \nID is: {db.Dimblk}\n");
  40.  
  41.             // Set to default 'Closed filled'
  42.             db.Dimblk = ObjectId.Null;
  43.             arrowName = (string)AcadApp.GetSystemVariable("DIMBLK");
  44.             ed.WriteMessage($"\nDIMBLK is: {arrowName} \nID is: {db.Dimblk}\n");
  45.  
  46.             // Set to '_DOT
  47.             AcadApp.SetSystemVariable("DIMBLK", "_DOT");
  48.             arrowName = (string)AcadApp.GetSystemVariable("DIMBLK");
  49.             ed.WriteMessage($"\nDIMBLK is: {arrowName} \nID is: {db.Dimblk}\n");
  50.         }
  51.  
  52.         private static void TM12() {
  53.             var doc = AcadApp.DocumentManager.MdiActiveDocument;
  54.             var db = AcadApp.DocumentManager.MdiActiveDocument.Database;
  55.             var ed = doc.Editor;
  56.             var arrowName = string.Empty;
  57.             var dimBlockId = ObjectId.Null;
  58.  
  59.             arrowName = (string)AcadApp.GetSystemVariable("DIMBLK");
  60.             ed.WriteMessage($"\nDIMBLK is: {arrowName} \nID is: {db.Dimblk}\n");
  61.  
  62.             dimBlockId = GetDimBlockId("DIMBLK", "_DOTBLANK");
  63.             db.Dimblk = dimBlockId;
  64.             arrowName = (string)AcadApp.GetSystemVariable("DIMBLK");
  65.             ed.WriteMessage($"\nDIMBLK is: {arrowName} \nID is: {dimBlockId}\n");
  66.  
  67.             dimBlockId = GetDimBlockId("DIMBLK", "");
  68.             db.Dimblk = dimBlockId;
  69.             arrowName = (string)AcadApp.GetSystemVariable("DIMBLK");
  70.             ed.WriteMessage($"\nDIMBLK is: {arrowName} \nID is: {dimBlockId}\n");
  71.         }
  72.  
  73.  
  74.         private static ObjectId GetDimBlockId(string dimBlkX, string newArrowName) {
  75.             ObjectId arrowObjId = ObjectId.Null;
  76.  
  77.             if(string.IsNullOrEmpty(newArrowName)) {
  78.                 return ObjectId.Null;
  79.             }
  80.  
  81.             var db = AcadApp.DocumentManager.MdiActiveDocument.Database;
  82.             var originalArrowName = (string)AcadApp.GetSystemVariable(dimBlkX);
  83.  
  84.             // Force AutoCAD to add the block to the db.
  85.             AcadApp.SetSystemVariable(dimBlkX, newArrowName);
  86.  
  87.             // Restore the previous setting
  88.             AcadApp.SetSystemVariable(dimBlkX, originalArrowName);
  89.  
  90.             using(Transaction tr = db.TransactionManager.StartTransaction()) {
  91.                 var bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
  92.                 arrowObjId = bt[newArrowName];
  93.                 tr.Commit();
  94.             }
  95.             return arrowObjId;
  96.         }
  97.     }
  98.  
  99.     public class InitMyCommands : Autodesk.AutoCAD.Runtime.IExtensionApplication {
  100.         public void Initialize() {
  101.             AcadApp.DocumentManager.MdiActiveDocument.Editor.WriteMessage("\n\t\t\t*** type DOIT to run test \t***");
  102.         }
  103.  
  104.         public void Terminate() {
  105.             AcadApp.DocumentManager.MdiActiveDocument.Editor.WriteMessage("Cleaning up...");
  106.         }
  107.     }
  108. }
  109.  


re the image:
The empty dimblk names and null ID's are for the default 'Closed filled' arrow.
« Last Edit: February 27, 2016, 03:01:23 AM by kdub »
Called Kerry in my other life
Retired; but they dragged me back in !

I live at UTC + 13.00

---
some people complain about loading the dishwasher.
Sometimes the question is more important than the answer.

Keith Brown

  • Swamp Rat
  • Posts: 601
Re: Getting and setting the DimBlk ID used for Closed Filled Arrow
« Reply #1 on: February 27, 2016, 10:00:03 AM »
Not real relevant to the thread but since you mentioned it.  The converter over at http://converter.telerik.com does not convert c#6.0 or the new VB stuff.  At least that is what I have found since I use it quite often.  I code in VB at work and C# at home.
Keith Brown | AutoCAD MEP Blog | RSS Feed
AutoCAD MEP 2014 / Revit MEP 2014 / EastCoast CAD/CAM addon / Visual Studio 2013

kdub_nz

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 2139
  • class keyThumper<T>:ILazy<T>
Re: Getting and setting the DimBlk ID used for Closed Filled Arrow
« Reply #2 on: February 27, 2016, 09:41:18 PM »
Not real relevant to the thread but since you mentioned it.  The converter over at http://converter.telerik.com does not convert c#6.0 or the new VB stuff.  At least that is what I have found since I use it quite often.  I code in VB at work and C# at home.

Thanks for the info Keith.
Fortunately I don't need to deal with VB often.

For those who may need one, which converter would you recommend ?

Called Kerry in my other life
Retired; but they dragged me back in !

I live at UTC + 13.00

---
some people complain about loading the dishwasher.
Sometimes the question is more important than the answer.

kdub_nz

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 2139
  • class keyThumper<T>:ILazy<T>
Re: Getting and setting the DimBlk ID used for Closed Filled Arrow
« Reply #3 on: February 28, 2016, 08:09:24 PM »
I've cleaned up a subtle bug in the  GetDimBlockId method.
If the current DIMBLK value stored in the variable originalArrowName was the default 'Closed filled' the subsequent restoring of the DIMBLK variable may result in a crash.

The revised code is
Code - C#: [Select]
  1.             // Restore the previous setting
  2.             if(string.IsNullOrEmpty(originalArrowName)) {
  3.                 db.Dimblk = ObjectId.Null;
  4.             }
  5.             else {
  6.                 AcadApp.SetSystemVariable(dimBlkX, originalArrowName);
  7.             }

I've also allowed for the newArrowName parameter to have the value of "." in line with the AutoCAD command line option.
Code - C#: [Select]
  1.             if(string.IsNullOrEmpty(newArrowName)
  2.                 || newArrowName.Equals(".")) {
  3.                 return ObjectId.Null;
  4.             }
  5.  


Revised code :

Code - C#: [Select]
  1. // Proof of concept code 2016-02-29
  2. // codehimbelonga kdub @ theSwamp
  3.  
  4. using System;
  5.  
  6. using Autodesk.AutoCAD.ApplicationServices;
  7. using Autodesk.AutoCAD.DatabaseServices;
  8. using Autodesk.AutoCAD.EditorInput;
  9. using Autodesk.AutoCAD.Geometry;
  10. using Autodesk.AutoCAD.Runtime;
  11.  
  12. using AcadApp = Autodesk.AutoCAD.ApplicationServices.Core.Application;
  13.  
  14. [assembly: CommandClass(typeof(DimBlocks.MyCommands))]
  15.  
  16. namespace DimBlocks {
  17.     public class MyCommands {
  18.         [CommandMethod("DoIt", CommandFlags.Modal)]
  19.         public void MyCommand() {
  20.             TM11();
  21.             AcadApp.DocumentManager.MdiActiveDocument.Editor.WriteMessage(
  22.                 "\n\n ====================================\n");
  23.             TM12();
  24.         }
  25.  
  26.         private static void TM11() {
  27.             var doc = AcadApp.DocumentManager.MdiActiveDocument;
  28.             var db = AcadApp.DocumentManager.MdiActiveDocument.Database;
  29.             var ed = doc.Editor;
  30.             var arrowName = string.Empty;
  31.  
  32.             // Get the current DIMBLK setting
  33.             arrowName = (string)AcadApp.GetSystemVariable("DIMBLK");
  34.             ed.WriteMessage($"\nDIMBLK is: {arrowName} \nID is: {db.Dimblk}\n");
  35.  
  36.             // Set to '_OPEN'
  37.             AcadApp.SetSystemVariable("DIMBLK", "_OPEN");
  38.             arrowName = (string)AcadApp.GetSystemVariable("DIMBLK");
  39.             ed.WriteMessage($"\nDIMBLK is: {arrowName} \nID is: {db.Dimblk}\n");
  40.  
  41.             // Set to default 'Closed filled'
  42.             db.Dimblk = ObjectId.Null;
  43.             arrowName = (string)AcadApp.GetSystemVariable("DIMBLK");
  44.             ed.WriteMessage($"\nDIMBLK is: {arrowName} \nID is: {db.Dimblk}\n");
  45.  
  46.             // Set to '_DOT'
  47.             AcadApp.SetSystemVariable("DIMBLK", "_DOT");
  48.             arrowName = (string)AcadApp.GetSystemVariable("DIMBLK");
  49.             ed.WriteMessage($"\nDIMBLK is: {arrowName} \nID is: {db.Dimblk}\n");
  50.  
  51.             // Set to '.'
  52.             AcadApp.SetSystemVariable("DIMBLK", ".");
  53.             arrowName = (string)AcadApp.GetSystemVariable("DIMBLK");
  54.             ed.WriteMessage($"\nDIMBLK '.'is: {arrowName} \nID is: {db.Dimblk}\n");
  55.         }
  56.  
  57.         private static void TM12() {
  58.             var doc = AcadApp.DocumentManager.MdiActiveDocument;
  59.             var db = AcadApp.DocumentManager.MdiActiveDocument.Database;
  60.             var ed = doc.Editor;
  61.             var arrowName = string.Empty;
  62.             var dimBlockId = ObjectId.Null;
  63.  
  64.             arrowName = (string)AcadApp.GetSystemVariable("DIMBLK");
  65.             ed.WriteMessage($"\nDIMBLK is: {arrowName} \nID is: {db.Dimblk}\n");
  66.  
  67.             dimBlockId = GetDimBlockId("DIMBLK", "_DOTBLANK");
  68.             db.Dimblk = dimBlockId;
  69.             arrowName = (string)AcadApp.GetSystemVariable("DIMBLK");
  70.             ed.WriteMessage($"\nDIMBLK is: {arrowName} \nID is: {dimBlockId}\n");
  71.  
  72.             dimBlockId = GetDimBlockId("DIMBLK", ".");
  73.             db.Dimblk = dimBlockId;
  74.             arrowName = (string)AcadApp.GetSystemVariable("DIMBLK");
  75.             ed.WriteMessage($"\nDIMBLK '.' is: {arrowName} \nID is: {dimBlockId}\n");
  76.  
  77.             dimBlockId = GetDimBlockId("DIMBLK", "");
  78.             db.Dimblk = dimBlockId;
  79.             arrowName = (string)AcadApp.GetSystemVariable("DIMBLK");
  80.             ed.WriteMessage($"\nDIMBLK '' is: {arrowName} \nID is: {dimBlockId}\n");
  81.         }
  82.  
  83.  
  84.         private static ObjectId GetDimBlockId(string dimBlkX, string newArrowName) {
  85.             ObjectId arrowObjId = ObjectId.Null;
  86.  
  87.             if(string.IsNullOrEmpty(newArrowName)
  88.                 || newArrowName.Equals(".")) {
  89.                 return ObjectId.Null;
  90.             }
  91.  
  92.             var db = AcadApp.DocumentManager.MdiActiveDocument.Database;
  93.             var originalArrowName = (string)AcadApp.GetSystemVariable(dimBlkX);
  94.  
  95.             // Force AutoCAD to add the block to the db.
  96.             AcadApp.SetSystemVariable(dimBlkX, newArrowName);
  97.  
  98.             // Restore the previous setting
  99.             if(string.IsNullOrEmpty(originalArrowName)) {
  100.                 db.Dimblk = ObjectId.Null;
  101.             }
  102.             else {
  103.                 AcadApp.SetSystemVariable(dimBlkX, originalArrowName);
  104.             }
  105.  
  106.             using(Transaction tr = db.TransactionManager.StartTransaction()) {
  107.                 var bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
  108.                 arrowObjId = bt[newArrowName];
  109.                 tr.Commit();
  110.             }
  111.             return arrowObjId;
  112.         }
  113.     }
  114.  
  115.     public class InitMyCommands : Autodesk.AutoCAD.Runtime.IExtensionApplication {
  116.         public void Initialize() {
  117.             AcadApp.DocumentManager.MdiActiveDocument.Editor.WriteMessage("\n\t\t\t*** type DOIT to run test \t***");
  118.         }
  119.  
  120.         public void Terminate() {
  121.             AcadApp.DocumentManager.MdiActiveDocument.Editor.WriteMessage("Cleaning up...");
  122.         }
  123.     }
  124. }
  125.  

New piccy attached:
« Last Edit: February 28, 2016, 08:18:30 PM by kdub »
Called Kerry in my other life
Retired; but they dragged me back in !

I live at UTC + 13.00

---
some people complain about loading the dishwasher.
Sometimes the question is more important than the answer.