Code Red > .NET
Can LispFunction return an atom ?
gile:
Hi,
I try to learn C#, creating some LISP extensions.
I read some post (here and there) about ResultBuffer to get the function arguments and to return the function result.
But this way the returned value is a list, i.e. (don't laught, I'm a complete beginner)
--- Code: ---using System;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.DatabaseServices;
namespace DivLisp
{
public class DivLisp
{
[LispFunction("div")]
public ResultBuffer div(ResultBuffer args)
{
Array inputArgs = args.AsArray();
double arg1 = Convert.ToDouble(((TypedValue)(inputArgs.GetValue(0))).Value);
double arg2 = Convert.ToDouble(((TypedValue)(inputArgs.GetValue(1))).Value);
ResultBuffer result = new ResultBuffer(new TypedValue((int)LispDataType.Double, (arg1 / arg2)));
return result;
}
}
}
--- End code ---
Commande: (div 10 3)
(3.33333)
Is there a way so that a LISP function which requieres arguments returns an atom (i.e. (div 10 3) returns 3.33333)
It's Alive!:
Hi gile,
The LispFunction can return any of these types.
From the docs..
int
double
TypedValue
ResultBuffer
string
Point2d
Point3d
bool
void
ObjectId
SelectionSet
Since in C#, any type can be cast as an Object, you can just return an Object I.e
--- Code: --- [LispFunction("Lisp1")]
public static Object Lisp1(ResultBuffer resBuf)
{
if (resBuf == null)
{
return false;
}
else
{
return true;
}
}
--- End code ---
gile:
Thank you very much Daniel, it was so simple !!!
This made me understand a little more how works C# and how many I have to learn...
It's Alive!:
My pleasure :-)
It's Alive!:
So let’s break down how AutoCAD ObjectARX interprets atoms and lists. ObjectARX sees lisp types as a resbuf, essentially a container that has a type code and a value. I.e. (5000 . “Gile”) where the type code describes the type of the value. In C#, the basic type for getting and returning values to lisp is called a TypedValue, it’s essentially the same as ObjectARX’s resbuf as it contains a type code and an Object.
So a real number would look like
(LispDataType.Double . 3.33333)
And a String
(LispDataType.Text . “Gile”)
Keep in mind that C# knows nothing of parenthesis or the dot in the dotted pair.
When returning custom lists to lisp, you must explicitly construct them
I.e
(LispDataType.ListBegin . null)
(LispDataType.Double . 3.33333)
(LispDataType.DottedPair . null)
(LispDataType.Text . “Gile”)
(LispDataType.ListEnd , null)
Would return (3.3333 . “Gile”) to lisp
A ResultBuffer in C# is a container for TypedValue types. Its unmanaged type is a resbuf or resbuf chain
an example
--- Code: --- [LispFunction("Lisp1")]
public static Object Lisp1(ResultBuffer resBuf)
{
//++-- A new buffer to return
ResultBuffer buf = new ResultBuffer();
//make a list
buf.Add(new TypedValue((int)LispDataType.ListBegin));
buf.Add(new TypedValue((int)LispDataType.Double , 3.3333));
buf.Add(new TypedValue((int)LispDataType.DottedPair));
buf.Add(new TypedValue((int)LispDataType.Text, "Gile"));
buf.Add(new TypedValue((int)LispDataType.ListEnd));
return buf;
}
--- End code ---
Sorry, I haven’t had my coffee yet, so I’m probably not doing this well
Navigation
[0] Message Index
[#] Next page
Go to full version