Jump to content

Improving workflow - Modifying LISP to search for string parts


Janouren

Recommended Posts

Good day,

I'm a student of construction engineering and recently I got student's job at a projection firm with a lot of old-school engineers and the workflow here was very slow. We get a bunch of maps with their own layers and blocks and up to this point, people here were replacing blocks&layers from these drawings one by one with our own custom blocks and layers (thousands of blocks!). So far I've learned how to implement LISPS and found

  • layertransfer to automatically change layers to our layers, with settings saved to file
  • allblockreplace LISP by alanjt to replace all selected blocks to a new replacement block

Replacing layers and blocks en-masse is my main objective.

allblockreplace increased speed of work significantly, but it's still loads of manual labour that I hope you could please help me with. It pains me to see these old engineers using their human hours to pick and replace blocks one by one and I'd also like to learn for myself how to do this.

I found this LISP that almost does the trick by Tharwat:

(defun c:Test  (/ lst ss i sn en f)
 ;;--------------------------------------------;;
 ;; Author : Tharwat . Date: 09.June.2015	;;
 ;; A function to replace a set of block names ;;
 ;; with a nother as per listed in the list	;;
 ;;--------------------------------------------;;
 (setq lst '(("old*" . "new")
             ("sad*" . "happy")
             )
       )
 (if (setq ss (ssget "_X" (list '(0 . "INSERT")
                                (cons 2 (apply 'strcat (mapcar '(lambda (x) (strcat (car x) ",")) lst))))))
   (repeat (setq i (sslength ss))
     (setq sn (ssname ss (setq i (1- i)))
           en (entget sn))
     (if (and (setq f (assoc (cdr (assoc 2 en)) lst))
              (tblsearch "BLOCK" (cdr f))
              (entmake (append (list '(0 . "INSERT") (cons 2 (cdr f)))
                (vl-remove-if-not '(lambda (v) (member (car v) '(6 8 10 41 42 43 50 62))) en)))
            )
        (entdel sn)
        )
     )
   )
 (princ)
 )

In short, I input a list of blocks that we receive with the raw drawings on the left side and I input a list of our replacement blocks to the right side. Thing is, there are blocks of the same shape in the raw drawings called 3040111,304012,304013,304014,304015 and so on... with always the first 6 numbers being the same. I tried using a wildcard - replacing "old" with "3040*" but that doesn't seem to work with LISP unfortunately. Is there a way to make "old" string search for all strings that start with for example "3040" so that I can change all 3040111,304012,304013,304014,304015 to a "new" block?

Edited by Janouren
Link to comment
Share on other sites

My apologies. The other 2 instances are replies to already existing posts, this is the only post that I've started for this issue. I tried removing the other 2, but I'm not sure if it's even possible.

Link to comment
Share on other sites

Give this a try:

(defun c:test (/ en f i lst sn ss x)
  ;;--------------------------------------------;;
  ;; Author : Tharwat . Date: 09.June.2015	;;
  ;; A function to replace a set of block names ;;
  ;; with a nother as per listed in the list	;;
  ;;--------------------------------------------;;
  (setq lst '(("3040*" . "new")))
  (if (setq ss (ssget "_X"
		      (list '(0 . "INSERT")
			    (cons 2 (apply 'strcat (mapcar '(lambda (x) (strcat (car x) ",")) lst)))
		      )
	       )
      )
    (repeat (setq i (sslength ss))
      (setq sn (ssname ss (setq i (1- i)))
	    en (entget sn)
	    n  (cdr (assoc 2 en))
      )
      (if
	(and
	  ;; RJP » 2021-04-13
	  ;; Added support for wildcards
	  (vl-some '(lambda (x) (and (wcmatch (strcase n) (strcase (car x))) (setq r (cdr x)))) lst)
	  (tblsearch "BLOCK" r)
	  (entmake
	    (append (list '(0 . "INSERT") (cons 2 r))
		    (vl-remove-if-not '(lambda (v) (member (car v) '(6 8 10 41 42 43 50 62))) en)
	    )
	  )
	)
	 (entdel sn)
      )
    )
  )
  (princ)
)

 

Edited by ronjonp
  • Thanks 1
Link to comment
Share on other sites

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