29/10/2020
Как можно динамически поменять цвет фона в редакторе блоков BEDIT в .NET API?
Вопрос: Как можно поменять цвет фона в редакторе блоков (BEDIT) при помощи кода .NET API?
Ответ: В ObjectARX есть возможность менять цвет элементов интерфейса AutoCAD (в том числе и цвет фона редактора блоков). Воспользуемся P/Invoke для вызова этого кода из .NET:
Код - C#: [Выделить]
- using Autodesk.AutoCAD.Colors;
- using Autodesk.AutoCAD.Runtime;
- using Autodesk.AutoCAD.Windows;
- using System;
- using System.Runtime.InteropServices;
- // This line is not mandatory, but improves loading performances
- [assembly: CommandClass(typeof(Rivilis.ColorUtils))]
- namespace Rivilis
- {
- public class ColorUtils
- {
- [System.Security.SuppressUnmanagedCodeSecurity]
- [DllImport("accore.dll", EntryPoint = "?acedGetCurrentColors@@YA_NPEAUAcColorSettings@@@Z",
- CharSet = CharSet.Auto, CallingConvention = CallingConvention.Cdecl)]
- static extern bool acedGetCurrentColors(ref AcColorSettings pColorSettings);
- [System.Security.SuppressUnmanagedCodeSecurity]
- [DllImport("accore.dll", EntryPoint = "?acedSetCurrentColors@@YA_NPEAUAcColorSettings@@@Z",
- CharSet = CharSet.Auto, CallingConvention = CallingConvention.Cdecl)]
- static extern bool acedSetCurrentColors(ref AcColorSettings pColorSettings);
- [CommandMethod("SetBeditBackColor")]
- public void SetBeditBackColorHandler()
- {
- AcColorSettings colors = new AcColorSettings();
- acedGetCurrentColors(ref colors);
- byte r, g, b;
- ColorToRGB(colors.dwBEditBkColor, out r, out g, out b);
- Color clr = Color.FromRgb(r, g, b);
- ColorDialog cd = new ColorDialog { Color = clr };
- System.Windows.Forms.DialogResult dr = cd.ShowDialog();
- if (dr != System.Windows.Forms.DialogResult.OK) return;
- clr = cd.Color;
- colors.dwBEditBkColor = RGBToColor(clr.Red, clr.Green, clr.Blue);
- acedSetCurrentColors(ref colors);
- }
- public UInt32 RGBToColor(byte r, byte g, byte b)
- {
- #pragma warning disable CS0675
- return (UInt32)((b << 16) | (g << 8) | (r));
- #pragma warning restore CS0675
- }
- public void ColorToRGB(UInt32 color, out byte r, out byte g, out byte b)
- {
- b = (byte)((color >> 16) & 0xFF);
- g = (byte)((color >> 8) & 0xFF);
- r = (byte)(color & 0xFF);
- return;
- }
- // Ниже код Gilles Chanteau - изменение цвета происходит только при выходе и входе в редактор блоков
- // [System.Security.SuppressUnmanagedCodeSecurity]
- // [DllImport("accore.dll", EntryPoint = "acedGetEnv",
- // CharSet = CharSet.Auto, CallingConvention = CallingConvention.Cdecl)]
- // extern static int acedGetEnv(string envName, string returnValue);
- // public static string GetEnv(string envName)
- // {
- // string returnValue = new string(char.MinValue, 1024);
- // int stat = acedGetEnv(envName, returnValue);
- // return stat == -5001 ? null : returnValue;
- // }
- // [System.Security.SuppressUnmanagedCodeSecurity]
- // [DllImport("accore.dll", EntryPoint = "acedSetEnv",
- // CharSet = CharSet.Auto, CallingConvention = CallingConvention.Cdecl)]
- // extern static int acedSetEnv(string envName, string newValue);
- // public static string SetEnv(string envName, string newValue)
- // {
- // int stat = acedSetEnv(envName, newValue);
- // return stat == -5001 ? null : newValue;
- // }
- // [CommandMethod("TEST")]
- // public static void Test()
- // {
- // SetEnv("BEditBackground", 0x7F7FC8.ToString());
- // }
- }
- #pragma warning disable 0169
- // From ObjectARX SDK \inc\core_rxmfcapi.h
- [StructLayout(LayoutKind.Sequential, Pack = 8)]
- public struct AcColorSettings
- {
- /* Solid background color for various contexts */
- public UInt32 dwGfxModelBkColor;
- public UInt32 dwGfxLayoutBkColor;
- public UInt32 dwPaperBkColor;
- public UInt32 dwParallelBkColor;
- public UInt32 dwBEditBkColor;
- public UInt32 dwCmdLineBkColor;
- public UInt32 dwPlotPrevBkColor;
- /* Background color for 3D perspective projection */
- public UInt32 dwSkyGradientZenithColor;
- public UInt32 dwSkyGradientHorizonColor;
- public UInt32 dwGroundGradientOriginColor;
- public UInt32 dwGroundGradientHorizonColor;
- public UInt32 dwEarthGradientAzimuthColor;
- public UInt32 dwEarthGradientHorizonColor;
- /* Crosshair color for various contexts */
- public UInt32 dwModelCrossHairColor;
- public UInt32 dwLayoutCrossHairColor;
- public UInt32 dwParallelCrossHairColor;
- public UInt32 dwPerspectiveCrossHairColor;
- public UInt32 dwBEditCrossHairColor;
- /* Ground plane grid major lines for various contexts */
- public UInt32 dwParallelGridMajorLines;
- public UInt32 dwPerspectiveGridMajorLines;
- /* Ground plane grid minor lines for various contexts */
- public UInt32 dwParallelGridMinorLines;
- public UInt32 dwPerspectiveGridMinorLines;
- /* Ground plane grid axis lines for various contexts */
- public UInt32 dwParallelGridAxisLines;
- public UInt32 dwPerspectiveGridAxisLines;
- /* Text window color */
- public UInt32 dwTextForeColor, dwTextBkColor;
- /* Command line text color */
- public UInt32 dwCmdLineForeColor;
- public UInt32 dwCmdLineTempPromptBkColor;
- public UInt32 dwCmdLineTempPromptTextColor;
- public UInt32 dwCmdLineCmdOptKeywordColor;
- public UInt32 dwCmdLineCmdOptBkColor;
- public UInt32 dwCmdLineCmdOptHighlightedColor;
- /* AutoTrack vector color for various contexts */
- // Note: dwAutoTrackingVecColor indicates autotrack vector color in model space.
- // We didn't change its name to dwModelATrackVecColor because that might break
- // existing arx app.
- public UInt32 dwAutoTrackingVecColor;
- public UInt32 dwLayoutATrackVecColor;
- public UInt32 dwParallelATrackVecColor;
- public UInt32 dwPerspectiveATrackVecColor;
- public UInt32 dwBEditATrackVecColor;
- /* Autosnap Marker color for various contexts */
- public UInt32 dwModelASnapMarkerColor;
- public UInt32 dwLayoutASnapMarkerColor;
- public UInt32 dwParallelASnapMarkerColor;
- public UInt32 dwPerspectiveASnapMarkerColor;
- public UInt32 dwBEditASnapMarkerColor;
- /* Drafting Tool tip color for various contexts */
- public UInt32 dwModelDftingTooltipColor;
- public UInt32 dwLayoutDftingTooltipColor;
- public UInt32 dwParallelDftingTooltipColor;
- public UInt32 dwPerspectiveDftingTooltipColor;
- public UInt32 dwBEditDftingTooltipColor;
- /* Drafting Tool tip background color for various contexts */
- public UInt32 dwModelDftingTooltipBkColor;
- public UInt32 dwLayoutDftingTooltipBkColor;
- public UInt32 dwParallelDftingTooltipBkColor;
- public UInt32 dwPerspectiveDftingTooltipBkColor;
- public UInt32 dwBEditDftingTooltipBkColor;
- /* Light glyphs color for various contexts */
- public UInt32 dwModelLightGlyphs;
- public UInt32 dwLayoutLightGlyphs;
- public UInt32 dwParallelLightGlyphs;
- public UInt32 dwPerspectiveLightGlyphs;
- public UInt32 dwBEditLightGlyphs;
- /* Light Hotspot color for various contexts */
- public UInt32 dwModelLightHotspot;
- public UInt32 dwLayoutLightHotspot;
- public UInt32 dwParallelLightHotspot;
- public UInt32 dwPerspectiveLightHotspot;
- public UInt32 dwBEditLightHotspot;
- /* Light Falloff color for various contexts */
- public UInt32 dwModelLightFalloff;
- public UInt32 dwLayoutLightFalloff;
- public UInt32 dwParallelLightFalloff;
- public UInt32 dwPerspectiveLightFalloff;
- public UInt32 dwBEditLightFalloff;
- /* Light start limit color for various contexts */
- public UInt32 dwModelLightStartLimit;
- public UInt32 dwLayoutLightStartLimit;
- public UInt32 dwParallelLightStartLimit;
- public UInt32 dwPerspectiveLightStartLimit;
- public UInt32 dwBEditLightStartLimit;
- /* Light end limit color for various contexts */
- public UInt32 dwModelLightEndLimit;
- public UInt32 dwLayoutLightEndLimit;
- public UInt32 dwParallelLightEndLimit;
- public UInt32 dwPerspectiveLightEndLimit;
- public UInt32 dwBEditLightEndLimit;
- /* Camera glyphs color for various contexts */
- public UInt32 dwModelCameraGlyphs;
- public UInt32 dwLayoutCameraGlyphs;
- public UInt32 dwParallelCameraGlyphs;
- public UInt32 dwPerspectiveCameraGlyphs;
- /* Camera frustrum plane color for various contexts */
- public UInt32 dwModelCameraFrustrum;
- public UInt32 dwLayoutCameraFrustrum;
- public UInt32 dwParallelCameraFrustrum;
- public UInt32 dwPerspectiveCameraFrustrum;
- /* Camera clipping plane color for various contexts */
- public UInt32 dwModelCameraClipping;
- public UInt32 dwLayoutCameraClipping;
- public UInt32 dwParallelCameraClipping;
- public UInt32 dwPerspectiveCameraClipping;
- /* Flags - set if true */
- /* Flags for use tInt32 X, Y, Z for crosshair */
- public Int32 nModelCrosshairUseTInt32XYZ;
- public Int32 nLayoutCrosshairUseTInt32XYZ;
- public Int32 nParallelCrosshairUseTInt32XYZ;
- public Int32 nPerspectiveCrosshairUseTInt32XYZ;
- public Int32 nBEditCrossHairUseTInt32XYZ;
- /* Flags for use tInt32 X, Y, Z for AutoTrack Vector */
- public Int32 nModelATrackVecUseTInt32XYZ;
- public Int32 nLayoutATrackVecUseTInt32XYZ;
- public Int32 nParallelATrackVecUseTInt32XYZ;
- public Int32 nPerspectiveATrackVecUseTInt32XYZ;
- public Int32 nBEditATrackVecUseTInt32XYZ;
- /* Flags for use tInt32 X, Y, Z for Drafting Tooltip Bk tInt32 */
- public Int32 nModelDftingTooltipBkUseTInt32XYZ;
- public Int32 nLayoutDftingTooltipBkUseTInt32XYZ;
- public Int32 nParallelDftingTooltipBkUseTInt32XYZ;
- public Int32 nPerspectiveDftingTooltipBkUseTInt32XYZ;
- public Int32 nBEditDftingTooltipBkUseTInt32XYZ;
- /* Flags for use tInt32 X, Y, Z for Ground plane grid major lines */
- public Int32 nParallelGridMajorLineTInt32XYZ;
- public Int32 nPerspectiveGridMajorLineTInt32XYZ;
- /* Flags for use tInt32 X, Y, Z for Ground plane grid minor lines */
- public Int32 nParallelGridMinorLineTInt32XYZ;
- public Int32 nPerspectiveGridMinorLineTInt32XYZ;
- /* Flags for use tInt32 X, Y, Z for Ground plane grid axis lines */
- public Int32 nParallelGridAxisLineTInt32XYZ;
- public Int32 nPerspectiveGridAxisLineTInt32XYZ;
- }
- #pragma warning restore 0169
- }
Автор: Александр Ривилис
Опубликовано 29.10.2020
Отредактировано 29.10.2020 в 17:57:17
Опубликовано 29.10.2020
Отредактировано 29.10.2020 в 17:57:17