Author Topic: Question for cheese. Autolisp small program  (Read 3515 times)

0 Members and 1 Guest are viewing this topic.

mrrgoncalves

  • Mosquito
  • Posts: 8
Question for cheese. Autolisp small program
« on: December 09, 2013, 03:28:47 PM »
Hi everyone,

Must say this at first line, need your lisp experience to complete a small lisp.
My experience with autolisp is almost nill, so...be some patiente, pls.

First part:
The program starts to read attributes from a block (dynamic) with a specific "TAG" then
 get those values, get the entity name  of the block and create a list (list1) with all entities found in the drawing.
The method to select blocks is by ssget X.
Second part:
Read a file (csv), and create a list (list2) with all row data. Each line have a value and layer name separated by tab \t

Now comes the part that need to compare list1 and list2...and if the value of attibute (list1) made part of list 2, select the respective block by entity name and change the layer.

How compare two lists and execute the modifications??
Example:
List1 (("FE214" <Entity name: 7ffff93cad0>) ("FE277" <Entity name: 7ffff93c950>>))
List2 (("FE213" "LAYER1") ("FE214" "LAYER2") ("FE277" "LAYER3"))


Each list could have more than 100 sublists.

Thanks in advance.

ribarm

  • Gator
  • Posts: 3297
  • Marko Ribar, architect
Re: Question for cheese. Autolisp small program
« Reply #1 on: December 09, 2013, 05:14:25 PM »
You can iterate list1 and get TAG value, and then use vl-member-if function to check existance of TAG value in list2... If values match then entmod corresponding ename from list1 with layername from list2...
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

mrrgoncalves

  • Mosquito
  • Posts: 8
Re: Question for cheese. Autolisp small program
« Reply #2 on: December 09, 2013, 05:53:50 PM »
Code: [Select]
(defun c:aca ()
  (setq ssall (ssget "X"
     '((0 . "INSERT")
       (66 . 1)
      )
      )
  )
  (setq attlist nill)
  (setq edata nill)
    (setq lst nill)
(repeat (sslength ssall)
  (setq ent (ssname ssall 0))
  (setq ent1 (ssname ssall 0))
  (setq bname (entget ent1))
  (setq bloc (car bname))
  (setq bloc (cons (assoc 1 bname) bloc))
  (while (setq ent (entnext ent))
    (setq edata (entget ent))
    (if (= (cdr (assoc 2 edata)) "IDENT")
      (progn
     (setq attlist (cons (cdr (assoc 1 edata)) attlist))
      (setq attlist (cons (cdr (assoc -1 bname)) attlist))
)
    ) ;end if
  ) ;end while


  (ssdel (ssname ssall 0) ssall)
) ;end repeat
(defun filo ()
  (setq op (getfiled "Open file" "" "txt" 2))
  (progn
    (setq rea (open op "r"))
  )
  (setq rline (read-line rea))
  (while (setq rline (read-line rea))
    (setq flst (cons rline flst))
  )
  (close rea)
)
  (filo)


Ok, thank you, but how could make this work using vanilla... using member function with 2 lists? sorry, i dont get it how 
« Last Edit: December 09, 2013, 05:59:01 PM by mrrgoncalves »

ribarm

  • Gator
  • Posts: 3297
  • Marko Ribar, architect
Re: Question for cheese. Autolisp small program
« Reply #3 on: December 09, 2013, 07:31:19 PM »
Code: [Select]
attlist
flst

(setq attlist (cons (list (cdr (assoc 1 edata)) (cdr (assoc -1 bname))) attlist))

(foreach a attlist
  (if (vl-member-if '(lambda ( x ) (equal (car a) (car x))) flst)
    (progn
      (entmod (subst (cons 8 (cdr (assoc (car a) flst))) (assoc 8 (entget (cadr a))) (entget (cadr a))))
      (entupd (cadr a))
    )
  )
)

(vl-member-if) doesn't require (vl-load-com) so it's Vanilla function...
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: Question for cheese. Autolisp small program
« Reply #4 on: December 10, 2013, 12:33:36 AM »
Answering the OP directly without any VL functions used
Code - Auto/Visual Lisp: [Select]
  1. (defun ChangeToCSVLayers (list1 list2 / found)
  2.   (foreach item list1
  3.     (if (setq found (assoc (car item) list2))
  4.       (entmod (list (cons -1 (cadr item)) (cons 8 (cadr found))))
  5.     )
  6.   )
  7. )
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

mrrgoncalves

  • Mosquito
  • Posts: 8
Re: Question for cheese. Autolisp small program
« Reply #5 on: December 10, 2013, 06:47:21 PM »
Ok, now i could go on.
Really enjoy your solutions.
Now have just some minors adjustments to do.
Thank you irneb and ribarm

mrrgoncalves

  • Mosquito
  • Posts: 8
Re: Question for cheese. Autolisp small program
« Reply #6 on: December 11, 2013, 06:00:36 PM »
Now i m stuck again.

Run the lisp and the end have this error.
use the vl-member-if solution by ribarm

aca ; error: bad DXF group: (8 "LAYER2")

I guess i need a dotted pair list, but...


« Last Edit: December 11, 2013, 06:19:37 PM by mrrgoncalves »

gile

  • Gator
  • Posts: 2520
  • Marseille, France
Re: Question for cheese. Autolisp small program
« Reply #7 on: December 11, 2013, 06:29:54 PM »
The dot is missing in your dotted pair:
(8 . "LAYER2")
Speaking English as a French Frog

mrrgoncalves

  • Mosquito
  • Posts: 8
Re: Question for cheese. Autolisp small program
« Reply #8 on: December 11, 2013, 06:33:52 PM »
Ok,
but whats wrong with the code

(cons 8 (cdr (assoc (car a) flst)))

doesnt creat a dotted pair
 
the result is (8 "LAYER2")
but should be (8 . "LAYER2")

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Question for cheese. Autolisp small program
« Reply #9 on: December 11, 2013, 06:46:13 PM »
try using cadr to return the layer name ;  something like:

Code - Auto/Visual Lisp: [Select]
  1. (setq flst '(("FE213" "LAYER1") ("FE214" "LAYER2") ("FE277" "LAYER3")))
  2.  
  3.  
  4. (setq index "FE214")
  5.  
  6. (cons 8 (cadr (assoc index flst)))

; -> (8 . "LAYER2")


Code - Auto/Visual Lisp: [Select]
  1.  
  2.  
  3. (cdr (assoc index flst)) ;;-> ("LAYER2")
  4. (cadr (assoc index flst)) ;;-> "LAYER2"
  5.  
  6.  
« Last Edit: December 11, 2013, 06:49:19 PM by Kerry »
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.

gile

  • Gator
  • Posts: 2520
  • Marseille, France
Re: Question for cheese. Autolisp small program
« Reply #10 on: December 11, 2013, 06:47:38 PM »
(cdr (assoc (car a) flst)) should return ("LAYER2"), a list, instead of "LAYER2" a string.
IO:
- if (assoc (car a) flist) returns (x "LAYER2") a list with two items, (cdr (assoc (car a) flst)) returns the list but the first item: ("LAYER2")
- if (assoc (car a) flist) returns (x . "LAYER2") a dotted pair, (cdr (assoc (car a) flst)) returns the second itemas an atom: "LAYER2"
in the first case, to get the second item of the list, use: (cadr (assoc (car a) flst))

Kerry was faster...
Speaking English as a French Frog

mrrgoncalves

  • Mosquito
  • Posts: 8
Re: Question for cheese. Autolisp small program
« Reply #11 on: December 11, 2013, 07:08:51 PM »

Cadr was missing!! Receive now an atom instead a list and all works perfectly.

Very important for me this BIG help from you.
Thank you, it works now!