ADN Open CIS
Сообщество программистов Autodesk в СНГ

29/10/2020

Как можно динамически поменять цвет фона в редакторе блоков BEDIT в .NET API?

Вопрос: Как можно поменять цвет фона в редакторе блоков (BEDIT) при помощи кода .NET API?

Ответ: В ObjectARX есть возможность менять цвет элементов интерфейса AutoCAD (в том числе и цвет фона редактора блоков). Воспользуемся P/Invoke для вызова этого кода из .NET:

 

Код - C#: [Выделить]
  1. using Autodesk.AutoCAD.Colors;
  2. using Autodesk.AutoCAD.Runtime;
  3. using Autodesk.AutoCAD.Windows;
  4. using System;
  5. using System.Runtime.InteropServices;
  6.  
  7.  
  8. // This line is not mandatory, but improves loading performances
  9. [assembly: CommandClass(typeof(Rivilis.ColorUtils))]
  10.  
  11. namespace Rivilis
  12. {
  13.   public class ColorUtils
  14.   {
  15.     [System.Security.SuppressUnmanagedCodeSecurity]
  16.     [DllImport("accore.dll", EntryPoint = "?acedGetCurrentColors@@YA_NPEAUAcColorSettings@@@Z",
  17.         CharSet = CharSet.Auto, CallingConvention = CallingConvention.Cdecl)]
  18.     static extern bool acedGetCurrentColors(ref AcColorSettings pColorSettings);
  19.     [System.Security.SuppressUnmanagedCodeSecurity]
  20.     [DllImport("accore.dll", EntryPoint = "?acedSetCurrentColors@@YA_NPEAUAcColorSettings@@@Z",
  21.         CharSet = CharSet.Auto, CallingConvention = CallingConvention.Cdecl)]
  22.     static extern bool acedSetCurrentColors(ref AcColorSettings pColorSettings);
  23.  
  24.     [CommandMethod("SetBeditBackColor")]
  25.     public void SetBeditBackColorHandler()
  26.     {
  27.       AcColorSettings colors = new AcColorSettings();
  28.       acedGetCurrentColors(ref colors);
  29.       byte r, g, b;
  30.       ColorToRGB(colors.dwBEditBkColor, out r, out g, out b);
  31.       Color clr = Color.FromRgb(r, g, b);
  32.       ColorDialog cd = new ColorDialog   { Color = clr };
  33.       System.Windows.Forms.DialogResult dr = cd.ShowDialog();
  34.       if (dr != System.Windows.Forms.DialogResult.OK) return;
  35.       clr = cd.Color;
  36.       colors.dwBEditBkColor = RGBToColor(clr.Red, clr.Green, clr.Blue);
  37.       acedSetCurrentColors(ref colors);
  38.     }
  39.     public UInt32 RGBToColor(byte r, byte g, byte b)
  40.     {
  41. #pragma warning disable CS0675
  42.       return (UInt32)((b << 16) | (g << 8) | (r));
  43. #pragma warning restore CS0675
  44.     }
  45.     public void ColorToRGB(UInt32 color, out byte r, out byte g, out byte b)
  46.     {
  47.       b = (byte)((color >> 16) & 0xFF);
  48.       g = (byte)((color >> 8) & 0xFF);
  49.       r = (byte)(color & 0xFF);
  50.       return;
  51.     }
  52.     //  Ниже код Gilles Chanteau - изменение цвета происходит только при выходе и входе в редактор блоков
  53.     //  [System.Security.SuppressUnmanagedCodeSecurity]
  54.     //  [DllImport("accore.dll", EntryPoint = "acedGetEnv",
  55.     //      CharSet = CharSet.Auto, CallingConvention = CallingConvention.Cdecl)]
  56.     //  extern static int acedGetEnv(string envName, string returnValue);
  57.  
  58.     //  public static string GetEnv(string envName)
  59.     //  {
  60.     //    string returnValue = new string(char.MinValue, 1024);
  61.     //    int stat = acedGetEnv(envName, returnValue);
  62.     //    return stat == -5001 ? null : returnValue;
  63.     //  }
  64.  
  65.     //  [System.Security.SuppressUnmanagedCodeSecurity]
  66.     //  [DllImport("accore.dll", EntryPoint = "acedSetEnv",
  67.     //      CharSet = CharSet.Auto, CallingConvention = CallingConvention.Cdecl)]
  68.     //  extern static int acedSetEnv(string envName, string newValue);
  69.  
  70.     //  public static string SetEnv(string envName, string newValue)
  71.     //  {
  72.     //    int stat = acedSetEnv(envName, newValue);
  73.     //    return stat == -5001 ? null : newValue;
  74.     //  }
  75.  
  76.     //  [CommandMethod("TEST")]
  77.     //  public static void Test()
  78.     //  {
  79.     //    SetEnv("BEditBackground", 0x7F7FC8.ToString());
  80.     //  }
  81.  
  82.   }
  83.  
  84. #pragma warning disable 0169
  85.   // From ObjectARX SDK \inc\core_rxmfcapi.h
  86.   [StructLayout(LayoutKind.Sequential, Pack = 8)]
  87.   public struct AcColorSettings
  88.   {
  89.     /* Solid background color for various contexts */
  90.     public UInt32 dwGfxModelBkColor;
  91.     public UInt32 dwGfxLayoutBkColor;
  92.     public UInt32 dwPaperBkColor;
  93.     public UInt32 dwParallelBkColor;
  94.     public UInt32 dwBEditBkColor;
  95.     public UInt32 dwCmdLineBkColor;
  96.     public UInt32 dwPlotPrevBkColor;
  97.     /* Background color for 3D perspective projection */
  98.     public UInt32 dwSkyGradientZenithColor;
  99.     public UInt32 dwSkyGradientHorizonColor;
  100.     public UInt32 dwGroundGradientOriginColor;
  101.     public UInt32 dwGroundGradientHorizonColor;
  102.     public UInt32 dwEarthGradientAzimuthColor;
  103.     public UInt32 dwEarthGradientHorizonColor;
  104.     /* Crosshair color for various contexts */
  105.     public UInt32 dwModelCrossHairColor;
  106.     public UInt32 dwLayoutCrossHairColor;
  107.     public UInt32 dwParallelCrossHairColor;
  108.     public UInt32 dwPerspectiveCrossHairColor;
  109.     public UInt32 dwBEditCrossHairColor;
  110.     /* Ground plane grid major lines for various contexts */
  111.     public UInt32 dwParallelGridMajorLines;
  112.     public UInt32 dwPerspectiveGridMajorLines;
  113.     /* Ground plane grid minor lines for various contexts */
  114.     public UInt32 dwParallelGridMinorLines;
  115.     public UInt32 dwPerspectiveGridMinorLines;
  116.     /* Ground plane grid axis lines for various contexts */
  117.     public UInt32 dwParallelGridAxisLines;
  118.     public UInt32 dwPerspectiveGridAxisLines;
  119.     /* Text window color */
  120.     public UInt32 dwTextForeColor, dwTextBkColor;
  121.     /* Command line text color */
  122.     public UInt32 dwCmdLineForeColor;
  123.     public UInt32 dwCmdLineTempPromptBkColor;
  124.     public UInt32 dwCmdLineTempPromptTextColor;
  125.     public UInt32 dwCmdLineCmdOptKeywordColor;
  126.     public UInt32 dwCmdLineCmdOptBkColor;
  127.     public UInt32 dwCmdLineCmdOptHighlightedColor;
  128.     /* AutoTrack vector color for various contexts */
  129.     // Note: dwAutoTrackingVecColor indicates autotrack vector color in model space.
  130.     // We didn't change its name to dwModelATrackVecColor because that might break
  131.     // existing arx app.
  132.     public UInt32 dwAutoTrackingVecColor;
  133.     public UInt32 dwLayoutATrackVecColor;
  134.     public UInt32 dwParallelATrackVecColor;
  135.     public UInt32 dwPerspectiveATrackVecColor;
  136.     public UInt32 dwBEditATrackVecColor;
  137.     /* Autosnap Marker color for various contexts */
  138.     public UInt32 dwModelASnapMarkerColor;
  139.     public UInt32 dwLayoutASnapMarkerColor;
  140.     public UInt32 dwParallelASnapMarkerColor;
  141.     public UInt32 dwPerspectiveASnapMarkerColor;
  142.     public UInt32 dwBEditASnapMarkerColor;
  143.     /* Drafting Tool tip color for various contexts */
  144.     public UInt32 dwModelDftingTooltipColor;
  145.     public UInt32 dwLayoutDftingTooltipColor;
  146.     public UInt32 dwParallelDftingTooltipColor;
  147.     public UInt32 dwPerspectiveDftingTooltipColor;
  148.     public UInt32 dwBEditDftingTooltipColor;
  149.     /* Drafting Tool tip background color for various contexts */
  150.     public UInt32 dwModelDftingTooltipBkColor;
  151.     public UInt32 dwLayoutDftingTooltipBkColor;
  152.     public UInt32 dwParallelDftingTooltipBkColor;
  153.     public UInt32 dwPerspectiveDftingTooltipBkColor;
  154.     public UInt32 dwBEditDftingTooltipBkColor;
  155.     /* Light glyphs color for various contexts */
  156.     public UInt32 dwModelLightGlyphs;
  157.     public UInt32 dwLayoutLightGlyphs;
  158.     public UInt32 dwParallelLightGlyphs;
  159.     public UInt32 dwPerspectiveLightGlyphs;
  160.     public UInt32 dwBEditLightGlyphs;
  161.     /* Light Hotspot color for various contexts */
  162.     public UInt32 dwModelLightHotspot;
  163.     public UInt32 dwLayoutLightHotspot;
  164.     public UInt32 dwParallelLightHotspot;
  165.     public UInt32 dwPerspectiveLightHotspot;
  166.     public UInt32 dwBEditLightHotspot;
  167.     /* Light Falloff color for various contexts */
  168.     public UInt32 dwModelLightFalloff;
  169.     public UInt32 dwLayoutLightFalloff;
  170.     public UInt32 dwParallelLightFalloff;
  171.     public UInt32 dwPerspectiveLightFalloff;
  172.     public UInt32 dwBEditLightFalloff;
  173.     /* Light start limit color for various contexts */
  174.     public UInt32 dwModelLightStartLimit;
  175.     public UInt32 dwLayoutLightStartLimit;
  176.     public UInt32 dwParallelLightStartLimit;
  177.     public UInt32 dwPerspectiveLightStartLimit;
  178.     public UInt32 dwBEditLightStartLimit;
  179.     /* Light end limit color for various contexts */
  180.     public UInt32 dwModelLightEndLimit;
  181.     public UInt32 dwLayoutLightEndLimit;
  182.     public UInt32 dwParallelLightEndLimit;
  183.     public UInt32 dwPerspectiveLightEndLimit;
  184.     public UInt32 dwBEditLightEndLimit;
  185.     /* Camera glyphs color for various contexts */
  186.     public UInt32 dwModelCameraGlyphs;
  187.     public UInt32 dwLayoutCameraGlyphs;
  188.     public UInt32 dwParallelCameraGlyphs;
  189.     public UInt32 dwPerspectiveCameraGlyphs;
  190.     /* Camera frustrum plane color for various contexts */
  191.     public UInt32 dwModelCameraFrustrum;
  192.     public UInt32 dwLayoutCameraFrustrum;
  193.     public UInt32 dwParallelCameraFrustrum;
  194.     public UInt32 dwPerspectiveCameraFrustrum;
  195.     /* Camera clipping plane color for various contexts */
  196.     public UInt32 dwModelCameraClipping;
  197.     public UInt32 dwLayoutCameraClipping;
  198.     public UInt32 dwParallelCameraClipping;
  199.     public UInt32 dwPerspectiveCameraClipping;
  200.     /* Flags - set if true */
  201.     /* Flags for use tInt32 X, Y, Z for crosshair */
  202.     public Int32 nModelCrosshairUseTInt32XYZ;
  203.     public Int32 nLayoutCrosshairUseTInt32XYZ;
  204.     public Int32 nParallelCrosshairUseTInt32XYZ;
  205.     public Int32 nPerspectiveCrosshairUseTInt32XYZ;
  206.     public Int32 nBEditCrossHairUseTInt32XYZ;
  207.     /* Flags for use tInt32 X, Y, Z for AutoTrack Vector */
  208.     public Int32 nModelATrackVecUseTInt32XYZ;
  209.     public Int32 nLayoutATrackVecUseTInt32XYZ;
  210.     public Int32 nParallelATrackVecUseTInt32XYZ;
  211.     public Int32 nPerspectiveATrackVecUseTInt32XYZ;
  212.     public Int32 nBEditATrackVecUseTInt32XYZ;
  213.     /* Flags for use tInt32 X, Y, Z for Drafting Tooltip Bk tInt32 */
  214.     public Int32 nModelDftingTooltipBkUseTInt32XYZ;
  215.     public Int32 nLayoutDftingTooltipBkUseTInt32XYZ;
  216.     public Int32 nParallelDftingTooltipBkUseTInt32XYZ;
  217.     public Int32 nPerspectiveDftingTooltipBkUseTInt32XYZ;
  218.     public Int32 nBEditDftingTooltipBkUseTInt32XYZ;
  219.     /* Flags for use tInt32 X, Y, Z for Ground plane grid major lines */
  220.     public Int32 nParallelGridMajorLineTInt32XYZ;
  221.     public Int32 nPerspectiveGridMajorLineTInt32XYZ;
  222.     /* Flags for use tInt32 X, Y, Z for Ground plane grid minor lines */
  223.     public Int32 nParallelGridMinorLineTInt32XYZ;
  224.     public Int32 nPerspectiveGridMinorLineTInt32XYZ;
  225.     /* Flags for use tInt32 X, Y, Z for Ground plane grid axis lines */
  226.     public Int32 nParallelGridAxisLineTInt32XYZ;
  227.     public Int32 nPerspectiveGridAxisLineTInt32XYZ;
  228.   }
  229.  
  230. #pragma warning restore 0169
  231. }

 

 

Автор: Александр Ривилис
Опубликовано 29.10.2020
Отредактировано 29.10.2020 в 17:57:17