Boolean flag in function

Boolean flag in function

Browning_Zed
Advocate Advocate
1,829 Views
8 Replies
Message 1 of 9

Boolean flag in function

Browning_Zed
Advocate
Advocate

How can a boolean argument be set in a function? I want the argument to take two values T or nil. Depending on the return value, one of the conditions is met. For example:

(defun Test ( A / ) 
    (if A 
	(progn)
	(progn)
    )
)

(test T) ; should return the first progn
(test nil) ; should return the second progn

0 Likes
1,830 Views
8 Replies
Replies (8)
Message 2 of 9

Kent1Cooper
Consultant
Consultant

Does it not work as you have it?  I modified it a little to get an observable result:

(defun Test (A / )
  (if A
    (prompt "\nArgument was T.")
    (prompt "\nArgument was nil.")
  )
  (princ)
)

 

and got these results:

Command: (test T)
Argument was T.
Command: (test nil)
Argument was nil.

 

And if the argument is not explicitly T or nil:

Command: (test (= 3 (+ 1 2)))
Argument was T.
Command: (test (= 3 (+ 1 5)))
Argument was nil.

Kent Cooper, AIA
0 Likes
Message 3 of 9

doaiena
Collaborator
Collaborator

I'm not sure i fully understand the author's request, but could it be something like this?

(defun test ( a / )

(cond
((= a 'T) (princ "\nArgument was T."))
((not a) (princ "\nArgument was nil."))
(T (princ "\nArgument was neither T nor nil."))
)
(princ)
)
0 Likes
Message 4 of 9

marko_ribar
Advisor
Advisor

Interesting thing is toggle example... Try to memorize this trick...

 

 

(defun c:toggle ()
  (if (setq f (not f))
    (prompt "\nT")
    (prompt "\nnil")
  )
  (princ)
)
(setq f nil) ;;; nil f after routine executions...

 

Marko Ribar, d.i.a. (graduated engineer of architecture)
0 Likes
Message 5 of 9

Browning_Zed
Advocate
Advocate

This does not work in my specific case. I am using several functions to create text styles based on the main function. Like this:

 

(defun C-TS (NAME FONT WIDTH OBLIQUE)
    (if (not (tblsearch "STYLE" NAME))
        (entmake 
    	    (list	
               '(0 . "STYLE")
               '(100 . "AcDbSymbolTableRecord")
               '(100 . "AcDbTextStyleTableRecord")
                (cons 2 NAME)
                (cons 3 FONT)
                (cons 41 WIDTH)
                (cons 50 OBLIQUE)
               '(40 . 0.0)
               '(70 . 0)
               '(71 . 0)
			)
        )
    )
)
(defun TS-Arial ( S / F ) (setq F "Arial")(C-TS F (strcat F ".ttf") 1 0) (if S (setvar 'TEXTSTYLE F)))
(defun TS-Times ( S / F ) (setq F "Times")(C-TS F (strcat F ".ttf") 1 0) (if S (setvar 'TEXTSTYLE F)))

 

As you can see, the C-TS function enters the definition of the style, and other functions create the style, and also, depending on the condition, set it current.

After that I create button macros:
^C^C(TS-Arial Т);
^C^C(TS-Times Т);
In this case, the "Arial" macro works, but the "Times" macro does not. That is, the text style will not be set current. I do not know why.

0 Likes
Message 6 of 9

Kent1Cooper
Consultant
Consultant

@Browning_Zed wrote:

....

....
(defun TS-Arial ( S / F ) (setq F "Arial")(C-TS F (strcat F ".ttf") 1 0) (if S (setvar 'TEXTSTYLE F)))
(defun TS-Times ( S / F ) (setq F "Times")(C-TS F (strcat F ".ttf") 1 0) (if S (setvar 'TEXTSTYLE F)))

....

^C^C(TS-Arial Т);
^C^C(TS-Times Т);
In this case, the "Arial" macro works, but the "Times" macro does not. That is, the text style will not be set current. I do not know why.


My computer does not come with any font file called "Times" -- does yours?  It's called "Times New Roman" here.  I imagine trying to assign a non-existent font would fail.  Are there any messages?

Kent Cooper, AIA
0 Likes
Message 7 of 9

Browning_Zed
Advocate
Advocate

The font file exists, but it doesn't matter in this case. I think I understand what is the reason. The boolean flag will work in this case:
(somefunction "T")
but it won't work if:
(somefunction T)
It is unclear why this is happening.

0 Likes
Message 8 of 9

Kent1Cooper
Consultant
Consultant

@Browning_Zed wrote:

..... The boolean flag will work in this case:
(somefunction "T")
but it won't work if:
(somefunction T)
It is unclear why this is happening.


I don't get it.  You said that (TS-Arial Т) does work.  Are you saying, applied in the specific case, that (TS-Times "Т") works but (TS-Times Т) does not?

 

In those (TS-... routines where the check on the S argument is simply (if S... , all it's checking is that S is not nil -- any kind of non-nil value would satisfy it.  So it wouldn't need to be (somefunction "T") seeing that specific text string as non-nil, but it could be (somefunction "MickeyMouse") or (somefunction 27.61) or (somefunction (/ pi 2))  or whatever.  Does (TS-Times "GoForIt") work?  If (somefunction T) doesn't work, nor (TS-Times Т), it suggests to me that something has used T as a variable name, and spoiled it for use as the True symbol, but if that were the cause, then (TS-Arial Т) shouldn't work, either.

Kent Cooper, AIA
0 Likes
Message 9 of 9

john.uhden
Mentor
Mentor

I agree with Kent.  And I have never trusted T.

Try (ts-times 1).  No one can change the value of 1.

BTW, both your functions work for me in 2002.  I'm sorry... make that all three functions.

John F. Uhden

0 Likes