using System;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Colors;
using System.Runtime.InteropServices;
[assembly: CommandClass(typeof(MpolygonTest.MyCommands))]
namespace MpolygonTest
{
public class MyCommands
{
// Зависит от версии AutoCAD и разрядности. В данном случае это для AutoCAD 2013-2014 и x64
[DllImport("AcMPolygonObj19.dbx", CallingConvention = CallingConvention.ThisCall,
EntryPoint = "?patternColor@AcDbMPolygon@@UEBA?AVAcCmColor@@XZ")]
// Для AutoCAD 2013-2014 и x86
// [DllImport("AcMPolygonObj19.dbx", CallingConvention = CallingConvention.ThisCall,
// EntryPoint = "?patternColor@AcDbMPolygon@@UBE?AVAcCmColor@@XZ")]
extern static private System.IntPtr getPatternColorOfMPolygon(System.IntPtr Mpolygon);
[CommandMethod("GetPatternColor")]
public static void GetPatternColor()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Editor ed = doc.Editor;
// Не обязательно, но желательно
SystemObjects.DynamicLinker.LoadModule("AcMPolygonObj" + Application.Version.Major + ".dbx", false, false);
PromptSelectionResult selection = ed.GetSelection();
if (selection.Status != PromptStatus.OK)
return;
ObjectId objId = selection.Value[0].ObjectId;
using (var trans = doc.Database.TransactionManager.StartTransaction())
{
var polygon = trans.GetObject(objId, OpenMode.ForRead) as MPolygon;
if (polygon == null)
return;
try
{
System.IntPtr clptr = getPatternColorOfMPolygon(polygon.UnmanagedObject);
Autodesk.AutoCAD.Colors.Color cl =
DisposableWrapper.Create(typeof(Autodesk.AutoCAD.Colors.Color), clptr, false) as Autodesk.AutoCAD.Colors.Color;
string colorString = string.Format("{0},{1},{2}", cl.Red, cl.Green, cl.Blue);
ed.WriteMessage("Цвет штриховки полигона: {0}\n", colorString);
}
catch (System.Exception ex)
{
ed.WriteMessage("Исключение при попытке получить цвет штриховки: " + ex);
}
}
}
}
}