TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: cadman6735 on June 18, 2013, 12:25:26 PM

Title: need some help with a While loop
Post by: cadman6735 on June 18, 2013, 12:25:26 PM
I got this code I am screwing around with learning how to access DXF codes and such...  Well, I want to not print the "Cancel*" and "bad argument type: lentityp nil" when the while loop ends

Code: [Select]
(defun c:test ( / )

  (setq insert (cdr (assoc 2 (entget (car (entsel))))));get the insert
  (setq blk (tblobjname "block" insert));get the block name of insert
  (setq eblk (entget blk));get the elist of the block name
  (setq e1 (cdr (assoc -2 eblk)));get the first element of the block record

  (princ "\n************************************")
  (while (not nil)
    (mapcar 'print (entget e1))
    (princ "\n************************************")
    (setq e1 (entnext e1))
  )

 
(princ)
)

If someone has a better way but keeping it beginner level super simple, I would like to see it...

Thanks
Title: Re: need some help with a While loop
Post by: ribarm on June 18, 2013, 12:55:57 PM
Try :

Code: [Select]
(defun c:test ( / )

  (setq insert (cdr (assoc 2 (entget (car (entsel))))));get the insert
  (setq blk (tblobjname "block" insert));get the block name of insert
  (setq eblk (entget blk));get the elist of the block name
  (setq e1 (cdr (assoc -2 eblk)));get the first element of the block record

  (princ "\n************************************")
  (mapcar 'print (entget e1))
  (while (setq e1 (entnext e1))
    (mapcar 'print (entget e1))
    (princ "\n************************************")
  )

 
(princ)
)
Title: Re: need some help with a While loop
Post by: cadman6735 on June 18, 2013, 01:28:06 PM
Magic,  thanks ribarm

I was so close

Thanks again...
Title: Re: need some help with a While loop
Post by: CAB on June 18, 2013, 01:31:31 PM
Another example to investigate.  8)
http://www.theswamp.org/index.php?topic=29953.0

Title: Re: need some help with a While loop
Post by: alanjt on June 18, 2013, 01:39:36 PM
Another example to investigate.  8)
http://www.theswamp.org/index.php?topic=29953.0
Cool. :kewl:
Title: Re: need some help with a While loop
Post by: cadman6735 on June 18, 2013, 01:43:37 PM
Thanks CAB...  your code is what started my journy...

http://www.theswamp.org/index.php?PHPSESSID=sdohi0uavk8gin3pivf6fbluu5&topic=1173.0

Well not really I found the above site while looking how to find block sub elements via vanilla lisp, there are a lot of examples out there with Vlisp but by finding this:

Code: [Select]
(setq data (dxf -2)) ; get first entity
I realized to look for the -2 but I could not figure how you got there so I did this:

Code: [Select]
(defun c:test ( / )

  (setq insert (cdr (assoc 2 (entget (car (entsel))))));get the insert
  (setq blk (tblobjname "block" insert));get the block name of insert
  (setq eblk (entget blk));get the elist of the block name
  (setq e1 (cdr (assoc -2 eblk)));get the first element of the block record

  (setq e2 (entnext e1))
  (setq e3 (entnext e2))
  (setq e4 (entnext e3))
 


  ;(print insert)
  ;(print blk)
  ;(mapcar 'print eblk)
  (mapcar 'print (entget e1))
  (print "************************************")
  (mapcar 'print (entget e2))
  (print "************************************")
  (mapcar 'print (entget e3))
  (print "************************************")
  (mapcar 'print (entget e4))
(princ)
)

Until I found the -2...  then I wanted to use the while and ended up here...

Long story you probably didn't care for...  But anyway your code is what helped me find the subenties of a block...
Title: Re: need some help with a While loop
Post by: CADDOG on June 19, 2013, 11:59:39 PM
If I'm wrong, and you know all this, then no biggy, just ignore me.
I love that the guys floating this site post answers to questions, instead of trying to tell you how your going about it wrong.  That said...

It almost looks as if your investigating an object to learn where data is and how to get at it.  If so, and are using VLIDE, then you can right-click your variable and choose to Watch it (after you entget at blk).  From there, you can double-click a var in the Watch Window to Inspect the item.  Another window pops up called an Inspect window.  If the object is a list, then you can right-click the most top entry in that window (above the 0), and choose to PrettyPrint.  The output is to the Immediate window (or Visual Lisp Console Window).
If run across a list item that is the entity name ex <Entity name: 7ffff6039f0>, then you keep double-clicking until you can't go any further on the entity name, and then right-click.  You can now choose to either inspect the raw data (the object the entity name represents) or to move on to the next item in the db.

I don't use PrettyPrint much, but rather stick with the Inspect dialogs.

The cool thing, is that the Watch Window tends to remember the vars you were watching, in between sessions, so it's easier to pick up where you left off.