Author Topic: A Better Code ?  (Read 1002 times)

0 Members and 1 Guest are viewing this topic.

WolfTempler

  • Mosquito
  • Posts: 2
A Better Code ?
« on: September 22, 2022, 07:51:35 AM »
Hello there,

i wrote a Code to check, if the Block have attributes and when not select a other one.
Just want to now if there is a better why.

Code: [Select]
(defun Multipass (/ blk ans)
  (while
    (= nil
       (Assoc 66
      (entget
(setq
  blk (ssname
(Progn
  (while
    (if (Setq blk (ssget "_:S" '((0 . "INSERT"))))
      nil
      T
      )
    )
  blk
  )
0
)
  )
)
      )
       )
    )
  (setq ans (attget->user blk)
)
 

  )

mhupp

  • Bull Frog
  • Posts: 250
Re: A Better Code ?
« Reply #1 on: September 22, 2022, 10:13:53 AM »
This uses a passed entity name to check
Code - Auto/Visual Lisp: [Select]
  1. (defun Multipass (blk / )
  2.   (if (eq (vla-get-hasattributes (vlax-ename->vla-object blk)) :vlax-true)
  3.     (prompt "\nYes")
  4.     (prompt "\nNo")
  5.   )
  6.   (princ)
  7. )

Tharwat

  • Swamp Rat
  • Posts: 710
  • Hypersensitive
Re: A Better Code ?
« Reply #2 on: September 22, 2022, 10:16:44 AM »
Just use the ssget function with the correct filter and it won't allow you to select anything else.
Code - Auto/Visual Lisp: [Select]
  1. (Setq blk (ssget "_:S" '((0 . "INSERT")(66 . 1))))
  2.  

WolfTempler

  • Mosquito
  • Posts: 2
Re: A Better Code ?
« Reply #3 on: September 27, 2022, 09:28:15 AM »
Just use the ssget function with the correct filter and it won't allow you to select anything else.
Code - Auto/Visual Lisp: [Select]
  1. (Setq blk (ssget "_:S" '((0 . "INSERT")(66 . 1))))
  2.  
This was my first attempt. The problem is that _:S returns nil if you select somthing wrong ( Or a sysvar is wrong). But i want to force the user for a right singel selection.
But i got hte Code smaller:
Code: [Select]
(defun Multipass (/ blk ans)
  (setq
    blk
     (progn
       (While (= nil
(setq blk (ssget "_:S" '((0 . "Insert") (66 . 1))))
)
)
       blk
       )
    )
  ans
  (attget->user blk)
  )

Tharwat

  • Swamp Rat
  • Posts: 710
  • Hypersensitive
Re: A Better Code ?
« Reply #4 on: September 27, 2022, 09:45:18 AM »
Something like this ?
Code - Auto/Visual Lisp: [Select]
  1. (princ "\nSelect Attributed block : ")
  2. (while (and (not f)
  3.             (setq s (ssget "_:S"))
  4.             )
  5.   (cond ((not (= "INSERT" (cdr (assoc 0 (setq e (entget (ssname s 0)))))))
  6.          (alert "Invlaid object. Please select attribted block")
  7.          )
  8.         ((zerop (cdr (assoc 66 e)))
  9.          (alert "Selected block is not attribted")
  10.          )
  11.         (t (setq f t))
  12.         )
  13.   )
  14.