Author Topic: Problem with variables in a LISP Routine  (Read 1475 times)

0 Members and 1 Guest are viewing this topic.

Vins120

  • Guest
Problem with variables in a LISP Routine
« on: January 15, 2018, 02:25:15 AM »
Hello everyone,

I'm new in LISP programming and I have some issue in a LISP Routine. I try to make a Routine which put elements of a drawing from an old Layer to a new one. The Routine do this:
 
1. import a formated text file
(Each Line is defined like this: old_Layer Bloc_name new_Color new_Line_Type new_layer)
2. For each Line the Routine affects the variables with the currently read Line
3. The Routine make a selection (ssget) according to the old_Layer
4. And put the selected elements to the new one.

Her is the code:
Code: [Select]
(defun C:TRI_ELMT_CALQUE( / Calque1 s)

 (setq FN1 (getfiled "Fichier format Calque1 Bloc Couleur Ligne Calque2" "" "txt" 12))
 (setq FL1 (open FN1 "r"))
(setvar "attreq" 1)
(setvar "attdia" 0)
(initdia 0)

    (while (setq DATA-STR (read-line FL1))

(setq DATA-LIST (read (strcat "(" DATA-STR ")")))

(setq Calque1 (car DATA-LIST))

(setq Nom_bloc (nth 1 DATA-LIST))

(setq Couleur (nth 2 DATA-LIST))

(setq Type_ligne (nth 3 DATA-LIST))

(setq Calque2 (nth 4 DATA-LIST))

(if (equal Nom_bloc 'vide)
(progn

(if (setq sset (ssget "_x"  (list (cons 8 Calque1))))
(progn
(command "_.chprop" sset "5" "Tangente" "CONTINUOUS" "")

)
)
)
(progn
(if (setq sset (ssget "_x"  (list (cons 0 "LINE")(cons 8 Calque1))))
(progn
  ;empty for now
)
)
)
)
)

(prin1)
  (close FL1)
 
)

The problem that I encounter is that the variable "Calque1" (old_Layer) is a Symbol type and it makes an Error (bad argument) in the ssget Selection and in the Chprop Command because it needs a string type. If I put the following line:
(setq Calque1 "old_Layer") instead of (setq Calque1 (car DATA-LIST))
I have no more error.
Can someone helps me to define the Calque 1 Variable correctly please? Do I need to convert it in String?

Thanks in advance

kpblc

  • Bull Frog
  • Posts: 396
Re: Problem with variables in a LISP Routine
« Reply #1 on: January 15, 2018, 02:53:25 AM »
Try this:
Code: [Select]
(defun c:tri_elmt_calque (/ calque1 calque2 couleur data-list data-str fl1 fn1 nom_bloc sset type_ligne)
  (setq fn1 (getfiled "Fichier format Calque1 Bloc Couleur Ligne Calque2" "" "txt" 12))
  (setq fl1 (open fn1 "r"))
  (setvar "attreq" 1)
  (setvar "attdia" 0)
  (initdia 0)
  (while (setq data-str (read-line fl1))
    (setq data-list (read (strcat "(" data-str ")")))
    (setq calque1 (vl-princ-to-string (car data-list)))
    (setq nom_bloc (vl-princ-to-string (nth 1 data-list)))
    (setq couleur (nth 2 data-list))
    (setq type_ligne (vl-princ-to-string (nth 3 data-list)))
    (setq calque2 (vl-princ-to-string (nth 4 data-list)))
    (if (equal nom_bloc 'vide)
      (progn (if (setq sset (ssget "_x" (list (cons 8 calque1))))
               (progn (command "_.chprop" sset "5" "Tangente" "CONTINUOUS" ""))
               ) ;_ end of if
             ) ;_ end of progn
      (progn (if (setq sset (ssget "_x" (list (cons 0 "LINE") (cons 8 calque1))))
               (progn ;empty for now
                )
               ) ;_ end of if
             ) ;_ end of progn
      ) ;_ end of if
    ) ;_ end of while
  (prin1)
  (close fl1)
  ) ;_ end of defun
Sorry for my English.

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1454
  • Marco
Re: Problem with variables in a LISP Routine
« Reply #2 on: January 15, 2018, 03:46:36 AM »
I think the best way is modify your "formated text file" to CSV, example:

Comando: (setq Data_line "old_Layer;Bloc_name;new_Color;new_Line_Type;new_layer")
"old_Layer;Bloc_name;new_Color;new_Line_Type;new_layer"

Comando: (setq DATA-LIST (ALE_String2List Data_line ";"))
("old_Layer" "Bloc_name" "new_Color" "new_Line_Type" "new_layer")

Code: [Select]
(defun ALE_String2List (InpStr CarDlm / SttPos EndPos TmpLst)
  (setq
    CarDlm (ascii CarDlm)   SttPos 0
    EndPos (vl-string-position CarDlm InpStr)
  )
  (while EndPos
    (setq
      TmpLst (cons (substr InpStr (1+ SttPos) (- EndPos SttPos)) TmpLst)
      SttPos (1+ EndPos) EndPos (vl-string-position CarDlm InpStr SttPos)
    )
  )
  (reverse (cons (substr InpStr (1+ SttPos)) TmpLst))
)

Vins120

  • Guest
Re: Problem with variables in a LISP Routine
« Reply #3 on: January 15, 2018, 07:46:29 AM »
Thank you very much!

I tried the first suggestion of kbplc by adding the function (vl-princ-to-string) in my code and it works very well. So no need to convert my original text file into csv.

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1454
  • Marco
Re: Problem with variables in a LISP Routine
« Reply #4 on: January 15, 2018, 08:19:08 AM »

Thank you very much!

I tried the first suggestion of kbplc by adding the function (vl-princ-to-string) in my code and it works very well. So no need to convert my original text file into csv.
Example: your old name of layer is  "old_Layer(ABC)" and there are spaces in "Bloc name"
Code: [Select]

Comando: (setq data-str "old_Layer(ABC) Bloc name new_Color new_Line_Type new_layer")
"old_Layer(ABC) Bloc name new_Color new_Line_Type new_layer"


Comando: (setq data-list (read (strcat "(" data-str ")")))
(OLD_LAYER (ABC) BLOC NAME NEW_COLOR NEW_LINE_TYPE NEW_LAYER)


Comando: (setq calque1 (vl-princ-to-string (car data-list)))
"OLD_LAYER"


Comando: (setq nom_bloc (vl-princ-to-string (nth 1 data-list)))
"(ABC)"