Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Text Editing 2

Status
Not open for further replies.

Mabjro

Programmer
Jun 3, 2003
127
US
I want to create a lisp routine for editing text with some restrictions. The best way to explain it is to show it.

I have a text entity with a value xxxx-(yy) occurring multiple times in a drawing.

When I use the ddedit command the whole string appears in the dialogue box for editing. The only thing I want to edit is the area between the parentheses. "yy". I have to select the yy with my mouse hundreds of times in a drawing. I tried creating an attribute block for this task. With the xxxx-( ) as text and the yy as an attribute. This worked OK but sometimes there are more than 2 digits between the parentheses.

Can anyone help and do I need to elaborate further?

Thank you,
Mabjro
 
I got to thinking about an easier way to explain what I am trying to accomplish. I want to add a default prefix and suffix to my single line text.

Thanks again.
 
Adding a default prefix and suffix to a selected text set would be a fairly straightforward lisp routine. Prompt user for the Prefix , Suffix, then select text items to apply this to. The routine would step through each text item, and "strcat" the prefix & suffic to the text, and "entmod" each entity. You could probably find many freeware routines that do this -but may be faster and better fit your needs to write your own.

Following your first description, assuming you have text with the prefix and suffix you want to edit, you could write a routine that prompts for the prefix and suffix, then user selects text, types in new string between the Prefix & suffix, then the text gets updated with the new information.
 
Thank you CarlAK, I will work on your suggestion. You gave me enough to get started as I am rusty with Autolisp. I have not toyed with it in a couple of years. It should be pretty easy now that you have jogged my memory.

Thanks.
Mabjro
 
Wow. I can't believe how it all comes back. I am rusty but it's still a lot of fun. I succeeded in creating a lisp routine that works exactly as it should with one exception. I have to enter the command every time for each text entity and I have to type in the suffix each time. Please see the routine below.

defun c:ledit ()
; Prompt for a suffix
(setq Suffix (getstring t "Enter a suffix."))
; Select Text
(setq a (ssget))
; Get the entity name from the selected text
(setq entityName (ssname a 0))
; Get DXF Codelist from using EntityName
(setq c (entget entityname))
; Break the text value out of the code list.
(setq Root (cdr (assoc 1 c)))
; Combine the prefix with the selected text
(setq newText (strcat suffix "-(" root ")"))
; Modify the DXF Code
(setq c (subst (cons 1 newtext) (assoc 1 c) c))
; Write the DXF Code back to the entity
(entmod c)
)

How can I select multiple text entities and have this suffix applied to all while only running the routine once. I'm going to look into doing some kind of a loop. Some pointers would be of great help to me.

Thanks
Mabjro
 
(repeat sslength(a)
(setq entityName (ssname a cnt))
...
...
(setq cnt (1+ cnt))
) ; end repeat

Hope this helps

Todd
 
Todd,

Although I just finished writing the routine and then read your post, I am going to try your suggestion.

Here is what I did so far.
(defun c:ledit ()

; Prompt for a suffix
(setq Suffix (getstring t "Enter a suffix."))
; Set initial value of I
(setq I 0)
; Select Text
(setq a (ssget '((0 . "TEXT"))))
; Create Loop Condition
(while (<= I 600)
; Get the entity name from the selected text
(setq entityName (ssname a I))
; Get DXF Codelist from EntityName
(setq c (entget entityname))
; Break the text value out of the code list.
(setq Root (cdr (assoc 1 c)))
; Combine the prefix with the selected text
(setq newText (strcat suffix "-(" root ")"))
; Modify the DXF Code
(setq c (subst (cons 1 newtext) (assoc 1 c) c))
; Write the DXF Code back to the entity
(entmod c)
; Increase the counter by 1
(setq I (1+ I)))

)

I was able to use "while" with a counter (see above). It works great but it is a little sloppy because it ends with an error.

(error: bad argument type: lentityp nil)

This error has no effect on the functionality of the routine, but it is anoying.

The error is because I am running the routine 600 times regardless of how many text entities were selected. This causes the routine to get to the end of the selection set and run with a value of nill over and over again. Maybe I could setq a variable to the count of the entities in the selection set and replace the number 600 with that variable. I'm going to try a couple of things and post back Monday. I may have the most luck using your method.

Thanks for your help.

Mabjro
 
Instead of a "while" loop, use "repeat" for the number of entities in the set:

(repeat (sslenght a))
 
OOps make that "sslength" (posted too fast!)
 
CarlAK,

Sounds like you and Todd are on the same track. I will try using repeat and will post back Monday. (on the road Fridays) Thanks for the help and have a good weekend.

Mabjro
 
I got excited, stayed late and did this right away. Works like a charm. Thanks to all.

(defun c:ledit ()

; Prompt for a suffix
(setq Suffix (getstring t "Enter a suffix."))
; Set initial value of I
(setq I 0)
; Select Text
(setq a (ssget '((0 . "TEXT"))))
; Create Loop Condition
(repeat (sslength a)
; Get the entity name from the selected text
(setq entityName (ssname a I))
; Get DXF Codelist from EntityName
(setq c (entget entityname))
; Break the text value out of the code list.
(setq Root (cdr (assoc 1 c)))
; Combine the prefix with the selected text
(setq newText (strcat suffix "-(" root ")"))
; Modify the DXF Code
(setq c (subst (cons 1 newtext) (assoc 1 c) c))
; Write the DXF Code back to the entity
(entmod c)
; Increase the counter by 1
(setq I (1+ I)))

)
 
HOW CAN THIS BE MODIFIED TO RETRIEVE A TEXT STRING THEN REPLACE IT WITH ANOTHER TEXT STRING. THIS WOULD BE SIMILAR TO FIND AND REPLACE BUT I WOULD LIKE TO HAVE IT BE ABLE TO SELECT 50-100 DIFFERENT TEXT STRINGS AND REPLACE THEM WITH ANOTHER WITHOUT HAVE TO DO "FIND AND REPLACE" 100 TIMES. THANKS.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top