Need Some help w/Global Variables and Output

Need Some help w/Global Variables and Output

Anonymous
Not applicable
903 Views
6 Replies
Message 1 of 7

Need Some help w/Global Variables and Output

Anonymous
Not applicable

So here's some code that actually does exactly what I want.

(VL-LOAD-COM)
(DEFUN C:RSFIND ( / PLEN CNTR PLENGTH PWIDTH SSVAL
		FINDLENGTH PTMP PANEL_LIST)
  (SETQ PLENGTH (GETDIST "What is Panel length: "))
  (SETQ PWIDTH  (GETDIST "What is Panel Width: "))
  (SETQ SSVAL   (SSGET '((8 . "RS Panels"))))
  (SETQ FINDLENGTH (+ (* PLENGTH 2.0) (* PWIDTH 2.0)))

;;;;;;;;;;;;;;;;;;;;;;;
;;;Typical vla-setup;;;
;;;get length and   ;;;
;;;highlight        ;;;
;;;;;;;;;;;;;;;;;;;;;;;
  (DEFUN VSET (CNTR SSVAL / ENTOBJ VLAENT)
  	(SETQ ENTOBJ (SSNAME SSVAL CNTR))								;;GET <E> NAME FROM COUNTER LOCATION IN SELECTION SET
  	(SETQ VLAENT (VLAX-ENAME->VLA-OBJECT ENTOBJ))							;;STORE OBJECT DATA FROM ENTITY                      
  	(SETQ PLEN (VLAX-GET-PROPERTY VLAENT 'Length))
        (COND ((= PLEN FINDLENGTH)
	       (VLA-HIGHLIGHT VLAENT :VLAX-TRUE))
	      (T NIL)
	  )
    )
  
;;;;;;;;;;;;;;;;;;;;;;;
;;;step through list;;;
;;;;;;;;;;;;;;;;;;;;;;;
(COND ((/= SSVAL NIL)
    (SETQ CNTR 0)
    (WHILE (< CNTR (SSLENGTH SSVAL))
     (VSET CNTR ssval)
     (SETQ CNTR (+ 1 CNTR))
     )
     )
    (T NIL)
    )
   (PRINC)
    )
  

THE PROBLEM:

When I run "Check in Text Editor:"

[CHECKING TEXT RFIND (2.0).lsp loading...]
..
; === Top statistic:
; Global variables: (:vlax-true FINDLENGTH PLEN)
; Function definition (with number of arguments): ((VSET . 2) (C:RSFIND . 0))
; Check done.

Those non-vlax variables return nothing after the script is finished running. I assume :vlax-true returns :vlax-true every single time. When I include "FINDLENGTH, PLEN, & :vlax-true" in my local declaration for subfunciton (VSET), I don't get the desired output. . . Which is just highlighting rectangles that equal the length I'm looking for(Job specific). 

Does having the three variables considered "GLOBAL" in this case matter? I don't think it does, but I'd like to ask you guys before implementing this script in my daily routine.

 

Thanks for the help.

 

Thaddeus

0 Likes
Accepted solutions (2)
904 Views
6 Replies
Replies (6)
Message 2 of 7

Kent1Cooper
Consultant
Consultant
Accepted solution

@Anonymous wrote:

 

Those non-vlax variables return nothing after the script is finished running. .... When I include "FINDLENGTH, PLEN, & :vlax-true" in my local declaration for subfunciton (VSET), I don't get the desired output. . . Which is just highlighting rectangles that equal the length I'm looking for(Job specific). 

Does having the three variables considered "GLOBAL" in this case matter? I don't think it does....


 

Yes, it does.  Any variable name that is in the list following the / in the parentheses after the command name:

(DEFUN C:RSFIND ( / PLEN CNTR PLENGTH PWIDTH SSVAL
     FINDLENGTH PTMP PANEL_LIST)

 

is local, existing only within and for the purposes of the command, and will not exist any more after the command has run.  If you remove  PLEN and FINDLENGTH from that list, they will become global, and will survive past the end of the command.

Kent Cooper, AIA
0 Likes
Message 3 of 7

Anonymous
Not applicable

Thanks @Kent1Cooper!

 

Ok. So they're "technically" local considering the big picture of the function and will ultimately be gone after wrapping up the main function.

 

 

BUT the text editor check in the vlisp console is calling them global because the subfunction (VSET) does not declare them local?

0 Likes
Message 4 of 7

Kent1Cooper
Consultant
Consultant

@Anonymous wrote:

.... 

 

BUT the text editor check in the vlisp console is calling them global because the subfunction (VSET) does not declare them local?


 

Maybe, but never mind all that....  I didn't notice before the probable real reason you're not getting the desired highlighting:  Change this:

 

(COND ((= PLEN FINDLENGTH)

 

to this:

 

(cond ((equal PLEN FINDLENGTH 1e-4)

 

because anything that's the result of calculations can have really tiny way-the-heck-down-deep-in-far-decimal-places variation from  what you think it's going to be, but the (=) function requires absolute equality.  Any comparison of that type should be made using (equal) with a fuzz factor.  [You can change the size of the fuzz factor -- that one considers the values equal if they're within one 10,000th of a drawing unit.]

Kent Cooper, AIA
0 Likes
Message 5 of 7

Anonymous
Not applicable

Thanks for the tip @Kent1Cooper.

 

I included it in the script and got the same thing. It works and highlights only when I don't declare the variables local in the subfunction. The "Check Text in Editor" is still telling me they're global. I don't think they really are though. Smiley Indifferent

0 Likes
Message 6 of 7

cadffm
Consultant
Consultant
Accepted solution
They are global, global for VSET in the "namespace" of C:RSFIND, where they are local.

Ok, namespace could be the wrong term,
ValidRange?

Sebastian

0 Likes
Message 7 of 7

Anonymous
Not applicable

Cool. So I'm good as long as I declare them in the main function. 

0 Likes