TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: mkweaver on August 20, 2015, 05:29:14 PM

Title: What block do I have open in the block editor?
Post by: mkweaver on August 20, 2015, 05:29:14 PM
I have a reactor running that logs file activity to a tab delimited tickler file.  It's working fine, has been for years.  But now I find that I need to know what block I am opening in the block editor.  I've seen examples of how to do this in .NET, but I would to do it with lisp.

Any suggestions would be appreciated.

Thanks,
Mike
Title: Re: What block do I have open in the block editor?
Post by: cmwade77 on August 20, 2015, 06:19:26 PM
I can't even find how to do it in .net, could you post a link to that information? I am wondering if I might be able to figure it out from there.
Title: Re: What block do I have open in the block editor?
Post by: mkweaver on August 25, 2015, 09:06:17 AM
Now I can't find it.  I thought it was on http://through-the-interface.typepad.com/through_the_interface/ (http://through-the-interface.typepad.com/through_the_interface/), but I can't see it now:-/
Title: Re: What block do I have open in the block editor?
Post by: Jeff_M on August 25, 2015, 03:25:28 PM
HERE (http://adndevblog.typepad.com/autocad/2015/03/identify-the-block-editing-mode-in-autocad.html) is a .NET solution.
Title: Re: What block do I have open in the block editor?
Post by: mkweaver on August 26, 2015, 08:43:39 AM
Jeff,
Thanks for that - this is more complete than what I had found earlier.

I would prefer to do this without having to get in to .NET, but maybe I won't have a choice.

Mike
Title: Re: What block do I have open in the block editor?
Post by: jjurkus on August 26, 2015, 01:24:24 PM
You might want to check this: http://www.cadtutor.net/forum/showthread.php?68387

I can get a list of all blocknames with something like this, however a block reference doesn't have the property 'active' or something.  :-(
Code: [Select]
(setq ActiveDocument (vlax-get-property (vlax-get-acad-object) 'ActiveDocument))
(setq blockeditor (vlax-variant-value (vlax-invoke-method ActiveDocument 'GetVariable "blockeditor")))
(setq blocks (vlax-get-property ActiveDocument 'Blocks))
(setq n 0)
(repeat (vlax-get-property blocks 'Count)
(setq blockentity (vlax-invoke-method blocks 'Item n))
(princ (vlax-get-property blockentity 'Name))
(setq n (1+ n))
)
Title: Re: What block do I have open in the block editor?
Post by: cmwade77 on August 26, 2015, 05:52:04 PM
HERE (http://adndevblog.typepad.com/autocad/2015/03/identify-the-block-editing-mode-in-autocad.html) is a .NET solution.
This only show if you are in the block editor, it does not let you know what the name of the block that is open in the block editor is. If all you need to know is if you are in the block editor, just read the variable blockedit, if it is 1, you are in the block editor.
Title: Re: What block do I have open in the block editor?
Post by: Lee Mac on August 26, 2015, 06:03:04 PM
HERE (http://adndevblog.typepad.com/autocad/2015/03/identify-the-block-editing-mode-in-autocad.html) is a .NET solution.
This only show if you are in the block editor, it does not let you know what the name of the block that is open in the block editor is. If all you need to know is if you are in the block editor, just read the variable blockedit, if it is 1, you are in the block editor.

Refer to the subsequent comments from Alexander Rivlis  :wink:
Title: Re: What block do I have open in the block editor?
Post by: Jeff_M on August 26, 2015, 06:16:08 PM
Thanks, Lee. I should've put that in my original reply.
Title: Re: What block do I have open in the block editor?
Post by: BlackBox on August 26, 2015, 09:04:01 PM
This is a recurring 'wish' - I too would like for this to come to fruition.

AutoCAD should simply append the BlockTableRecord's name to the contextual tab, much like how Civil 3D does for Pipe Networks, Parcels, etc.
Title: Re: What block do I have open in the block editor?
Post by: cmwade77 on August 27, 2015, 11:51:33 AM
HERE (http://adndevblog.typepad.com/autocad/2015/03/identify-the-block-editing-mode-in-autocad.html) is a .NET solution.
This only show if you are in the block editor, it does not let you know what the name of the block that is open in the block editor is. If all you need to know is if you are in the block editor, just read the variable blockedit, if it is 1, you are in the block editor.

Refer to the subsequent comments from Alexander Rivlis  :wink:
I did and I didn't see anything that would return the block name, only the drawing name and if you are in the block editor.
Title: Re: What block do I have open in the block editor?
Post by: BlackBox on August 27, 2015, 12:05:06 PM
HERE (http://adndevblog.typepad.com/autocad/2015/03/identify-the-block-editing-mode-in-autocad.html) is a .NET solution.
This only show if you are in the block editor, it does not let you know what the name of the block that is open in the block editor is. If all you need to know is if you are in the block editor, just read the variable blockedit, if it is 1, you are in the block editor.

Refer to the subsequent comments from Alexander Rivlis  :wink:
I did and I didn't see anything that would return the block name, only the drawing name and if you are in the block editor.

Can be easy to overlook if not familiar with C#.NET - Note the highlighted lines below:

Quote from: Alexander Rivilis said...
Code - C#: [Select]
  1. using System;
  2. using Autodesk.AutoCAD.Runtime;
  3. using Autodesk.AutoCAD.ApplicationServices;
  4. using Autodesk.AutoCAD.EditorInput;
  5.  
  6. [assembly: CommandClass(typeof(Rivilis.BEdit))]
  7.  
  8. namespace Rivilis
  9. {
  10.     public class BEdit
  11.     {
  12.         [CommandMethod("TestBEDIT")]
  13.         public void TestBEdit()
  14.         {
  15.             Document doc =
  16.             Application.DocumentManager.MdiActiveDocument;
  17.             Editor ed;
  18.             if (doc != null)
  19.             {
  20.                 ed = doc.Editor;
  21.                 if (Autodesk.AutoCAD.Internal.AcAeUtilities.IsInBlockEditor())
  22.                 {
  23.                     ed.WriteMessage("\nWe are in block editor of block \"{0}\"",
  24.                     Autodesk.AutoCAD.Internal.AcAeUtilities.GetBlockName());
  25.                 }
  26.                 else
  27.                 {
  28.                     ed.WriteMessage("\nWe are NOT in block editor");
  29.                 }
  30.             }
  31.         }
  32.     }
  33. }
  34.  
Title: Re: What block do I have open in the block editor?
Post by: Jeff_M on August 27, 2015, 12:40:43 PM
And just as proof that this works:

Command: TESTBEDIT
We are in block editor of block "Benchmark"
Command: (getvar "dwgname")
"15102 - base-ip.dwg"
Title: Re: What block do I have open in the block editor?
Post by: BlackBox on August 27, 2015, 02:02:52 PM
And just as proof that this works:

Per usual, you beat me to it... But I'll post this anyway, as it is a slight adaptation  :-)



Here's a quick Jing video (http://screencast.com/t/ubAAD4oIM6) of a small .NET app, which registers an Activated event handler for the "Block Editor" tab, automagically changing the displayed text to be "Block Editor: <BlockName>", and restores the original text when deactivated (regardless of BCLOSE, U [Undo], etc.)

If anyone's interested, I will gladly post an Autoloader .bundle, which may ironically take longer to assemble than coding the app itself (Thanks to Alexander's earlier linked post, that is)! :-D Haha

Cheers

Code - C#: [Select]
  1. //
  2. using Autodesk.AutoCAD.ApplicationServices;
  3. using Autodesk.AutoCAD.Internal;
  4. using Autodesk.AutoCAD.Runtime;
  5. using Autodesk.Windows;
  6.  
  7. using acApp = Autodesk.AutoCAD.ApplicationServices.Application;
  8.  
  9. using System;
  10.  
  11. [assembly: ExtensionApplication(typeof(BlackBox.AutoCAD.BlockEditorTools.Events))]
  12.  
  13. namespace BlackBox.AutoCAD.BlockEditorTools
  14. {
  15.     public class Events : IExtensionApplication
  16.     {
  17.         private static DocumentCollection acDocs = acApp.DocumentManager;
  18.         private static RibbonTab blockEditorTab;
  19.         private static string tabName = "Block Editor";
  20.  
  21.         void IExtensionApplication.Initialize()
  22.         {
  23.             acApp.Idle += onIdle;
  24.         }
  25.         void IExtensionApplication.Terminate()
  26.         {
  27.             if (blockEditorTab != null)
  28.                 blockEditorTab.Title = tabName;
  29.         }
  30.         private static void onIdle(object sender, EventArgs e)
  31.         {
  32.             acApp.Idle -= onIdle;
  33.  
  34.             Document doc = acDocs.MdiActiveDocument;
  35.  
  36.             if (doc == null)
  37.             {
  38.                 acDocs.DocumentCreated += onDocumentCreated;
  39.                 return;
  40.             }
  41.  
  42.             Register();
  43.         }
  44.         private static void onActivated(object sender, EventArgs e)
  45.         {
  46.             blockEditorTab.Activated -= onActivated;
  47.             blockEditorTab.Deactivated += onDeactivated;
  48.  
  49.             string blockName = tabName + ": " + AcAeUtilities.GetBlockName();
  50.  
  51.             blockEditorTab.Title = blockName;
  52.  
  53.             acDocs.MdiActiveDocument.Editor.WriteMessage("\n{0} \n", blockName);
  54.         }
  55.         private static void onDeactivated(object sender, EventArgs e)
  56.         {
  57.             blockEditorTab.Deactivated -= onDeactivated;
  58.             blockEditorTab.Activated += onActivated;
  59.             blockEditorTab.Title = tabName;
  60.         }
  61.         private static void onDocumentCreated(object sender, DocumentCollectionEventArgs e)
  62.         {
  63.             Register();
  64.         }
  65.         private static void Register()
  66.         {
  67.             RibbonControl rc =
  68.                 Autodesk.AutoCAD.Ribbon.RibbonServices.RibbonPaletteSet.RibbonControl;
  69.  
  70.             blockEditorTab = rc.FindTab("ACAD.ID_BlockEditorTab");
  71.  
  72.             if (blockEditorTab == null)
  73.             {
  74.                 acDocs.MdiActiveDocument.Editor.WriteMessage(
  75.                     "\n** \"Block Editor\" ribbon tab cannot be found ** \n");
  76.  
  77.                 return;
  78.             }
  79.  
  80.             blockEditorTab.Activated += onActivated;
  81.  
  82.             acDocs.MdiActiveDocument.Editor.WriteMessage(
  83.                 "\nBlackBox Block Editor Tools loaded. \n");
  84.         }
  85.     }
  86. }
  87.  

[Edit] - Revised code to also report the Block's name to Command Line for log file.