Author Topic: blocks to layers based on list  (Read 1481 times)

0 Members and 1 Guest are viewing this topic.

therealjd

  • Mosquito
  • Posts: 11
blocks to layers based on list
« on: July 28, 2020, 05:39:24 PM »
I need to move blocks to specified layers based on a list. either with a list i can add to within the lisp or move based on a csv file.
The block names do not match the layer name. The layers exist in the drawing already.

ie:
blockname,target layer
elbow,ag_water
tee,ag_water
hydrant,ag_hydrant

I know i can do this quick select and the properties panel, but i need to do this for possibly hundreds of different blocks.

Any help appreciated!

Jd

JohnK

  • Administrator
  • Seagull
  • Posts: 10626
Re: blocks to layers based on list
« Reply #1 on: July 28, 2020, 06:32:27 PM »
I wrote something along those lines years ago (but mine also created the layers and loaded linetypes); I will look through what little AutoLisp stuff I have left and post anything but I wouldn't get your hopes up (I honestly do not have much AutoLisp left).
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

therealjd

  • Mosquito
  • Posts: 11
Re: blocks to layers based on list
« Reply #2 on: July 28, 2020, 07:31:47 PM »
I did find some code in this group that does create a layer based on block name and moves the block to that layer. I don't know if it would be easier to modify the code to accept a csv list instead?

Code: [Select]
(defun c:Blocks->Layers ( / SubstDXFUpdate Layer ApplyFootoSelSet )
  ;; © Lee Mac 2010

  (defun SubstDXFUpdate ( code value elist )
    (if
      (setq elist
        (entmod
          (subst
            (cons code value) (assoc code elist) elist
          )
        )
      )
      (entupd (cdr (assoc -1 elist)))
    )
  )

  (defun Layer ( Name )
    (entmake
      (list
        (cons 0 "LAYER")
        (cons 100 "AcDbSymbolTableRecord")
        (cons 100 "AcDbLayerTableRecord")
        (cons 2 Name)
        (cons 70 0)
      )
    )
  )

  (defun ApplyFooToSelSet ( foo SelSet )
    (
      (lambda ( i / e )
        (if SelSet
          (while (setq e (ssname SelSet (setq i (1+ i))))
            (foo e)
          )
        )
      )
      -1
    )
  )

  (ApplyFooToSelSet
    (lambda ( e / l )
      (or (tblsearch "LAYER" (setq l (cdr (assoc 2 (entget e)))))
          (Layer l)
      )

      (SubstDXFUpdate 8 l (entget e))
    )
    (ssget "_:L" '((0 . "INSERT")))
  )

  (princ)
)

ronjonp

  • Needs a day job
  • Posts: 7527
Re: blocks to layers based on list
« Reply #3 on: July 29, 2020, 12:42:26 PM »
Try this:
Code - Auto/Visual Lisp: [Select]
  1. (defun c:foo (/ d f r s x)
  2.   (if (and (setq f (getfiled "Select CSV File" "" "csv" 16))
  3.            ;; Get Lee's code here
  4.            ;; http://www.lee-mac.com/readcsv.html
  5.            (setq d (mapcar '(lambda (x) (mapcar 'strcase x)) (lm:readcsv f)))
  6.            (setq s (ssget "_X" '((0 . "insert"))))
  7.       )
  8.     (foreach e (mapcar 'cadr (ssnamex s))
  9.       (if (setq r (cadr (assoc (strcase (vla-get-effectivename (vlax-ename->vla-object e))) d)))
  10.         (entmod (append (entget e) (list (cons 8 r))))
  11.       )
  12.     )
  13.   )
  14.   (princ)
  15. )
  16.  

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

therealjd

  • Mosquito
  • Posts: 11
Re: blocks to layers based on list
« Reply #4 on: July 29, 2020, 05:13:49 PM »
Ronjonp,

I loaded the readcsv lisp by Lee, ran your code after that. worked great!!!

Thanks so much! :)

Jd

ronjonp

  • Needs a day job
  • Posts: 7527
Re: blocks to layers based on list
« Reply #5 on: July 30, 2020, 01:58:43 PM »
Ronjonp,

I loaded the readcsv lisp by Lee, ran your code after that. worked great!!!

Thanks so much! :)

Jd
Glad to help  :-)

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC