Jump to content

Concatenating in LISP


spiker7221

Recommended Posts

I'm trying to strip the "( )" characters off the beginning and end of a string of text...

 

SSGET with "_CP" to filter for the text, then trim it down.

 

I.E.

 

2xsz = 24' ("2x8")

 

My goal is, if the final string equals 2x6, 2x8, 2x10, etc...then do something else.

 

(defun C:get2xsz (/)
(setvar "OSMODE" 0)
   (setq d6 5.0)
   (setq d7 2.0)
   (setq a6 0.0)
   (setq a7 135.0)
   (setq a8 180.0)
   (setq a9 45.0)
   (setq CNT 0)
   (setq npt6 (polar mpt (- ang1 a7) d6))
   (setq npt7 (polar mpt (+ ang1 a6) d6))
   (setq npt8 (polar mpt (- ang1 a8) d6))
   (setq npt9 (polar mpt (+ ang1 a9) d6))
       (setq ent (ssget "_CP" (list npt6 npt7 npt9 npt8)
                       '((0 . "TEXT") (8 . "A-ANNO-*")
                       )
                     );end ssget
       );end setq
   (setq LEN (sslength ent))
   (setq ent1 (ssname ent CNT))
   (setq 2xsz (cdr (assoc 1 (entget ent1))))

   (setq sz (strlen 2xsz))
   (setq sz2 (- fg 4))
   (setq f2xsz (substr 2xsz 5 sz2))
)

 

end results:

f2xsz = 2x8

 

I know the crossing window is messy, but it works good enough.

Any help would be much appreciated.

 

Thanks,

Mike in Dallas

Link to comment
Share on other sites

Here is a quick example Mike,

(defun c:stripbrackets ( / e i p q s x )
   (if (setq s (ssget "_:L" '((0 . "TEXT") (1 . "*(*)*"))))
       (repeat (setq i (sslength s))
           (setq e (entget (ssname s (setq i (1- i))))
                 x (cdr (assoc 1 e))
                 p (vl-string-position 40 x)
                 q (vl-string-position 41 x)
           )
           (entmod (subst (cons 1 (substr x (+ 2 p) (- q p 1))) (assoc 1 e) e))
       )
   )
   (princ)
)

Link to comment
Share on other sites

Without having a sample drawing to test it on... This seems to work pretty well (UNDO supported) given your example of 24' ("2x8") == 2x8:

 

(vl-load-com)

(defun c:GET2XSZ (/ *error* acDoc s i)

 (defun *error* (msg)
   (if acDoc
     (vla-endundomark acDoc)
   )
   (cond ((not msg))                                                   ; Normal exit
         ((member msg '("Function cancelled" "quit / exit abort")))    ; <esc> or (quit)
         ((princ (strcat "\n** Error: " msg " ** ")))                  ; Fatal error, display it
   )
   (princ)
 )

 (if (ssget "_:L" '((0 . "TEXT") (1 . "*(*2x*)*") (8 . "A-ANNO-*")))
   (progn
     (vla-startundomark
       (setq acDoc (vla-get-activedocument (vlax-get-acad-object)))
     )
     (vlax-for x (vla-get-activeselectionset acDoc)
       (vla-put-textstring
         x
         (substr (setq s (vla-get-textstring x))
                 (setq i (+ 3 (vl-string-search "(\"" s)))
                 (- (vl-string-search "\")" s) (1- i))
         )
       )
     )
   )
 )
 (*error* nil)
)

Link to comment
Share on other sites

  • 2 weeks later...

Good evening Lee,

Thanks so much for the great code. I modified it to strip the text down to it's last digit. Now all I need is to convert the final to a number. Then write a COND statement to capture 4,6,8,0,2...and setq the final 2x size. etc...

 

Scenerio 1:

The user needs to re-calculate a roof rafter slope length. The rafter already has a label (text) at the midpoint of the line. The user will be prompted to select the rafter to change it's slope. Then prompted to set the 2x size once again before my label routine re-creates the size label (text) showing the new length. I'm trying eliminate prompting the user for the 2x size by extracting the existing size from the line text label...I.E. (3) 20' 2x4....end results being 4,6,8,0 or 2. Then setting the 2x size variable before my label routine runs.

 

Your code give me a result of . But when I type !e, it's nil. How do I setq the final single digit to a number? I know it's simple, just been staring at it way too long at the end of the day.

 

Thanks,

Hope it makes sense.

Mike in Dallas

 

Here is a quick example Mike,

(defun c:stripbrackets ( / e i p q s x )
   (if (setq s (ssget "_:L" '((0 . "TEXT") (1 . "*(*)*"))))
       (repeat (setq i (sslength s))
           (setq e (entget (ssname s (setq i (1- i))))
                 x (cdr (assoc 1 e))
                 p (vl-string-position 40 x)
                 q (vl-string-position 41 x)
           )
           (entmod (subst (cons 1 (substr x (+ 2 p) (- q p 1))) (assoc 1 e) e))
       )
   )
   (princ)
)

Link to comment
Share on other sites

Your code give me a result of . But when I type !e, it's nil.

 

My program should not be returning any value, since the closing (princ) will return a null symbol for a 'clean' exit. Furthermore, all variables have been declared local to the program, hence their scope is limited to the program definition.

 

How do I setq the final single digit to a number? I know it's simple, just been staring at it way too long at the end of the day.

 

Here is a simple function to return the last number in a supplied string:

(defun finaldigit ( str )
   (vl-some '(lambda ( x ) (if (< 47 x 58) (atoi (chr x)))) (reverse (vl-string->list str)))
)

Example:

_$ (finaldigit "2xsz = 24' (\"2x8\")")
8

Link to comment
Share on other sites

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...