TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: therealjd on July 28, 2020, 05:39:24 PM

Title: blocks to layers based on list
Post by: therealjd 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
Title: Re: blocks to layers based on list
Post by: JohnK 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).
Title: Re: blocks to layers based on list
Post by: therealjd 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)
)
Title: Re: blocks to layers based on list
Post by: ronjonp 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.  
Title: Re: blocks to layers based on list
Post by: therealjd 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
Title: Re: blocks to layers based on list
Post by: ronjonp 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  :-)