mohammadreza Posted April 29 Posted April 29 Hello everybody, I wrote a LISP script to convert English characters to Persian characters. However, I have a problem: when I select objects to convert, only a few of them are processed correctly. For most of the objects, nothing happens. What are my faults? could you please correct me? (defun *error* (ms) (princ "\n--- Error Detected ---\n") (princ ms) (if ss (command-s "ERASE" ss "") ) (setvar "CMDECHO" 1) (setvar "HIGHLIGHT" 1) (setvar "BLIPMODE" 1) (prin1) ) (defun r_t_d (d) (setq d (/ (* d 180.0) pi)) ) ;;;(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 taviz (so) (setq l (strlen so)) (setq n 1) (setq nwo "") (while (<= n l) (setq f (substr so n 1)) (setq ascf (ascii f)) ;; ÈÑÑÓí ÚÏÏ ÈæÏä ˜ÇÑǘÊÑ (if (and (>= ascf 48) (<= ascf 57)) ; ÇÑ ÑÞã 0-9 ÈæÏ (progn ;; ÔÑæÚ ÑÝÊä ÚÏÏ (setq start n) (while (and (<= n l) (setq f (substr so n 1)) (setq ascf (ascii f)) (and (>= ascf 48) (<= ascf 57)) ) (setq n (1+ n)) ) ;; ÑÔÊå ÚÏÏí ˜Çãá (setq number (substr so start (- n start))) ;; ãÚ˜æÓ ˜ÑÏä ÚÏÏ (setq reversed "") (setq i (strlen number)) (while (> i 0) (setq reversed (strcat reversed (substr number i 1))) (setq i (1- i)) ) ;; ÇÖÇÝå ˜ÑÏä ÚÏÏ ãÚ˜æÓ Èå ÎÑæÌí (setq nwo (strcat nwo reversed)) ) ;; ÇÑ ÚÏÏ äÈæÏ¡ ÊÈÏíá ØÈÞ ÑæÇá ÞÈáí (progn (if (= n 1) (setq ee " ") (setq ee (substr so (1- n) 1)) ) (setq g (if (= n l) " " (substr so (1+ n) 1) ) ) (setq asce (ascii ee)) (setq ascg (ascii g)) ;; äåÏÇÑí äÓÎå ÇÕáí ˜ÇÑǘÊÑ (setq ascff ascf) ;; ÚãáíÇÊ ÌÇíÒíäí ÈÑ ÇÓÇÓ ÞæÇÚÏ ÔãÇ (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 nwo (strcat nwo (cond ((= ascf 177) "%%177") ((= ascf 176) "%%176") (T (chr ascf)) ) ) ) (setq n (1+ n)) ) ) ) nwo ) (princ ".") (defun c:b () (setvar "CMDECHO" 0) (setvar "HIGHLIGHT" 0) (command "style" "NASKHD" "naskhd" "" "" "" "yes" "") ;; Select the set of TEXT or MTEXT (for conversion to Naskhd font) (setq ss (ssget '((0 . "TEXT,MTEXT")))) (if ss (progn (setq n (sslength ss)) (setq i 0) (while (< i n) ;; Select each entity from the selection set (setq en (ssname ss i)) (if en (progn (setq ent (entget en)) ;; Get the current style of the entity (setq style (cdr (assoc 7 ent))) ;; If the style is not NASKHD, convert to NASKHD font style (if (not (equal (strcase style) "NASKHD")) (progn ;; Get the text string (setq s (cdr (assoc 1 ent))) ;; Get the insertion point, rotation, and height of the text (setq inspt (cdr (assoc 10 ent))) (setq ro (cdr (assoc 50 ent))) (setq ht (cdr (assoc 40 ent))) ;; Apply the necessary adjustments (possibly related to conversion) (setq s (taviz s)) ; Here: Apply adjustments (the exact details of `taviz` are unclear) ; Delete the original entity (entdel en) ; Create a new text entity with the updated style (command "text" inspt ht (r_t_d ro) s) ) ) ) ) (setq i (1+ i)) ) ) (prompt "\nNo valid text selected.") ) ; Restore original settings (setvar "HIGHLIGHT" 1) (setvar "CMDECHO" 1) (setvar "BLIPMODE" 1) (prin1) ) (princ ".") (princ ". Completed.") (prin1) Quote
GLAVCVS Posted April 29 Posted April 29 Hi I haven't looked into this in depth. But at first glance, it seems like you might have a problem with the variable 'n': it's in the main function and reappears in the auxiliary function 'taviz'. This wouldn't be a problem if 'n' were a local variable to 'taviz', but it isn't. Therefore, when 'taviz' is called from 'c:b', the value of 'n' is modified, which can cause objects to be processed multiple times or, conversely, not at all, as seems to be your case. Therefore: try changing this '(defun taviz (so)' to this '(defun taviz (so / n)' 1 Quote
GLAVCVS Posted April 29 Posted April 29 (edited) Therefore: it is necessary that you look for information about the scope of the variables so that, if it appears in several functions and in different processes, you can avoid this problem from occurring. P.S.: I haven't examined your code thoroughly enough, so you should take a look to check that this doesn't happen with other variables in your code. Edited April 29 by GLAVCVS Quote
mohammadreza Posted April 29 Author Posted April 29 1 hour ago, GLAVCVS said: Therefore: it is necessary that you look for information about the scope of the variables so that, if it appears in several functions and in different processes, you can avoid this problem from occurring. P.S.: I haven't examined your code thoroughly enough, so you should take a look to check that this doesn't happen with other variables in your code. yes. you are right. thank so much 1 Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.