Author Topic: using lisp command transparently  (Read 10854 times)

0 Members and 1 Guest are viewing this topic.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
using lisp command transparently
« on: July 06, 2010, 12:56:15 AM »
Ever had this happen ??
Quote
Command:
Select Start Point (ENTER to QUIT) :
Select End Point: 's

Can't reenter LISP.
Invalid point.


Select End Point:
Quite often you may need to set the SNAPANG in the middle of a lisp routine. .. well I often do anyway.

Using a  transparent command such as 'Snapang in the middle of a routine when prompted for points works fine

.. except it a pain typing the command
SO, defining SA  as an alias for SNAPANG in your pgp solves that.

Except : SNAPANG only accepts selected points ( a pain to select OSNAPS for sometimes ) or a typed angle value  (which you may not remember, or get jumbled if you're a little dyslectic )


So a solution that works for me is to define a function ( not a c: command) to select an object and have the routine change SNAPANG to suit the object.

Then, use  (vlax-add-cmd  with the  ACRX_CMD_TRANSPARENT flag to register the command.

Here's the code :

Code: [Select]


;;;----------------------------------------------------------------------------
;;;----------------------------------------------------------------------------
;;;*  Snap to entity.
;;; codehimbelonga kdub
;;;* (KDUB:SnapTo)
(defun kdub:snapto (/ ent elist i li e1 e2 ang tmp)
  (if (setq ent
             (entsel
               (strcat
                 "\nSNAP TO line,pline,text,insert ... select object OR ENTER for <"
                 (angtos (getvar "SNAPANG") 0 4)
                 ">:"
               )
             )
      )
    (progn (setq ename (car ent)             ; (type (car ent))
                 elist (entget ename)
           )
           (cond ((= (cdr (assoc 0 elist)) "POLYLINE")
                  (setq e1 (entget (entnext ename))
                        e2 (entget (entnext (cdr (assoc -1 e1))))
                  )
                  (setq ang (angle (trans (cdr (assoc 10 e1)) 0 1)
                                   (trans (cdr (assoc 10 e2)) 0 1)
                            )
                  )
                 )
                 ((= (cdr (assoc 0 elist)) "LWPOLYLINE")
                  (setq i   (fix (vlax-curve-getparamatpoint
                                   ename
                                   (vlax-curve-getclosestpointto ename (osnap (cadr ent) "nea"))
                                 )
                            )
                        ang (angle (trans (vlax-curve-getpointatparam ename i) 0 1)
                                   (trans (vlax-curve-getpointatparam ename (1+ i)) 0 1)
                            )
                  )
                 )
                 ((= (cdr (assoc 0 elist)) "LINE")
                  (setq ang (angle (trans (cdr (assoc 10 elist)) 0 1)
                                   (trans (cdr (assoc 11 elist)) 0 1)
                            )
                  )
                 )
                 ((member (cdr (assoc 0 elist)) '("TEXT" "MTEXT" "INSERT"))
                  (setq ang (cdr (assoc 50 elist)))
                 )
           )
           (if ang
             ;;(setvar "SNAPANG" (kdub:rtd ang))      
             (setvar "SNAPANG" ang)
           )
    )
  )
  (prompt (strcat "\n Snap Angle set to "
                  (angtos (getvar "SNAPANG") 0 4)
                  " Degrees\n"
          )
  )
  ;;(PRINC)
)




;;;------------------------------------------------------------------
;;;------------------------------------------------------------------


(vlax-add-cmd "saa" 'kdub:snapto  "saa"  ACRX_CMD_TRANSPARENT )

NOTE : change  the 'global-name' "SAA" to whatever suits you.
jsyn,  I used saa because the letters are close together on the keyboard, didn't conflict with any other command name
and is sort of an acronym for SnapAngle to Aligned  ... works for me :)


Then, in the middle of a lisp routine you can enter 'SAA to transparently select an entity and have the SNAPANG value change to suit.
The command can also be used normally at the command line.

No more stopping commands in the middle  ....







« Last Edit: July 06, 2010, 03:27:25 AM by Kerry Brown »
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: using lisp command transparently
« Reply #1 on: July 06, 2010, 07:44:04 AM »
Nice one Kerry.  8-)

Does that work for ACAD2000 to 2011?
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: using lisp command transparently
« Reply #2 on: July 06, 2010, 07:51:13 AM »
Nice one Kerry.  8-)

Does that work for ACAD2000 to 2011?


Quote from: Daniel&Chuck
Have not yet conformed to Rule #1749-5 of the programmers handbook

Hi Alan
Can't test further back than 2010, 2011 at the moment.
Hopefully we'll get enough testers here to be able to tell by the end of the week :-)


kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

alanjt

  • Needs a day job
  • Posts: 5352
  • Standby for witty remark...
Re: using lisp command transparently
« Reply #3 on: July 06, 2010, 08:22:50 AM »
Kerry, I could kiss you. That's awesome! I've been wondering if it was possible to do this. How did you ever come across: (vlax-add-cmd "saa" 'kdub:snapto  "saa"  ACRX_CMD_TRANSPARENT )?

Excellent work. A+

Oh, and it works in C3D 2009.
Civil 3D 2019 ~ Windohz 7 64bit
Dropbox

hermanm

  • Guest
Re: using lisp command transparently
« Reply #4 on: July 06, 2010, 09:36:00 AM »
Clever.
looks like bits 2 & 4 of cmd-flags could be useful as well.
Thx, Kerry.
:)

Lee Mac

  • Seagull
  • Posts: 12905
  • London, England
Re: using lisp command transparently
« Reply #5 on: July 06, 2010, 10:11:45 AM »
That's a new one for me - thanks Kerry  :wink:

VovKa

  • Water Moccasin
  • Posts: 1626
  • Ukraine
Re: using lisp command transparently
« Reply #6 on: July 06, 2010, 11:15:38 AM »
what's the difference between
Code: [Select]
(vlax-add-cmd "saa" 'kdub:snapto  "saa"  ACRX_CMD_TRANSPARENT )
and
Code: [Select]
(defun C:saa () (kdub:snapto))

alanjt

  • Needs a day job
  • Posts: 5352
  • Standby for witty remark...
Re: using lisp command transparently
« Reply #7 on: July 06, 2010, 11:17:54 AM »
what's the difference between
Code: [Select]
(vlax-add-cmd "saa" 'kdub:snapto  "saa"  ACRX_CMD_TRANSPARENT )
and
Code: [Select]
(defun C:saa () (kdub:snapto))
Try executing the latter transparently within an active LISP routine.
Civil 3D 2019 ~ Windohz 7 64bit
Dropbox

VovKa

  • Water Moccasin
  • Posts: 1626
  • Ukraine
Re: using lisp command transparently
« Reply #8 on: July 06, 2010, 11:26:37 AM »
Try executing the latter transparently within an active LISP routine.
aaha, now i see, thanx

Crank

  • Water Moccasin
  • Posts: 1503
Re: using lisp command transparently
« Reply #9 on: July 06, 2010, 01:45:19 PM »
Is it possible to pass a return value? For instance in this example:
Code: [Select]
(defun rc (/ p1 p2 p3 p4 a g om)
(setq om (getvar "OSMODE"))(setvar "OSMODE" (boole 6 (getvar "OSMODE") 16384));(setvar "OSMODE" 0)
(setq p1 (getpoint "\nSelect objects to stretch by (rotated) crossing..."))

(princ "\nOther corner...")
(while (not a)
(setq g (grread T) p3 (cadr g))
(cond ((= (car g) 3) ; PICK BUTTON?
(setq a T)
(redraw)
)
((= (car g) 5) ; POSITIE?
(setq p2 (list (car p1)(cadr p3)))
(setq p4 (list (car p3)(cadr p1)))
(redraw)(grvecs (list -256 p1 p2 p2 p3 p3 p4 p4 p1))
)
)
)
(setvar "OSMODE" om)
(ssget "CP" (list p1 p2 p3 p4))
)
(vlax-add-cmd "RC" 'rc  "RC"  ACRX_CMD_TRANSPARENT )
Quote
.stretch (rc)
works, but
Quote
.stretch 'rc
doesn't.
Vault Professional 2023     +     AEC Collection

JohnK

  • Administrator
  • Seagull
  • Posts: 10603
Re: using lisp command transparently
« Reply #10 on: July 06, 2010, 02:15:31 PM »
I recommend using the bit flags because the modes are...ummm...`bit flags' like osnaps are.
In other words: this would the proper syntax:
Code: [Select]
(vlax-add-cmd "saa" 'kdub:snapto "saa" 1)
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: using lisp command transparently
« Reply #11 on: July 06, 2010, 08:17:13 PM »
I recommend using the bit flags because the modes are...ummm...`bit flags' like osnaps are.
In other words: this would the proper syntax:
Code: [Select]
(vlax-add-cmd "saa" 'kdub:snapto "saa" 1)

I'd call it a "Magic Number" syntax rather than "proper" syntax . 
even if the code viewer DID know that 1 represented and had the same value as ACRX_CMD_TRANSPARENT

:-)
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: using lisp command transparently
« Reply #12 on: July 06, 2010, 08:49:19 PM »
< ... > How did you ever come across: (vlax-add-cmd "saa" 'kdub:snapto  "saa"  ACRX_CMD_TRANSPARENT )?

Excellent work. A+

Oh, and it works in C3D 2009.

How?
Necessity and an old memory Alan.
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

highflyingbird

  • Bull Frog
  • Posts: 415
  • Later equals never.
Re: using lisp command transparently
« Reply #13 on: July 07, 2010, 07:02:44 AM »
Ever had this happen ??
Code: [Select]


;;;----------------------------------------------------------------------------
;;;----------------------------------------------------------------------------
;;;*  Snap to entity.
;;; codehimbelonga kdub
;;;* (KDUB:SnapTo)
(defun kdub:snapto (/ ent elist i li e1 e2 ang tmp)
  (if (setq ent
             (entsel
               (strcat
                 "\nSNAP TO line,pline,text,insert ... select object OR ENTER for <"
                 (angtos (getvar "SNAPANG") 0 4)
                 ">:"
...
very good routine! I added some for  restoring the variale of "SNAPANG",then it's more convenient.
Code: [Select]
;;;Creat a command reactor
(defun Creat-Command-Reactor ()
  (if (not *CommandReactor*)
    (setq *CommandReactor*
   (vlr-command-reactor
     nil
     '((:vlr-commandwillstart . CMD-Start)
       (:vlr-commandended . CMD-Ended)
      )
   )
    );setq
  );if
)
;;;Save the current SNAPANG
(defun CMD-start (reactor command-list / )
  (if (not (member "SAA" command-list))
    (setvar "USERR1" (getvar "SNAPANG"))
  )
  (princ)
)
;;;Restore the current SNAPANG
(defun CMD-Ended (reactor command-list / )
  (if (not (member "SAA" command-list))
    (setvar "SNAPANG" (getvar "USERR1"))
  )
  (princ)
)
(Creat-Command-Reactor)
« Last Edit: July 07, 2010, 11:24:51 PM by highflybird »
I am a bilingualist,Chinese and Chinglish.

JohnK

  • Administrator
  • Seagull
  • Posts: 10603
Re: using lisp command transparently
« Reply #14 on: July 07, 2010, 09:23:24 AM »
I recommend using the bit flags because the modes are...ummm...`bit flags' like osnaps are.
In other words: this would the proper syntax:
Code: [Select]
(vlax-add-cmd "saa" 'kdub:snapto "saa" 1)

I'd call it a "Magic Number" syntax rather than "proper" syntax . 
even if the code viewer DID know that 1 represented and had the same value as ACRX_CMD_TRANSPARENT

:-)


*lol* Cool with me. Its your code not mine.

Quick, what's osmode 247 ?

:P

*** EDIT ***
Ok, ok. I'll try to be less cryptic and/or brief:
My point is: at some point, who cares if they dont know what it is...its their responsibility to know this stuff not yours to hold their hand through everything. If there is a bit flag involved then chances are that there could be some math involved. I say just add a comment and move on.
Code: [Select]
( (lambda ( / )
   ;; all my fancy code
   
   (defun ZX ()
    (vl-load-com)
    (vla-ZoomPickWindow
     (vlax-get-acad-object)) )

   ;; more fancy code
   
   (vlax-add-cmd "ZX" (function ZX) "ZX" 1)
   ;; 1 = ACRX_CMD_TRANSPARENT
  )
)

But, either way its cool with me (I'm not the lisp police)

:)
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org