TheSwamp
Code Red => AutoLISP (Vanilla / Visual) => Topic started by: jtoverka 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.
; 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:
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.
-
Interesting!
Thanks for sharing :-)
-
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.
-
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.
-
I have found a better implementation without calling the DotNetWrapper every time.
(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:
(JO:Netload '("C:/Users/jtoverka/Desktop/NetDBX Library.dll"))
.NET function:
/// <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 :-)