Author Topic: Is it possible to convert from Lisp to C#  (Read 6574 times)

0 Members and 1 Guest are viewing this topic.

thenndral

  • Guest
Is it possible to convert from Lisp to C#
« on: May 08, 2014, 04:42:26 AM »
Hi,

I'm using Autocad 2013 with C#

I don't have a knowledge in Lisp, so I'm looking for convert Lisp code.
Is it possible to convert from Lisp to C#?
Any free tools or Link available?

Code:
Code - Auto/Visual Lisp: [Select]
  1. [code]
  2. (defun c:xx ()
  3.  
  4.   (acet-error-init
  5.    (list (list  "cmdecho" 0
  6.                 "osmode" (getvar "osmode")
  7.                 "highlight" (getvar "highlight")
  8.                 "limcheck" 0
  9.                 "pickstyle" 0
  10.                 "orthomode" (getvar "orthomode")
  11.          );list
  12.          T
  13.    );list
  14.   );acet-error-init
  15.  
  16.  
  17.  
  18.   (setvar "osmode" 0)
  19.   (setq ss (ssget "X" '((0 . "LINE") (67 . 0))))
  20.   (setq index 0 Xmin 1E10 Xmax 0 Ymin 1E10 Ymax 0)
  21.   (repeat (sslength ss)
  22.     (setq ename (ssname ss index)
  23.           obj   (vlax-ename->vla-object ename))
  24.     (if (= (strcase (vla-get-layer obj)) "CENTER")
  25.       (setq index (1+ index))
  26.       (progn
  27.         (setq spt_x (car  (vlax-curve-getstartpoint obj))
  28.               spt_y (cadr (vlax-curve-getstartpoint obj))
  29.               ept_x (car  (vlax-curve-getendpoint obj))
  30.               ept_y (cadr (vlax-curve-getendpoint obj))
  31.         )
  32.         (if (<= Xmin spt_x) (if (<= Xmin ept_x) (setq Xmin Xmin) (setq Xmin ept_x)) (setq Xmin spt_x))
  33.         (if (>= Xmax spt_x) (if (>= Xmax ept_x) (setq Xmax Xmax) (setq Xmax ept_x)) (setq Xmax spt_x))
  34.         (if (<= Ymin spt_y) (if (<= Ymin ept_y) (setq Ymin Ymin) (setq Ymin ept_y)) (setq Ymin spt_y))
  35.         (if (>= Ymax spt_y) (if (>= Ymax ept_y) (setq Ymax Ymax) (setq Ymax ept_y)) (setq Ymax spt_y))
  36.         (setq index (1+ index))
  37.        )
  38.     )
  39.   )
  40.   (setq TOP   (list Xmin Ymax)
  41.         FRONT (list Xmin Ymin)
  42.         RIGHT (list Xmax Ymin)
  43.   )
  44.  
  45.   (setq fname (vl-filename-base (getvar "dwgname")))
  46.   (setq index_test (substr fname 5 2))
  47.   (setq bname_T (strcat "c:/Mark/20140204" index_test "T.dwg"))
  48.   (setq bname_F (strcat "c:/Mark/20140204" index_test "F.dwg"))
  49.   (setq bname_R (strcat "c:/Mark/20140204" index_test "R.dwg"))
  50.  
  51.   (command "-insert" bname_T TOP "" "" "")
  52.   (command "-insert" bname_F FRONT "" "" "")
  53.   (command "-insert" bname_R RIGHT "" "" "")
  54.  
  55.  
  56.   (setq mblock (ssget "X" '((0 . "INSERT"))))
  57.   (setq time (rtos (getvar "cdate") 2 8))
  58.   (setq name (substr time 10 6))
  59.   (command "-group" "c" name name mblock "")
  60.  
  61.    
  62.  
  63.   (acet-error-restore)
  64.   (princ)
  65. );;defun
  66.  
  67.  
[/code]

Please give me some suggestion to convert from Lisp to C#.

Thanks in advance,
thenndral

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Is it possible to convert from Lisp to C#
« Reply #1 on: May 08, 2014, 05:22:11 AM »
\
Any free tools or Link available?

< .. >

No. It's all manual work, concept by concept. Lisp doesn't translate line by line unfortunately.
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

gile

  • Gator
  • Posts: 2520
  • Marseille, France
Re: Is it possible to convert from Lisp to C#
« Reply #2 on: May 08, 2014, 05:39:15 AM »
Have a look here.
Speaking English as a French Frog

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Is it possible to convert from Lisp to C#
« Reply #3 on: May 08, 2014, 05:42:37 AM »
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

JohnK

  • Administrator
  • Seagull
  • Posts: 10655
Re: Is it possible to convert from Lisp to C#
« Reply #4 on: May 08, 2014, 01:37:03 PM »
It's posts like this that make me wish I never stopped working on that Autolisp Intermediate Representation idea I had (I posted it as a language called Kaylee I believe).

Have a look here.
lol
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

thenndral

  • Guest
Re: Is it possible to convert from Lisp to C#
« Reply #5 on: May 08, 2014, 08:03:07 PM »
Hi gile,

Thanks for your suggestion.
Let me try to learn Lisp routine and try to manually in C# Code.

Thanks a lot,
thenndral

thenndral

  • Guest
Re: Is it possible to convert from Lisp to C#
« Reply #6 on: May 20, 2014, 05:50:13 AM »
Hi,
 
Thanks for your code.
 
I use your code in C#.
Lisp Routine is in "C:\Test" folder
file name: exam.LSP

Code - C#: [Select]
  1. Application.Invoke(new ResultBuffer(new TypedValue((int)LispDataType.Text, "c:xx")));

My Code:
Code - C#: [Select]
  1. [code]   [CommandMethod("OpenDrawing", CommandFlags.Session)]
  2.         public static void OpenDrawing()
  3.         {
  4.             string strFileName = @"D:\AutoCAD\testing\Stu49.dwg";
  5.             DocumentCollection acDocMgr = Application.DocumentManager;
  6.    
  7.             if (File.Exists(strFileName))
  8.             {
  9.                 acDocMgr.Open(strFileName, false);
  10.                 Document doc = acDocMgr.MdiActiveDocument;
  11.                 Editor ed = doc.Editor;
  12.                
  13.                 //get titlemode
  14.                 short tilemode = (short)Application.GetSystemVariable("TILEMODE");
  15.                 using (DocumentLock docLock = doc.LockDocument())
  16.                 {
  17.                     //check Model space or paperspace
  18.                     if (tilemode == 0)
  19.                     {
  20.                         try
  21.                         {
  22.                             //If paperspace switch to modelspace.
  23.                             HostApplicationServices.WorkingDatabase.TileMode = true;
  24.  
  25.                             //insert block from Lisp routines.
  26.                             Application.Invoke(new ResultBuffer(new TypedValue((int)LispDataType.Text, "c:xx")));
  27.                         }
  28.                         catch (Exception ex)
  29.                         {
  30.                             AcadApp.ShowAlertDialog(ex.Message);
  31.                         }
  32.                     }
  33.                 }
  34.             }
  35.             else
  36.             {
  37.                 acDocMgr.MdiActiveDocument.Editor.WriteMessage("File " + strFileName + " does not exist.");
  38.             }
  39.         }
[/code]

I got error invalid AutoCAD Command.
Please see the image.

Could you please tell me how to solve this.
I don't know, any syntax error in lisp calling line.
 
Thanks in advance,
thenndral

gile

  • Gator
  • Posts: 2520
  • Marseille, France
Re: Is it possible to convert from Lisp to C#
« Reply #7 on: May 20, 2014, 07:44:56 AM »
Hi,

You have to set the newly opened drawing as active document.

Replace:
Code - C#: [Select]
  1.                 acDocMgr.Open(strFileName, false);
  2.                Document doc = acDocMgr.MdiActiveDocument;
with:
Code - C#: [Select]
  1.                 Document doc = acDocMgr.Open(strFileName, false);
  2.                 acDocMgr.MdiActiveDocument = doc;

You have also to be sure the LISP file is automatically loaded in each document (i.e. using acaddoc.lsp, acad.mnl).
Speaking English as a French Frog

fixo

  • Guest
Re: Is it possible to convert from Lisp to C#
« Reply #8 on: May 20, 2014, 10:15:15 AM »
Try somehing like
instead of:
Code - C#: [Select]
  1. Autodesk.AutoCAD.ApplicationServices.Application.Invoke(new ResultBuffer(new TypedValue((int)LispDataType.Text, "c:xx")));
try these lines:
 
Code - C#: [Select]
  1.                           Autodesk.AutoCAD.Internal.Utils.CancelAndRunCmds("(C:xx) ");//<-- blank space require
  2.                             Autodesk.AutoCAD.Internal.Utils.PostCommandPrompt();
Also make sure you have in lisp file line like this, perhaps right after:
Code - Lisp: [Select]
  1.  ( defun C:xx()
  2. (princ "\nCommand \"xx\" performs...");<-- this line!
  3. ;;rest your lisp code
  4. (princ)
  5. )
in addition change this line too:
if (tilemode != 0)
« Last Edit: May 20, 2014, 10:38:10 AM by fixo »

thenndral

  • Guest
Re: Is it possible to convert from Lisp to C#
« Reply #9 on: May 21, 2014, 12:54:26 AM »
Hi,

Thanks you gile.
Thanks you fixo.

Amazing! Its works well.

Now I'm open drawing using document.
Is it possible to open drawing in memory using ReadDwgFile and use this code?

Code - C#: [Select]
  1. Autodesk.AutoCAD.Internal.Utils.CancelAndRunCmds("(C:xx) ");//<-- blank space require
  2. Autodesk.AutoCAD.Internal.Utils.PostCommandPrompt();


Code - C#: [Select]
  1. [CommandMethod("dwgmem")]
  2.         public void openMemory()
  3.         {
  4.             string strFileName = @"D:\AutoCAD\testing\Stu19.dwg";
  5.             Database ans_db_F = new Database(false, true);
  6.             ans_db_F.ReadDwgFile(strFileName, System.IO.FileShare.Read, true, string.Empty);
  7.            
  8.             using (Transaction acTrans = ans_db_F.TransactionManager.StartTransaction())
  9.             {
  10.                 // how to use lisp calling code here
  11.                 acTrans.Dispose();
  12.             }
  13.             ans_db_F.Dispose();
  14.         }

Please give me some suggestion.

Thanks in advance,
thenndral.

fixo

  • Guest
Re: Is it possible to convert from Lisp to C#
« Reply #10 on: May 21, 2014, 02:01:59 AM »
Not sure about, you can use this way in memory,
not tested yet, instead, if you need to run documents in batch,
 try this method
Code - C#: [Select]
  1.        /*dummy lisp for test
  2.           (defun C:HEY ()
  3.         (princ "\nCommand \"HEY\" performs:")
  4.         (command "_.circle" "_non" (list 0 0 0) 12.345)
  5.         (princ)
  6.         )*/
  7.         // make sure a lisp must be loaded to 'Contents' in 'Startup Suite' before
  8.         // do not use this code from Drawing1.dwg, use another drawing from other
  9.         // folder that would not using in batch process
  10.         [CommandMethod("BPL", CommandFlags.Session)]
  11.         public void batchProcessExample()
  12.         {
  13.             // get all drawings from the folder "C:\WH" - change path to suit
  14.             System.IO.DirectoryInfo directory = new System.IO.DirectoryInfo(@"C:\WH");//- change path to suit
  15.             List<string> files = directory.GetFiles("*.dwg").Select(x => x.FullName).ToList();
  16.             DocumentCollection mgr = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager;
  17.             try
  18.             {
  19.                 Autodesk.AutoCAD.ApplicationServices.Application.SetSystemVariable("filedia", 0);
  20.                 Autodesk.AutoCAD.ApplicationServices.Application.SetSystemVariable("cmdecho", 0);
  21.                 Autodesk.AutoCAD.ApplicationServices.Application.SetSystemVariable("expert", 2);
  22.                 Autodesk.AutoCAD.ApplicationServices.Application.SetSystemVariable("ctab", "Model");
  23.  
  24.                 foreach (string file in files)
  25.                 {
  26.                     Document doc = mgr.Open(file, false);
  27.                     using (DocumentLock doclock = doc.LockDocument())
  28.                     {
  29.                         Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument = doc;
  30.                         Autodesk.AutoCAD.Internal.Utils.CancelAndRunCmds("(C:HEY) ");// change lisp command to suit
  31.                         Autodesk.AutoCAD.Internal.Utils.CancelAndRunCmds("_.qsave ");
  32.                         Autodesk.AutoCAD.Internal.Utils.PostCommandPrompt();
  33.  
  34.                     }
  35.                 }
  36.                 Autodesk.AutoCAD.Internal.Utils.CancelAndRunCmds("(princ) ");
  37.                 Autodesk.AutoCAD.Internal.Utils.PostCommandPrompt();
  38.             }
  39.             catch (System.Exception ex)
  40.             {
  41.                 Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog(ex.Message);
  42.             }
  43.             finally
  44.             {
  45.                 Autodesk.AutoCAD.ApplicationServices.Application.SetSystemVariable("cmdecho", 1);
  46.                 Autodesk.AutoCAD.ApplicationServices.Application.SetSystemVariable("expert", 0);
  47.                 Autodesk.AutoCAD.ApplicationServices.Application.SetSystemVariable("filedia", 1);
  48.             }
  49.         }
  50.         // after the work will ends just close AutoCAD and check all drawings how a program is executed
  51.  

thenndral

  • Guest
Re: Is it possible to convert from Lisp to C#
« Reply #11 on: May 21, 2014, 03:56:24 AM »
Hi,

Thanks for your quick reply.

If we use "Document", Drawing will load in AutoCAD not in memory.
At this stage drawing file opened in AutoCAD.
Code - C#: [Select]
  1. Document doc = mgr.Open(file, false);

Using ReadDwgFile, file will be load in memory but I don't know how to use this code in memory.

Code - C#: [Select]
  1. //If paperspace switch to modelspace.
  2. HostApplicationServices.WorkingDatabase.TileMode = true;
  3. Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument = doc;
  4. Autodesk.AutoCAD.Internal.Utils.CancelAndRunCmds("(C:xx) ");// change lisp command to suit
  5. Autodesk.AutoCAD.Internal.Utils.CancelAndRunCmds("_.qsave ");
  6. Autodesk.AutoCAD.Internal.Utils.CancelAndRunCmds("_.close ");
  7. Autodesk.AutoCAD.Internal.Utils.PostCommandPrompt();


Please give me some solution.

Thanks in advance,
thenndral.

fixo

  • Guest
Re: Is it possible to convert from Lisp to C#
« Reply #12 on: May 21, 2014, 05:09:48 AM »
Sorry, I could not more help

gile

  • Gator
  • Posts: 2520
  • Marseille, France
Re: Is it possible to convert from Lisp to C#
« Reply #13 on: May 21, 2014, 07:04:05 AM »
thenndral,

Keep in mind that when you use a side database (i.e. Database.ReadDwgFile()), you only have acces to the Database of the dwg file, you do not have access to the Editor so you cannot call commands nor LISP functions.

If you want to follow the side database route, you'll have to call a .NET method which does the same as the LISP without using selection set nor calling commands.
To write this method, don't try to convert the LISP (as you do not know LISP), just write a method wich give the same expected result.
Speaking English as a French Frog

thenndral

  • Guest
Re: Is it possible to convert from Lisp to C#
« Reply #14 on: May 22, 2014, 04:20:22 AM »
Hi,

Thank you fixo.
Thank you gille.

I follow your suggestion.

Thanks a lot,
thenndral