Change in lisp code to give prefix and suffix for Number increments

Change in lisp code to give prefix and suffix for Number increments

Anonymous
Not applicable
1,529 Views
5 Replies
Message 1 of 6

Change in lisp code to give prefix and suffix for Number increments

Anonymous
Not applicable

Dear Experts,

 

Requesting your help to update the code as it should ask Prefix and Suffix at user input from command prompt. The code is below

(defun c:NI (/ oldent newent)
   (setq oldent (cdr (entget(car(entsel "Select text: ")))))
   (if (/= (cdr(assoc 0 oldent)) "TEXT")
      (progn
         (princ "No Text Selected")
      )
      
      ;loop until user cancels command
      (while T
         (progn
            
            ;increment text and substitute it in the DXF group 1 entity data
            (setq newent (subst (cons 1 (itoa (+ 1 (atoi (cdr(assoc 1 oldent)))))) (assoc 1 oldent) oldent))
            
            ;get a new point and substitute the DXF group 10 code with the new point
            (setq newent (subst (cons 10 (getpoint "\nCopy to: ")) (assoc 10 newent) newent))
            
            ;reset ready to start again            
            (setq oldent (entmake newent))
         )
      )
   )
   
  ;exit cleanly
   (princ)
)
0 Likes
Accepted solutions (1)
1,530 Views
5 Replies
Replies (5)
Message 2 of 6

ВeekeeCZ
Consultant
Consultant

You don't need an expert for that. Just little of an effort.

 

(defun c:NI (/ oldent newent)
   (setq oldent (cdr (entget(car(entsel "Select text: ")))))
   (setq p (getstring T "\nPrefix: "))
   (setq s (getstring T "\nSuffix: "))
   (if (/= (cdr(assoc 0 oldent)) "TEXT")
      (progn
         (princ "No Text Selected")
      )
      
      ;loop until user cancels command
      (while T
         (progn
            
            ;increment text and substitute it in the DXF group 1 entity data
            (setq newent (subst (cons 1 (strcat p (itoa (+ 1 (atoi (cdr(assoc 1 oldent))))) s)) (assoc 1 oldent) oldent))
            
            ;get a new point and substitute the DXF group 10 code with the new point
            (setq newent (subst (cons 10 (getpoint "\nCopy to: ")) (assoc 10 newent) newent))
            
            ;reset ready to start again            
            (setq oldent (entmake newent))
         )
      )
   )
   
  ;exit cleanly
   (princ)
)
0 Likes
Message 3 of 6

Anonymous
Not applicable

Dear Sir,

Thanks for your efforts. The code is able to make both prefix and suffix, but not able to increment the number. Please refer to attachment.

0 Likes
Message 4 of 6

ВeekeeCZ
Consultant
Consultant

(defun c:NI (/ oldent newent)
(setq oldent (cdr (entget(car(entsel "Select text: ")))))
(setq p (getstring T "\nPrefix: "))
(setq s (getstring T "\nSuffix: "))
(if (/= (cdr(assoc 0 oldent)) "TEXT")
(progn
(princ "No Text Selected")
)

;loop until user cancels command
(progn
(setq i (atoi (cdr(assoc 1 oldent))))
(while T
(progn

;increment text and substitute it in the DXF group 1 entity data
(setq newent (subst (cons 1 (strcat p (itoa (setq i (1+ i))) s))
(assoc 1 oldent)
oldent))

;get a new point and substitute the DXF group 10 code with the new point
(setq newent (subst (cons 10 (getpoint "\nCopy to: ")) (assoc 10 newent) newent))

;reset ready to start again
(setq oldent (entmake newent))
)
))
)

;exit cleanly
(princ)
)

0 Likes
Message 5 of 6

dbhunia
Advisor
Advisor
Accepted solution

Try this.....

 

(defun c:NI (/ oldent newent pre suf No)
(defun Num_Str (str)
   (vl-list->string (vl-remove-if-not 'num-char (vl-string->list str)))
)
(defun num-char (char)
   (< 46 char 58)
)

   (setq oldent (cdr (entget(car(entsel "Select text: ")))))
   (if (/= (cdr(assoc 0 oldent)) "TEXT")
	(progn
	   (princ "No Text Selected")
	)

	;loop until user cancels command
	(progn
   	   (setq pre (getstring T "\nPrefix: "))
   	   (setq suf (getstring T "\nSuffix: "))
	   (while T
		(progn

		;increment text and substitute it in the DXF group 1 entity data
		(setq newent (subst (cons 1 (strcat pre (itoa (+ 1 (atoi (Num_Str (cdr(assoc 1 oldent)))))) suf)) (assoc 1 oldent) oldent))

		;get a new point and substitute the DXF group 10 code with the new point
		(setq newent (subst (cons 10 (getpoint "\nCopy to: ")) (assoc 10 newent) newent))

		;reset ready to start again 
		(setq oldent (entmake newent))
		)
	   )
	)
   )

;exit cleanly
(princ)
)

Debashis Bhunia
Co-Founder of Geometrifying Trigonometry(C)
________________________________________________
Walking is the First step of Running, Technique comes Next....
Message 6 of 6

Anonymous
Not applicable

Working Perfectly as per requirement. Thanks a lot Sir.

0 Likes