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

to call VisualLisp routines from C#

4 REPLIES 4
Reply
Message 1 of 5
Anonymous
406 Views, 4 Replies

to call VisualLisp routines from C#

. . . because I'm considering to migrate from VisualLisp toward C#,
I'm thinking that is very interseting to know if it is possible
to call VisualLisp routines, form C#.

And if this is possible
I wish know if it is possible to do this
while a modal C# dialog is active.

What type of problems are there ?

Maybe conversion from VisualLisp types and C# types and viceversa ?

If all these things are possible, this migration will be sweet,
because all the time that I will need to write something that is too much
hard for me in C#,
but it is already done or easy in VisualLisp,
I will call a VisualLisp routine from C#. (temporarily, until I will be able
to do it in C#)

thanks

ciao

domenico
4 REPLIES 4
Message 2 of 5
Anonymous
in reply to: Anonymous

I did not deep testing, but you can try this code:
[code]
using System;
using System.Runtime.InteropServices;
using System.Text;
using System.Collections;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;

[assembly: CommandClass(typeof(Rivilis.EvalLisp))]

namespace Rivilis
{
public class EvalLisp
{

[System.Security.SuppressUnmanagedCodeSecurity]
[DllImport("acad.exe", CallingConvention = CallingConvention.Cdecl,
EntryPoint="?acedEvaluateLisp@@YAHPBDAAPAUresbuf@@@Z") ]
// For AutoCAD 2007: CharSet = CharSet.Unicode
// [DllImport("acad.exe", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl
// EntryPoint="?acedEvaluateLisp@@YAHPB_WAAPAUresbuf@@@Z") ]
extern static int acedEvaluateLisp(string arg, out IntPtr result);

//-----------------------------------------------------------------
// AcadEvalLisp
// ---------------
// Function evaluate lisp-expression passed as string
// arg - lisp-expression
// Return: ResultBuffer
//-----------------------------------------------------------------
static public ResultBuffer AcadEvalLisp(string arg)
{
IntPtr rb = IntPtr.Zero;
acedEvaluateLisp(arg, out rb);
if (rb != IntPtr.Zero)
{
try
{
ResultBuffer rbb = DisposableWrapper.Create(typeof(ResultBuffer), rb, true) as ResultBuffer;
return rbb;
}
catch
{
return null;
}
}
return null;
}
//-----------------------------------------------------------------
//
// Only for testing
//
//-----------------------------------------------------------------

[CommandMethod("TestEval")]
static public void TestEval()
{
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
PromptStringOptions pr = new PromptStringOptions("\nType Lisp-expression: ");
pr.AllowSpaces = true;
PromptResult res = ed.GetString(pr);
if (res.Status == PromptStatus.OK)
{
ResultBuffer args = AcadEvalLisp(res.StringResult);
if (args != null)
{
StringBuilder s = new StringBuilder();
s.Append("\n-----------------------------");
try
{
foreach (TypedValue val in (IEnumerable) args)
{
s.AppendFormat("\n{0} -> {1}", val.TypeCode, val.Value.ToString());
}
}
catch
{
}
s.Append("\n-----------------------------");
ed.WriteMessage(s.ToString());
}
else
{
ed.WriteMessage("\nNo return! :(");
}
}
}
}
}
[/code]
Message 3 of 5
Anonymous
in reply to: Anonymous

Thanks
I will try it.
Message 4 of 5
Anonymous
in reply to: Anonymous

Has any better method come up? Or has this code been verified to be stable?
Message 5 of 5
Anonymous
in reply to: Anonymous

domenico,

Check out this forum a few posts back ...
AutoCAD: .NET for LISP Programmers – October 16th, 2008.

[System.Security.SuppressUnmanagedCodeSecurity]
[DllImport("acad.exe", CallingConvention = CallingConvention.Cdecl)]
extern static private int acedInvoke(IntPtr args, out IntPtr result);
public static ResultBuffer InvokeLisp(ResultBuffer args, ref int stat)
{
IntPtr rb = IntPtr.Zero;
stat = acedInvoke(args.UnmanagedObject, out rb);
if (stat == (int)PromptStatus.OK && rb != IntPtr.Zero)
return (ResultBuffer)DisposableWrapper.Create(typeof(ResultBuffer), rb, true);
return null;
}

r,
dennis

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