Where was this forum when I was starting out, sheesh.
I don't think Mark would have enough bandwidth for that
Andre, you're doing a good job trying to figure it out but somehow I don't think you have the patience to do an even better job. Whether or not that assumption is right, it's not uncommon to lack patience - especially when trying to squeeze a little coding in between daily choirs.
But - and I think most here can testify to this - if you don't try to figure out exactly what it is you are asking the machine to do then you'll spend twice, triple or quadruple the amount of time writing, editing and debugging code.
The rather simple code that Daron posted is an excellent example of how more understanding could be gained by testing .. and testing and testing.
When you look up the IF function in the help reference ('cos you did that, right?), it will tell you that the first expression is a test expression, that the second expression will be evaluated if the
test expression is not nil and that the third expression will be evaluated if the
test expression is nil.
That's ALL you need to know about IF. Understanding that very basic concept allows you to decipher any IF statement - at first by testing and later by merely looking at it.
So, how to test Daron's IF statement? You made the first step, which is to identify each of the components that the IF statement is made up of:
function name : (if
test expression: (= (strcase (getvar "loginname") t) "drogers")
2nd expression : (command ".zoom" "") ; line 1
3rd expression : (command ".zoom" ".9X") ; line 2
end of function: )
Now use the command line in AutoCAD and test each component. The expression that makes the decision is the test expression, which is always the first expression (as pointed out in the help file). Break it down, identify each component and test it, always starting with the innermost expressions:
Command: (getvar "loginname")
"andre"
Command: (strcase (getvar "loginname") t)
"andre"
Hmmm, you might say, this gives the same answer so why use STRCASE? Let's test that function. Looking it up in the help reference will tell you that it converts a string to
either uppercase
or lowercase. The third argument, if present, specifies which operation to perform. Test it to understand it:
Command: (strcase (getvar "loginname"))
"ANDRE"
Command: (strcase "AnDrE")
"ANDRE"
Command: (strcase "AnDrE" t)
"andre"
Ahh, so Daron may have experienced that login names comes in all forms and shapes and wants to make sure that he is only comparing lowercase strings. That's why he added the second argument, T.
Now that you have deciphered all arguments to the equality function, test it:
Command: (= "andre" "drogers")
nil
Command: (= "drogers" "drogers")
T
Command: (= "DRogers" "drogers")
nil
Now you know what IF has to work with in order to decide which expression to choose, the second or the third. If the test is not nil then it chooses to evaluate the second expression. If the test is nil then the third. The outcome of the test expression is the "pointer" to the selected statement.
To sum it up, if you are not quite sure why a statement does as it does then TEST it. Break it down into components and test each of the components. Think of the command line as being your own personal code testing tool!
Have the help file ready in the background, already opened at the "AutoLISP reference" so you can quickly identify the arguments to a function. Note which data types that the function accepts and compare them to your test results.