Author Topic: VLISP code not working  (Read 2258 times)

0 Members and 1 Guest are viewing this topic.

Aksha

  • Mosquito
  • Posts: 2
VLISP code not working
« on: October 06, 2023, 10:22:43 PM »
I am a novice in VLISP programming. By using examples of code written by others, I created the following code. Here's what I want to achieve with it.
When the value of CANNOSSCALE is changed, I want to trigger a change of DIMSCALE. And then Regen the drawing. In the code below, the line for regenerating the drawing is executed. But the line for changing DIMSCALE is not executed. It also does not produce any error.

Code - Auto/Visual Lisp: [Select]
  1. (if (null casr)
  2.   (setq casr (vlr-sysvar-reactor nil '((:vlr-sysvarchanged . rgnlt))))
  3. )
  4. (defun rgnlt (casr cl)
  5.     (cond
  6.         ((= (car cl) "CANNOSCALE")
  7.             (vl-cmdf "dimstyle" "_R" (strcat "SR" (vl-princ-to-string (fix (/ 1 (getvar "CANNOSCALEVALUE"))))))
  8.             (vla-regen (vla-get-activedocument (vlax-get-acad-object)) acActiveViewport)
  9.         )
  10.     )
  11. )


EDIT (John): Added code tags.
« Last Edit: October 10, 2023, 10:40:44 AM by JohnK »

ribarm

  • Gator
  • Posts: 3278
  • Marko Ribar, architect
Re: VLISP code not working
« Reply #1 on: October 07, 2023, 01:53:16 AM »
You can't use command function inside callback function... That's why that part haven't executed... Try something with vla-xxx - Visual LISP...
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

BIGAL

  • Swamp Rat
  • Posts: 1416
  • 40 + years of using Autocad
Re: VLISP code not working
« Reply #2 on: October 07, 2023, 02:24:41 AM »
This is not tested but may work.

Code: [Select]
(if (null casr)
  (setq casr (vlr-sysvar-reactor nil '((:vlr-sysvarchanged . rgnlt))))
)
(defun rgnlt (casr c can)
    (cond
        ((= (car cl) "CANNOSCALE")
(progn
(setq can (strcat "SR" (vl-princ-to-string (fix (/ 1 (getvar "CANNOSCALEVALUE"))))))
                (vl-cmdf "dimstyle" "_R" can)
)
            (vla-regen (vla-get-activedocument (vlax-get-acad-object)) acActiveViewport)
        )
    )
)

Another way may be (vla-sendcommand acdoc "your big line")
(setq acdoc (vla-get-activedocument (vlax-get-acad-object)))
« Last Edit: October 07, 2023, 02:27:43 AM by BIGAL »
A man who never made a mistake never made anything

ribarm

  • Gator
  • Posts: 3278
  • Marko Ribar, architect
Re: VLISP code not working
« Reply #3 on: October 07, 2023, 02:35:23 AM »
I think BIGAL's won't work as he used command calls inside callback function...
Instead, try something like this :

Code - Auto/Visual Lisp: [Select]
  1. (if (null casr)
  2.   (setq casr (vlr-sysvar-reactor nil '((:vlr-sysvarchanged . rgnlt))))
  3. )
  4. (defun rgnlt (casr cl / adoc dstls)
  5.   (if (not adoc)
  6.   )
  7.   (setq dstls (vla-get-dimstyles adoc))
  8.   (if (= (car cl) "CANNOSCALE")
  9.     (progn
  10.       (vla-sendcommand adoc "_.-DIMSTYLE\nR\n(strcat \"SR\" (rtos (fix (/ 1.0 (getvar \"CANNOSCALEVALUE\"))) 2 0))))\n")
  11.       (vla-regen adoc acActiveViewport)
  12.     )
  13.   )
  14. )
  15.  

HTH.
M.R.
« Last Edit: October 07, 2023, 08:04:23 AM by ribarm »
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

Aksha

  • Mosquito
  • Posts: 2
Re: VLISP code not working
« Reply #4 on: October 10, 2023, 01:10:11 AM »
I think BIGAL's won't work as he used command calls inside callback function...
Instead, try something like this :

Code - Auto/Visual Lisp: [Select]
  1. (if (null casr)
  2.   (setq casr (vlr-sysvar-reactor nil '((:vlr-sysvarchanged . rgnlt))))
  3. )
  4. (defun rgnlt (casr cl / adoc dstls)
  5.   (if (not adoc)
  6.   )
  7.   (setq dstls (vla-get-dimstyles adoc))
  8.   (if (= (car cl) "CANNOSCALE")
  9.     (progn
  10.       (vla-sendcommand adoc "_.-DIMSTYLE\nR\n(strcat \"SR\" (rtos (fix (/ 1.0 (getvar \"CANNOSCALEVALUE\"))) 2 0))))\n")
  11.       (vla-regen adoc acActiveViewport)
  12.     )
  13.   )
  14. )
  15.  

HTH.
M.R.

Perfect! Your code worked like a charm. I created a separate LISP to change Textstyle, Dimstyle and CMLeaderstyle based on the same style name. Now on changing the CANNOSCALE, my textstyle, dimstyle and mleaderstyle all are changing as intended.
Thank you!

cmwade77

  • Swamp Rat
  • Posts: 1443
Re: VLISP code not working
« Reply #5 on: October 10, 2023, 07:19:58 PM »
Alternatively, you could use something like this:

Code - Auto/Visual Lisp: [Select]
  1. (if (null casr)
  2.   (setq casr (vlr-sysvar-reactor nil '((:vlr-sysvarchanged . rgnlt))))
  3. )
  4. (defun rgnlt (casr cl)
  5.     (defun BDG_SetDimStyleCurrent (dim / acdoc Style_Restore)
  6.     (if (tblsearch "DIMSTYLE" dim)
  7.         acdoc
  8.         (vla-item (vla-get-Dimstyles acdoc) dim)
  9.       )
  10.     )
  11.   )
  12.     (cond
  13.         ((= (car cl) "CANNOSCALE")
  14.           (progn
  15.             (setq Style_Restore (strcat "SR" (vl-princ-to-string (fix (/ 1 (getvar "CANNOSCALEVALUE"))))))
  16.             (if (tblsearch "dimstyle" Style_Restore)
  17.               (progn
  18.                 (BDG_SetDimStyleCurrent Style_Restore)
  19.                 (vla-regen (vla-get-activedocument (vlax-get-acad-object)) acActiveViewport)
  20.               )
  21.             )            
  22.           )
  23.         )
  24.     )
  25. )

This will check to make sure the dimstyle exists in the drawing before setting it current, which should further reduce errors.