So I'm trying to understand the process for creating a Lisp function in c# .net.
Code:
public class LispException : System.Exception
{
public LispException(string msg) : base(msg) { }
}
public class TooFewArgsException : LispException
{
public TooFewArgsException() : base("too few arguments") { }
}
public class TooManyArgsException : LispException
{
public TooManyArgsException() : base("too many arguments") { }
}
public class ArgumentTypeException : LispException
{
public ArgumentTypeException(string s, TypedValue tv)
: base(string.Format("Bad argument type: {0} {1}", s, tv.TypeCode == (int)LispDataType.Nil ? "nil" : tv.Value)) { }
}
[LispFunction("DoubleValue")]
public object DoubleValue(ResultBuffer argBuffer)
{
Editor ed = MgdAcApplication.DocumentManager.MdiActiveDocument.Editor;
Object Res = new ResultBuffer();
try
{
if (argBuffer == null)
{
throw new TooFewArgsException();
}
else
{
TypedValue[] argArray = argBuffer.AsArray();
int ArrayLen = argArray.Length;
if (ArrayLen > 1)
{
throw new TooManyArgsException();
}
else
{
if (argArray[0].TypeCode == (short)LispDataType.Double)
{
Res = ((double)argArray[0].Value) * 2.0;
}
else if (argArray[0].TypeCode == (short)LispDataType.Int16)
{
Res = ((Int16)argArray[0].Value) * 2;
}
else if (argArray[0].TypeCode == (short)LispDataType.Int32)
{
Res = ((Int32)argArray[0].Value) * 2;
}
else
{
throw new ArgumentTypeException("numberp", argArray[0]);
}
}
}
return Res;
}//end try
catch (LispException ex)
{
ed.WriteMessage("\n; error: {0}\n", ex.Message);
return null;
}
catch (Autodesk.AutoCAD.Runtime.Exception ex)
{
ed.WriteMessage("\nAutoCAD error: {0}\n", ex.Message);
return null;
}
catch (System.Exception ex)
{
ed.WriteMessage("\nSystem error: {0}\n", ex.Message);
return null;
}
}
I can netload this without a problem, and it does what it's supposed to do...up to a point...
Code:
Command: (setq Num 12)
12
Command: (setq Num (DoubleValue Num))
24
Command: (setq Num (DoubleValue 12.34))
24.68
Command: (setq Num (abs))
; error: too few arguments
Command: !Num
24.68
Command: (setq Num (DoubleValue))
; error: too few arguments
nil
Command: !Num
nil
Notice that the call to (abs) with no arguments resulted in an error, but (abs) didn't return a value to the setq, so Num kept it's previous value.
How do I create a Lisp function in c# that will properly catch exceptions, but not return nil to the calling function, just like normal autolisp functions?
Thanks!