Help in translating understanding a code example

Discussion of other Lisp dialects (Arc, Clojure, AutoLisp, XLISP, etc.)
Post Reply
studleylee
Posts: 2
Joined: Fri Apr 13, 2012 10:07 am

Help in translating understanding a code example

Post by studleylee » Fri Apr 13, 2012 10:17 am

Hello, I'm very new to lisp and have been an assembly, C,C++ programmer embedded guy for most my life.
I'm struggling to diagram and add comments on how this utility(below) works as my learning point
then convert it to C. It's a "Clear Duplicates" utility from CAD programs that would be useful for my
cad drawings as I convert them to CNC paths.

Does anyone have some moments to spare to guide me on this? I appreciate it. -Lee

==========

Code: Select all

; from http://www.eng-tips.com/viewthread.cfm?qid=152022
;Tip1744:  CLEAR.LSP        Clear duplicates      (c)2001, ;Andrzej Gumula
;===================================================================================
(defun DXF (A) (cdr (assoc A (entget ONE)))) ;end dxf

(defun COMPLEX  ()
  (while (not (wcmatch (DXF 0) "*END*"))
    (setq ONE  (entnext ONE)
          ELEM (append ELEM (list ONE)))
    (CHECK)) ;end while
  ) ;end complex

(defun CORECT  (A)
  (cond ((= (type (cdr A)) 'list)
         (cons (car A) (mapcar '(lambda (X) (atof (rtos X 2 8))) (cdr A))))
        ((member (type (cdr A)) (list 'INT 'REAL))
         (cons (car A) (mapcar '(lambda (X) (atof (rtos X 2 8))) (list (cdr A)))))
        (t A))) ;end corect

(defun CHECK  ()
  (foreach X  (entget ONE)
    (if (not (member (car X) '(-2 -1 5 6 8 62 100)))
      (setq TEMP (cons (CORECT X) TEMP))))) ;end check

 ;(defun c:clear (/  CM LISTA NEW ONE TEMP OLD ZNACZNIK)
(defun C:CLEAR  ()
  (setq CM    (getvar "cmdecho")
        LISTA NIL
        NEW   NIL
        ONE   NIL
        TEMP  NIL
        OLD   (ssget "_X"))
  (setvar "cmdecho" 0)
  (cond
    (OLD
     (command "_-layer" "_u" "*" "")
     (prompt "\nDrawing clearing. ")
     (prompt "\nPlease wait... \n")
     (while (cond (ONE (setq ONE (entnext ONE)))
                  (t (setq ONE (entnext))))
       (setq ELEM (append ELEM (list ONE)))
       (CHECK)
       (if (or (and (= (DXF 0) "INSERT") (= (DXF 66) '1)) (= (DXF 0) "POLYLINE"))
         (COMPLEX))
       (if (member TEMP LISTA)
         (foreach X ELEM (entdel X)))
       (setq LISTA (cons TEMP LISTA)
             TEMP  NIL
             ELEM  NIL)
       (cond (ZNACZNIK (setq ZNACZNIK NIL) (princ "\r\\"))
             (t (setq ZNACZNIK (princ "\r/"))))) ;end while
     (prompt (strcat "\nNumber of elements before clearing- " (itoa (sslength OLD))))
     (prompt
       (strcat "\nNumber of elements after clearing- " (itoa (sslength (ssget "_X"))))))
    (t (prompt "\nFound zero elements. "))) ;end cond
  (setvar "cmdecho" CM)
  (princ)) ;end file
(prompt "\nLoaded new command CLEAR. ")
(princ)

studleylee
Posts: 2
Joined: Fri Apr 13, 2012 10:07 am

Re: Help in translating understanding a code example

Post by studleylee » Sat Apr 14, 2012 10:55 am

I know this is one of those roll your eyes at the newbie questions, but Lisp is really odd to me so far( but I see its power ) much
like java string handling far superior than normal C++ w/o adding code. I will also be willing to paypal a giftcard to anyone
who helps as my appreciation.

I also realize now that his is a very AutoCad Lisp syntax example. I found a nice Dos help program that defines well those commands.
davidbethel<dot>com LispHelp

I've been search/replacing functions for a readable(no code) version to get a feel for the flow. i.e "lambda (x)-->AnonFunction(x)"
here's that so far.

Thanks, -Lee

;===================================================================================
(definefunction: DXF (A) (cdr (assoc A (EntityGet ONE)))) ;end dxf

(definefunction: COMPLEX ()
(while (!(WildCardStringMatch (DXF 0) "*END*"))
( ONE = (EntityGetNext ONE)
ELEM (append ELEM (list ONE)))
(CHECK)) ;end while
) ;end complex

(definefunction: CORECT (A)
(SWITCH ((= (type (cdr A)) "list)
(CONSTRUCTNEWLIST (car A) (mapcar "(AnonFunction(X) (atof (RealToString X 2 8))) (cdr A))))
((member (type (cdr A)) (list "INT "REAL))
(CONSTRUCTNEWLIST (car A) (mapcar "(AnonFunction(X) (atof (RealToString X 2 8))) (list (cdr A)))))
(t A))) ;end corect

//NOTES:
//lambda(AnonFunction) is the symbol for an anonymous function, a function without a name.
//mapcar is a function that calls its first argument with each element of its second argument, in turn. The second argument must be a sequence.


(definefunction: CHECK ()
(foreach X (EntityGet ONE)
(if (!(member (car X) "(-2 -1 5 6 8 62 100)))
( TEMP =(CONSTRUCTNEWLIST (CORECT X) TEMP))))) ;end check

;(definefunction: c:clear (/ CM LISTA NEW ONE TEMP OLD MARKER)
(definefunction: C:CLEAR ()
( CM = (getvar "cmdecho")
LISTA NULL
NEW NULL
ONE NULL
TEMP NULL
OLD (ssget "_X"))
(setvar "cmdecho" 0)
(SWITCH
(OLD
(command "_-layer" "_u" "*" "")
(prompt "\nDrawing clearing. ")
(prompt "\nPlease wait... \n")
(while (SWITCH (ONE ( ONE =(EntityGetNext ONE)))
(t (ONE = (EntityGetNext))))
( ELEM = (append ELEM (list ONE)))
(CHECK)
(if (or (and (= (DXF 0) "INSERT") (= (DXF 66) "1)) (= (DXF 0) "POLYLINE"))
(COMPLEX))
(if (member TEMP LISTA)
(foreach X ELEM (entdel X)))
( LISTA =(CONSTRUCTNEWLIST TEMP LISTA)
TEMP NULL
ELEM NULL)
(SWITCH (MARKER ( MARKER = NULL) (princ "\r\\"))
(t ( MARKER = (princ "\r/"))))) ;end while
(prompt (strcat "\nNumber of elements before clearing- " (itoa (sslength OLD))))
(prompt
(strcat "\nNumber of elements after clearing- " (itoa (sslength (ssget "_X"))))))
(t (prompt "\nFound zero elements. "))) ;end SWITCH
(setvar "cmdecho" CM)
(princ)) ;end file
(prompt "\nLoaded new command CLEAR. ")
(princ)

wvxvw
Posts: 127
Joined: Sat Mar 26, 2011 6:23 am

Re: Help in translating understanding a code example

Post by wvxvw » Mon Apr 16, 2012 11:39 pm

I never used CAD Lisp, so, sorry, I won't be able to help much here. Just saying that it's certainly not because you are new to the forum/language - this is just a forum with not much traffic, unfortunately.
Nevertheless, CAD Lisp has many things in common with other Lisps - so you could have better chances asking more particular questions about functions or constructions that you don't understand - if they will so happen to be a commonplace in other Lisps, then you have a better chance of getting a reply.
For example, mapcar is a function used in many other Lisps, while AnonFunction seems to be a CAD Lisp citizen only.
As I've absolutely no clue about how CAD Lisp works, these are things I'd try, but I can't promise they'll work:
You probably have some kind of interactive shell where you run the script (or otherwise you have a way to evaluate bits of it and see the output). There is an idiomatic (describe 'symbol-name) function that will show you short info about the symbol called "symbol-name". It looks like in CAD Lisp they use double quote instead of tick - so I'd try (describe "symbolName) - because it looks more "CAD-ish".

Post Reply