TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: Lupo76 on July 26, 2012, 07:58:09 AM

Title: Problem with redefining command
Post by: Lupo76 on July 26, 2012, 07:58:09 AM
Hello everyone,
I wrote the following code:

Code: [Select]
(defun undefine-command (cname)
   (if (getcname cname)
     (progn
        (setvar "cmdecho" 0)
        (command "._UNDEFINE" cname)
     )
   )
)

(defun purgenew ()
  (alert "HELLO")
  (command "_.purge")
)


(defun c:test ()
  (undefine-command "_purge")
  (undefine-command "_-purge")
  ;--------------------------
  (defun c:purge ( )
    (purgenew "si")
  )

  (defun c:elimina ( )
    (purgenew "si")
  )

  (defun c:-elimina ( )
     (purgenew "-")
  )

  (defun c:-purge ( )
     (purgenew "-")
  )

  (defun c:_-purge ( )
     (purgenew "-")
  )
  ;--------------------------
)

I have a problem you describe with the following steps:

1. I open AutoCAD
2. Load the file lisp
3. Start the command "PURGE" ok! is the normal AutoCAD command
4. Starting c: test
5. Start the command "PURGE" ok! the purge command is redefined
6. I open a new drawing
7. Start the command "PURGE" unknown command, why?


Whereas I have opened a new file and I have not loaded the file lisp and I've never started the test function, I expected it to be started normally PURGE command in AutoCAD.

Can you help me to achieve this solution?
thanks
Title: Re: Problem with redefining command
Post by: irneb on July 26, 2012, 11:35:36 AM
Lisp is loaded only into the current drawing, while ARX/DotNet commands are instantiated for ACad as a whole. So you need to have your lisp loaded automatically - IMO best is to load it from the ACadDoc.LSP file. Also you'll need to run the c:test function as soon as loaded - so it defines the new purge command as soon as loaded for a DWG file.

Alternatively if you only want to turn on this feature of yours, then use vl-load-all instead of a normal load. Also still needed to run the c:test. Just add the following to the end of your LSP file:
Code: [Select]
(c:test)
Unfortunately the vl-propagate only applies to variables, not defuns. Otherwise it might have been possible to define new commands in other DWGs as well.
Title: Re: Problem with redefining command
Post by: Lupo76 on July 26, 2012, 03:29:20 PM
Hello irneb,
thanks for the answer, but I want to do exactly the opposite.

I would like to start the command c: test manually only when I need to redefine the PURGE command.
When I open a new file I would use the original command of autocad.
Unfortunately, however, the commands are unknown in this case  :cry:

Also: there is a lisp function that restores the original command in AutoCAD?
Title: Re: Problem with redefining command
Post by: PKENEWELL on July 26, 2012, 03:40:31 PM
Try This:

Code: [Select]
(command "_.REDEFINE" "_purge")
Title: Re: Problem with redefining command
Post by: Lupo76 on July 26, 2012, 04:00:04 PM
perfect,
I tried the command _.REDEFINE in google, but probably was using the wrong search terms.

thanks