Jump to content

A lisp remove the last character of a text.


Tunnelrat

Recommended Posts

I did mean that it is new to me to know that the mtext would behave that way when it is being formatted .

Thanks for the clarification .

 

See... we learn something new everyday! :D

Link to comment
Share on other sites

  • 1 year later...
  • Replies 33
  • Created
  • Last Reply

Top Posters In This Topic

  • BlackBox

    11

  • Lee Mac

    9

  • Reu

    6

  • Tharwat

    5

Top Posters In This Topic

I'm just getting started with AutoLISP, how would I rewrite this to remove more than one character?

 

Consider the following code:

 

(defun c:rchar ( / e i n s x )
   
   (setq n 1) ;; Number of characters to remove from end of string
   
   (if (setq s (ssget "_:L" '((0 . "TEXT"))))
       (repeat (setq i (sslength s))
           (setq e (ssname s (setq i (1- i)))
                 x (cdr (assoc 1 (entget e)))
           )
           (if (< n (strlen x))
               (entmod (list (cons -1 e) (cons 1 (substr x 1 (- (strlen x) n)))))
           )
       )
   )
   (princ)
)

 

Welcome to CADTutor thunderfoot :)

Link to comment
Share on other sites

Consider the following code:

 

(defun c:rchar ( / e i n s x )
   
   (setq n 1) ;; Number of characters to remove from end of string
   
   (if (setq s (ssget "_:L" '((0 . "TEXT"))))
       (repeat (setq i (sslength s))
           (setq e (ssname s (setq i (1- i)))
                 x (cdr (assoc 1 (entget e)))
           )
           (if (< n (strlen x))
               (entmod (list (cons -1 e) (cons 1 (substr x 1 (- (strlen x) n)))))
           )
       )
   )
   (princ)
)

 

Welcome to CADTutor thunderfoot :)

 

Thanks Lee. Works perfectly. It may take me a while to figure out exactly what is going on there though. AutoLISP is great can't wait to write some real programs of my own.:geek:

Link to comment
Share on other sites

Thanks Lee. Works perfectly. It may take me a while to figure out exactly what is going on there though. AutoLISP is great can't wait to write some real programs of my own.:geek:

 

You're very welcome - if you have any questions when dissecting the code, just ask.

Link to comment
Share on other sites

You're very welcome - if you have any questions when dissecting the code, just ask.

Could you explain each line with "; comments"?

 

 

*edit*

 

Starting with the "if" statement, I do understand the basics of setq, and I thought I understood if. . .

 

Thanks,

Reuben Shilling

Link to comment
Share on other sites

Here's a quick dissection:

 

([color=BLUE]defun[/color] c:rchar ( [color=BLUE]/[/color] e i n s x ) [color=GREEN];; define function, declare local variables[/color]
   
   ([color=BLUE]setq[/color] n 1) [color=GREEN];; Number of characters to remove from end of string[/color]
   
   ([color=BLUE]if[/color] [color=GREEN];; If the following expression returns a non-nil value[/color]
       ([color=BLUE]setq[/color] s [color=GREEN];; Assign the following value to the symbol 's'[/color]
           ([color=BLUE]ssget[/color] [color=GREEN];; Collect a selection set of[/color]
               [color=MAROON]"_:L"[/color] [color=GREEN];; Object on unlocked layers[/color]
               '((0 . [color=MAROON]"TEXT"[/color])) [color=GREEN];; whose entity type (DXF Group 0) = TEXT[/color]
           ) [color=GREEN]; end ssget[/color]
       ) [color=GREEN];; end setq[/color]
       ([color=BLUE]repeat[/color] [color=GREEN];; repeat a set of expressions a number of times[/color]
           ([color=BLUE]setq[/color] i [color=GREEN];; Assign the following value to the symbol 'i'[/color]
               ([color=BLUE]sslength[/color] s)  [color=GREEN];; Number of items in the selection set[/color]
           ) [color=GREEN];; end setq [/color]
           ([color=BLUE]setq[/color] e [color=GREEN];; Assign the following value to the symbol 'e'[/color]
               ([color=BLUE]ssname[/color] s [color=GREEN];; Retrieve the entity name at a specific index in the set[/color]
                   ([color=BLUE]setq[/color] i ([color=BLUE]1-[/color] i)) [color=GREEN];; Decrement the counter variable 'i'[/color]
               ) [color=GREEN];; end ssname[/color]
               x [color=GREEN];; Assign the following value to the symbol 'x'[/color]
               ([color=BLUE]cdr[/color] [color=GREEN];; Return the second item of the dotted pair[/color]
                   ([color=BLUE]assoc[/color] 1 [color=GREEN];; Retrieve the dotted pair with DXF Group 1 (the text string)[/color]
                       ([color=BLUE]entget[/color] e) [color=GREEN];; Retrieve the DXF data for the entity assigned to the symbol 'e'[/color]
                   ) [color=GREEN];; end assoc[/color]
               ) [color=GREEN];; end cdr[/color]
           ) [color=GREEN];; end setq[/color]
           ([color=BLUE]if[/color] [color=GREEN];; If the following expression returns a non-nil value[/color]
               ([color=BLUE]<[/color] n ([color=BLUE]strlen[/color] x)) [color=GREEN];; If the length of the text string is greater than the number of characters to be subtracted[/color]
               ([color=BLUE]entmod[/color] [color=GREEN];; Modify the following DXF data[/color]
                   ([color=BLUE]list[/color] [color=GREEN];; Contruct a list of DXF data[/color]
                       ([color=BLUE]cons[/color] -1 e) [color=GREEN];; Create a dotted pair with the entity to be modified (1 . <entity-name>)[/color]
                       ([color=BLUE]cons[/color] 1 [color=GREEN];; Create a dotted pair with the new string value (DXF Group 1)[/color]
                           ([color=BLUE]substr[/color] x [color=GREEN];; Return a substring of the entity text string[/color]
                               1 [color=GREEN];; from the first character[/color]
                               ([color=BLUE]-[/color] ([color=BLUE]strlen[/color] x) n) [color=GREEN];; spanning the length of the string minus the number of characters to be removed[/color]
                           ) [color=GREEN];; end substr[/color]
                       ) [color=GREEN];; end cons[/color]
                   ) [color=GREEN];; end list[/color]
               ) [color=GREEN];; end entmod[/color]
           ) [color=GREEN];; end if[/color]
       ) [color=GREEN];; end repeat[/color]
   ) [color=GREEN];; end if[/color]
   ([color=BLUE]princ[/color]) [color=GREEN];; Suppress the return of the last evaluated expression[/color]
) [color=GREEN];; end defun[/color]

Link to comment
Share on other sites

Here's a quick dissection:

 

([color=BLUE]defun[/color] c:rchar ( [color=BLUE]/[/color] e i n s x ) [color=GREEN];; define function, declare local variables[/color]
   
   ([color=BLUE]setq[/color] n 1) [color=GREEN];; Number of characters to remove from end of string[/color]
   
   ([color=BLUE]if[/color] [color=GREEN];; If the following expression returns a non-nil value[/color]
       ([color=BLUE]setq[/color] s [color=GREEN];; Assign the following value to the symbol 's'[/color]
           ([color=BLUE]ssget[/color] [color=GREEN];; Collect a selection set of[/color]
               [color=MAROON]"_:L"[/color] [color=GREEN];; Object on unlocked layers[/color]
               '((0 . [color=MAROON]"TEXT"[/color])) [color=GREEN];; whose entity type (DXF Group 0) = TEXT[/color]
           ) [color=GREEN]; end ssget[/color]
       ) [color=GREEN];; end setq[/color]
       ([color=BLUE]repeat[/color] [color=GREEN];; repeat a set of expressions a number of times[/color]
           ([color=BLUE]setq[/color] i [color=GREEN];; Assign the following value to the symbol 'i'[/color]
               ([color=BLUE]sslength[/color] s)  [color=GREEN];; Number of items in the selection set[/color]
           ) [color=GREEN];; end setq [/color]
           ([color=BLUE]setq[/color] e [color=GREEN];; Assign the following value to the symbol 'e'[/color]
               ([color=BLUE]ssname[/color] s [color=GREEN];; Retrieve the entity name at a specific index in the set[/color]
                   ([color=BLUE]setq[/color] i ([color=BLUE]1-[/color] i)) [color=GREEN];; Decrement the counter variable 'i'[/color]
               ) [color=GREEN];; end ssname[/color]
               x [color=GREEN];; Assign the following value to the symbol 'x'[/color]
               ([color=BLUE]cdr[/color] [color=GREEN];; Return the second item of the dotted pair[/color]
                   ([color=BLUE]assoc[/color] 1 [color=GREEN];; Retrieve the dotted pair with DXF Group 1 (the text string)[/color]
                       ([color=BLUE]entget[/color] e) [color=GREEN];; Retrieve the DXF data for the entity assigned to the symbol 'e'[/color]
                   ) [color=GREEN];; end assoc[/color]
               ) [color=GREEN];; end cdr[/color]
           ) [color=GREEN];; end setq[/color]
           ([color=BLUE]if[/color] [color=GREEN];; If the following expression returns a non-nil value[/color]
               ([color=BLUE]<[/color] n ([color=BLUE]strlen[/color] x)) [color=GREEN];; If the length of the text string is greater than the number of characters to be subtracted[/color]
               ([color=BLUE]entmod[/color] [color=GREEN];; Modify the following DXF data[/color]
                   ([color=BLUE]list[/color] [color=GREEN];; Contruct a list of DXF data[/color]
                       ([color=BLUE]cons[/color] -1 e) [color=GREEN];; Create a dotted pair with the entity to be modified (1 . <entity-name>)[/color]
                       ([color=BLUE]cons[/color] 1 [color=GREEN];; Create a dotted pair with the new string value (DXF Group 1)[/color]
                           ([color=BLUE]substr[/color] x [color=GREEN];; Return a substring of the entity text string[/color]
                               1 [color=GREEN];; from the first character[/color]
                               ([color=BLUE]-[/color] ([color=BLUE]strlen[/color] x) n) [color=GREEN];; spanning the length of the string minus the number of characters to be removed[/color]
                           ) [color=GREEN];; end substr[/color]
                       ) [color=GREEN];; end cons[/color]
                   ) [color=GREEN];; end list[/color]
               ) [color=GREEN];; end entmod[/color]
           ) [color=GREEN];; end if[/color]
       ) [color=GREEN];; end repeat[/color]
   ) [color=GREEN];; end if[/color]
   ([color=BLUE]princ[/color]) [color=GREEN];; Suppress the return of the last evaluated expression[/color]
) [color=GREEN];; end defun[/color]

 

THANKS! I appreciate the time you invested in that.

:D

Link to comment
Share on other sites

So the DXF codes are basically used to narrow down the accepted selection set?

 

Correct, the majority* of DXF group codes, along with special filter operators (using a -4 group code), may be used to form the filter list argument for the ssget function. For more information, consult the documentation on the ssget function.

 

*not all: you cannot filter for entity name types [DXF -1, 330, 340, 360 etc.], handles [DXF 5], or xData groups [DXF 1000 onwards]

Link to comment
Share on other sites

  • 3 weeks later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...