mercredi 30 juin 2010

AutoLisp - Remplacer Texte

;;; Texte à copier et coller dans un fichier Lisp : reptext.lsp

; REPTEXT.LSP by P.Heald Sept'92-Apr'93 Ver. 3.01

;;; THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT EXPRESS OR IMPLIED
;;; WARRANTY. ALL IMPLIED WARRANTIES OF FITNESS FOR ANY PARTICULAR
;;; PURPOSE AND OF MERCHANTABILITY ARE HEREBY DISCLAIMED.
;;; Peter J T Heald, Autodesk UK Ltd.
;;; Tech are the biz.

;;; Utilisation
;;; -----------
;;; Charger la routine : (load "reptext")
;;; taper la commande reptext
;;; répondre aux 5 ou 6 questions :
;;; 1 - Le texte à remplacer (insensible à la case, espaces autorisés)
;;; 2 - Le texte de remplacement (espaces autorisés)
;;; 3 - On traite les textes et Mtextes ? (Y/N)
;;; 4 - Les textes de dimension ? (Y/N)
;;; 5 - Les textes contenus dans les blocs ? (Y/N)
;;; 6 - Les textes enregistrés en attributs de blocs ? (Y/N) (à renseigner uniquement si répondu Y à la question 5
;;;
;;; Sélectionner les éléments à traiter (taper TOUT pour traiter tous les éléments)
;;; Si on désire remplacer plusieurs textes différents par plusieurs autres et sur plusieurs fichiers DWG,
;;; on aura intérêt à rédiger un script (reptext.scr) qu'on jettera dans chaque plan.

;;; exemple : (
ATTENTION : CETTE PARTIE NE DOIT PAS RESTER DANS LE LISP !)

(LOAD "C:/Dossier 1/Dossier 2/Dossier 3/REPTEXT.LSP")
;-------------

REPTEXT
plon à vérifier
plan à vérifier
Y
N
Y
Y
tout

;-------------

;;; Début du Lisp :

;;; Global variables are: blist oldtxt newtxt txttoo dimtoo blktoo atttoo
(defun c:reptext ( / sslist ss count en bllen bdata nen)
(setvar "cmdecho" 0)
(setq oldtxt (getstring T "\nText to search for: ")
newtxt (getstring T "\nReplace with: ")
txttoo F
dimtoo F
blktoo F
)
(while (not (or txttoo dimtoo blktoo))
(prompt "\nSelect desired operation.")
(initget "Yes No")
(setq txttoo (getkword "\nProcess normal text? <Y> : ")
sslist '((-4 . "<OR"))
)
(if (= txttoo "No")
(setq txttoo F)
(setq sslist (append sslist '((0 . "TEXT")))
sslist (append sslist '((0 . "MTEXT")))
txttoo T
)
)
(initget "Yes No")
(setq dimtoo (getkword "\nProcess dimensions? <N> : "))
(if (= dimtoo "Yes")
(setq sslist (append sslist '((0 . "DIMENSION"))))
(setq dimtoo F)
)
(initget "Yes No")
(setq blktoo (getkword "\nProcess blocks? <N> : "))
(if (= blktoo "Yes")
(progn (initget "Yes No")
(setq sslist (append sslist '((0 . "INSERT")))
atttoo (getkword "\nProcess attributes? <N> : ")
)
)
(setq blktoo F)
)
(if (/= atttoo "Yes") (setq atttoo F))
)
(setq sslist (append sslist '((-4 . "OR>")))
ss (ssget sslist)
count 0
blist (list)
)
(prompt "\nProcessing:-\n")
(repeat (sslength ss)
(setq en (ssname ss count)
count (1+ count)
)
(entscan en oldtxt newtxt)
(prompt (strcat "\r" (itoa count) " done."))
)
(prompt "\rDone ")
(setq count 0
bllen (length blist)
)
(if (> bllen 0) (prompt "\nProcessing blocks:-\n"))
(while (and (> bllen 0) (setq bname (nth count blist)))
(setq count (1+ count)
bdata (tblsearch "BLOCK" bname)
nen (cdr (assoc -2 bdata))
)
(while (and nen
(/= (cdr (assoc 0 (entget nen))) "ENDBLK")
)
(entscan nen oldtxt newtxt)
(setq nen (entnext nen))
)
(prompt (strcat "\r" (itoa count) " done."))
)
(prompt "\rDone ")
(if (or dimtoo blktoo)
(progn (prompt "\nRegenerating drawing.")
(command ".regen")
)
)
(princ)
)
(defun entscan (en oldtxt newtxt / ed et 2f tmp)
(setq ed (entget en)
et (cdr (assoc 0 ed))
2f (cdr (assoc 2 ed))
)
(cond ((and (= et "TEXT") txttoo) (swaptext en oldtxt newtxt))
((and (= et "MTEXT") txttoo) (swaptext en oldtxt newtxt))
((and (= et "DIMENSION") dimtoo)
(progn (swaptext en oldtxt newtxt)
(if (setq tmp (chklist blist 2f))
(setq blist tmp)
)
)
)
((and (= et "INSERT") blktoo)
(progn (if (setq tmp (chklist blist 2f))
(setq blist tmp)
)
(if (and atttoo (cdr (assoc 66 ed)))
(attscan en oldtxt newtxt)
)
)
)
)
)
(defun attscan (ename otxt ntxt / edata parent etype)
(setq edata (entget ename)
parent edata
etype "INSERT"
)
(while (and ename (/= etype "SEQEND"))
(setq ename (entnext ename))
(if ename (setq edata (entget ename)
etype (cdr (assoc 0 edata))
)
)
(if (= etype "ATTRIB") (swaptext ename otxt ntxt))
)
(entmod parent)
)

(defun swaptext (ename otxt ntxt / edata oldt newt edata)
(setq edata (entget ename)
oldt (assoc 1 edata)
)
(if oldt
(progn (setq newt (cons 1 (swapstr (cdr oldt) otxt ntxt))
edata (subst newt oldt edata)
)
(entmod edata)
)
)
)
(defun swapstr (line oldstr newstr / charpos oldlen)
(setq oldlen (strlen oldstr)
oldstr (strcase oldstr)
charpos 1
)
(repeat (1+ (- (strlen line) oldlen))
(if (= (strcase (substr line charpos oldlen)) oldstr)
(setq line (strcat (substr line 1 (1- charpos))
newstr
(substr line (+ charpos oldlen))
)
charpos (+ charpos (strlen newstr))
)
)
(setq charpos (1+ charpos))
)
(setq line line)
)
(defun chklist (itemlist itemname)
(if (member itemname itemlist)
(setq itemlist F)
(setq itemlist (append itemlist (list itemname)))
)
)
(princ)
; - - - - - - - - - - - - - - - - - - - End

Aucun commentaire:

Enregistrer un commentaire