.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Return Type of LispFunction in C#

9 REPLIES 9
Reply
Message 1 of 10
Anonymous
2674 Views, 9 Replies

Return Type of LispFunction in C#

Hi,

I am starting to work with autocad 2007 and C# 2005. I have a function that normally return a ResultBuffer. But when it encounter an error I would like to return a single string. Is it possible to do something like that ?

[LispFunction("TestFct")]
public ResultBuffer TestFct(ResultBuffer buf)
{


Array args = buf.AsArray();
string path = Convert.ToString(((TypedValue)(args.GetValue(0))).Value);

ResultBuffer buffResult = new ResultBuffer();
string[] directories = Directory.GetDirectories(path);
foreach (string subDirName in directories)
buffResult.Add(new TypedValue(RTSTR, subDirName.Substring(subDirName.LastIndexOf("\\") + 1)));

if (directories.Length = 0)
{
return "none"; // <-- will not work here. but it is the result that i would like.
}
}

--------------------
I would like to have "none" and not ("none").
9 REPLIES 9
Message 2 of 10
Anonymous
in reply to: Anonymous

woups I forgot a:

return buffResult;

near the end. But my question still remain.
Message 3 of 10
Anonymous
in reply to: Anonymous

The runtime uses reflection to look at the result type of your
LispFunction-attributed method, to determine what to return.

Unfortunately, in terms of LISP's dynamic and untyped nature,
that is nothing short of blasphemy.

I haven't tried it, but you might try declaring your function's
return type as 'object', and prey that the runtime wrapper will
look at the returned value's type and select the appropriate acedRetXxxx() function to call.

If it works, they're forgiven 🙂

--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2004/2005/2006/2007
http://www.acadxtabs.com

wrote in message news:5258429@discussion.autodesk.com...
Hi,

I am starting to work with autocad 2007 and C# 2005. I have a function that normally return a ResultBuffer. But when it encounter an error I would like to return a single string. Is it possible to do something like that ?

[LispFunction("TestFct")]
public ResultBuffer TestFct(ResultBuffer buf)
{


Array args = buf.AsArray();
string path = Convert.ToString(((TypedValue)(args.GetValue(0))).Value);

ResultBuffer buffResult = new ResultBuffer();
string[] directories = Directory.GetDirectories(path);
foreach (string subDirName in directories)
buffResult.Add(new TypedValue(RTSTR, subDirName.Substring(subDirName.LastIndexOf("\\") + 1)));

if (directories.Length = 0)
{
return "none"; // <-- will not work here. but it is the result that i would like.
}
}

--------------------
I would like to have "none" and not ("none").
Message 4 of 10
Anonymous
in reply to: Anonymous

"Tony Tanzillo" wrote

>> The runtime uses reflection to look at the result type of your
>> LispFunction-attributed method, to determine what to return.

This is completely and utterly wrong.

There's no reflection involved, the runtime just looks at the
type of the value your method returns, and it calls the
appropriate acedRetXxxx() function.

It also accepts boxed results.

So what I said you should pray will happen, does happen.
Just declare your function's return type as 'object', and
return any compatible type.

Oh and sorry to the developers, no blasphemy there...

--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2004/2005/2006/2007
http://www.acadxtabs.com
Message 5 of 10
Anonymous
in reply to: Anonymous

Yes thank you.

And now instead of returning a string, my function now returns the object null and AutoCAD writes nil in the output.

bye
Message 6 of 10
Anonymous
in reply to: Anonymous

What about this code:
[code]
[LispFunction("TestFnc")]
static public object TestFnc(ResultBuffer args)
{
ResultBuffer res = new ResultBuffer();
StringBuilder s = new StringBuilder();
foreach (TypedValue rb in args) {
if (rb.Value is string) {
s.Append(rb.Value);
res.Add(new TypedValue(RTSTR, rb.Value));
}
}
if (s.Length > 0)
return res;
else
return new TypedValue(RTSTR, "none");
}
[/code]
[code]
Command: (testfnc "") "none"
Command: (testfnc '("" "" "")) "none"
Command: (testfnc '("aaa" "bbb" "ccc")) ("aaa" "bbb" "ccc")
[/code]

Your's fixed sample:
[code]
[LispFunction("TestFct")]
public object TestFct(ResultBuffer buf)
{
Array args = buf.AsArray();
string path = Convert.ToString(((TypedValue)(args.GetValue(0))).Value);
ResultBuffer buffResult = new ResultBuffer();
string[] directories = Directory.GetDirectories(path);
foreach (string subDirName in directories)
buffResult.Add(new TypedValue(RTSTR, subDirName.Substring(subDirName.LastIndexOf("\\") + 1)));
if (directories.Length == 0)
return new TypedValue(RTSTR,"none");
else
return buffResult;
}
[/code]
[code]
Command: (testfct "c:\\ua-ix") ("Lists" "V_TMeterLAT" "WIPFW")
Command: (testfct "c:\\t") "none"
[/code] Message was edited by: Alexander Rivilis
Message 7 of 10
Anonymous
in reply to: Anonymous

If that's not what you intended, post your revised
code and we'll have a look.

It is quite easy to return any type (that can be
returned to LISP), and the runtime interprets
the value 'null'. as NIL in LISP.

--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2004/2005/2006/2007
http://www.acadxtabs.com

wrote in message news:5259486@discussion.autodesk.com...
Yes thank you.

And now instead of returning a string, my function now returns the object null and AutoCAD writes nil in the output.

bye
Message 8 of 10
Anonymous
in reply to: Anonymous

The file at the link below shows a sample LispFunction
I wrote some time ago while prototyping some custom
attributes to help automate validation of LISP arguments,
and a custom exception class that reports caller-friendly
error information.

The test case function was returning a list of strings, and
so I just made a simple change to it to return either a list
of strings, or a simple string if the list contains only one
element.

http://www.caddzone.com/LispFunction.cs

--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2004/2005/2006/2007
http://www.acadxtabs.com

wrote in message news:5259486@discussion.autodesk.com...
Yes thank you.

And now instead of returning a string, my function now returns the object null and AutoCAD writes nil in the output.

bye
Message 9 of 10
Hallex
in reply to: Anonymous

Dear mr.Tony

 

Here is wrong link on LispFunction.cs:

 

http://www.caddzone.com/LispFunction.cs

 

I see InsertJig class code instead

 

Can you replace it with the right one

 

Thanks in advance

 

~'J'~

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Message 10 of 10
Anonymous
in reply to: Hallex

HI. Sorry about that and thanks for pointing that out.

 

It's a very old post that you must have found it in, and I'm not sure

if I still have the correct file. If I can find it, I'll replace it.

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost