Author Topic: Passing a list of doubles to a lispfunction  (Read 2110 times)

0 Members and 1 Guest are viewing this topic.

GUIDO ROOMS

  • Guest
Passing a list of doubles to a lispfunction
« on: September 17, 2012, 08:55:07 AM »
Hi again,

I'm writing a lispfuntion that takes a list of doubles and returns a weighted average.
No problem, as long as I tested with lists of more than three doubles.
But when I pass a list of two or three doubles, the recieved argument is of type Point2D or Point3D respectively.
Is there anything I can do about this?
There may  be something I'm missing,or maybe I'm just dumb as a doorknob, but if that is not the case, then the nincompoop who's cooked up such a blunder should be executed at once.

BlackBox

  • King Gator
  • Posts: 3770
Re: Passing a list of doubles to a lispfunction
« Reply #1 on: September 17, 2012, 09:06:40 AM »
Perhaps posting your code would allow others to more quickly assist you.  :wink:
"How we think determines what we do, and what we do determines what we get."

GUIDO ROOMS

  • Guest
Re: Passing a list of doubles to a lispfunction
« Reply #2 on: September 17, 2012, 10:14:49 AM »
Code - Visual Basic: [Select]
  1.   <LispFunction("silly-test")>
  2.     Public Function SillyTest(args As ResultBuffer) As ResultBuffer
  3.         Return args
  4.     End Function
  5.  

This silly example should do.

Now, call these from the autocad command prompt:
(silly-test '(1 2 3))
(silly-test '(1 2 3 4))
The first returns a list containing a point list, the second returns the original list, which, I think, is to be expected . :kewl:

BlackBox

  • King Gator
  • Posts: 3770
Re: Passing a list of doubles to a lispfunction
« Reply #3 on: September 17, 2012, 10:58:51 AM »
Admittedly, I do not understand the purpose for a LispFunction Method that simply returns it's ResultBuffer parameter.

None-the-less, using your example, you should account for potential Exceptions such as:
Code - Auto/Visual Lisp: [Select]
  1. (silly-test)
  2.  

Gile helped me with the LispException Classes shown below, which have proved very useful... Here's a quickly written example to return only Point2d, or Point3d, that handles Exceptions for invalid arguments:

Code - C#: [Select]
  1. using Autodesk.AutoCAD.ApplicationServices;
  2. using Autodesk.AutoCAD.DatabaseServices;
  3. using Autodesk.AutoCAD.EditorInput;
  4. using Autodesk.AutoCAD.Geometry;
  5. using Autodesk.AutoCAD.Runtime;
  6.  
  7. namespace Foo
  8. {
  9.     public class Example
  10.     {
  11.  
  12.         private Editor ed =
  13.             Application.DocumentManager.MdiActiveDocument.Editor;
  14.  
  15.         [LispFunction("FOO")]
  16.         public object FOO(ResultBuffer res)
  17.         {
  18.             try
  19.             {
  20.                 if (res == null)
  21.                     throw new TooFewArgsException();
  22.  
  23.                 TypedValue[] args = res.AsArray();
  24.                 if (args.Length < 1)
  25.                     throw new TooFewArgsException();
  26.  
  27.                 if (args.Length > 1)
  28.                     throw new TooManyArgsException();
  29.  
  30.                 if (args[0].TypeCode == (int)LispDataType.Point2d)
  31.                 {
  32.                     Point2d value = (Point2d)args[0].Value;
  33.                     return value;
  34.                 }
  35.  
  36.                 if (args[0].TypeCode == (int)LispDataType.Point3d)
  37.                 {
  38.                     Point3d value = (Point3d)args[0].Value;
  39.                     return value;
  40.                 }
  41.  
  42.                 throw new ArgTypeException("Point2d, or Point3d", args[0]);
  43.  
  44.             }
  45.  
  46.             catch (LispException ex)
  47.             {
  48.                 ed.WriteMessage("\n; error: " + ex.Message + " \n");
  49.                 return null;
  50.             }
  51.  
  52.             catch (System.Exception ex)
  53.             {
  54.                 ed.WriteMessage("\n; error: " + ex.Message + " \n");
  55.                 return null;
  56.             }
  57.         }
  58.     }
  59.  
  60.     class LispException : System.Exception
  61.     {
  62.         public LispException(string msg) : base(msg) { }
  63.     }
  64.  
  65.     class TooFewArgsException : LispException
  66.     {
  67.         public TooFewArgsException() : base("too few arguments") { }
  68.     }
  69.  
  70.     class TooManyArgsException : LispException
  71.     {
  72.         public TooManyArgsException() : base("too many arguments") { }
  73.     }
  74.  
  75.     class ArgTypeException : LispException
  76.     {
  77.         public ArgTypeException(string s, TypedValue tv)
  78.             : base(string.Format(
  79.             "invalid argument type: {0}: {1}",
  80.             s, tv.TypeCode == (int)LispDataType.Nil ? "nil" : tv.Value))
  81.         { }
  82.     }
  83. }
  84.  
"How we think determines what we do, and what we do determines what we get."

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8702
  • AKA Daniel
Re: Passing a list of doubles to a lispfunction
« Reply #4 on: September 17, 2012, 11:05:31 AM »
Admittedly, I do not understand the purpose for a LispFunction Method that simply returns it's ResultBuffer parameter.

I do all the time, just to see how the lisp engine behaves. (the lisp engine tries to interpret the ResultBuffer)

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8702
  • AKA Daniel
Re: Passing a list of doubles to a lispfunction
« Reply #5 on: September 17, 2012, 11:07:17 AM »
Is there anything I can do about this?

not really, change your data format : )

BlackBox

  • King Gator
  • Posts: 3770
Re: Passing a list of doubles to a lispfunction
« Reply #6 on: September 17, 2012, 11:19:09 AM »
Admittedly, I do not understand the purpose for a LispFunction Method that simply returns it's ResultBuffer parameter.

I do all the time, just to see how the lisp engine behaves. (the lisp engine tries to interpret the ResultBuffer)

Guess I never really thought of it that way (nor had a need to)... Regardless, thanks for clarifying.
"How we think determines what we do, and what we do determines what we get."

GUIDO ROOMS

  • Guest
Re: Passing a list of doubles to a lispfunction
« Reply #7 on: September 18, 2012, 03:21:50 AM »
I do all the time, just to see how the lisp engine behaves. (the lisp engine tries to interpret the ResultBuffer)

Well, if I put in a list of three doubles on the lisp side, and on the .net side I get a Point3d structure, which is something completely different, I wouldn't call that "interpreting", I'd call that "mangling".
That kind of  "interpretation" is a bit like depositing money on your bank account, and the bank "interpreting" that as a gift.
Interpreting is all very well, as long as you leave the original intact.
If I want something to be interpreted, I'll do that myself, that's what typecasts and conversion functions are for.
Have one on me.

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8702
  • AKA Daniel
Re: Passing a list of doubles to a lispfunction
« Reply #8 on: September 18, 2012, 04:29:57 AM »
I do all the time, just to see how the lisp engine behaves. (the lisp engine tries to interpret the ResultBuffer)

Well, if I put in a list of three doubles on the lisp side, and on the .net side I get a Point3d structure, which is something completely different, I wouldn't call that "interpreting", I'd call that "mangling".

yep, I think it's the same with three ints too.
Just add something to the list, so the lisp engine doesn't nuke it ... there are only a few types to work around