Author Topic: Convert list to .Net Array  (Read 2535 times)

0 Members and 1 Guest are viewing this topic.

civil.eng

  • Newt
  • Posts: 66
Convert list to .Net Array
« on: September 24, 2022, 05:18:12 AM »
Hello everyone,

I want to convert a list of strings which is called from AutoLisp to vb.net array :

Code: [Select]
(defun c:test ()
  (setq lst '("ss" "ff" "ee"))
  (MyLispFunction lst)
)

And code in .Net :
Code: [Select]
<LispFunction("MyLispFunction")>
        Public Function MyLispFunction(ByVal args As ResultBuffer) ' This method can have any name

            Dim tv() As TypedValue = args.AsArray

            Dim list As New List(Of String)

            For i = 0 To tv.Length
                list.Add(tv(i).ToString())
            Next

            Return 1
        End Function

I'm running the produced DLL in Autocad but the .Net array is wrong so that it's not including just strings. What the problem is with my code ?

Thanks in advance.
« Last Edit: September 24, 2022, 05:22:24 AM by civil.eng »

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8659
  • AKA Daniel
Re: Convert list to .Net Array
« Reply #1 on: September 24, 2022, 06:17:44 AM »
there's some classes written by Tony T, somewhere in this forum,  that has operators to convert result buffers to arrays and back.
I think its C# but there's online converters

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8659
  • AKA Daniel
Re: Convert list to .Net Array
« Reply #2 on: September 24, 2022, 06:21:02 AM »
I found it here
https://www.theswamp.org/index.php?topic=14495.msg186823#msg186823

not sure if the conversion is correct but...

Code - Visual Basic: [Select]
  1. Public Class TypedValueList
  2.     Inherits List(Of TypedValue)
  3.     Public Sub New(ParamArray args As TypedValue())
  4.         AddRange(args)
  5.     End Sub
  6.  
  7.     ' Make it a bit easier to add items:
  8.  
  9.     Public Sub Add(ByVal typecode As Integer, ByVal value As Object)
  10.         MyBase.Add(New TypedValue(typecode, value))
  11.     End Sub
  12.  
  13.     ' Implicit conversion to SelectionFilter
  14.    Public Shared Widening Operator CType(ByVal src As TypedValueList) As SelectionFilter
  15.         Return If(src IsNot Nothing, New SelectionFilter(src), Nothing)
  16.     End Operator
  17.  
  18.     ' Implicit conversion to ResultBuffer
  19.    Public Shared Widening Operator CType(ByVal src As TypedValueList) As ResultBuffer
  20.         Return If(src IsNot Nothing, New ResultBuffer(src), Nothing)
  21.     End Operator
  22.  
  23.     ' Implicit conversion to TypedValue[]
  24.    Public Shared Widening Operator CType(ByVal src As TypedValueList) As TypedValue()
  25.         Return If(src IsNot Nothing, src.ToArray(), Nothing)
  26.     End Operator
  27.  
  28.     ' Implicit conversion from TypedValue[]
  29.    Public Shared Widening Operator CType(ByVal src As TypedValue()) As TypedValueList
  30.         Return If(src IsNot Nothing, New TypedValueList(src), Nothing)
  31.     End Operator
  32.  
  33.     ' Implicit conversion from SelectionFilter
  34.    Public Shared Widening Operator CType(ByVal src As SelectionFilter) As TypedValueList
  35.         Return If(src IsNot Nothing, New TypedValueList(src.GetFilter()), Nothing)
  36.     End Operator
  37.  
  38.     ' Implicit conversion from ResultBuffer
  39.    Public Shared Widening Operator CType(ByVal src As ResultBuffer) As TypedValueList
  40.         Return If(src IsNot Nothing, New TypedValueList(src.AsArray()), Nothing)
  41.     End Operator
  42.  
  43. End Class
  44.  
« Last Edit: September 24, 2022, 06:26:33 AM by It's Alive! »

civil.eng

  • Newt
  • Posts: 66
Re: Convert list to .Net Array
« Reply #3 on: September 24, 2022, 07:08:39 AM »
Thanks, but It's not what I wanted, I just want to pass a Lisp list to .net array.

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8659
  • AKA Daniel
Re: Convert list to .Net Array
« Reply #4 on: September 24, 2022, 09:09:17 AM »
oops, anyway that's a pretty handy class for working with ResultBuffers and AutoLisp.
Have a look deeper into TypedValue, its a structure with two items, a type code, and a value; so TypedValue.ToString might not be what you want.





gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: Convert list to .Net Array
« Reply #5 on: September 24, 2022, 03:57:54 PM »
Hi,

The main difficulty when connecting LISP and .NET is that LISP is poorly dynamic typed and .NET is strongly static typed.

The first thing you should do to understand how .NET sees the LISP arguments ('args' ResultBuffer) is to inspect the contents this ResultBuffer.
You can use some LispFunction like this:
Code - C#: [Select]
  1.         [LispFunction("argsTest")]
  2.         public static ResultBuffer argsTest(ResultBuffer resbuf)
  3.         {
  4.             if (resbuf != null)
  5.             {
  6.                 var ed = Application.DocumentManager.MdiActiveDocument.Editor;
  7.                 foreach (TypedValue typedValue in resbuf.AsArray())
  8.                 {
  9.                     ed.WriteMessage($"{(LispDataType)typedValue.TypeCode}: {typedValue.Value}\n");
  10.                 }
  11.             }
  12.             return resbuf;
  13.         }

Some times ago, I tried to make a [ur=http://www.theswamp.org/index.php?topic=45841.msg510047#msg510047l]little library[/url] to convert LISP arguments into .NET types, it defines a GetListContent method which may interest you.
Speaking English as a French Frog