Jump to content

Select Text LISP


vmuntean

Recommended Posts

Hello,

 

I am trying to get a LISP to work and I am really struggling. I start with a drawing that has several instances of a number located on it (can be anything from 001-020) and I need to replace it with another number (021-040) such that 001 becomes 021 and so on. There are only 20 possible combinations and every drawing will only have one unique number on it.

I am using the tfind lisp developed with the (tfindfun "old" "new" 1) command to replace it. There are probably better ways to do this with IF statements but I basically have 20 tfindfun commands and 19 fail and 1 works when I run the LISP. That hasn't caused me any problems so far. After all instances of the number are replaced I am able to run the rest of the commands and then my problem comes in - I need to save the file with a new name. The file names currently are of the following type:

 

text_001_text.dxf

text_002_text.dxf etc

 

I need to replace it with text_021_text.dxf, text_022_text.dxf etc. I am really struggling with selecting the 021, or storing it so that I may modify the file name. I've been looking at different threads and trying different selection commands but with absolutely no luck. I could not get ss1 to equal 021 (the number that replaced the initial value) in order to concatinate it and form the new file name.

 

(command "saveas" "dxf" "v" "2004" "" (STRCAT "c:\\test" ss1 "rest of file name") "y" )

(command "close" "n")

 

Thank you very much!

Link to comment
Share on other sites

Hope this will get you started not sure about what you exactly need though, so sorry.

This one was written for special task, but you could be use the same algorithm

;; RNZERO.LSP
;; Fatty T.O.H () 2006 * all rights removed
;; renumbering stations 
;; (partialy borrowed from
;; REGEXP.LSP
;; Copyright (c) 2004, Tony Tanzillo)
(defun C:RNZERO (/ *error* *debug* init match maxlen match_list newstr newtxt newvalue
        numadd obj_list oldtxt regex result ss)
;; error trapping function
;; based on function
;; published by Joe Burke 12/5/2005

(defun *error*  (msg)     ; create standard error handler
     (cond ((not msg))      ; normal exit, no error
    ((member msg '("Function cancelled" "quit / exit abort"))) ; escape
    ((princ (strcat "\nError: " msg))   ; display fatal error
     (cond (*debug* (vl-bt)))))    ; if in debug mode, dump backtrace
  (vl-catch-all-apply
     (function(lambda()
(vlax-release-object result))))
   (vl-catch-all-apply
     (function(lambda()
(vlax-release-object regex))))
     (vla-endundomark (vla-get-activedocument (vlax-get-acad-object)))
     )

(vla-startundomark (vla-get-activedocument (vlax-get-acad-object)))  
(if
 (not
   (vl-catch-all-error-p
     (vl-catch-all-apply
(function
  (lambda ()
    (setq regex (vlax-create-object "VBScript.RegExp")))))))
  (progn
    (vlax-put-property regex 'Global :vlax-true)
    (vlax-put-property regex 'IgnoreCase :vlax-true)
    (setq init (getstring "\Enter initial value to find <001> : "))
    (if (eq "" init)
        (setq init "001"))
    (initget 6)
    (setq numadd (getint "\nNumber to add <20> : "))
    (if (not numadd)
      (setq numadd 20))
    (setq maxlen (strlen init)
   match  "")
    (if
      (setq ss (ssget (list
   (cons 0 "TEXT")
   (cons 1
         (strcat "*"
          (repeat maxlen
     (setq match (strcat match "#")))
          "*")))))
(progn
  (setq match_list
  (mapcar 'cons
  (setq obj_list  
  (mapcar 'vlax-ename->vla-object
   (vl-remove-if
     'listp
     (mapcar 'cadr
      (ssnamex ss)
      )
     )
   )
 )
   (mapcar 'vla-get-textstring obj_list))
  )

  (while (setq obj_list (vl-remove-if-not
  (function (lambda (x)
       (wcmatch (cdr x) (strcat "*" init "*"))))
       match_list)
   )

    (vlax-put-property regex 'Pattern (strcat "(" init ")"))


    (foreach obj (mapcar 'car obj_list)
    (setq oldtxt (vla-get-textstring obj))
    (setq newvalue (+ numadd (atoi init)))
    (setq newtxt (itoa newvalue))
    (while (< (strlen newtxt) maxlen)
      (setq newtxt (strcat "0" newtxt))
      )
      (if (not (eq "0" (substr newtxt 1 1)))
   (setq newtxt (strcat "0" newtxt)))

           (princ (strcat "\n" init " ==> " newtxt));for debug only
    (setq result
    (vlax-invoke-method regex 'Execute oldtxt)
   )
    (if (> (vlax-get-property result 'Count) 0)
      (progn
 (setq newstr
        (vlax-invoke-method regex 'Replace oldtxt newtxt)
       )
 (vla-put-textstring obj newstr)
 ))
    (vlax-release-object result)

    )
(setq match_list (vl-remove-if
  (function (lambda (x)
       (member x obj_list)))
       match_list)
   )
         (setq init (1+ (atoi init))
   init (itoa init)
   )
    (while (< (strlen init) maxlen)
      (setq init (strcat "0" init))
      )

  )
    (vlax-release-object regex)
    )
  )
    )
 )
(*error* nil)
(princ)
)
(vl-load-com)
(princ "\n***   Type RNZERO to renumbering stations   ***")
(prin1)

 

 

~'J'~

Link to comment
Share on other sites

Thank you. I should clarify and simplify what I am having problems with. The logic would be:

If 021 is on the drawing Then save drawing as text_021_text.dxf

Elseif 022 is on the drawing Then save drawing as text_022_text.dxf

Elseif 023 is on the drawing Then save drawing as text_023_text.dxf

Elseif 024 and so on.

 

How would that be written using LISP syntax? Thank you!

Link to comment
Share on other sites

For a general case, the COND statement is your answer:

 

(cond
((= DrawingID 021)
 (setq DrawingName "text_021_text.dxf"))
((= DrawingID 022)
 (setq DrawingName "text_022_text.dxf"))
((= DrawingID 023)
 (setq DrawingName "text_023_text.dxf"))
)

 

But if test argument is always an integer may use:

 

(setq DrawingName (strcat "text_" (itoa DrawingID) "_text.dxf"))

 

Regards,

Link to comment
Share on other sites

I think that is what I am looking for, thank you! Just one more question please... Where the code is ((= DrawingID 021)

I would need it to look for MTEXT = "021" as the first condition, MTEXT = "022" for the second etc.

 

Here is what I was trying below but I am not having too much luck. It should probably begin with ((= MTEXT???...

(cond

((setq ss (ssget ":L" '((0 . "021,mtext"))))

(setq DrawingName "text_021_text.dxf"))))

((

 

So basically what I need it to do is

(cond

(( look for MTEXT "021" inside the drawing contents. If 021 sting found in drawing name drawing as in next line)

(setq DrawingName "text_021_text.dxf"))

(( look for MTEXT "022". If found, name drawing as in next line)

(setq DrawingName "text_022_text.dxf"))

(( etc

 

Thank you again!

 

For a general case, the COND statement is your answer:

 

(cond
((= DrawingID 021)
 (setq DrawingName "text_021_text.dxf"))
((= DrawingID 022)
 (setq DrawingName "text_022_text.dxf"))
((= DrawingID 023)
 (setq DrawingName "text_023_text.dxf"))
)

But if test argument is always an integer may use:

 

(setq DrawingName (strcat "text_" (itoa DrawingID) "_text.dxf"))

Regards,

Link to comment
Share on other sites

In this case the selection interrogation filter should look like this:

 

(ssget "X" '((0 . "MTEXT") (1 . "*201*")))

 

Take a look on Help for SSGET function selection method arguments and also on WCMATCH for the wild characters usage.

 

Regards,

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