I am getting close to the correct code.
I have a few questions:
1) How do I break a while loop?
2) how can I easily prevent the user from inputing nothing when prompted for the new text to translate to? I tried (initget 1) but it didn't work.
3) I ma having troubles writing to the lookup file from autocad. I don't understand if it always prepends a newline character when writing to the end of the lookup file. Sometimes it does sometimes it doesn't.
4) I also think I have a problem when the text has parenthesis. If a text on drawing has a parenthesis and autocad needs to create a new entry in the lookup file, then it crashes. The text on the drawing gets updated, when when it writes to the file it crashes.
EDIT: I was right concerning the parenthesis, autocad cannot write a line to a file if it has a parenthesis, but it can ready from it. How to overcome that? Do I need to escape the parenthesis character?
EDIT2: actually no. I don't understand what's happening. I have removed the parenthesis, and it still crashes even on simple text with no parenthesis. It looks like if the script chrashes once, it's doomed, womething wrong must be written to the lookup file, and the file gets corrupted.
Here is the latest code:
(defun C:PT (/ )
;newlines are marked as \P in autocad text.
(setq path2file (findfile "ProTranslator.dat")) ;path to the data file
(setq listOfText (ssget "_X" '((0 . "*TEXT")))) ;fetch all the text entities on the drawing
;(alert (itoa (sslength listofText)))
;To display a number in alert box, you have to first make it into a string.
(setq pointer 0);that's the pointer inside the listOfText. Lists begin with an index of 0
(repeat (setq n (sslength listOfText))
;count how many text entities have been found, and loop n times
;because of the way we move in our list, we need to redefine n each time we loop
(setq fileData (open path2file "r")) ;open the data file and assign it to fileData
(setq textEntity (entget (ssname listOfText pointer)))
;retrieve the text entity so that we can modify its text attribute using entmod
(setq textEntityContent (cdr (assoc 1 textEntity)));select the text content from the
;text entity
;strcase converts the string into HIGHCAPS so we will be able to still match capitalization
;typos.
(setq matchFound "false") ;this variable is used to detect text entities that have no
;entry in the lookup file and that need one.
(while (setq oneLine (read-line fileData)) ;we loop through each line of the file
;that small block of code is dirty, but it is to overcome a bug when autocad writes
;a new line in the lookup file. There is a problem when it creates a new line
(if (= oneLine "")
(setq oneLine (read-line fileData))
);
(setq listed (read (strcat "(" oneLine ")")));here is a nice trick. Each line in the
;lookup file has the data written between double quotes. We want to turn a line into
;a list of two members with the first member being the textToLookFor and the second
;member being the newText that will update theTextToLookFor.
(setq textToLookFor (car listed))
(setq newText (nth 1 listed));nth 1 is the equivalent of cadr.
(if (= textToLookFor textEntityContent);look for a string match
(progn ;always use a progn after an if. ALWAYS!!!
(entmod (subst (cons 1 newText) (assoc 1 textEntity) textEntity))
;update the text content with the usual entmod+subst combo
;Note that here (cons 1 newText) is mandatory, as '(1 . newText) doesn't work.
(setq matchFound "true")
);end of progn
);end of the if that looks for a string match
;The following if block, is used when the script is launched for more than once
;it looks for a match in the translated text, otherwise it would try to translate
;text that has already been translated.
(if (= textEntityContent newText)
(progn
(setq matchFound "true")
);end of progn
);end of the if that looks for a string match
);end of the while loop that goes through each line of the file
(close fileData); close the file after reaching the end otherwise the next while
;will not loop through the file again
;The file must be closed and reopened as append for adding data. Opening the file
;in first place as append doesn't allow to read lines from it.
(if (= matchFound "false")
(progn ;progn is used to evalutate more than one expression with an if block
(princ (strcat "\nThe text: " textEntityContent))
(princ (strcat "\nhas no entry in the lookup file!"))
(setq textToAppend (getstring T "\nEnter the new text to replace with: "))
;prompt user for the name of the text that will act as a replacement.
(entmod (subst (cons 1 textToAppend) (assoc 1 textEntity) textEntity));Replace text
(setq addLine (strcat "\n"" textEntityContent "\" " "\"" textToAppend "\""))
;we need to create the new that will be appended to the lookup file
(setq fileData (open path2file "a"))
(write-line addLine fileData) ;append the new line to the end of fileData
(close fileData)
);end of progn
);end of the if when matchFound is "false"
(setq pointer (1+ pointer));increment the pointer
); end of the repeat loop
(princ "\nProTranslator has finished.")
(princ)
) ;end of defun
EDIT3:
Once again complete nonsense, I haven't touched my code and it refuses to work on a file on which it used to work. I don't know what's happening, it seems that the lookup file gets messed up.
EDIT4; it's getting ridiculous!!!
I have commented tout the portion of code that writes to the file, and replaced it with an alert box. Guess what! The text gets updated, autocad prompts me for a new entry, does all the text, and never shows the alert box although it should appear!!! :ugly: :pissed: :realmad: