Author Topic: Entlast function not working as expected  (Read 1093 times)

0 Members and 1 Guest are viewing this topic.

Magnus

  • Mosquito
  • Posts: 1
Entlast function not working as expected
« on: December 12, 2022, 03:15:53 PM »
I am trying to create a small program where you draw a polyline, and then it uses (entget (entlast)) to get the DXF info from that polyline.

What should happen is that the DXF info should contain an x,y coordinate for each vertex in the polyline with the DXF code 10. But when I run my code, it only gives one line where the DXF code is 10. The weird thing is that when I run each line individually, it works exactly as expected. Or when I use (entget (car (entsel))) and select the polyline, it works fine.

Here is the code:

(defun c:ra ()
  (setq outlist nil) ; initiate empty list of vertex coordinates
  (command "pline" pause); draw polyline
  (setq object (entget (entlast))) ; get DXF data
  (setq count 0)
  (repeat (length object)
    (setq item (nth count object))
    (if   (= (car item) 10) ; is the item in the DXF list a vertex?
      (setq outlist (cons (cdr item) outlist)) ; add vertex to list
    )
    (setq count (+ 1 count))
  )
  (setq outlist (reverse outlist)) ; properly order list

  (setq count 0)
  (repeat (length outlist)
    (prompt "\n")(princ (nth count outlist))(princ)
    (setq count (+ 1 count))
  )
)

This code is searching through the DXF info line by line and adding each instance of a code 10 to a list, so it gets a list of vertices. But the list is only being generated with a length of 1.

Does anyone know how to fix this? Or why it's behaving differently when I run the whole code vs. running it one line at a time?

Lee Mac

  • Seagull
  • Posts: 12937
  • London, England
Re: Entlast function not working as expected
« Reply #1 on: December 12, 2022, 04:28:39 PM »
You're only pausing for a single point input - you'll need to construct some form of loop to allow the user to supply more than one point to the PLINE command, e.g.:
Code - Auto/Visual Lisp: [Select]
  1. (command "_.pline")
  2. (while (= 1 (logand 1 (getvar 'cmdactive)))
  3.     (command "\\")
  4. )
  5. (setq object (entget (entlast)))

Jim2018

  • Mosquito
  • Posts: 16
Re: Entlast function not working as expected
« Reply #2 on: December 14, 2022, 12:07:13 PM »
Thanks Lee for your countless useful answers, if you could write an explanation, for the (command "\\") I would be grateful to you

gile

  • Gator
  • Posts: 2522
  • Marseille, France
Re: Entlast function not working as expected
« Reply #3 on: December 14, 2022, 12:37:41 PM »
"\\" stands for: pause.
Code - Auto/Visual Lisp: [Select]
  1. (while (= 1 (logand 1 (getvar 'cmdactive)))
  2.     (command pause)
  3. )
Speaking English as a French Frog

Lee Mac

  • Seagull
  • Posts: 12937
  • London, England
Re: Entlast function not working as expected
« Reply #4 on: December 14, 2022, 02:26:21 PM »
Thanks Lee for your countless useful answers, if you could write an explanation, for the (command "\\") I would be grateful to you

The pause symbol evaluates to a backslash -
Code - Auto/Visual Lisp: [Select]
  1. _$ pause
  2. "\\"
(Note that the above represents a single backslash, as the backslash is an escape character in AutoLISP)

However, since pause is an unprotected symbol, I don't like to rely on it being defined in the current session and so prefer to use a literal string.

mhupp

  • Bull Frog
  • Posts: 250
Re: Entlast function not working as expected
« Reply #5 on: December 14, 2022, 03:33:09 PM »
is their any difference?

Code - Auto/Visual Lisp: [Select]
  1. (while (< 0 (getvar 'CMDACTIVE))
  2.   (command pause)
  3. )

ronjonp

  • Needs a day job
  • Posts: 7535
Re: Entlast function not working as expected
« Reply #6 on: December 15, 2022, 08:53:03 AM »
is their any difference?

Code - Auto/Visual Lisp: [Select]
  1. (while (< 0 (getvar 'CMDACTIVE))
  2.   (command pause)
  3. )
If it accidentally gets set to some other value then yes :). When using "\\" you'll always have the correct value.
(setq pause 1)

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

mhupp

  • Bull Frog
  • Posts: 250
Re: Entlast function not working as expected
« Reply #7 on: December 15, 2022, 12:01:56 PM »
sorry, I was talking more about the while evaluation.

Lee Mac

  • Seagull
  • Posts: 12937
  • London, England
Re: Entlast function not working as expected
« Reply #8 on: December 15, 2022, 02:58:56 PM »
CMDACTIVE is bit-coded; as such, I'm testing whether bit 1 is set; whereas your code is testing whether any bit is set.