Author Topic: Retrieve point from line command  (Read 2112 times)

0 Members and 1 Guest are viewing this topic.

kasmo

  • Newt
  • Posts: 28
Retrieve point from line command
« on: May 26, 2023, 10:28:58 AM »
Hi,

I'm trying to retrieve the point from the line command, if only one point is selected or alternatively the line entities when more than one point is selected, which works fine.
Does somebody know why this doesn't work:

Code - Auto/Visual Lisp: [Select]
  1. (command "_line")
  2. (while (> (getvar "cmdactive") 0)
  3.   (command pause)
  4.   (setq lpt2 (getvar 'lastpoint))
  5. )


ribarm

  • Gator
  • Posts: 3256
  • Marko Ribar, architect
Re: Retrieve point from line command
« Reply #1 on: May 26, 2023, 11:43:12 AM »
Because you are interrupting command functionality...
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

JohnK

  • Administrator
  • Seagull
  • Posts: 10625
Re: Retrieve point from line command
« Reply #2 on: May 26, 2023, 11:49:35 AM »
You are making it too complicated. You should be able to replace those lines with these two and get the result you are looking for.

Code - Auto/Visual Lisp: [Select]
  1. (command "_line" pause)
  2. (setq lpt2 (getvar 'lastpoint))
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

kasmo

  • Newt
  • Posts: 28
Re: Retrieve point from line command
« Reply #3 on: May 26, 2023, 12:51:28 PM »
Because you are interrupting command functionality...
Do I? I assumed the command would only end after the loop. Any ideas how to fix it?

You are making it too complicated. You should be able to replace those lines with these two and get the result you are looking for.

Code - Auto/Visual Lisp: [Select]
  1. (command "_line" pause)
  2. (setq lpt2 (getvar 'lastpoint))


Unfortunately this doesn't work for me, because the command is terminated as soon as you pick the first point.

ribarm

  • Gator
  • Posts: 3256
  • Marko Ribar, architect
Re: Retrieve point from line command
« Reply #4 on: May 26, 2023, 01:25:27 PM »
Because you are interrupting command functionality...

Why not like this :

Code - Auto/Visual Lisp: [Select]
  1. (command "_.LINE")
  2. (while (< 0 (getvar (quote cmdactive)))
  3.   (command "\\")
  4. )
  5. (setq pt (getvar (quote lastpoint)))
  6.  
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

JohnK

  • Administrator
  • Seagull
  • Posts: 10625
Re: Retrieve point from line command
« Reply #5 on: May 26, 2023, 02:16:05 PM »
Unfortunately this doesn't work for me, because the command is terminated as soon as you pick the first point.

Unfortunately, this tells me you are trying to build a function that may be trying to do too much. Are you building your functions like this (or just one giant function that does 50 different things)?

Code - Auto/Visual Lisp: [Select]
  1. (defun drawline (/)
  2.   ;; drawline
  3.   ;; Issues the line command and returns the first picked point.
  4.   (command "_line" pause)
  5.   (getvar 'lastpoint)
  6.   )
  7.  
  8. ((lambda ( / lpt2)
  9.    ;; my main function
  10.    ;;
  11.  
  12.    (setq lpt2 (drawline))                                       ; -Draw line and get the
  13.                                                                 ;  first picked point.
  14.  
  15.    ;; NOTE: Without the line below, the prompt will be make immediately,
  16.    ;; but if you want to delay the prompt, uncomment the line below.
  17.  
  18.    ; (while (eq 1 (logand 1 (getvar 'cmdactive))) (command pause))
  19.  
  20.    (princ "\nFirst Picked point of the line command is: ")      ; -prompt the first picked point
  21.    lpt2
  22.    )
  23.  )
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

kasmo

  • Newt
  • Posts: 28
Re: Retrieve point from line command
« Reply #6 on: May 26, 2023, 02:55:14 PM »

Why not like this :

Code - Auto/Visual Lisp: [Select]
  1. (command "_.LINE")
  2. (while (< 0 (getvar (quote cmdactive)))
  3.   (command "\\")
  4. )
  5. (setq pt (getvar (quote lastpoint)))
  6.  

What does this do: (command "\\")? Never seen it before.
Unfortunately this doesn't work either. It also gives me the previous last point, the one before the line command is started.

Unfortunately this doesn't work for me, because the command is terminated as soon as you pick the first point.


Unfortunately, this tells me you are trying to build a function that may be trying to do too much. Are you building your functions like this (or just one giant function that does 50 different things)?

Code - Auto/Visual Lisp: [Select]
  1. (defun drawline (/)
  2.   ;; drawline
  3.   ;; Issues the line command and returns the first picked point.
  4.   (command "_line" pause)
  5.   (getvar 'lastpoint)
  6.   )
  7.  
  8. ((lambda ( / lpt2)
  9.    ;; my main function
  10.    ;;
  11.  
  12.    (setq lpt2 (drawline))                                       ; -Draw line and get the
  13.                                                                 ;  first picked point.
  14.  
  15.    ;; NOTE: Without the line below, the prompt will be make immediately,
  16.    ;; but if you want to delay the prompt, uncomment the line below.
  17.  
  18.    ; (while (eq 1 (logand 1 (getvar 'cmdactive))) (command pause))
  19.  
  20.    (princ "\nFirst Picked point of the line command is: ")      ; -prompt the first picked point
  21.    lpt2
  22.    )
  23.  )


I'm trying to write a function that labels water lines with as little user input as possible.
Ideally, I only need to click once on each line and the label is there, correctly aligned.
I could go with getpoint instead, but I need the perpendicular snap and it's also nice to have a preview of the angle the lines will be drawn at.
It's a relatively simple lisp that doesn't require many sub functions, but there are some.
Everything works great with multiple lines, just not with a single one.

JohnK

  • Administrator
  • Seagull
  • Posts: 10625
Re: Retrieve point from line command
« Reply #7 on: May 26, 2023, 02:59:26 PM »
<snip>retrieve the point from the line command, if only one point is selected or alternatively the line entities when more than one point is selected <snip>
Wait a minute!? I just re-read you initial post.

You want to get which point from the line command (the first/last point)?
Also, you want to get the line segment(s)?

I'm confused.


"(command "\\")" means "PAUSE". Pause is one of those things like: PI (a constant) and "may not" be defined (very rare and if that were to happen, you'd have far larger problems); I prefer the gamble (to be descriptive in my programs).
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

kasmo

  • Newt
  • Posts: 28
Re: Retrieve point from line command
« Reply #8 on: May 26, 2023, 03:41:07 PM »
<snip>retrieve the point from the line command, if only one point is selected or alternatively the line entities when more than one point is selected <snip>
Wait a minute!? I just re-read you initial post.

You want to get which point from the line command (the first/last point)?
Also, you want to get the line segment(s)?

I'm confused.


"(command "\\")" means "PAUSE". Pause is one of those things like: PI (a constant) and "may not" be defined (very rare and if that were to happen, you'd have far larger problems); I prefer the gamble (to be descriptive in my programs).

Oh, I see thanks, thought it would return whatever input you give the command.
I attached a clip of what the function does, to give a better picture.
I'll probably have to think of another way to do it.
« Last Edit: May 27, 2023, 12:40:15 PM by kasmo »

kasmo

  • Newt
  • Posts: 28
Re: Retrieve point from line command
« Reply #9 on: May 27, 2023, 04:00:22 AM »
Figured it out:

Code - Auto/Visual Lisp: [Select]
  1. (setq lpt2 (getpoint "P1: "))
  2. (command "_line" lpt2)
  3. (while (< 0 (getvar (quote cmdactive)))
  4.   (command pause)
  5. )
  6.  

Pretty obvious solution after sleeping over it. Thanks for the input guys.

Grrr1337

  • Swamp Rat
  • Posts: 812
Re: Retrieve point from line command
« Reply #10 on: May 28, 2023, 01:32:13 PM »
FYI, I tried to obtain the point with reactors, but it doesn't seem to be possible as long as the ObjectAppended event is not called.
The interesting thing is that a LWPOLYLINE entity is drawn/created somewhere, but its not the same as the one which is displayed
while drawing with the LINE command. (because (cdr (assoc 10 enx)) doesn't match the picked point - not even close. )

Few dump results:

objectOpenedForModify
Code: [Select]
; With the `objectOpenedForModify` event:
; Entget: nil
; | Erased: nil
; | Readable: nil
; | ID: "1915256028368"
; | Obj: "Automation Error. Description was not provided."

objectModified
Code: [Select]
; With the `objectModified` event:
; Entget: ((-1 . <Entity name: 1d7fbf004e0>) (0 . "LWPOLYLINE") (330 . <Entity name: 1d7fbf004d0>) (5 . "28E") (100 . "AcDbEntity")
; (67 . 0) (8 . "0") (62 . 0) (6 . "ByBlock") (100 . "AcDbPolyline") (90 . 2) (70 . 1) (43 . 0.5) (38 . 0.0) (39 . 0.0)
; (10 -0.0625 0.0) (40 . 0.5) (41 . 0.5) (42 . 1.0) (91 . 0) (10 0.0625 0.0) (40 . 0.5) (41 . 0.5) (42 . 1.0) (91 . 0) (210 0.0 0.0 1.0))
; | Erased: nil
; | Readable: T
; | ID: "2027156407520"
; | Obj: #<VLA-OBJECT IAcadLWPolyline 000001d7fbccfab8>

Note: for the above results I used LM:ename->objectid and LM:hex->decstr subs to access the object, for which I get the automation error, like so:
(vla-ObjectIDToObject (vla-get-ActiveDocument (vlax-get-acad-object)) (LM:ename->objectid (last arg)))

EDIT:
Nevermind, I was able to access the object (should've used the IAcadDatabase, and not the active doc), but I still get the wrong point
Code: [Select]
; callback foo within the `objectModified` event
(setq o
  (vla-ObjectIDToObject
    (car arg) ; IAcadDatabase
    (LM:ename->objectid (last arg))
  )
)
(princ (vlax-get o 'Coordinates))

; I always get this:
(-0.0625 0.0 0.0625 0.0)


Sample code for testing:
Code - Auto/Visual Lisp: [Select]
  1. ; Retrieve point from line command
  2. ; https://www.theswamp.org/index.php?topic=58283.0
  3.  
  4. (defun C:StartReactor nil (GetpointFromLine:InitializeOrStop t) (princ) )
  5. (defun C:StopReactor nil (GetpointFromLine:InitializeOrStop nil) (princ) )
  6.  
  7.  
  8. (defun GetpointFromLine:InitializeOrStop ( b )
  9.   (foreach rtr (cdar (vlr-reactors :vlr-Command-Reactor)) (if (= "GetpointFromLine" (vlr-data rtr)) (vlr-remove rtr)) )
  10.   (foreach rtr (cdar (vlr-reactors :vlr-AcDb-Reactor)) (if (= "GetpointFromLine" (vlr-data rtr)) (vlr-remove rtr)) )
  11.   (if b (vlr-Command-Reactor "GetpointFromLine" '((:VLR-commandWillStart . GetpointFromLine:CB1))))
  12. ); defun
  13.  
  14.  
  15. ; GetpointFromLine:CB1 - callback on the 1st level
  16. (defun GetpointFromLine:CB1 ( rtr arg / v doc acad )
  17.   (cond
  18.     ( (and (listp arg) (eq (car arg) "LINE"))
  19.       (if (not (vl-some (function (lambda (x) (= "GetpointFromLine" (vlr-data rtr)) ))  (cdar (vlr-reactors :vlr-AcDb-Reactor))))
  20.         (vlr-AcDb-Reactor "GetpointFromLine" '((:VLR-objectModified . GetpointFromLine:CB2))) ; objectModified objectOpenedForModify
  21.       )
  22.     )
  23.   ); cond
  24. ); defun
  25.  
  26.  
  27. ; GetpointFromLine:CB2 - callback on a 2nd level
  28. (defun GetpointFromLine:CB2 ( rtr arg / e enx p r )
  29.   (and
  30.     (setq e (last arg))
  31.     (eq (type e) 'ENAME)
  32.     (not (vlax-erased-p e))
  33.     (setq enx (entget e))
  34.     (setq p (cdr (assoc 10 enx)))
  35.     (setq r p)
  36.   ); and
  37.   (princ "\n") (princ enx)
  38. ); defun GetpointFromLine:CB2
  39.  
« Last Edit: May 28, 2023, 01:48:14 PM by Grrr1337 »
(apply ''((a b c)(a b c))
  '(
    (( f L ) (apply 'strcat (f L)))
    (( L ) (if L (cons (chr (car L)) (f (cdr L)))))
    (72 101 108 108 111 32 87 111 114 108 100)
  )
)
vevo.bg

VovKa

  • Water Moccasin
  • Posts: 1628
  • Ukraine
Re: Retrieve point from line command
« Reply #11 on: May 28, 2023, 02:07:34 PM »
(because (cdr (assoc 10 enx)) doesn't match the picked point - not even close. )
for me your GetpointFromLine:CB2 shows the correct results

ribarm

  • Gator
  • Posts: 3256
  • Marko Ribar, architect
Re: Retrieve point from line command
« Reply #12 on: May 28, 2023, 02:31:38 PM »
Why not just without (getvar 'lastpoint)... Like this :

Code - Auto/Visual Lisp: [Select]
  1. (defun c:getlastpt ( / lstpt )
  2.   (vl-cmdf "_.LINE")
  3.   (while (< 0 (getvar 'cmdactive))
  4.     (vl-cmdf "\\")
  5.   )
  6.   (setq lstpt (cdr (assoc 11 (entget (entlast)))))
  7.   ;;; do something with lstpt ;;;
  8.   (princ lstpt)
  9. )
  10.  
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

Grrr1337

  • Swamp Rat
  • Posts: 812
Re: Retrieve point from line command
« Reply #13 on: May 28, 2023, 04:39:26 PM »
(because (cdr (assoc 10 enx)) doesn't match the picked point - not even close. )
for me your GetpointFromLine:CB2 shows the correct results

Hmm... that is interesting, for me it is always the same point - Tested multiple times and I have triple-checked for any global variables that I may have missed.



Maybe someone else to try it on his machine and give us feeback ?
(apply ''((a b c)(a b c))
  '(
    (( f L ) (apply 'strcat (f L)))
    (( L ) (if L (cons (chr (car L)) (f (cdr L)))))
    (72 101 108 108 111 32 87 111 114 108 100)
  )
)
vevo.bg

VovKa

  • Water Moccasin
  • Posts: 1628
  • Ukraine
Re: Retrieve point from line command
« Reply #14 on: May 28, 2023, 05:24:55 PM »
Hmm... that is interesting, for me it is always the same point - Tested multiple times and I have triple-checked for any global variables that I may have missed.
(setvar "DYNMODE" 0)