Community Tip - Did you get called away in the middle of writing a post? Don't worry you can find your unfinished post later in the Drafts section of your profile page. X
My goal is to create an IF / THEN / ELSE statement in LISP.
To test this I tried to delete "/w1" and "/p1" at startup. (Since some users may have their system to automatically add these at startup.)
At a basic level, this works in some cases:
But if they dont have "/w1" or "/p1" loaded, then it will create an error trying to delete something that isn't there,
So I tried to make a conditional statement:
I tried this, but after many variations, I had no luck creating an "ELSE" part of the statement
Does anyone have a solution for this?
Solved! Go to Solution.
Hi Gary,
You're on the right track -- you just need to adjust the way you're asking about the objects. If you know what the name of the object is, you want to ask the system if an object with that name exists. In this case, you don't want to do anything if it doesn't exist, so you can use "when" rather than "if" (fyi, the counterpart of "when" is "unless").
The following should work:
(when (oli:sd-pathname-to-obj "/w1") (delete_3d "/w1"))
(when (oli:sd-pathname-to-obj "/p1") (delete_3d "/p1"))
Does that help?
Good luck!
andy
Hi Gary,
You're on the right track -- you just need to adjust the way you're asking about the objects. If you know what the name of the object is, you want to ask the system if an object with that name exists. In this case, you don't want to do anything if it doesn't exist, so you can use "when" rather than "if" (fyi, the counterpart of "when" is "unless").
The following should work:
(when (oli:sd-pathname-to-obj "/w1") (delete_3d "/w1"))
(when (oli:sd-pathname-to-obj "/p1") (delete_3d "/p1"))
Does that help?
Good luck!
andy
Thanks Andy,
That worked perfectly!
I would still like to know when I need to use an IF THEN ELSE statement, is there anything special I need to know about using it in this application, and when would I choose that method over WHEN UNLESS?
From what I just read the main difference is that the WHEN method has no limit on expressions that can be used in the do-actions,
While using the IF method, only a single expression can be used in do-action or else-action, and multiple expressions for actions must be contained by a progn expression.
(WHEN (test-expression) do-actions)
vs
(IF (test-expression) (do-action) (else-action))
Hi Gary,
Well, the main difference is that WHEN and UNLESS don't have an ELSE! You are correct that IF just allows a single expression (and, as you noted, you can use progn to create a compound expression).
So if you have functions 1 and 2 that you want to execute in situation 1, and 3 and 4 that you want to execute in any other situation, you could do this:
(if situation1
(progn
(function1)
(function2)) ;; second close paren to complete the progn
(progn
(function3)
(function4)) ;; as above
) ;; close the if statement
If you wanted to do it using when and unless, you'd do this:
(when situation1
(function1)
(function2)) ;; again, 2 close parens to close to complete when
(unless situation1
(function3)
(function4)) ;; also here
There's another construct (cond ...) that's sort of a combination of the above. It's a little more complex, and more powerful, too. I can provide info on that if you'd like (but didn't want to muddy the waters too much here!)
Glad the info helps! (though you can just have them turn off the initial part and initial workplane in their settings, too!)
Enjoy!
andy
There is a good introduction to LISP included in the Creo Elements/Direct Modeling Help. Click on Help->Documentation for advanced users->Integration Kit->Examples. Just below the examples is the Appendix. In the Appendix is a link to The ME Macro Programmer's Introduction to Lisp. It is certainly not a complete set of documentation on LISP, but as indicated in the title, it does provide an introduction-including a description of if, when, unless, cond, and case.
Gary,
LISP is a mighty language - you can "program" things in many different ways.
Modeling (aka SolidDesigner) implements a KCL (Kyoto Common LISP) Standard.
Search for KCL in the Internet and you will find online books and tutorials.
Here are a few starting points:
http://en.wikipedia.org/wiki/Kyoto_Common_Lisp
http://www.cs.cmu.edu/Groups/AI/html/cltl/cltl2.html
Regards,
Max (one of the developers of SolidDesigner)