Author Topic: Help with code to extract a string from text.  (Read 7853 times)

0 Members and 1 Guest are viewing this topic.

Reu

  • Guest
Help with code to extract a string from text.
« on: February 08, 2013, 03:08:14 PM »
Code: [Select]
;;; Function (1ststring)
;;; Description: Get the string from the first text object to be edited. ;
(defun 1ststring ()
  (setq ss  (ssget ":S" '((0 . "TEXT")) )
str (cdr (assoc 1 entget ss))
  ) ;_end setq
) ;_end defun

What is wrong with this code?

owenwengerd

  • Bull Frog
  • Posts: 451
Re: Help with code to extract a string from text.
« Reply #1 on: February 08, 2013, 03:27:30 PM »
Missing parentheses around (entget ss), and ss should be (ssname ss 0).

Reu

  • Guest
Re: Help with code to extract a string from text.
« Reply #2 on: February 08, 2013, 03:33:26 PM »
Missing parentheses around (entget ss), and ss should be (ssname ss 0).

Code: [Select]
;;; Function (1ststring)
;;; Description: Get the string from the first text object to be edited. ;
(defun 1ststring ()
  (setq ss  (ssget "_:S" '((0 . "TEXT"))) ; Select the text to be edited.
str (cdr (assoc 1 (entget ssname 0))) ; Get the text string from the selected text.
  ) ;_end setq
) ;_end defun

Like this?

It still doesn't work.

Reu

  • Guest
Re: Help with code to extract a string from text.
« Reply #3 on: February 08, 2013, 03:40:42 PM »
Missing parentheses around (entget ss), and ss should be (ssname ss 0).

Code: [Select]
;;; Function (1ststring)
;;; Description: Get the string from the first text object to be edited. ;
(defun 1ststring ()
  (setq ss  (ssget "_:S" '((0 . "TEXT"))) ; Select the text to be edited.
str (cdr (assoc 1 (entget ssname 0))) ; Get the text string from the selected text.
  ) ;_end setq
) ;_end defun

Like this?

It still doesn't work.

Code: [Select]
;;; Function (1ststring)
;;; Description: Get the string from the first text object to be edited. ;
(defun 1ststring ()
  (setq ss  (ssget "_:S" '((0 . "TEXT"))) ; Select the text to be edited.
str (cdr (assoc 1 (entget (ssname ss 0)))) ; Get the text string from the selected text.
  ) ;_end setq
) ;_end defun

EUREKA!!!


Here was the problem, gotta watch those parentheses. . .

(cdr (assoc 1 (entget ssname 0)))
(cdr (assoc 1 (entget (ssname ss 0))))
« Last Edit: February 08, 2013, 03:44:19 PM by Reu »

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Help with code to extract a string from text.
« Reply #4 on: February 08, 2013, 04:32:26 PM »
Lost
In
Stupid
Parenthesis
 :laugh:
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

Reu

  • Guest
Re: Help with code to extract a string from text.
« Reply #5 on: February 08, 2013, 04:38:40 PM »
Lost
In
Stupid
Parenthesis
 :laugh:

This is the code I have so far, would someone care to help me better understand 'entmod', and if you can understand what I am trying to do maybe point me in the right direction.

Code: [Select]
;;; Function (1ststring)
;;; Description: Get the string from the first text object to be edited. ;
(defun 1ststring ()
  (setq ss1  (ssget "_:S" '((0 . "TEXT")))
; Select the text to be edited.
str1 (cdr (assoc 1 (entget (ssname ss1 0))))
; Get the text string from the selected text.
  ) ;_end setq
) ;_end defun



;;; Function (2ndstring)
;;; Description: Get the string from the second text object to be edited. ;
(defun 2ndstring ()
  (setq ss2  (ssget "_:S" '((0 . "TEXT")))
; Select the text to be edited.
str2 (cdr (assoc 1 (entget (ssname ss2 0))))
; Get the text string from the selected text.
  ) ;_end setq
) ;_end defun




;;; Function (C:MCHAR) ;
;;; Description: This program is to get a number of characters from the end ;
;;; of a text string and place them at the beginning of a 2nd string. ;
(defun C:MCHAR (/ ss1 ss2 str1 str2)
  (setq noofchartomove
(getreal "\nHow many characters do you want to move?")
  ) ;_end setq
  (1ststring)
  (2ndstring)
  (setq chartomove
(substr (- (noofchartomove) (strlen str1)) noofchartomove)
)
  (entmod ss2
  )
  (princ "\nType MCHAR to invoke.")
  (princ)
)

Thanks,
     Reuben Shilling
« Last Edit: February 08, 2013, 04:52:46 PM by Reu »

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Help with code to extract a string from text.
« Reply #6 on: February 08, 2013, 05:19:28 PM »
Try this one on for size.
Code - Auto/Visual Lisp: [Select]
  1. ;;; Function (C:MCHAR) ;
  2. ;;; Description: This program is to get a number of characters from the end ;
  3. ;;; of a text string and place them at the beginning of a 2nd string. ;
  4. (defun c:mchar (/ #chrs ss1 ss2 txt1 txt2 str entlst)
  5.  
  6.   (and
  7.     (princ "\nAppend characters from 1st text to the 2nd text")
  8.     (setq #chrs (getint "\nHow many characters do you want to move?"))
  9.     (princ "\nSelect the 1st Text to get chars.")
  10.     (setq ss1 (ssget "_:S" '((0 . "TEXT"))))
  11.     (setq txt1 (cdr (assoc 1 (entget (ssname ss1 0)))))
  12.     (princ "\nSelect the Text to add chars to.")
  13.     (setq ss2 (ssget "_:S" '((0 . "TEXT"))))
  14.     (setq entlst (entget (ssname ss2 0)))
  15.     (setq str (substr txt1 (1+ (- (strlen txt1) #chrs))))
  16.  
  17.     (entmod ; update the following ent list
  18.       (subst ; make some changes to the list
  19.         (cons 1 (strcat (cdr (assoc 1 entlst)) str)) ; new string + dxf 1
  20.         (assoc 1 entlst) ; old string with dxf code
  21.         entlst ; original ent list
  22.       )
  23.     )
  24.   )
  25.   (princ "\nType MCHAR to invoke.")
  26.   (princ)
  27. )
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

bchapman

  • Guest
Re: Help with code to extract a string from text.
« Reply #7 on: February 09, 2013, 02:10:02 AM »
Wanted to try a slightly difference approach.
Code: [Select]

;;;Removes characters from end of one string and attaches them to beginning of another ;;;;
(defun c:test (/ textobja textobjb texta textb cutnumber remainder prefix finalstring trimmedstring)
(command "undo" "begin")
(vl-load-com)
(setq textobja (vlax-ename->vla-object (car (entsel "\nSelect Text to Remove Characters ELSE YOU ARE A NOOB:"))))
(setq textobjb (vlax-ename->vla-object (car (entsel "\nSelect Text to Add Characters ELSE YOU ARE A NOOB:"))))
(setq texta (vlax-get-property textobja 'TextString))
(setq textb (vlax-get-property textobjb 'TextString))
(setq cutnumber (getint "\nNumber of text characters to relocate: "))
(setq remainder (- (strlen texta) cutnumber))
(setq prefix (substr texta (+ 1 remainder)))
(setq finalstring (strcat prefix textb))
(vlax-put-property textobjb 'TextString finalstring)
(setq trimmedstring (substr texta 1 remainder))
(vlax-put-property textobja 'TextString trimmedstring)
(princ));;;;

Modified to wipe out variables at end and axed the right-trim deal per Lee's recommendations. Thank ya sir...as always
« Last Edit: February 10, 2013, 03:11:47 AM by BC_in_NV »

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: Help with code to extract a string from text.
« Reply #8 on: February 09, 2013, 05:41:11 AM »
Wanted to try a slightly difference approach.

A few points of constructive criticism BC:
  • Be sure to declare your local variables within the symbol list of the defun expression; here is an old tutorial of mine discussing the topic.
  • If the user misses a pick or right clicks at either selection prompt, the program will error since the vlax-ename->vla-object function does not accept a nil argument.
  • If the user presses enter or right-clicks at the prompt for number of characters with no input, the program will error since the '-' function requires a non-nil numerical argument.
  • Be careful of using the vl-string-right-trim function to remove characters, since this (along with the other vl-string-*-trim functions) will remove all characters in the given character set from the start/end of the string, until a character not found in the given set is reached.
For example:
Code: [Select]
_$ (vl-string-right-trim "ness" "carelessness")
"carel"

bchapman

  • Guest
Re: Help with code to extract a string from text.
« Reply #9 on: February 09, 2013, 08:40:34 PM »
Thanks Lee!  Modified somewhat as recommended... have to figure out how to force text selection with entsel... don't know how to do that

If no error trapping is provided is that a bad thing in this case? The function will simply "nil" out ....

Wanted to try a slightly difference approach.

A few points of constructive criticism BC:
  • Be sure to declare your local variables within the symbol list of the defun expression; here is an old tutorial of mine discussing the topic.
  • If the user misses a pick or right clicks at either selection prompt, the program will error since the vlax-ename->vla-object function does not accept a nil argument.
  • If the user presses enter or right-clicks at the prompt for number of characters with no input, the program will error since the '-' function requires a non-nil numerical argument.
  • Be careful of using the vl-string-right-trim function to remove characters, since this (along with the other vl-string-*-trim functions) will remove all characters in the given character set from the start/end of the string, until a character not found in the given set is reached.
For example:
Code: [Select]
_$ (vl-string-right-trim "ness" "carelessness")
"carel"
« Last Edit: February 10, 2013, 03:15:24 AM by BC_in_NV »

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Help with code to extract a string from text.
« Reply #10 on: February 09, 2013, 11:39:12 PM »
If no error trapping is provided is that a bad thing in this case? The function will simply "nil" out ....

Won't nil out, it will throw an error.
Command: (setq textobja (vlax-ename->vla-object (car (entsel "\nSelect Text to Remove Characters:"))))

Select Text to Remove Characters:

< bad argument type: lentityp nil >
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

bchapman

  • Guest
Re: Help with code to extract a string from text.
« Reply #11 on: February 10, 2013, 03:09:15 AM »
Modified the code to address some things... As you can see I adequately handled user's who do not select text as specifically told to do so  :lmao:

but really... I'll have to figure out how to make it test to see if the entsel is a text... don't know how to do that (never had to since I only write routines for myself lol)

CAB... if you have time can you please help me understand what the repercussions are of not handling this in this case?

Thanks!

If no error trapping is provided is that a bad thing in this case? The function will simply "nil" out ....

Won't nil out, it will throw an error.
Command: (setq textobja (vlax-ename->vla-object (car (entsel "\nSelect Text to Remove Characters:"))))

Select Text to Remove Characters:

< bad argument type: lentityp nil >
« Last Edit: February 10, 2013, 09:19:33 PM by BC_in_NV »

fixo

  • Guest
Re: Help with code to extract a string from text.
« Reply #12 on: February 10, 2013, 10:33:53 AM »
To select some, always start from parent access eg
Code: [Select]
(setq eh (entsel "\nSelect Text to Remove Characters: "))then go further from this point
Code: [Select]
(if eh
(progn
(setq en (car eh)
        pt (cadr eh));etc
;;------------------------;;
)
this will allow you to use a robust way if
you will be check on validity all of the next parts :)
Just my 2c

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Help with code to extract a string from text.
« Reply #13 on: February 10, 2013, 03:35:47 PM »
CAB... if you have time can you please help me understand what the repercussions are of not handling this in this case?

It is bad practice to allow your lisp to crash. Better practice to plan ahead and make a soft landing.  8-)
In a more complicated lisp you may have changed the ACAD environment and a crash will leave the DWG setting in an unexpected state.
This can be very aggravating to the end user. I'm not saying that in these code snippets that you impalement a full error trap but when you can avoid the crash with line or two of extra code that it is good form to do so.
I'm sure I've been guilty of the same faux pas in my past but do make an effort to avoid it today.  :-)
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Help with code to extract a string from text.
« Reply #14 on: February 10, 2013, 03:39:57 PM »
This is an example of a robust entget.
Code - Auto/Visual Lisp: [Select]
  1. ;; Stig
  2. ;  http://www.theswamp.org/index.php?topic=5814.msg70776#msg70776
  3. ;;  (setq ent (getSubent "\nSelect line: " '("LINE")))
  4.  
  5. ;;; Return an entity/subentity or nil
  6. (defun getSubent (prmpt lst / ent parent)
  7.   (setvar "ERRNO" 0)
  8.   (while (and (not ent) (/= (getvar "ERRNO") 52))
  9.     (cond ((setq ent (nentsel prmpt))
  10.            (if (not (member (cdr (assoc 0 (entget (car ent)))) lst))
  11.              (setq ent nil)
  12.            )
  13.           )
  14.     )
  15.   )
  16.   ent
  17. )
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.