.NET

Reply
Active Contributor
konstantinos.sakellaris
Posts: 43
Registered: ‎07-26-2001
Message 1 of 5 (288 Views)

Example of a function in C# that can be called from Autolisp

288 Views, 4 Replies
05-03-2006 04:19 AM
In .NET for ACAD 2007 there is a new functionality
( LispFunction) to develop functions with parameters and return values, which can be called from Autolisp / Visualisp.

Has anybody a code example of that in C#, showing how to define a function with parameters and return values in C# and call it from Autolisp ?

In the \ARX2007\samples\dotNet\CuiSamp one finds the definition of such a function but without callable parameters and return values !!!!
Distinguished Contributor
Alexander Rivilis
Posts: 303
Registered: ‎09-26-2004
Message 2 of 5 (288 Views)

Re: Example of a function in C# that can be called from Autolisp

05-03-2006 07:44 AM in reply to: konstantinos.sakellaris
Ok! :smileyhappy:
It is a simple example of function PrintArgs which print all it's parameters and return it back to lisp:
[code]
using System;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.DatabaseServices;

namespace ClassLibrary
{

public class Class
{
//
// From adscodes.h :
//
const int RTNONE = 5000; /* No result */
const int RTREAL = 5001; /*Real number */
const int RTPOINT = 5002; /* 2D point X and Y only */
const int RTSHORT = 5003; /* Short integer */
const int RTANG = 5004; /* Angle */
const int RTSTR = 5005; /* String */
const int RTENAME = 5006; /* Entity name */
const int RTPICKS = 5007; /* Pick set */
const int RTORINT = 5008; /* Orientation */
const int RT3DPOINT = 5009; /* 3D point - X, Y, and Z */
const int RTLONG = 5010; /* Long integer */
const int RTVOID = 5014; /* Blank symbol */
const int RTLB = 5016; /* list begin */
const int RTLE = 5017; /* list end */
const int RTDOTE = 5018; /* dotted pair */
const int RTNIL = 5019; /* nil */
const int RTDXF0 = 5020; /* DXF code 0 for ads_buildlist only */
const int RTT = 5021; /* T atom */
const int RTRESBUF = 5023; /* resbuf */
const int RTMODELESS = 5027; /* interrupted by modeless dialog */

[LispFunction("PrintArgs")]
static public ResultBuffer PrintArgs(ResultBuffer args)
{
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
foreach (TypedValue rb in args) {
switch (rb.TypeCode) {
case RTLB:
ed.WriteMessage("(");
break;
case RTLE:
ed.WriteMessage(")");
break;
case RTSTR:
ed.WriteMessage("\"{0}\"",rb.Value);
break;
case RTT:
ed.WriteMessage(" T ");
break;
case RTNIL:
ed.WriteMessage(" nil ");
break;

default:
ed.WriteMessage(" {0} ", rb.Value);
break;
}
}
return args;
}
}
}
[/code]
Test:
Command: (printargs '("String" (1.0 2.0) (1.0 2.0 3.0) T nil 100 200.5))
("String" (1,2) (1,2,3) T nil 100 200,5 )("String" (1.0 2.0) (1.0 2.0 3.0)
T nil 100 200.5)

Another example, such as (strcat ...) function:
[code]
[LispFunction("ConcatStrings")]
static public ResultBuffer ConcatStrings(ResultBuffer args)
{
ResultBuffer resbuf = new ResultBuffer();
String s = "";
foreach (TypedValue rb in args)
{
if (rb.TypeCode == RTSTR)
{
s += rb.Value;
}
}
resbuf.Add(new TypedValue(RTSTR, s));
return resbuf;
}
[/code]
Test:
Command: (concatstrings "Hello" ", " "World" "!") ("Hello, World!")
*Tony Tanzillo
Message 3 of 5 (288 Views)

Re: Example of a function in C# that can be called from Autolisp

05-03-2006 10:27 AM in reply to: konstantinos.sakellaris
Just to add to that, a return type of ResultBuffer results
in a LIST being returned to LISP with the contennts of the
ResultBuffer. To return an atom, you can just declare the
function with the LispFunction attribute to return any type
that can go in a result buffer.

So for example, your ConcatStrings function should return
a string, not a ResultBuffer:

[code]
[LispFunction("ConcatStrings")]
static public string ConcatStrings(ResultBuffer args)
{
StringBuilder sb = new StringBuilder();
foreach (TypedValue rb in args)
{
if( rb.Value is string )
sb.Append((string) rb.Value);
}
return sb.ToString();
}
[/code]

One other thing, is that when you iteratively concatenate
strings in .NET, it is much more efficient to use StringBulider
instead of using += on a string.

--
http://www.caddzone.com

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

wrote in message news:5162318@discussion.autodesk.com...
Ok! :smileyhappy:
It is a simple example of function PrintArgs which print all it's parameters and return it back to lisp:
[code]
using System;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.DatabaseServices;

namespace ClassLibrary
{

public class Class
{
//
// From adscodes.h :
//
const int RTNONE = 5000; /* No result */
const int RTREAL = 5001; /*Real number */
const int RTPOINT = 5002; /* 2D point X and Y only */
const int RTSHORT = 5003; /* Short integer */
const int RTANG = 5004; /* Angle */
const int RTSTR = 5005; /* String */
const int RTENAME = 5006; /* Entity name */
const int RTPICKS = 5007; /* Pick set */
const int RTORINT = 5008; /* Orientation */
const int RT3DPOINT = 5009; /* 3D point - X, Y, and Z */
const int RTLONG = 5010; /* Long integer */
const int RTVOID = 5014; /* Blank symbol */
const int RTLB = 5016; /* list begin */
const int RTLE = 5017; /* list end */
const int RTDOTE = 5018; /* dotted pair */
const int RTNIL = 5019; /* nil */
const int RTDXF0 = 5020; /* DXF code 0 for ads_buildlist only */
const int RTT = 5021; /* T atom */
const int RTRESBUF = 5023; /* resbuf */
const int RTMODELESS = 5027; /* interrupted by modeless dialog */

[LispFunction("PrintArgs")]
static public ResultBuffer PrintArgs(ResultBuffer args)
{
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
foreach (TypedValue rb in args) {
switch (rb.TypeCode) {
case RTLB:
ed.WriteMessage("(");
break;
case RTLE:
ed.WriteMessage(")");
break;
case RTSTR:
ed.WriteMessage("\"{0}\"",rb.Value);
break;
case RTT:
ed.WriteMessage(" T ");
break;
case RTNIL:
ed.WriteMessage(" nil ");
break;

default:
ed.WriteMessage(" {0} ", rb.Value);
break;
}
}
return args;
}
}
}
[/code]
Test:
Command: (printargs '("String" (1.0 2.0) (1.0 2.0 3.0) T nil 100 200.5))
("String" (1,2) (1,2,3) T nil 100 200,5 )("String" (1.0 2.0) (1.0 2.0 3.0)
T nil 100 200.5)

Another example, such as (strcat ...) function:
[code]
[LispFunction("ConcatStrings")]
static public ResultBuffer ConcatStrings(ResultBuffer args)
{
ResultBuffer resbuf = new ResultBuffer();
String s = "";
foreach (TypedValue rb in args)
{
if (rb.TypeCode == RTSTR)
{
s += rb.Value;
}
}
resbuf.Add(new TypedValue(RTSTR, s));
return resbuf;
}
[/code]
Test:
Command: (concatstrings "Hello" ", " "World" "!") ("Hello, World!")
Distinguished Contributor
Alexander Rivilis
Posts: 303
Registered: ‎09-26-2004
Message 4 of 5 (288 Views)

Re: Example of a function in C# that can be called from Autolisp

05-03-2006 10:33 AM in reply to: konstantinos.sakellaris
Thanks, Tony!
Active Contributor
konstantinos.sakellaris
Posts: 43
Registered: ‎07-26-2001
Message 5 of 5 (288 Views)

Re: Example of a function in C# that can be called from Autolisp

05-04-2006 07:49 AM in reply to: konstantinos.sakellaris
Many many Thanks to you Alexander !!!!
Many Thanks, Tony!
One really needs programming examples.

I think, it would be a good idea if Autodesk could create a
learning by example ".NET Outlet" much like a searchable
knowledge base for beginning and intermediate C# API programmers.

Once again, you helped me a lot. Thanks !!!

Konstantin
University Building Services
CAD Dept.
Heidelberg, Germany

You are not logged in.

Log into access your profile, ask and answer questions, share ideas and more. Haven't signed up yet? Register

Announcements
Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.

Need installation help?

Start with some of our most frequented solutions to get help installing your software.

Ask the Community