Skip to main content
1-Visitor
June 6, 2014
Solved

Making a conditional statements in "pesd_startup" or "user input line"...

  • June 6, 2014
  • 5 replies
  • 2530 views

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:

 

  • delete_3d "/w1" complete
  • delete_3d "/p1" complete

 

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

 

  • (if "/w1" (delete_3d "/w1") ( ? )) ;; IF "/w1" exists delete it, If it doesn't exist, NIL - End statement and continue
  • (if "/p1" (delete_3d "/p1") ( ? ))

 

Does anyone have a solution for this?


This thread is inactive and closed by the PTC Community Management Team. If you would like to provide a reply and re-open this thread, please notify the moderator and reference the thread. You may also use "Start a topic" button to ask a new question. Please be sure to include what version of the PTC product you are using so another community member knowledgeable about your version may be able to assist.
Best answer by AndyPoulsen

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

5 replies

12-Amethyst
June 6, 2014

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

1-Visitor
June 6, 2014

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))

12-Amethyst
June 6, 2014

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