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 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


Subscribe via RSS
Dear Kean
Thanks again for an example
it worked like a charm
Here is VB.NET version I used
[Code deleted by Kean at the request of Fatty]
Posted by: Fatty | March 19, 2007 at 10:47 PM
Oops...
the code uploaded incorrectly
remove it please
Oleg
Posted by: Fatty | March 19, 2007 at 10:51 PM
Kean,
Is there .Net interface for autocad command "DrawOrder"? For example: after solid-hatch a polyline, need to reorder polyline and hatch, how to make it happen using .NET? Thanks!
Posted by: Limin | March 27, 2007 at 03:39 AM
Limin,
The short answer is that it should be possible, using Autodesk.AutoCAD.DatabaseServices.DrawOrderTable.
When I have some time I'll take a look into creating some sample code. In this case do you want to put the hatch behind the boundary? They don't typically intersect, so that change won't be very visible... unless you mean they need to be reordered relative to other entities.
Posted by: Kean | March 27, 2007 at 09:29 AM
Kean, thanks for the quick response.
Actually it's not only pline boundary. I have some furniture inside that polyline. So after running the hatch, I want them behind hatch. When you start working on draworder example, can you also include some code add xdata? I have tried hatch.XData.add(ResultBuffer), it always retruns error like following:
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.NullReferenceException: Object reference not set to an instance of an object.
Have a great trip to China. I have some good friends in Beijing, if you need some help, please let me know.
Posted by: Limin | March 27, 2007 at 04:55 PM
I've created a new post on this:
http://through-the-interface.typepad.com/through_the_interface/2007/03/manipulating_th.html
Regarding the XData issue... it's not specifically relevant to the topic, so I've left it off. There are two likely reasons for your error: either the Registered Appication has not been added properly, or the list of typed values you're passing in via the ResultBuffer is malformed, in some way.
Thanks for your kind offer, by the way. My Autodesk colleagues in Beijing have been incredibly welcoming and helpful, but I'll certainly let you know if I need additional help.
Posted by: Kean | March 30, 2007 at 09:16 AM
This example doesn't work on Autocad 2009. Can I use this method to show hatch dialog with Autocad 2009 and vb.net 2008?
Posted by: Andrey | March 20, 2009 at 08:22 AM
It worked fine for me with AutoCAD 2009.
Kean
Posted by: Kean Walmsley | March 20, 2009 at 09:53 AM
Hi Kean,
I have been attempting to follow the same path for showing the text style dialog. I can get the Dialog to show but it doesn't initialise properly. Any pointers gratefully received.
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.Runtime
Namespace TextStyleDialogTest
Public Class Commands
Private Declare Auto Sub acedTextStyleDialog2006 Lib "acad.exe" Alias "?invokeTextStyleDialog@@YAXPAVAcDbDatabase@@PAV?$CStringT@DV?$StrTraitMFC@DV?$ChTraitsCRT@D@ATL@@@@@ATL@@@Z" (ByVal AcadDatabase As IntPtr, ByVal StyleName As String)
Private Declare Auto Sub acedTextStyleDialog2007 Lib "acad.exe" Alias "?invokeTextStyleDialog@@YAXPAVAcDbDatabase@@PAV?$CStringT@_WV?$StrTraitMFC_DLL@_WV?$ChTraitsCRT@_W@ATL@@@@@ATL@@@Z" (ByVal AcadDatabase As IntPtr, ByVal StyleName As String)
Public Sub ShowTextStyleDialog()
Dim db As Autodesk.AutoCAD.DatabaseServices.Database
db = Application.DocumentManager.MdiActiveDocument.Database
If Autodesk.AutoCAD.ApplicationServices.Application.Version.Major > 16 Then
acedTextStyleDialog2007(db.UnmanagedObject, "Standard")
Else
acedTextStyleDialog2006(db.UnmanagedObject, "Standard")
End If
End Sub
End Class
End Namespace
Thanks
Adam
Posted by: Adam Davis | July 14, 2009 at 04:27 PM
Hi Adam,
Sorry - nothing immediately obvious springs to mind.
I suggest posting your question via ADN, if you're a member, of otherwise the AutoCAD .NET Discussion Group.
Regards,
Kean
Posted by: Kean Walmsley | July 14, 2009 at 04:34 PM
Hi Kean,
Thanks for the speedy reply.
I tried ADN and was given pointers to either Send command or recreating the dialog in .NET.
Fine, but as was so near with InvokeTextStyleDialog and you obviously have coerced AutoCAD with other dialogs I thought I would post just in case it was something obvious :-)
I will try the .NET forums.
Thanks
Adam
Posted by: Adam Davis | July 14, 2009 at 05:04 PM
Hi Adam,
OK, I just unmangled the name using DependencyWalker, and see the signature is more complex than you thought:
void invokeTextStyleDialog(class AcDbDatabase *,class ATL::CStringT<wchar_t,class StrTraitMFC_DLL<wchar_t,class ATL::ChTraitsCRT<wchar_t> > > *)
I think the chances of calling this unpublished function via P/Invoke are slim to none, I'm afraid. And much closer to none than slim, speaking frankly. At least it's not something I'd attempt, personally.
Regards,
Kean
Posted by: Kean Walmsley | July 14, 2009 at 05:21 PM
Hi Kean,
No problem - I thought I was close but obviously not :-)
Still I learnt today that you can Undecorate a name in Dependency Walker which makes it all worthwhile.
Thanks again,
Adam
Posted by: Adam Davis | July 14, 2009 at 05:38 PM