Author Topic: transparent lisp command  (Read 15641 times)

0 Members and 1 Guest are viewing this topic.

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1453
  • Marco
Re: transparent lisp command
« Reply #30 on: May 13, 2016, 06:09:57 PM »
Have you tried opening a 2nd drawing in the same session and testing if the command still works? That's the problem is it doesn't work if you open a 2nd drawing in the same session.

If the OP just wanted a button I don't think it would be an issue. I was under the impression that he also wanted a transparent command that you can use with ( ' ) in front of it. The whole point of my replies is that there used to be a working method for this - but it doesn't work anymore. (vlax-add-cmd) does not want to function in MDI mode.
See #1 on previous post


Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1453
  • Marco
Re: transparent lisp command
« Reply #31 on: May 13, 2016, 06:40:25 PM »
This version works AutoCAD/Bricscad command line or Lisp, nesting more transparent commands only in Lisp+AutoCAD:
Code: [Select]
(defun ALE_TR ( / obj)
  (setq obj (vla-get-ActiveDocument (vlax-get-acad-object)))
  (if (eq 3 (getvar 'CMDACTIVE))
    (vla-SendCommand obj "!(ALE_Track \"ACTIVE\")\n")
    (vla-SendCommand obj "(ALE_Track nil)\n")
  )
  (vlax-release-object obj)
)
The only problem in Bricscad is vlax-remove-cmd get error if cmd is not definited also with vl-catch-all-apply:
(vl-catch-all-apply 'vlax-remove-cmd (list "tr"))

Is too late in Italy, good nigth.  :-)

kdub_nz

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 2138
  • class keyThumper<T>:ILazy<T>
Re: transparent lisp command
« Reply #32 on: May 13, 2016, 10:36:44 PM »
Hi Marc'Antonio,

Can you confirm you are able to
open a new drawing, load your routines, run the commands.
then,
open a new drawing in the same session and load the necessary files, then run the commands.
then,
switch to the first drawing in the session and run the commands.

ie: typical MDI operations.

if you can do this without error could you advise the load sequence/procedure for the files you are using (into the initial and subsequent drawings) please.

Are you compiling as VLX ? if so what are you compiling and with which switches ?


I'm ignoring for the moment that you are using a 'command' function in your code.
From the Help docs for vlax-add-cmd :

Caution: You cannot use the command function call in a transparently defined vlax-add-cmd function. Doing so can cause AutoCAD to close unexpectedly.


Thanks
Regards,
Called Kerry in my other life
Retired; but they dragged me back in !

I live at UTC + 13.00

---
some people complain about loading the dishwasher.
Sometimes the question is more important than the answer.

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1453
  • Marco
Re: transparent lisp command
« Reply #33 on: May 14, 2016, 01:33:13 AM »
Hi Marc'Antonio, can you confirm you are able to open a new drawing, load your routines, run the commands.
Yes.

Quote
then, open a new drawing in the same session and load the necessary files, then run the commands.
Yes.

Quote
then, switch to the first drawing in the session and run the commands. ie: typical MDI operations.
if you can do this without error could you advise the load sequence/procedure for the files you are using (into the initial and subsequent drawings) please.
Yes, but, at this moment, ONLY if I reload ALE_AddCommand.lsp (see below).

Quote
Are you compiling as VLX ? if so what are you compiling and with which switches?
Sorry, not tested.  :-(

Code: [Select]
; ALE_AddCommand.lsp
;
(vlax-remove-cmd "tr")
(vlax-add-cmd "tr" 'ALE_TR  "tr" 1)
;
(defun ALE_TR ( / obj) ; version 1.00
  (setq obj (vla-get-ActiveDocument (vlax-get-acad-object)))
  (if (eq 3 (getvar 'CMDACTIVE))
    (vla-SendCommand obj "!(ALE_Track \"ACTIVE\")\n")
    (vla-SendCommand obj "(ALE_Track nil)\n")
  )
  (vlax-release-object obj)
)

kdub_nz

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 2138
  • class keyThumper<T>:ILazy<T>
Re: transparent lisp command
« Reply #34 on: May 14, 2016, 02:02:59 AM »

Thanks,
So you haven't solved the problem. Having to reload the file each time we access a drawing that we've accessed previously in the same session is not an 'ideal' situation.

Thanks again for the time and effort.

Called Kerry in my other life
Retired; but they dragged me back in !

I live at UTC + 13.00

---
some people complain about loading the dishwasher.
Sometimes the question is more important than the answer.

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1453
  • Marco
Re: transparent lisp command
« Reply #35 on: May 14, 2016, 02:18:36 AM »

Thanks,
So you haven't solved the problem. Having to reload the file each time we access a drawing that we've accessed previously in the same session is not an 'ideal' situation.

Thanks again for the time and effort.
Perhaps it is a step more toward the solution... Until now only in Bricscad works, any ideas?  :idea:

Code: [Select]
; ALE_AddCommand.lsp ; version 1.01 - modified for Bricscad
;
; AutoCAD : need to be reloaded if switch to existing drawing where is already loaded
;
; Bricscad: does not need to be reloaded
;
;
(if (= (getvar "PRODUCT") "Bricscad")
  (vlax-add-cmd "tr" 'ALE_TR  "tr" 1)
  (progn
    (vlax-remove-cmd "tr")
    (vlax-add-cmd "tr" 'ALE_TR  "tr" 1)
  )
)
;
(defun ALE_TR ( / obj) ; version 1.00
  (setq obj (vla-get-ActiveDocument (vlax-get-acad-object)))
  (if (eq 3 (getvar 'CMDACTIVE))
    (vla-SendCommand obj "!(ALE_Track \"ACTIVE\")\n")
    (vla-SendCommand obj "(ALE_Track nil)\n")
  )
  (vlax-release-object obj)
)

kdub_nz

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 2138
  • class keyThumper<T>:ILazy<T>
Re: transparent lisp command
« Reply #36 on: May 14, 2016, 02:33:29 AM »
Just to poke a stick in the sand for posterity ...

\\kwb_Archive\e_develop\_INFO\_Storage\Vital_LISP_Help_3x\Apps\Vital_LISP\VILL30\SAMPLES\RCTR-TST.lsp
Tuesday, ‎July ‎1, ‎1997, ‏‎07:40:42

That is from Vital Lisp 3.0 before AutoDesk purchased it from Basis.

It worked then ... perhaps :)

Code - Auto/Visual Lisp: [Select]
  1. ;;;;;;; start-up initialisation
  2.  
  3. ;;; Prevent code re-loading
  4. (setq *VILL-NEW-FULL-INIT* NIL)
  5.  
  6. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  7. (vlax-add-cmd "rctr-tst" 'rctr-tst)
  8.  
  9.  
  10. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; register application
  11.  
  12. (vlax-regapp
  13.   (strcat "SOFTWARE\\Basis Software\\Vital Lisp\\"
  14.           (_vill-version)
  15.           "\\rctr-tst" )
  16.   '("rctr-tst")
  17.   13
  18.   )
  19.  
  20. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;; print information
  21.  
  22. (princ "\n; Type RCTR-TST to start test\n")
  23.  
  24. ;;EOF
  25.  
  26.  
Called Kerry in my other life
Retired; but they dragged me back in !

I live at UTC + 13.00

---
some people complain about loading the dishwasher.
Sometimes the question is more important than the answer.

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1453
  • Marco
Re: transparent lisp command
« Reply #37 on: May 14, 2016, 05:41:52 AM »
Thanks,
So you haven't solved the problem. Having to reload the file each time we access a drawing that we've accessed previously in the same session is not an 'ideal' situation.
Thanks again for the time and effort.
I do not really like reactors but this seems to work (see attachments):
Code: [Select]
(and
  (/= (getvar "PRODUCT") "Bricscad")
  (not #DocumentReactor)
  (setq #DocumentReactor
    (vlr-docmanager-reactor
       nil
     '(
        (:vlr-documenttobeactivated . ALE_LoadAddCommand)
        (:vlr-documenttobedestroyed . ALE_CleanReactor)
      )
    )
  )
)
;
(defun ALE_LoadAddCommand (r d)
; (print ">>Loading ALE_AddCommand<<")
  (load "Z:/Temp/ALE_AddCommand.lsp");modify this
  (princ)
)
(defun ALE_CleanReactor (r d)
  (mapcar 'VLR-remove (list #DocumentReactor))
  (setq #DocumentReactor nil)
  (princ)
)

PKENEWELL

  • Bull Frog
  • Posts: 318
Re: transparent lisp command
« Reply #38 on: May 16, 2016, 10:36:00 AM »
I do not really like reactors but this seems to work (see attachments):

Marc'Antonio,

WOW! I applied your method to my original functions i.e. removing the commands with (vlax-remove-cmd) before redefining them with (vlax-add-cmd) within the ":vlr-documenttobeactivated" reactor and IT WORKS! What I cannot understand is I have tried to employ this previously, but from within the same (mapcar) loop and I caused an error. Apparently it seems to work if the (mapcar) loops are separated.

THANK YOU!!!  :-D

So here's my modified code from the original method:

Code - Auto/Visual Lisp: [Select]
  1. ;|==============================================================================
  2.   Function Name: (pjk-AddMDICmd)
  3.   Arguments: <Command List> = List of lists in the format (list (list "<Global Command Name>" '<Function Name> "[Local Command Name]" [Cmd-Flags]))
  4.                 Sub-list Arguments:
  5.                 <Global Command Name> = String for the Name of the Command
  6.                 <Function Name> = Quoted symbol for the autolisp function to run
  7.                 [Local Command Name] = String (opt.) for the Name of the command - usually same as global name
  8.                 [Cmd-Flags] = Integer Bit Flag for Command Behaivior - Bits are as follows:
  9.                    0 = ACRX_CMD_MODAL; Command cannot be invoked while another command is active.
  10.                    1 = ACRX_CMD_TRANSPARENT; Command can be invoked while another command is active.
  11.                    2 = ACRX_CMD_USEPICKSET;When the pickfirst set is retrieved it is cleared within AutoCAD.
  12.                        Command will be able to retrieve the pickfirst set. Command cannot retrieve or set grips.
  13.                    4 = ACRX_CMD_REDRAW; When the pickfirst set or grip set is retrieved, neither will be cleared within AutoCAD.
  14.                        Command can retrieve the pickfirst set and the grip set.
  15.                        
  16.   Usage: (pjk-AddMDICmd <Command List>)
  17.   Returns: N/A
  18.   Description:
  19.                 This command defines a list of functions as AutoCAD Commands
  20.                 that are MDI aware. First the Commands are undefined if they
  21.                 already exist in the ObjectARX enviroment. Then, a document
  22.                 manager reactor is established to test if another document has
  23.                 become active and redefines the commands in the active document.
  24.                 This function can only be called once for Each drawing opened to
  25.                 establish the Command list and functions. then the reactor does the
  26.                 work.
  27.                 *** Original Author: Stephan Koster - 1999 ***
  28. ================================================================================|;
  29. (defun pjk-AddMDICmd (lst / old)
  30.         (foreach x (setq PJK:commandslist lst)
  31.                 (vlax-remove-cmd (car x))
  32.         )
  33.       (if (= "PJK-Commands" (vlr-data obj))(vlr-remove obj))
  34.    )
  35.    (vlr-docmanager-reactor "PJK-Commands" '((:vlr-documentBecameCurrent . pjk-CmdRedef)))
  36.         (pjk-CmdRedef nil nil)
  37. ) ;; End Function (pjk-AddMDICmd)
  38.  
  39. ;|==============================================================================
  40.   Function Name: (pjk-CmdRedef)
  41.   Arguments:
  42.   Usage: (pjk-CmdRedef [Reactor Callback][Command Info])
  43.   Returns: A list of the results of the vlax-add-cmd call - length depends on the command list defined in PJK:Commandslist
  44.            Example: given 4 commands to be defined - '(T T T T) if all commands were defined correctly.
  45.   Description:
  46.                 This Function is a reactor callback function for (pjk-addMDIcmd),
  47.                 or can be called alone with both arguments set to NIL. If the
  48.                 global variable PJK:COMMANDSLIST is defined, it will use it
  49.                 to create AutoCAD Commands with (vlax-add-cmd).
  50.   *** Original Author: Stephan Koster - 1999 ***
  51.    Update 5/16/2016: Added (mapcar) loop to undefined the commands prior to redefining them. This corrects an issue that
  52.                                caused the original function not to work correctly since AutoCAD 2009 (I think).
  53.                                THANKS to Marc'Antonio Alessi for the method!
  54. ================================================================================|;
  55. (defun pjk-CmdRedef (calbck cmd)
  56.    (mapcar
  57.       (function
  58.          (lambda (x)
  59.             (vlax-remove-cmd (car x))
  60.          )
  61.       )
  62.       PJK:commandslist
  63.    )
  64.    (mapcar
  65.       (function
  66.          (lambda (x / ok)
  67.             (if (and
  68.                    (member (type (eval (cadr x))) '(SUBR USUBR EXRXSUBR))
  69.                    (setq ok (apply 'vlax-add-cmd x))
  70.                          )
  71.                 ok
  72.                 )
  73.          )
  74.       )
  75.       PJK:commandslist
  76.    )
  77. ) ;; End Function (pjk-CmdRedef)
  78.  

Test Code:

Code - Auto/Visual Lisp: [Select]
  1. ;|==============================================================================
  2.   Function Name: (pjk-zw)
  3.   Arguments: NONE
  4.   Usage: (pjk-zw)
  5.   Description:
  6.                 ActiveX Zoom Command to emulate Zoom > Window. This
  7.                 function definition is used to define the "ZW" command
  8.                 with (pjk-addmdicmd)
  9. ================================================================================|;
  10. (defun pjk-zw ( / thisdwg)
  11.         (vla-sendcommand thisdwg "'._zoom _w ")
  12.         (vlax-release-object thisdwg)
  13.         (princ)
  14. ) ;; End Function (pjk-zw)
  15.  
  16. ;; Add the command(s) using (pjk-addMDICmd)
  17. (pjk-AddMDICmd
  18.    (list (list "ZW"  'pjk-zw     "ZW" 1))
  19. )
  20.  
"When you are asked if you can do a job, tell 'em, 'Certainly I can!' Then get busy and find out how to do it." - Theodore Roosevelt

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1453
  • Marco
Re: transparent lisp command
« Reply #39 on: May 16, 2016, 04:52:44 PM »
I do not really like reactors but this seems to work (see attachments):

Marc'Antonio,

WOW! I applied your method to my original functions i.e. removing the commands with (vlax-remove-cmd) before redefining them with (vlax-add-cmd) within the ":vlr-documenttobeactivated" reactor and IT WORKS! What I cannot understand is I have tried to employ this previously, but from within the same (mapcar) loop and I caused an error. Apparently it seems to work if the (mapcar) loops are separated.

THANK YOU!!!  :-D
...
Prego!  :-) :-) :-)

hawstom

  • Newt
  • Posts: 22
  • AutoCAD Civil 3D, Mesa, AZ, USA
Re: transparent lisp command
« Reply #40 on: March 08, 2017, 03:40:39 PM »
I just verified this does work.  Extensive testing in AutoCAD 2016.  Multiple cases and scenarios are listed below with conclusions at the very end.

Code - Auto/Visual Lisp: [Select]
  1. ;|==============================================================================
  2.   Function Name: (pjk-AddMDICmd)
  3.   Arguments: <Command List> = List of lists in the format (list (list "<Global Command Name>" '<Function Name> "[Local Command Name]" [Cmd-Flags]))
  4.                 Sub-list Arguments:
  5.                 <Global Command Name> = String for the Name of the Command
  6.                 <Function Name> = Quoted symbol for the autolisp function to run
  7.                 [Local Command Name] = String (opt.) for the Name of the command - usually same as global name
  8.                 [Cmd-Flags] = Integer Bit Flag for Command Behaivior - Bits are as follows:
  9.                    0 = ACRX_CMD_MODAL; Command cannot be invoked while another command is active.
  10.                    1 = ACRX_CMD_TRANSPARENT; Command can be invoked while another command is active.
  11.                    2 = ACRX_CMD_USEPICKSET;When the pickfirst set is retrieved it is cleared within AutoCAD.
  12.                        Command will be able to retrieve the pickfirst set. Command cannot retrieve or set grips.
  13.                    4 = ACRX_CMD_REDRAW; When the pickfirst set or grip set is retrieved, neither will be cleared within AutoCAD.
  14.                        Command can retrieve the pickfirst set and the grip set.
  15.                        
  16.   Usage: (pjk-AddMDICmd <Command List>)
  17.   Returns: N/A
  18.   Description:
  19.                 This command defines a list of functions as AutoCAD Commands
  20.                 that are MDI aware. First the Commands are undefined if they
  21.                 already exist in the ObjectARX enviroment. Then, a document
  22.                 manager reactor is established to test if another document has
  23.                 become active and redefines the commands in the active document.
  24.                 This function can only be called once for Each drawing opened to
  25.                 establish the Command list and functions. then the reactor does the
  26.                 work.
  27.                 *** Original Author: Stephan Koster - 1999 ***
  28. ================================================================================|;
  29. ;|
  30. (defun pjk-AddMDICmd (lst / old)
  31.         (foreach x (setq PJK:commandslist lst)
  32.                 (vlax-remove-cmd (car x))
  33.         )
  34.    (foreach obj (cdar (vlr-reactors :vlr-docmanager-reactor))
  35.       (if (= "PJK-Commands" (vlr-data obj))(vlr-remove obj))
  36.    )
  37.    (vlr-docmanager-reactor "PJK-Commands" '((:vlr-documentBecameCurrent . pjk-CmdRedef)))
  38.         (pjk-CmdRedef nil nil)
  39. ) ;; End Function (pjk-AddMDICmd)
  40. |;
  41. ;|==============================================================================
  42.   Function Name: (pjk-CmdRedef)
  43.   Arguments:
  44.   Usage: (pjk-CmdRedef [Reactor Callback][Command Info])
  45.   Returns: A list of the results of the vlax-add-cmd call - length depends on the command list defined in PJK:Commandslist
  46.            Example: given 4 commands to be defined - '(T T T T) if all commands were defined correctly.
  47.   Description:
  48.                 This Function is a reactor callback function for (pjk-addMDIcmd),
  49.                 or can be called alone with both arguments set to NIL. If the
  50.                 global variable PJK:COMMANDSLIST is defined, it will use it
  51.                 to create AutoCAD Commands with (vlax-add-cmd).
  52.   *** Original Author: Stephan Koster - 1999 ***
  53.    Update 5/16/2016: Added (mapcar) loop to undefined the commands prior to redefining them. This corrects an issue that
  54.                                caused the original function not to work correctly since AutoCAD 2009 (I think).
  55.                                THANKS to Marc'Antonio Alessi for the method!
  56. ================================================================================|;
  57. ;|
  58. (defun pjk-CmdRedef (calbck cmd)
  59.    (mapcar
  60.       (function
  61.          (lambda (x)
  62.             (vlax-remove-cmd (car x))
  63.          )
  64.       )
  65.       PJK:commandslist
  66.    )
  67.    (mapcar
  68.       (function
  69.          (lambda (x / ok)
  70.             (if (and
  71.                    (member (type (eval (cadr x))) '(SUBR USUBR EXRXSUBR))
  72.                    (setq ok (apply 'vlax-add-cmd x))
  73.                          )
  74.                 ok
  75.                 )
  76.          )
  77.       )
  78.       PJK:commandslist
  79.    )
  80. ) ;; End Function (pjk-CmdRedef)
  81. |;
  82. (defun c:vt () (alert "This is a test of VLX private."))
  83. (vl-acad-defun 'vte)
  84. (defun vte () (alert "This is a test of VLX export non-command."))
  85. (vl-acad-defun 'c:vtec)
  86. (defun c:vtec () (alert "This is a test of VLX export command."))
  87. ;;; Test results:  2017-03-08 Compiled to VLX with optimize and link.  
  88. ;;; Drag into dwg 1: All the above worked
  89. ;;; Open dwg 2.  None work.
  90. ;;; Drag into dwg 2. All work.
  91. ;|
  92. (vlax-add-cmd "VT" 'c:vt)
  93. (vlax-add-cmd "VTE" 'vte)
  94. (vlax-add-cmd "VTEC" 'c:vtec)
  95. |;
  96. ;;; Added above declarations.
  97. ;;; Test results:  2017-03-08 Compiled to VLX with optimize and link (and separate namespace).  
  98. ;;; Drag into dwg 1: All worked including (command "vtec") etc.
  99. ;;; Drag into dwg 2: All give "; error: Visual LISP command document mismatch:"
  100. ;|
  101. (pjk-AddMDICmd
  102.   (list
  103.     (list "VT"  'c:vt)
  104.     (list "VTE"  'vte)
  105.     (list "VTEC"  'c:vtec)
  106.   )
  107. )
  108. |;
  109. ;;; Added above from https://www.theswamp.org/index.php?topic=51409.msg565523#msg565523
  110. ;;; Test results: 2017-03-08
  111. ;;; Compiled to VLX with optimize and link (and separate namespace).
  112. ;;; Drag into dwg 1: All worked.
  113. ;;; Opened dwg 2.  None worked.  Dragged in.  All worked.
  114. ;;; Switched to dwg 1: All worked!
  115. (defun vtp ()(alert "This was supposed to be private"))
  116. (defun vtpc () (vtp))
  117. ;;; Added above to test private functions
  118. ;;; Test results 2017-03-08
  119. ;;; Drag into dwg 1: All previous worked.  vtp and vtpc didn't work.  Yay!
  120. ;;; Switch to dwg 2: All same behavior.  No extra load needed.  One load works in all docs.
  121. ;;; Open dwg 3: ; error: Visual LISP command document mismatch: VT
  122. ;;; Load vlx: All works as desired.
  123. ;;; Add vlx to Appload Startup suite.  Restart AutoCAD. Caused AutoCAD crash on restart.
  124. ;;; Commented out pjk functions, reverting to vlax-add-cmd.
  125. ;;; Started AutoCAD.  All worked as desired.  
  126. ;;;   Opened drawing 2.  ; error: Visual LISP command document mismatch: VTE etc.
  127. ;;
  128. ;;; Reverted to pjk functions.  Restarted AutoCAD.  Worked in dwg1.  Worked in dwg2.
  129. ;;;  Switched to dwg 1. All worked.
  130. ;;; Removed both pjk and vlax-add-cmd calls.  Left vlx in startup suite.  
  131. ;;;  vl-acad-defun works (privates were private and publics were public).
  132. ;;; Removed vlx from startup suite.  Caused AutoCAD crash on restart (just like when I added it).
  133. ;;; On restart, dragged vlx into one drawing after another. All works as desired.(vtP)
  134. ;;; Conclusion: To control exporting exposing Visual Lisp functions, use a single VLX.
  135. ;;;  To export the commands to *native" AutoCAD commands (command "My_command"),
  136. ;;;  use the PJK functions and load in each drawing.
  137.  
Not so terse now.  You may not feel so clever in 6 months or years.  http://lisp-lang.org/style-guide/
hawsedc.com, and autocad.wikia.com

PKENEWELL

  • Bull Frog
  • Posts: 318
Re: transparent lisp command
« Reply #41 on: April 04, 2017, 06:35:31 PM »
Hi Hawstom,

Thanks for the test work. Enlightening! I generally just make code for my own use so I sometimes don't test scenarios that I am not using.
"When you are asked if you can do a job, tell 'em, 'Certainly I can!' Then get busy and find out how to do it." - Theodore Roosevelt