Author Topic: Expose ARX Function to .NET  (Read 37521 times)

0 Members and 1 Guest are viewing this topic.

TR

  • Guest
Re: Expose ARX Function to .NET
« Reply #75 on: August 28, 2007, 05:09:07 PM »
Ok here is the code I'm using to test with Dan's latest C++ DLL.
Code: [Select]
// Created By: Mick Duprez, 27th August 2007
// Used to register commands using a C wrapper for the acedRegCmds() macro

using System ;
using System.Runtime.InteropServices;
using Autodesk.AutoCAD.Runtime ;
using Autodesk.AutoCAD.EditorInput;

namespace PyAcadDotNet
{
/// <summary>
/// PyAcadCmd Class:
/// Used to register commands on the AutoCAD command stack.
/// </summary>
public class PyAcadCmd
{
public PyAcadCmd()
{
}
public delegate void CmdDelegate();

/// <summary>
/// RegPyAcadCmd:
/// Registers a delegate (callback) with the AutoCAD command string
/// on the command stack.
/// </summary>
        [DllImport("PyRegCmd.dll",
CallingConvention=CallingConvention.Cdecl,
EntryPoint = "?RegPyCmd@@YAXPB_W0HP6AXXZ@Z")]
public static extern void RegPyCmd(
string cmd_group,
string cmd_name,
Autodesk.AutoCAD.Runtime.CommandFlags cmd_flags,
[MarshalAs(UnmanagedType.FunctionPtr)] PyAcadCmd.CmdDelegate cmd_delegate);

        //testing stuff
        public static void testcommand()
        {
            Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
            ed.WriteMessage("\ncb1 delegate seems to work!\n");
        }
        [CommandMethod("regcmds")]
        static public void test() // This method can have any name
        {
            CmdDelegate cb1 = new CmdDelegate(PyAcadCmd.testcommand);
            RegPyCmd("_pycmds", "TESTER", CommandFlags.Session, cb1);
            Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
            ed.WriteMessage("\nRegister command was called, but does it work?\n");
        }
}
}
I call "regcmds" from the command line and the message is written to the command line. However my "TESTER" command is not available from the command line. I and doing this from debug mode in VC# Express. When I close AutoCAD it gives me a System.StackOverflowException.

Now if I passed bad values to the underlying C function should I expect an error? I ask because the C code askes for an int on cmd_flags and I'm passing a AutoDesk.AutoCAD.Runtime.CommandFlag, which I don't think is an int.

The C code in the dll I'm using:
Code: [Select]
#pragma unmanaged
__declspec(dllexport) void __cdecl RegPyCmd(const ACHAR * cmdGroup,
const ACHAR * cmdName,
int cmdFlags,
void(*functionptr)())
{
// Note: cast functionptr as AcRxFuctionPtr.
acedRegCmds->addCommand(cmdGroup, cmdName, cmdName, (int)cmdFlags,(AcRxFunctionPtr)functionptr);
}
Note: I'm almost certain I have the entry point correct as I ran "dumpbin /EXPORTS PyRegCmd.dll" and it gave me the one I use in the code above.

MickD

  • King Gator
  • Posts: 3637
  • (x-in)->[process]->(y-out) ... simples!
Re: Expose ARX Function to .NET
« Reply #76 on: August 28, 2007, 05:57:43 PM »
Tim, add the bold code below into the arx function, this should give us an idea of what's not working. (I should have had it in there anyways  :roll: )

Quote
__declspec(dllexport) void __cdecl RegPyCmd(const char * cmdGroup,
                                 const char * cmdName,
                                 int cmdFlags,
                                 void(*functionptr)())
{
   // Note: cast functionptr as AcRxFuctionPtr.
   Acad::ErrorStatus es =
   acedRegCmds->addCommand(cmdGroup, cmdName, cmdName, (int)cmdFlags,(AcRxFunctionPtr)functionptr);
   if(es != Acad::ErrorStatus::eOk)
   {
      acutPrintf("Error loading command %s: error status %d", cmdName, acadErrorStatusText(es));
   }

}

The command flags should be fine as they translate to an int (Adesk::Int32), they are defined in accmd.h if you want to have a look, the .net cmd flags just wrap those.
« Last Edit: August 28, 2007, 05:59:07 PM by MickD »
"Programming is really just the mundane aspect of expressing a solution to a problem."
- John Carmack

"Short cuts make long delays,' argued Pippin.”
- J.R.R. Tolkien

MickD

  • King Gator
  • Posts: 3637
  • (x-in)->[process]->(y-out) ... simples!
Re: Expose ARX Function to .NET
« Reply #77 on: August 28, 2007, 06:03:29 PM »
Another thing to try is change the ACHAR's to plain char's, it may not compile due to unicode in arx now, I don't know. Perhaps the command string is to big for its slot in the command stack??
"Programming is really just the mundane aspect of expressing a solution to a problem."
- John Carmack

"Short cuts make long delays,' argued Pippin.”
- J.R.R. Tolkien

LE

  • Guest
Re: Expose ARX Function to .NET
« Reply #78 on: August 28, 2007, 06:19:04 PM »
a positive attitude.... and to encourage you :)

by now, we might end up having all what it is in python - ported to C# or having an alternative from scratch.... he he

Sorry, could not resist.... (and I'm joking eh!  :evil: )

Now the 64,000 dollars question, why not doing this project using C# ? -----  :roll:

LE

  • Guest
Re: Expose ARX Function to .NET
« Reply #79 on: August 28, 2007, 06:21:52 PM »
I think I have python22 installed in my home PC (it came with that pre-installed).... never had tried before.... maybe is something in there that I am missing ?

Time will tell (after reading about it)

MickD

  • King Gator
  • Posts: 3637
  • (x-in)->[process]->(y-out) ... simples!
Re: Expose ARX Function to .NET
« Reply #80 on: August 28, 2007, 06:25:38 PM »
Luis, Python isn't required at this stage, we have just created a brute force way of registering commands without using attributes in C#/VB.net. IronPython from what I understand does not use attributes but handles everything else.

Jump in anytime, the water's fine :)
"Programming is really just the mundane aspect of expressing a solution to a problem."
- John Carmack

"Short cuts make long delays,' argued Pippin.”
- J.R.R. Tolkien

LE

  • Guest
Re: Expose ARX Function to .NET
« Reply #81 on: August 28, 2007, 06:32:05 PM »
Luis, Python isn't required at this stage, we have just created a brute force way of registering commands without using attributes in C#/VB.net. IronPython from what I understand does not use attributes but handles everything else.

Jump in anytime, the water's fine :)

I see - and it will be for general purposes not just for python no?..... still I will put python in my reading list....

Gracias!, Mick.

ps... I am afraid of water.....   :-P

MickD

  • King Gator
  • Posts: 3637
  • (x-in)->[process]->(y-out) ... simples!
Re: Expose ARX Function to .NET
« Reply #82 on: August 28, 2007, 06:42:31 PM »
>> and it will be for general purposes not just for python no?

That is correct.
"Programming is really just the mundane aspect of expressing a solution to a problem."
- John Carmack

"Short cuts make long delays,' argued Pippin.”
- J.R.R. Tolkien

TR

  • Guest
Re: Expose ARX Function to .NET
« Reply #83 on: August 28, 2007, 09:55:09 PM »
I've been trying for the past hour to get the C++ dll with the changes Mick noted above without any luck. I keep getting missing "mfc80u.lib" errors in visual C++ 2005 express.

Dan: can you bail me out again?

MickD

  • King Gator
  • Posts: 3637
  • (x-in)->[process]->(y-out) ... simples!
Re: Expose ARX Function to .NET
« Reply #84 on: August 28, 2007, 10:06:33 PM »
That's no good Tim, did you ever get it to compile or are you using Dan's dll?
Have a look at your project settings/properties under configuration->general and make sure that 'use of MFC' is set to 'use std windows lib's' or similar, that may help.
"Programming is really just the mundane aspect of expressing a solution to a problem."
- John Carmack

"Short cuts make long delays,' argued Pippin.”
- J.R.R. Tolkien

TR

  • Guest
Re: Expose ARX Function to .NET
« Reply #85 on: August 28, 2007, 10:13:03 PM »
I have never gotten it to compile. Every time I get past one error another rears it's ugly head. I have all my paths straight now and I have "Use Standard Windows Libraries" under "Use of MFC" under the general tab. I have searched my system and I don't have that lib anywhere. Even though I have the platform SDK installed and referenced in my paths.

MickD

  • King Gator
  • Posts: 3637
  • (x-in)->[process]->(y-out) ... simples!
Re: Expose ARX Function to .NET
« Reply #86 on: August 28, 2007, 10:44:11 PM »
Tim, this may not work with the express edition but it might be worth a shot (the add/remove features part) -> http://msdn2.microsoft.com/en-us/library/wsdfs47e(VS.80).aspx
"Programming is really just the mundane aspect of expressing a solution to a problem."
- John Carmack

"Short cuts make long delays,' argued Pippin.”
- J.R.R. Tolkien

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8722
  • AKA Daniel
Re: Expose ARX Function to .NET
« Reply #87 on: August 28, 2007, 10:58:27 PM »
Here you go, You may need to neload it as well,

TR

  • Guest
Re: Expose ARX Function to .NET
« Reply #88 on: August 28, 2007, 11:03:00 PM »
Thanks Dan. I'll take a look.

TR

  • Guest
Re: Expose ARX Function to .NET
« Reply #89 on: August 28, 2007, 11:33:03 PM »
Ok. I tried Dan's new dll and still no luck with the example code I recently posted. I loaded the pyacaddotnet.dll file and ran the arx command with the option C to list all arx commands, everything looked fine. I then ran the regcmds command and listed the arx commands again. The following screenshot is what I found. Note the squares. This is what was added after I ran the regcmds command. This leads me to believe something is getting lost in the translation somewhere.
« Last Edit: August 28, 2007, 11:34:17 PM by Tim Riley »