Author Topic: LISP Dot Net Wrapper  (Read 1400 times)

0 Members and 1 Guest are viewing this topic.

jtoverka

  • Newt
  • Posts: 127
LISP Dot Net Wrapper
« on: September 23, 2020, 04:11:55 PM »
I wasn't sure where to post this. This could be a "show your stuff, AutoLISP, or .NET" post. I made an AutoLISP wrapper for a .NET LISP function.
Code: [Select]
; Name: J. Overkamp
; Date: 09/23/2020
; Provided a C# .NET Wrapper to allow .NET
; to call the *error* function and pass on
; an error message as well as cancel the LISP
; Function
(defun DotNetWrapper (fun args / *error* temp rtn)
  (defun *error* (msg)
    (princ)
  )
  (defun temp (fun args / *error*)
    (defun *error* (msg)
      (princ "\n; error: ")
      (princ msg)
      (setq error t)
    )
    (vl-acad-defun '*error*)
    (apply fun (list args))
  )
 
  (setq rtn (temp fun args))
  (and error (/ 1 0))
  rtn
)

Here is the .NET code I used to test this:

Code: [Select]
try
{
    throw new System.Exception("test successful");
}
catch (System.Exception e)
{
    ResultBuffer error = new ResultBuffer(
        new TypedValue((int)LispDataType.Text, "*error*"),
        new TypedValue((int)LispDataType.Text, e.Message)
        );

    ResultBuffer rtn = Application.Invoke(error);
}

In a nutshell I finally found a way to push an error in LISP from .NET and have the LISP function stop.

huiz

  • Swamp Rat
  • Posts: 913
  • Certified Prof C3D
Re: LISP Dot Net Wrapper
« Reply #1 on: September 24, 2020, 02:41:36 AM »
Interesting!


Thanks for sharing :-)
The conclusion is justified that the initialization of the development of critical subsystem optimizes the probability of success to the development of the technical behavior over a given period.

Rustabout

  • Newt
  • Posts: 135
Re: LISP Dot Net Wrapper
« Reply #2 on: September 27, 2020, 10:04:37 AM »
Much of the amazing stuff in the "show your stuff" threads tend to get hidden. I only recently browsed those threads. It was funny because I recall almost being in tears trying to get an AutoLISP code to work (prevailed none the less) only to see the exact code in the "show your stuff"... just a better version.

I kind of wish there was a way to get more attention/credit to all the nifty code snippets provided here... for free none the less.

jtoverka

  • Newt
  • Posts: 127
Re: LISP Dot Net Wrapper
« Reply #3 on: September 28, 2020, 12:06:23 PM »
I kind of wish there was a way to get more attention/credit to all the nifty code snippets provided here... for free none the less.

Agreed. I would really prefer a like button, with the ability to rank, sort, and search code by the amount of likes it gets.

jtoverka

  • Newt
  • Posts: 127
Re: LISP Dot Net Wrapper
« Reply #4 on: September 28, 2020, 12:19:43 PM »
I have found a better implementation without calling the DotNetWrapper every time.
Code: [Select]
(defun JO:Netload (files / *error* cmdecho)
  (defun *error* (msg / )
    (and cmdecho (setvar 'cmdecho cmdecho))
    (princ "; error: ")
    (princ msg)
    (princ)
  )
  (defun JO:*error* (msg / *error*)
    (defun *error* (msg)
      (princ)
    )
    (princ "\n; error: ")
    (princ msg)
    (nil)
  )
 
  (setq cmdecho (getvar 'cmdecho))
  (setvar 'cmdecho 0)
 
  (foreach file files
    (command-s "NETLOAD" file)
  )
 
  (setvar 'cmdecho cmdecho)
  (setq cmdecho nil)
 
  (vl-acad-defun 'JO:*error*)
  (princ)
)

Call with:
Code: [Select]
(JO:Netload '("C:/Users/jtoverka/Desktop/NetDBX Library.dll"))

.NET function:
Code: [Select]
/// <summary>
/// Invokes a Lisp error message.
/// </summary>
/// <param name="message"></param>
/// <remarks>
/// This will trigger the end of the running lisp function.
/// </remarks>
public static void ThrowLispError(string message)
{
    try
    {
        using ResultBuffer error = new ResultBuffer(
            new TypedValue((int)LispDataType.Text, "JO:*error*"),
            new TypedValue((int)LispDataType.Text, message));

        ResultBuffer rtn = Application.Invoke(error);
    }
    catch (System.Exception e)
    {
        Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
        ed.WriteMessage($"\n; error: {message}\n");
        ed.WriteMessage($"\n; Unhandled LISP Error Exception: {e.Message}\n");
    }
}
As long as the .NET code invokes the "JO:*error*" autolisp function.

The only way to truly improve upon this implementation is the ability to create and evaluate lambda Lisp functions in .NET without the need for vl-acad-defun. If anyone knows how to do this, or something equivalent, please share  :-)