Author Topic: Need a lisp to replace blocks  (Read 3776 times)

0 Members and 1 Guest are viewing this topic.

mtubbs

  • Guest
Need a lisp to replace blocks
« on: February 27, 2009, 05:01:51 PM »
I need a lisp that will  look for blocks "A, B, C, D...etc", and replace them with blocks "1, 2, 3, 4...etc.". So..."A" is replaced by "1", "B" is replaced by "2", and so on. It needs to maintain the blocks rotation, layer, and so on. All of these have attributes, but the attributes between each set is the same..("A"' attribute is identical to "1"'s attribute). I have a solution that works with a single selection by "ENTSEL". But I need an automated solution that replaces all instances of each in the drawing. I tried to call (c:-blockreplace) into the lisp, but i couldn't specify the entry data automatically. Help!

This is what I have so far:
Code: [Select]
;Replace blocks
;
(defun c:rb (bss / n lst)
   (if (tblsearch "block" "ID_KEY2")
      (progn
         (setq bss (ssget "x" '((2 . "ID_KEY2")(410 . "Model")))))
         (if (/= bss nil)
            (repeat (setq n (sslength bss))
               (setq n (1- n)
                         lst (cons (ssname bss n) lst)
                );end setq
             );end repeat
          );end if
         (foreach n lst (subst(cons 2 "ID_KEY2-E")(assoc 2 n) n))
       );end progn
    );end if
);end defun


ronjonp

  • Needs a day job
  • Posts: 7529
Re: Need a lisp to replace blocks
« Reply #1 on: February 27, 2009, 05:06:47 PM »
Could you not just redefine the block definition?

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Need a lisp to replace blocks
« Reply #2 on: February 27, 2009, 05:08:11 PM »
If nothing but geometry changes, then you can just change the dxf code for the name of it.
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

ronjonp

  • Needs a day job
  • Posts: 7529
Re: Need a lisp to replace blocks
« Reply #3 on: February 27, 2009, 05:26:39 PM »
This code is not complete but something to mull over...let me know if you need further help.

Code: [Select]
(defun c:rb (/ bss el lst n name)
  (setq n -1)
  (if (setq bss (ssget "x" '((0 . "INSERT") (2 . "A,B,C,D") (410 . "Model"))))
    (progn (repeat (sslength bss) (setq lst (cons (ssname bss (setq n (1+ n))) lst)))
           (foreach n lst
             (setq el   (entget n)
                   name (cdr (assoc 2 el))
             )
             (cond ((= name "A") (do something))
                   ((= name "B") (do something))
                   ((= name "C") (do something))
                   ((= name "D") (do something))
             )
           )
    )
  )
)

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

Bob Wahr

  • Guest
Re: Need a lisp to replace blocks
« Reply #4 on: February 27, 2009, 08:09:44 PM »
Code: [Select]
(defun c:bobit ()
 (command ".-rename" "b" "a" "1")
 (command ".-insert" "1=" nil)
 (command ".-rename" "b" "b" "2")
 (command ".-insert" "2=" nil)
 (command ".-rename" "b" "c" "3")
 (command ".-insert" "3=" nil)
 (command ".-rename" "b" "d" "4")
 (command ".-insert" "4=" nil)
 (command ".-purge" "b" "*" "n")
)
Elegance?  Proper coding?  I know not these words.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Need a lisp to replace blocks
« Reply #5 on: February 27, 2009, 08:16:15 PM »


form follows function ... there IS elegance in integrity.

... besides, your naming convention wins points full in my estimation. :)






kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Andrea

  • Water Moccasin
  • Posts: 2372
Re: Need a lisp to replace blocks
« Reply #6 on: February 27, 2009, 08:16:53 PM »
check here

By the Way...MTUBBS...
Welcome to theSwamp !  ;-)
« Last Edit: February 27, 2009, 08:26:13 PM by Andrea »
Keep smile...

Bob Wahr

  • Guest
Re: Need a lisp to replace blocks
« Reply #7 on: February 27, 2009, 08:19:31 PM »


... besides, your naming convention wins points full in my estimation. :)
Well, it is just a snippet.