Convert Lisp to C#

Convert Lisp to C#

hakocivil
Contributor Contributor
674 Views
3 Replies
Message 1 of 4

Convert Lisp to C#

hakocivil
Contributor
Contributor

Hello,
I have a Lisp code and I am not familiar with Lisp commands. How can I convert it to C#?

(textscr)
(princ "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n")

(defun r_t_d (d)
 (setq d (/ (* d 180.0) pi))
)

(defun lst (d) (setvar "lastpoint" st))

(princ ".")

(defun d_t_r (d)
 (setq d (/ (* d pi) 180.0))
)

(princ ".")

 (setq charm1 (list 104 151 152 153 78 110 169 170 171 66 98 133 134
		   135 67 99 136 137 138 70 92 124 211 212 213 44 226
		   227 228 60 229 230 231 62 232 233 234 235 236 237
		   32 71 72 86 118 193 194 195 167 168))
 (setq charm2 (list 32 33 35 36 37 38 40 41 42 43 45 46 47 48
		   49 50 51 52 53 54 55 56 57 61 63 64 94 127 128 129))
 (setq charm (append charm1 charm2))
 (setq charn (append charm2 (list 32 70 71 72)))
 (setq charo (list (list 96 126) (list 58 127) (list 59 127) (list 34 128)
		  (list 39 128) (list 44 129) (list 60 131) (list 62 132)
	    )
 )
 (setq charp (list 96 58 59 34 39 44 60 62))
 (setq charq (list 104 110 98 99 118 124 129 130 131))

(defun mirp(a b)
   (setq x1 (car a) y1 (cadr a) x2 (car b) y2 (cadr b))
   (setq pt3 (list (- (* 2 x1) x2) (- (* 2 y1) y2)))
   (setq an (angle a b))
)
(princ ".")

(defun taviz (so)
  (setq l (strlen so))
  (setq n 1)
  (setq nwo "")
  (while (<= n l)
   (if (= n 1)
    (setq ee " ")
    (setq ee (substr so (1- n) 1))
   )
   (setq f (substr so n 1))
   (if (= n l)
    (setq g " ")
    (setq g (substr so (1+ n) 1))
   )
   (setq asce (ascii ee))
   (setq ascff (setq ascf (ascii f)))
   (setq ascg (ascii g))
   (if (= (member ascf charm2) nil)
    (if (= (member ascf (list 32 95 70 71 72)) nil)
     (progn
      (if (/= (and (> ascf 64) (< ascf 94)) nil)
	(setq ascff (setq ascf (+ ascf 32)))
      )
      (if (/= (member ascf charp) nil)
	(setq ascf (car (cdr (assoc ascf charo))))
      )
      (if (/= (member asce charm) nil)
       (if (/= (member ascg charn) nil)
	(if (= (member ascf charq) nil)
	 (setq ascf (+ (* (- ascf 97) 3) 132))
	 (setq ascf ascff)
	)
	(setq ascf ascff)
       )
       (if (/= (member ascg charn) nil)
	(if (= (member ascf charq) nil)
	 (setq ascf (+ (* (- ascf 97) 3) 131))
	 (setq ascf (+ (* (- ascf 97) 3) 130))
	)
	(setq ascf (+ (* (- ascf 97) 3) 130))
       )
      )
     )
    )
   )
   (setq n (1+ n))
   (setq nwo (strcat nwo (chr ascf)))
  )
  (setq so nwo)
)

(princ ".")

(defun hr ()
 (setq f_heigth (cdr (assoc 40 (tblsearch "style" (getvar "textstyle")))))
 (if (= f_heigth 0.0)
   (progn
     (princ (strcat "\nHeight <" (rtos (getvar "textsize")) ">: "))
     (setq ht (getdist st))
     (if (/= ht nil)
       (setvar "textsize" ht)
       (setq ht "")
     )
   )
 )
 (princ (strcat "\nRotation angle <" (angtos (getvar "userr1")) ">: "))
 (setq ro (getangle st))
 (if (/= ro nil)
  (setvar "userr1" ro)
 )
)
(princ ".")

(defun *error* (ms)
 (if (= ms "Function cancelled")
     (progn (command) (command))
 )
 (princ "\nerror: ")
 (princ ms)
 (command "ERASE" ss "")
 (setvar "CMDECHO" 1)
 (setvar "HIGHLIGHT" 1)
 (prin1)
)

(princ ".")



(princ ".")


(defun farsit(text_to_convert)
(setvar "CMDECHO" 0)
  (if (/= passcode "passcode")
      (progn
      (code_control)
	 (if (= (and (= fcode fcodm)
		     (/= ucode nil)
		     (= scode "10-248253")
		)   t
	     )
	     (progn
	       (setq passcode "passcode")
	       (taviz text_to_convert)
	     )
	     (progn
	       (lic_pr)
	       (prin1)
	     )
	  )
      )
      (taviz text_to_convert)
  )
)

(princ ".")

(defun c:KATEB()
   (setvar "CMDECHO" 0)
   (setvar "HIGHLIGHT" 0)
   (princ (strcat "Current TEXT style is " (getvar "TEXTSTYLE") " !!\n"))
   (setq st (list 1 4))
   (setq ss (ssadd))
   (command "POINT" "0,0")
   (setq entl (entlast))
   (ssadd entl ss)
   (setq rp "Style")
   (while (= rp "Style")
    (initget "Style Align Center Fit Middle Left")
    (setq rp (getpoint "Start point or Align/Center/Fit/Middle/Left/Style:"))
    (if (= rp "Style")
     (progn
      (setq sty (getstring
		 (strcat "\nStyle name (or ?) <" (getvar "TEXTSTYLE") ">: ")
		)
      )
      (if (/= sty "")
	(progn
	    (if (= sty "?")
	      (progn
		   (command "TEXT" "S" "?")
		   (command)
	      )
	      (progn
		 (if (/= (tblsearch "style" sty) nil)
		   (progn
			 (command "TEXT" "S" (eval sty))
			 (command)
		   )
		   (prompt "\nUnknown text style name.\n")
		 )
	      )
	    )
	)
      )
     )
    )
   )
   (if (= (getvar "userr1") 0)
       (setvar "userr1" pi)
   )
   (if (= rp nil)
       (setq st nil)
   )
   (if (/= st nil)
    (progn
     (if (listp rp)
      (progn
       (setq st rp)
       (lst st)
       (hr)
      )
     )
     (if (= rp "Align")
      (progn
       (setq st (getpoint "\nFirst text line point:"))
       (lst st)
       (setq pt2 (getpoint st "\nSecond text line point:"))
      )
     )
     (if (= rp "Center")
      (progn
       (setq st (getpoint "\nCenter point:"))
       (lst st)
       (hr)
      )
     )
     (if (= rp "Fit")
      (progn
       (setq st (getpoint "\nFirst text line point:"))
       (lst st)
       (setq pt2 (getpoint st "\nSecond text line point:"))
       (setq f_heigth (cdr (assoc 40 (tblsearch "style"
				   (getvar "textstyle")))))
       (if (= f_heigth 0.0)
	(progn
	  (princ (strcat "\nHeight <" (rtos (getvar "textsize")) ">: "))
	  (setq ht (getdist st))
	  (if (/= ht nil)
	   (setvar "textsize" ht)
	   (setq ht "")
	  )
	)
       )
      )
     )
     (if (= rp "Middle")
      (progn
       (setq st (getpoint "\nMiddle point:"))
       (lst st)
       (hr)
      )
     )
     (if (= rp "Left")
      (progn
       (setq st (getpoint "\nLeft point:"))
       (lst st)
       (hr)
      )
      )
     )
    )
    (setq ro (+ (getvar "userr1") pi))
    (setq rl (getvar "AUNITS"))
    (cond ((= rl 0) (setq rog (angtos ro)))
	  ((= rl 1) (setq rog (angtos ro)))
	  ((= rl 2) (setq rog (atof (angtos ro))))
	  ((= rl 3) (setq rog (atof (angtos ro))))
	  ((= rl 4) (setq rog (r_t_d ro)))
    )
    (princ "\nText:")
    (if (= st nil)
     (command "dtext" "")
     (progn
      (if (listp rp)
	 (if (= f_heigth 0.0)
	     (command "dtext" st ht rog)
	     (command "dtext" st rog)
	 )
      )
      (if (= rp "Align")
       (progn
	(mirp st pt2)
	(command "dtext" "a" st pt3)
      )
     )
     (if (= rp "Center")
	(if (= f_heigth 0.0)
	    (command "dtext" "c" st ht rog)
	    (command "dtext" "c" st rog)
	)
     )
     (if (= rp "Fit")
       (progn
	 (mirp st pt2)
	 (if (= f_heigth 0.0)
	     (command "dtext" "f" st pt3 ht)
	     (command "dtext" "f" st pt3)
	 )
       )
     )
     (if (= rp "Middle")
	 (if (= f_heigth 0.0)
	     (command "dtext" "m" st ht rog)
	     (command "dtext" "m" st rog)
	)
     )
     (if (= rp "Left")
	 (if (= f_heigth 0.0)
	     (command "dtext" "r" st ht rog)
	     (command "dtext" "r" st rog)
	 )
     )
    )
   )
   (while (/= (entnext entl) nil)
    (setq txt (entnext entl))
    (setq e (entget txt))
    (if (or (= rp "Fit") (= rp "Align"))
      (progn(setq za (cdr (assoc 10 e)))
      (command "ROTATE" (ssadd txt) "" za "180"))
    )
    (setq e (entget txt))
    (setq s (cdr (setq as (assoc 1 e))))
    (setq s (taviz s))
    (setq e (subst (cons 1 nwo) as e))
    (entmod e)
    (setq entl txt)
   )
   (command "ERASE" ss "")
   (setvar "HIGHLIGHT" 1)
   (setvar "CMDECHO" 1)
   (prin1)
)

(princ ".")

(defun c:chg()
 (setvar "CMDECHO" 0)
 (command "UNDO" "M")
   (setq chm 0 p (ssget))
   (if p (progn
	   (setq cont t)
	   (while cont
	     (setq os (getstring "\n Type OLD text : " T))
	     (setq osl (strlen os))
	     (if (= osl 0)
		     (princ "Null input invalid. Try again !?")
		     (setq cont nil)
	     )
	   )
      (setq ns (getstring "\n Type NEW text : " T))
      (setq os (taviz os))
      (setq ns (taviz ns))
      (setq nsl (strlen ns))
      (setq l 0 n (sslength p))
      (while (< l n)
	 (if  (= "TEXT"
		  (cdr (assoc 0 (setq ec (entget (ssname p l)))))
	      )
	    (progn
	       (setq chf nil si 1)
	       (setq sc (cdr (setq ass (assoc 1 ec))))
	       (while (= osl (setq sl (strlen
			     (setq stc (substr sc si osl)))))
		  (if (= stc os)
		      (progn
			(setq sc (strcat (substr sc 1 (1- si)) ns
					(substr sc (+ si osl))))
			(setq chf t)
			(setq si (+ si nsl))
		      )
		      (setq si (1+ si))
		  )
	       )
	       (if chf
		      (progn
			(setq ec (subst (cons 1 sc) ass ec))
			(entmod ec)
			(setq chm (1+ chm))
		      )
	       )
	    )
	 )
	 (setq l (1+ l))
      )
   ))
   (princ "Changed ")
   (princ chm)
   (princ " text lines.")
   (command "UNDO" "E")
   (setvar "CMDECHO" 1)
   (prin1)
)

(princ ".")

(defun c:chn()
(setvar "CMDECHO" 0)
  (if (/= passcode "passcode")
      (progn
	 (code_control)
	 (if (= (and (= fcode fcodm)
		     (/= ucode nil)
		     (= scode "10-248253")
		)   t
	     )
	     (progn
	       (setq passcode "passcode")
	       (farsichange)
	     )
	     (progn
	       (lic_pr)
	       (prin1)
	     )
	  )
      )
      (farsichange)
  )
)

(princ ". Completed.")
(prin1)

0 Likes
675 Views
3 Replies
Replies (3)
Message 2 of 4

kerry_w_brown
Advisor
Advisor

As far as I know, there is no programatic converter from AutoLisp to C#.

The languages are significantly different and the individual writing styles used for AutoLisp make it very difficult to write a converter.

I believe the conversion would need to be done manually, which would require :
an analysis of the code intent rather than of the actual line by line 'code'.
. . . so we're almost forced to start from scratch.

I think the 'trap' would be attempting to reproduce 'how' something was done in minutia rather than looking at the purpose of the application.

The addage 'Form follows function' can't be ignored.

Regards,

 


// Called Kerry or kdub in my other life.

Everything will work just as you expect it to, unless your expectations are incorrect. ~ kdub
Sometimes the question is more important than the answer. ~ kdub

NZST UTC+12 : class keyThumper<T> : Lazy<T>;      another  Swamper
Message 3 of 4

hakocivil
Contributor
Contributor
Yes,you are absolutely right.
Thank you for taking the time for me.
0 Likes
Message 4 of 4

jeff.wangD95HG
Collaborator
Collaborator

have you tried chat GPT?

The given Lisp code appears to be a collection of functions and commands related to manipulating and formatting text in AutoLISP, a dialect of the Lisp programming language used in AutoCAD. Let's go through the code and explain each part:

1. `(textscr)` and `(princ "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n")` are used to clear the command line screen and add several newlines for visual separation.

2. `(defun r_t_d (d) ...)` defines a function `r_t_d` that converts radians to degrees.

3. `(defun lst (d) ...)` defines a function `lst` that sets the last point (`lastpoint`) to the value of `st`.

4. `(princ ".")` is a print statement used for visual separation.

5. `(defun d_t_r (d) ...)` defines a function `d_t_r` that converts degrees to radians.

6. `(princ ".")` is another print statement.

7. `(setq charm1 ...)` and `(setq charm2 ...)` set the values of two variables `charm1` and `charm2` to lists of ASCII character codes. These lists represent different characters used for text manipulation.

8. `(setq charm ...)` combines `charm1` and `charm2` lists into a single list.

9. `(setq charn ...)` sets the value of `charn` to a modified version of `charm2` list with additional characters.

10. `(setq charo ...)` sets the value of `charo` to a list of lists representing character code transformations.

11. `(setq charp ...)` sets the value of `charp` to a list of character codes used for transformations.

12. `(setq charq ...)` sets the value of `charq` to a list of character codes.

13. `(defun mirp (a b) ...)` defines a function `mirp` that performs some calculations involving coordinates.

14. `(princ ".")` is another print statement.

15. `(defun taviz (so) ...)` defines a function `taviz` that performs character code transformations and replacements based on predefined rules.

16. `(princ ".")` is another print statement.

17. `(defun hr () ...)` defines a function `hr` that handles text height and rotation angle settings.

18. `(princ ".")` is another print statement.

19. `(defun *error* (ms) ...)` defines an error handling function that displays error messages and performs cleanup operations.

20. `(princ ".")` is another print statement.

21. `(defun farsit (text_to_convert) ...)` defines a function `farsit` that converts English text to a specific Persian font.

22. `(princ ".")` is another print statement.

23. `(defun c:KATEB () ...)` defines a command `KATEB` that handles the main functionality of the AutoLISP program. It prompts the user for text input, coordinates, rotation angles, and other parameters to create and modify text entities in AutoCAD.

24. `(princ ".")` is another print statement.

25. `(defun c:chg () ...)` defines a command `chg` that allows the user to change the text content of selected text entities in AutoCAD.

26. `(princ ".")` is another print statement.

27. `(defun c:chn () ...)` defines a command `chn` that performs additional text manipulations.

The code overall seems to be a script for text manipulation and formatting in AutoCAD using AutoLISP. It includes functions for converting

 

 

0 Likes