Author Topic: Passing list from lisp to vb.net DLL  (Read 1696 times)

0 Members and 1 Guest are viewing this topic.

civil.eng

  • Newt
  • Posts: 66
Passing list from lisp to vb.net DLL
« on: July 12, 2021, 06:05:55 AM »
Hi all,

I'm looking for a way to pass a list to DLL, for exmaple I have a  list of Entity names and Numbers :

(setq lst ((<Entity name: 1a9f9948470> "1") (<Entity name: 1a9f9948480> "2") (<Entity name: 1a9f9948490>  "3"))

And on the other hand there is a .Net DLL for doing some works on each entity.

Can someone give me a solution?

Jeff_M

  • King Gator
  • Posts: 4094
  • C3D user & customizer
Re: Passing list from lisp to vb.net DLL
« Reply #1 on: July 12, 2021, 01:04:53 PM »
Create a LispFunction in your .NET code which takes a ResultBuffer for the arguments. Something like this:
Code - C#: [Select]
  1.         [LispFunction("MyLispFunc")]
  2.         public ResultBuffer mylispfunc(ResultBuffer resBuf)
  3.         {
  4.             ResultBuffer rbfResult = default(ResultBuffer);
  5.             var args = resBuf.AsArray();
  6.             var theList = new List<KeyValuePair<ObjectId, short>>();
  7.             for (int i = 2; i < args.Length - 2; i += 4)
  8.             {
  9.                 theList.Add(new KeyValuePair<ObjectId, short>((ObjectId)args[i].Value, (short)args[i + 1].Value));
  10.             }
  11.             //do what you need with the list
  12.             rbfResult = new ResultBuffer(new TypedValue(Convert.ToInt32(5005), "ProbableSuccess"));
  13.             return rbfResult;
  14.         }
  15.  

And you can use it like so:
Code - Auto/Visual Lisp: [Select]
  1. (setq i 0)
  2. (while (setq e (entsel "\nSelect object:"))
  3.   (setq thelist (cons (list (car e) i)  thelist))
  4.   (setq i (1+ i))
  5.   )
  6. (mylispfunc thelist)
  7.  

civil.eng

  • Newt
  • Posts: 66
Re: Passing list from lisp to vb.net DLL
« Reply #2 on: July 14, 2021, 10:00:02 AM »
Many thanks Jeff_M,
I tried to write some codes to read vertices of each polyline after creation list :

Code: [Select]
...........
...........

ShapeFile shp = new Esri.ShapeFile.Polygon();

foreach (var item in theList)
            {

                Polyline lwp = item.Key as Polyline;
               int vn = lwp.NumberOfVertices;

               for (int i = 0; i < vn; i++)

              {
                Point2d vertex = lwp.GetPoint2dAt(i);
                shp.Vertices.Add(vertex.x, vertex.y);
              }

                shp.Fields["Text"].Value = item.Value.ToString();
                shp.Create();
            }

Shape functions is correct, But the loop of polyline's vertices is wrong. could you correct it?
Thanks in advance.
« Last Edit: July 14, 2021, 10:17:38 AM by civil.eng »

Jeff_M

  • King Gator
  • Posts: 4094
  • C3D user & customizer
Re: Passing list from lisp to vb.net DLL
« Reply #3 on: July 14, 2021, 10:53:54 AM »
This should do it.
Code - C#: [Select]
  1.         private void processTheList(List<KeyValuePair<ObjectId, short>> theList)
  2.         {
  3.             var db = HostApplicationServices.WorkingDatabase;
  4.             using (Transaction tr = db.TransactionManager.StartTransaction())
  5.             {
  6.                 foreach (var item in theList)
  7.                 {
  8.                     ShapeFile shp = new Esri.ShapeFile.Polygon();
  9.                     Polyline lwp = tr.GetObject(item.Key, OpenMode.ForRead) as Polyline;
  10.                     int vn = lwp.NumberOfVertices;
  11.  
  12.                     for (int i = 0; i < vn; i++)
  13.                     {
  14.                         Point2d vertex = lwp.GetPoint2dAt(i);
  15.                         shp.Vertices.Add(vertex.X, vertex.Y);
  16.                     }
  17.                     shp.Fields["Text"].Value = item.Value.ToString();
  18.                     shp.Create();
  19.                 }
  20.                 tr.Commit();
  21.             }
  22.         }
  23.  

civil.eng

  • Newt
  • Posts: 66
Re: Passing list from lisp to vb.net DLL
« Reply #4 on: July 15, 2021, 12:12:25 PM »
Great, that worked,
And my last request, I intend to add a string to LispFunction arguments, like this :

(mylispfunc "F:\shp\Poly.shp" thelist)

I added this line code to call the string argument, but not worked :

Code: [Select]
[LispFunction("MYLISPFUNC")]
        public static ResultBuffer mylispfunc(ResultBuffer resBuf)
        {
            ResultBuffer rbfResult = default(ResultBuffer);
            var args = resBuf.AsArray();

            string shpAdress = (string)args[0].Value;
            ......

And at the last I want to replace this address of C# codes with the string which is called from lisp :

Code: [Select]
shp.run(@"F:\shp\Poly.shp");
« Last Edit: July 15, 2021, 12:29:56 PM by civil.eng »

Jeff_M

  • King Gator
  • Posts: 4094
  • C3D user & customizer
Re: Passing list from lisp to vb.net DLL
« Reply #5 on: July 15, 2021, 01:51:22 PM »
The lisp line should be:
(mylispfunc "F:\\shp\\Poly.shp" thelist)
which the shpAdress will correctly be set to.

Then it should work in the .net code like so:
shp.run(shpAdress);

be sure you modify the for(int  i = 2;.... to start at 3

Here is what I have (I don't have the ESRI libraries so those object are commented out).
Code - C#: [Select]
  1.         [LispFunction("MyLispFunc")]
  2.         public ResultBuffer mylispfunc(ResultBuffer resBuf)
  3.         {
  4.             ResultBuffer rbfResult = default(ResultBuffer);
  5.             var args = resBuf.AsArray();
  6.             var theList = new List<KeyValuePair<ObjectId, short>>();
  7.             var shpAdress = (string)args[0].Value;
  8.             for (int i = 3; i < args.Length - 2; i += 4)
  9.             {
  10.                 theList.Add(new KeyValuePair<ObjectId, short>((ObjectId)args[i].Value, (short)args[i + 1].Value));
  11.             }
  12.             processTheList(shpAdress, theList);
  13.             rbfResult = new ResultBuffer(new TypedValue(Convert.ToInt32(5005), "ProbableSuccess"));
  14.             return rbfResult;
  15.         }
  16.  
  17.         private void processTheList(string shpAdress, List<KeyValuePair<ObjectId, short>> theList)
  18.         {
  19.             var db = HostApplicationServices.WorkingDatabase;
  20.             using (Transaction tr = db.TransactionManager.StartTransaction())
  21.             {
  22.                 foreach (var item in theList)
  23.                 {
  24.                     //ShapeFile shp = new Esri.ShapeFile.Polygon();
  25.                     Polyline lwp = tr.GetObject(item.Key, OpenMode.ForRead) as Polyline;
  26.                     int vn = lwp.NumberOfVertices;
  27.  
  28.                     for (int i = 0; i < vn; i++)
  29.                     {
  30.                         Point2d vertex = lwp.GetPoint2dAt(i);
  31.                         //shp.Vertices.Add(vertex.X, vertex.Y);
  32.                     }
  33.                     //shp.Fields["Text"].Value = item.Value.ToString();
  34.                     //shp.Create();
  35.                     //shp.run(shpAdress);
  36.                 }
  37.                 tr.Commit();
  38.             }
  39.         }
  40.  

civil.eng

  • Newt
  • Posts: 66
Re: Passing list from lisp to vb.net DLL
« Reply #6 on: July 17, 2021, 04:55:11 AM »
Thank you so much,, you did great.
My project is finished, I used this version of ojectarx :
"Autodesk_ObjectARX_2017_Win_64_and_32_Bit"

I know that this DLL is compatible with Autocad 2013 and higher versions, but what about .Net Framework version? Is it compatible with all version of .Net Framework and all windows ? I mean the DLL I've created is dependent on Net Framework versions?

My visual studio version is 2015 and I choosed version 4.5 of Net Framework, Actually I intend to create a DLL for all versions of autocad and windows, I just know I have to create one DLL for Autocad 2007 to 2012 and one DLL for Autocad 2013 to higher.
Someone told me I should use c++ to get rid of Net Framework version error.
« Last Edit: July 17, 2021, 05:37:45 AM by civil.eng »