;Shusei Hayashi
;OffshoreCad&Management Inc.
;10F Jaka Bldg., 6780 Ayala Ave.,
;Makati, Philippines
;http://www.offshorecad.com.ph/
;http://www.offshore-management.com.ph/

(defun c:SD_513 (/ obj1 obj2 a txt ins ntxt num n Position prevtext)
	(if (= SD:Lang "E")
		(princ "\n Increment Text Modify")
		(princ "\n 好きな位置の数字を増やしながら文字をコピー")
	)
	(princ "\n **********************************")
	(setq *error* *myerror*)   
	(SD1028)
	(Input)
	(while (= a nil)
		(while (= obj2 nil)
			(if (= SD:Lang "E")
				(setq 	obj2 (entsel "\n Select number to copy: "))
				(setq 	obj2 (entsel "\n 文字を選択： "))
			)
			(if obj1
				(if (and (/= (cdr (assoc 0 (entget (car obj2)))) "TEXT") (/= (cdr (assoc 0 (entget (car obj2)))) "MTEXT"))
					(progn
						(setq obj2 nil)
						(if (= SD:Lang "E")
							(princ "\n Not a Text Object")
							(princ "\n 文字ではありません")
						)
					)
				)
			)
		)
		(setq newnum1 (car obj2))
		(if (= prevtext nil) (setq prevtext (cons 1 (get_text_of_mtext (car obj1)))))
		(entmod (subst prevtext (cons 1 (get_text_of_mtext (car obj2))) (entget (car obj2))))
		(gr_move_513 newnum1)
		(setq prevtext (cons 1 (get_text_of_mtext (car obj2))))
		(setq obj2 nil)
	)
	
	(SD2056)
	(setq *error* nil)
	(princ)	
)
;;********************************************************************
(defun gr_move_513 (obj1 / x1 x pos)
	(command "select" obj1 "")
	(while (/= 3 x1)
		(setq 	x (grread T 10 1)
				x1 (car x)
				pos (nth 1 x))
		(if (= x1 2)
			(cond 
				((or (= pos 68)(= pos 100))(increment num obj1))
				((or (= pos 65)(= pos 97))(decrement num obj1))
				((= pos 46)(if (/= num (- ntxt 1)) (setq num (+ num 1))))
				((= pos 44)(if (/= num 0) (setq num (- num 1))))
				((= pos 9)(caps num obj1))
			)
		)
	)
)

;;********************************************************************
(defun Input ()
	(while (= obj1 nil)
			(if (= SD:Lang "E")
				(setq 	obj1 (entsel "\n Select Number: "))
				(setq 	obj1 (entsel "\n 文字を選択： "))
			)
			(if obj1
				(if (and (/= (cdr (assoc 0 (entget (car obj1)))) "TEXT") (/= (cdr (assoc 0 (entget (car obj1)))) "MTEXT"))
					(progn
						(setq obj1 nil)
						(if (= SD:Lang "E")
							(princ "\n Not a Text Object")
							(princ "\n 文字ではありません")
						)
					)
				)
			)
	)
	(setq	txt (get_text_of_mtext (car obj1))
			ins (cdr (assoc 11 (entget (car obj1))))
			ntxt (strlen txt))
	(if (= SD:Lang "E")
			(setq 	num (getint "\n Input Position of Editable Character or [Enter] for last:"))
			(setq 	num (getint "\n 数字を加減させたい文字の位置を入力(0：最後の文字 TAB：大文字小文字 <>：位置変更) "))
	)
	(if num (setq num (- num 1)))
	(cond
		((= num nil)
			(setq num (- ntxt 1)))
		((minusp num)
			(if (= SD:Lang "E")
				(Princ "\n Cannot find zero and negative position. Go to last")
				(Princ "\n 0・負の数字が入力→最後の文字を増減させます")
			)
			(setq num (- ntxt 1)))
		((>= num ntxt )
			(if (= SD:Lang "E")
				(Princ "\n Cannot find position in character list.Go to last")
				(Princ "\n 0・負の数字が入力→最後の文字を増減させます")
			)
			(setq num (- ntxt 1)))
	)
)

;;*************************************************************************
(defun get_text_of_mtext (obj1 / txt1 pos1 txt2)
	(setq 	txt1 (cdr (assoc 1 (entget obj1))))
	(if (= (substr txt1 1 1) "{")
		(setq	pos1 (vl-string-position (ascii ";") txt1)
				txt2 (vl-string-right-trim "}" (substr txt1 (+ pos1 2))))
		(setq 	txt2 txt1)
	)
	txt2
)

;共通コマンド
(defun SD1028 ()
  (setq OldCmdEcho (getvar "CMDECHO"))
  (setvar "CMDECHO" 0)
  (command "undo" "be")
  (setq OldOsmode (getvar "OSMODE"))
  (setq OldLayer (getvar "CLAYER"))
  (setq OldLType (getvar "CeLType"))
  (setq OldCeLWeight (getvar "CeLWeight"))
  (setq OldColor (getvar "CeColor"))
  (setq OldOrtho (getvar "ORTHOMODE"))
  (setq OldDStyle  (getvar "DIMSTYLE"))
  (setq OldExpert (getvar "Expert"))
  (setvar "EXPERT" 0)
  (setq Path_Lang "HKEY_CURRENT_USER\\Software\\SpeedDraftLT")
  (if (vl-registry-read Path_Lang "SD_Language" )
  	(setq SD:Lang (vl-registry-read Path_Lang "SD_Language" ))
  	(progn	(setq SD:Lang "J")
  			(vl-registry-write Path_Lang "SD_Language" "J")
  	)
  )
  (princ)
)
;********************************
(defun SD2056 ()
  (setvar "OSMODE" OldOsmode)
  (command "undo" "end")
  (setvar "CLAYER" OldLayer)
  (setvar "CeLType" OldLType)
  (setvar "CeLWeight" OldCeLWeight)
  (setvar "CeColor" OldColor)
  (setvar "ORTHOMODE" OldOrtho)
  (setvar "Expert" OldExpert)
  (if (and (/= (getvar "DIMSTYLE") OldDStyle)(tblsearch "DIMSTYLE" OldDStyle))
  	(command "-dimstyle" "Restore" OldDStyle)
  )
  (princ "\n (C)OffshoreCad&Management")
  (setvar "CMDECHO" OldCmdEcho)
  (princ)
)


(if (= SD:Lang "E")
	(princ "\n Command Name: SD_513 \n")
	(princ "\n コマンド名：SD_513 \n")
)
(princ)
