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

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

4 REPLIES 4
Reply
Message 1 of 5
kosak956
1557 Views, 4 Replies

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

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 !!!!
4 REPLIES 4
Message 2 of 5
Anonymous
in reply to: kosak956

Ok! 🙂
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!")
Message 3 of 5
Anonymous
in reply to: kosak956

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! 🙂
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!")
Message 4 of 5
Anonymous
in reply to: kosak956

Thanks, Tony!
Message 5 of 5
kosak956
in reply to: kosak956

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

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