Jump to content

Recommended Posts

Posted

I'm trying to figure out how to do a find/replace type routine that will increment the digits at the end. To explain I have to edit some drawing to reflect the position of devices on a circuit based on the wiring done in the field since it differs from the original drawing. This typically means I have to edit each entry by hand however the editing leaves much of the callout unchanged with only the last digits changing and those digits are always in numerical sequence. A typical callout might go like this ES-4-SP-105, with the next device being ES-4-SP-106 and so on. I can use a traditional find/replace to change the first part of the callout, however I would like to be able to simply click on an object and have it automatically replace the last digits with the next increment so instead of having to double click into object I can simply click on it and move to the next device.

My knowledge of AutoLISP is pretty minimal. I originally hoped that the find/replace command would have a command line version so I could build an increment and repeatedly call the command and have the increment added each time; however I am unable to find a way to use the find/replace feature in this manner. The main challenge for me using straight code is handling multiple objects such as blocks, Mtext and Text. Any advice, direction, or code would be appreciated.

 

Thanks in advance

Posted

Thanks for the link, however the implimentation there is in VB which I don't know at all so I can't modify the code to do the increment I'm aiming for. More so, that seems to be a standard find/replace where I need to modify it so that i can select the objects sequentially.

 

Basically I need a program that does the following:

 

1) Get the string to search for in the find/relace

2) Gets the first increment to start counting from

3) Request user input on the object to edit, then does the text swap

4) Continue to request user input after incrementing

5) Stop when the user says "stop"

 

For example, I'd start the command then type 3-EN-6-SP-?? where ?? is a wildcard. Then I'd type 51 to indicate that the first object I click should be labeled 3-EN-6-SP-51. Then I'd click a block/text/Mtext object and it would find/replace the text on that object. Then I'd click another block/text/Mtext object and it would find/relace with the string 3-EN-6-SP-52 in that object. I'd then keep clicking until I've finished the sequence and hit enter to finish the command.

Posted

I found this old lisp that might get what you're looking for.

;;;Routine for Renumbering

;;;Realization {Smirnoff}

;;;http://www.autocad.ru/cgi-bin/f1/board.cgi?t=29829Am

;;;http://www.autocad.ru/cgi-bin/f1/board.cgi?t=21807yD

;;;Edition 23.10.2006 Vladimir Azarko (VVA)

;;;http://www.autocad.ru/cgi-bin/f1/board.cgi?t=30394Ae

(defun c:renum (/ oldPref oldSuf oldStart curText curStr vlaObj keepText)

(vl-load-com)

(defun TTC_Paste(pasteStr keepText / nslLst vlaObj)

(if (setq nslLst(nentsel "\nPaste text >>"))

(progn (cond

((and (= 4(length nslLst))

(= "DIMENSION"(cdr(assoc 0(entget(car(last nslLst))))))); end and

(setq vlaObj (vlax-ename->vla-object (cdr(assoc -1(entget(car(last nslLst)))))))

(setq oldStat (vla-get-Measurement vlaObj))

(if keepText

(if (= (vla-get-TextOverride vlaObj) "")

(setq pasteStr (strcat pasteStr (rtos oldStat (vla-get-UnitsFormat vlaObj) (vla-get-PrimaryUnitsPrecision vlaObj))))

(setq pasteStr (strcat pasteStr (vla-get-TextOverride vlaObj)))))

(if (vl-catch-all-error-p(vl-catch-all-apply 'vla-put-TextOverride(list vlaObj pasteStr)))

(princ "\n Can't paste. Object may be on locked layer. "))); end condition #1

((and (= 4(length nslLst))

(= "ACAD_TABLE"(cdr(assoc 0(entget(car(last nslLst))))))); end and

(setq vlaObj (vlax-ename->vla-object(car nslLst)))

(if keepText (setq pasteStr (strcat pasteStr (vla-get-TextString vlaobj))))

(if (vl-catch-all-error-p (vl-catch-all-apply 'vla-put-TextString(list vlaObj pasteStr)))

(princ "\nError. Can't pase text. ")(entupd (car(last nslLst))))); end condition # 2

((and (= 4(length nslLst))

(= "INSERT"(cdr(assoc 0(entget(car(last nslLst))))))); end and

(princ "\nCan't paste to block's DText or MText. ")); end condition #3

((and (= 2(length nslLst))

(member(cdr(assoc 0(entget(car nslLst)))) '("TEXT" "MTEXT" "ATTRIB" "ATTDEF"))); end and

(setq vlaObj (vlax-ename->vla-object(car nslLst)))

(if keepText (setq pasteStr (strcat pasteStr (vla-get-TextString vlaobj))))

(if (vl-catch-all-error-p (vl-catch-all-apply 'vla-put-TextString(list vlaObj pasteStr)))

(princ "\nError. Can't pase text. "))); end condition #4

(T (princ "\nCan't paste. Invalid object. ")); end condition #5

); end cond

T); end progn

nil); end if

);_TTC_PASTE

(setq aDoc(vla-get-ActiveDocument(vlax-get-acad-object)))

(vla-StartUndoMark aDoc)

(if(not rnm:Pref)(setq rnm:Pref ""))(if(not rnm:Suf)(setq rnm:Suf ""))

(if(not rnm:Start)(setq rnm:Start 1))

(setq oldPref rnm:Pref oldSuf rnm:Suf oldStart rnm:Start); end setq

(setq rnm:Pref (getstring T (strcat "\nType prefix: : ")))

(if(= "" rnm:Pref)(setq rnm:Pref oldPref))(if(= " " rnm:Pref)(setq rnm:Pref ""))

(setq rnm:Suf (getstring T (strcat "\nType suffix: : ")))

(if(= "" rnm:Suf)(setq rnm:Suf oldSuf));(if(= " " rnm:Suf)(setq rnm:Suf ""))

(setq rnm:Start (getint (strcat "\nEnter start number

(itoa rnm:Start)">: ")))

(if(null rnm:Start)(setq rnm:Start oldStart))

(initget "Yes No Ä

Posted

I didn't have a lot of time to play with the renum.lsp but my preliminary attempt to use it didn't work. I wrote the following code which does what I need to do for text object.

 

However the code has some problems. Firstly, there is no error handling. The code should probably have a test to see if the object selected is a text object before running the rest of the code. Secondly, the use of the (command "change") was a work around since I couldn't figure out how to edit the objects properties directly - anyone have a resource for that? My main complaint with the way I've written it is that the collection of "" "" "" "" in the code lack transparency.

 

So without further ado, here is the code. The fuction is called "cx" because they are close on the keyboard and not used for anything else.

 

(defun c:cx() ;;Edits text objects by replacing current text with a prefix and an incremental numerical suffix

 (setq prefix (getstring "Prefix:")) ;; prompts the user for the string prefix
 (setq startint (getint "Initial number:")) ;; prompts the user for the initial number
 (setq cnt 0) ;; intializes the counter

 (while
   (PROGN
     (setq ent (entsel)) ;;requests the first text to edit
     (setq newstring (strcat prefix (itoa (+ startint cnt)))) ;;created the string
     (command "change" (car ent) "" "" "" "" "" "" newstring) ;;replace the string
     (setq cnt (1+ cnt)) ;; increment the counter
    )
  )
 )

 

Thanks for any input on improving this code. Eventually I want to be able to edit block attributes and MText with this routine, but it's working for now for my current drawings.

Posted
I didn't have a lot of time to play with the renum.lsp but my preliminary attempt to use it didn't work.

Sorry, the code I posted earlier had problems in it because I took it out of a bigger lisp routine. This is the stand alone version by ASMI.

;; ============================================================ ;;
;;                                                              ;;
;;  RENUM.LSP - This program converts TEXT, MTEXT and  ;;
;;              ATTRIBUTES in numbers with a prefix and  ;;
;;              a suffix.     ;;
;;                                                           ;;
;; ============================================================ ;;
;;                                                             ;;
;;  Command(s) to call: RENUM                                   ;;
;;                                                             ;;
;;  Specify a suffix, a prefix and starting number (for erase  ;;
;;  the old suffix or prefix you should press Spacebar). Pick  ;;
;;  to TEXT, MTEXT, ATTRIBUTES or press Esc to quit. The  ;;
;;  program remembers old properties and it is possible to  ;;
;;  confirm it pressing of Spacebar key.   ;;
;;                                                              ;;
;; ============================================================ ;;
;;                                                              ;;
;;  THIS PROGRAM AND PARTS OF IT MAY REPRODUCED BY ANY METHOD ;;
;;  ON ANY MEDIUM FOR ANY REASON. YOU CAN USE OR MODIFY THIS ;;
;;  PROGRAM OR PARTS OF IT ABSOLUTELY FREE.                  ;;
;;                                                              ;;
;;  THIS PROGRAM PROVIDES THIS PROGRAM 'AS IS' WITH ALL FAULTS ;;
;;  AND SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTY OF  ;;
;;  MERCHANTABILITY OR FITNESS FOR A PARTICULAR USE.            ;;
;;                                                             ;;
;; ============================================================ ;;
;;                                                              ;;
;;  V1.3, 12 May, 2005, Riga, Latvia                            ;;
;;  © Aleksandr Smirnov (ASMI)                           ;;
;;  For AutoCAD 2000 - 2008 (isn't tested in a next versions) ;;
;;                                                              ;;
;;                             [url]http://www.asmitools.com[/url]    ;;
;;                                                             ;;
;; ============================================================ ;;

(defun c:renum (/ oldPref oldSuf oldStart curText curStr) 
 (vl-load-com) 
 (if(not rnm:Pref)(setq rnm:Pref "")) 
 (if(not rnm:Suf)(setq rnm:Suf "")) 
 (if(not rnm:Start)(setq rnm:Start 1)) 
 (setq oldPref rnm:Pref 
       oldSuf rnm:Suf 
       oldStart rnm:Start); end setq 
 (setq rnm:Pref 
   (getstring T 
     (strcat "\nPrefix: <"rnm:Pref">: "))) 
 (if(= "" rnm:Pref)(setq rnm:Pref oldPref)) 
 (if(= " " rnm:Pref)(setq rnm:Pref "")) 
 (setq rnm:Suf 
   (getstring T 
     (strcat "\nSuffix: <"rnm:Suf">: "))) 
 (if(= "" rnm:Suf)(setq rnm:Suf oldSuf)) 
 (if(= " " rnm:Suf)(setq rnm:Suf "")) 
 (setq rnm:Start 
   (getint 
     (strcat "\nStarting number <" 
        (itoa rnm:Start)">: "))) 
 (if(null rnm:Start)(setq rnm:Start oldStart))
    (while T 
      (setq curStr(strcat rnm:Pref(itoa rnm:Start)rnm:Suf)) 
        (setq curText 
          (car 
            (nentsel
  "\n<<< Pick TEXT, MTEXT or ATTRIBUTE or press Esc to quit >>> "))) 
      (if 
        (and 
          curText 
          (member(cdr(assoc 0(entget curText)))
        '("TEXT" "MTEXT" "ATTRIB")) 
          ); end and 
        (progn
   (if
     (vl-catch-all-error-p
       (vl-catch-all-apply 'vla-put-TextString
  (list(vlax-ename->vla-object curText)curStr)))
      (princ "\n<!> Entity on locked layer <!>") 
             (setq rnm:Start(1+ rnm:Start))
     ); end if
        ); end progn 
       (princ "\n<!> This is not DText or MText <!>") 
       ); end if 
      ); end while 
  (princ) 
 ); end of c:renum
(princ
 (strcat "\n*** Text, MText or Attributes"
         " numbering tool. Type RENUM to run.*** "))
   
         

Check out ASMI's website.http://www.asmitools.com:)

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