Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Reading objects in a block definition

2 REPLIES 2
Reply
Message 1 of 3
wai1954
417 Views, 2 Replies

Reading objects in a block definition

G'day,

 

I am having some problems with trying to get the attached function to run.

 

Basically, I create a custom anonymous INSERT that contains just 3 objects - a WIPEOUT, a (LW)POLYLINE, and a (M)TEXT.  The program that creates it allows the text to be edited, so I wrote this function to check that the object that is picked is firstly an anonymous INSERT. I then create a list of the objects that make up the block. To do this I look up the BLOCK record, and then step through the objects that make up the block definition by stepping through until I reach ENDBLK. I then check to see if the list contains the 3 key objects and that there are just 3 objects in the block definition.

 

The problem is that the once it establishes it is an insert or anonymous insert, it does not clear out of the (while) loop. It compiles the list of the objects, however it crashes with the error:

 

Error: bad argument type: lentityp nil

 

This happens when it tries to execute the line

 

(setq e (entnext e))

 

in the (while loop)

 

This is the code:

 

(defun wai:bubtrue (insen / blkdata lst)
  (setq lst nil)
  (if (and (or (= (cdr (car (member (cons 100 "AcDbBlockReference") (entget insen)))) "AcDbBlockReference")
               (= (cdr (car (member (cons 100 "AcDbMInsertBlock") (entget insen)))) "AcDbMInsertBlock")
           )
           (= (substr (cdr (assoc 2 (entget insen))) 1 2) "*U")
      )      
    (progn
      (setq blkdata (tblsearch "BLOCK" (cdr (assoc 2 (entget insen)))))
      (setq e (cdr (assoc -2 blkdata)))
      (while (/= (cdr (assoc 0 (entget e))) "ENDBLK")
        (setq lst (cons (cdr (assoc 0 (entget e))) lst))
        (setq e (entnext e))
      )
      (if (and (= (length lst) 3)
               (or (= (member "TEXT" lst))
                   (= (member "MTEXT" lst))
               )
               (or (= (member "POLYLINE" lst))
                   (= (member "LWPOLYLINE" lst))
               )
               (= (member "WIPEOUT" lst))
          )   
        T
        nil
      )
    )
    nil
  )
)

 

 

And you can test it with this statement

 

(wai:bubtrue (car(entsel)))

I have attached a small drawing with some anonymous blocks, a normal block and a line so that you have something to work with.

 

Any idea as to why this is happening? It should be the ENDBLK that it picks up, instead, it crashes.

 

Thanks.

wai1954 (Ian A. White)
2 REPLIES 2
Message 2 of 3
pbejse
in reply to: wai1954

One way

 

(defun wai:bubtrue (insen / blkdata lst)
  (setq lst nil)
  (if (and (or (= (cdr (car (member (cons 100 "AcDbBlockReference") (entget insen)))) "AcDbBlockReference")
               (= (cdr (car (member (cons 100 "AcDbMInsertBlock") (entget insen)))) "AcDbMInsertBlock")
           )
           (= (substr (cdr (assoc 2 (entget insen))) 1 2) "*U")
      )      
    (progn
      (setq blkdata (tblsearch "BLOCK" (cdr (assoc 2 (entget insen)))))
      (setq e (cdr (assoc -2 blkdata))
            lst (cons (cdr (assoc 0 (entget e))) lst))
	
      (while    (setq e (entnext e))
        	(setq lst (cons (cdr (assoc 0 (entget e))) lst))
        
      )      (if (and (= (length lst) 3)
               (or (= (member "TEXT" lst))
                   (= (member "MTEXT" lst))
               )
               (or (= (member "POLYLINE" lst))
                   (= (member "LWPOLYLINE" lst))
               )
               (= (member "WIPEOUT" lst))
          )   
        T
        nil
      )
    )
    nil
  )
  lst
)

 

 

Your while loop will never evaluate to nil

This line

 

 (/= (cdr (assoc 0 (entget e))) "ENDBLK")

 

 

You can safely start creating  the lst as you're assured you are evaluating a block

 

(setq e (cdr (assoc -2 blkdata))
            lst (cons (cdr (assoc 0 (entget e))) lst));<---

Then you evaluate variable e using the while function as it will inevitably evaluates to nil via entnext

 

But like i've said, thats just one way of doing it  Smiley Happy

 

hope this helps

Message 3 of 3
wai1954
in reply to: pbejse

Thanks.

 

I will have a look at it and see how it goes.

wai1954 (Ian A. White)

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report

”Boost