Author Topic: If then statements  (Read 4998 times)

0 Members and 1 Guest are viewing this topic.

MSTG007

  • Gator
  • Posts: 2606
  • I can't remeber what I already asked! I need help!
If then statements
« on: November 02, 2017, 08:05:33 AM »
I am trying to understand how to write a simple if then that statement.

What I am trying to do is if a path under template settings / Default Template File Name for QNEW has a "C:\*" then run command 1, if it does not have "C:\*" do nothing.

Can anything like this be done? thanks
Civil3D 2020

huiz

  • Swamp Rat
  • Posts: 919
  • Certified Prof C3D
Re: If then statements
« Reply #1 on: November 02, 2017, 10:10:25 AM »
I use things like this to configure a new AutoCAD:

Code: [Select]
(vl-load-com)
(setq *files* (vla-get-files (vla-get-preferences (vlax-get-acad-object))))

(setq CurrentSupportPaths (vla-get-SupportPath *files*))
(if (not (vl-string-search "H:\\Application Data\\" CurrentSupportPaths)) (vla-put-SupportPath *files* (strcat "H:\\Application Data\\" ";" CurrentSupportPaths)))

(vla-put-TemplateDwgPath *files* "H:\\Application Data\\Template\\")


I assume you can ignore the last line if you don't want to set it, but do something like:

Code: [Select]
(setq CurrentTemplatePath (vla-get-TemplateDwgPath *files*))
(if ((vl-string-search "C:\\" CurrentTemplatePath)) ( ... do something ... )

The conclusion is justified that the initialization of the development of critical subsystem optimizes the probability of success to the development of the technical behavior over a given period.

Dommy2Hotty

  • Swamp Rat
  • Posts: 1128
Re: If then statements
« Reply #2 on: November 02, 2017, 10:19:47 AM »
I am trying to understand how to write a simple if then that statement.

AfraLISP has excellent tutorials for LISP:
http://www.afralisp.net/autolisp/tutorials/conditionals.php

Quote from: Kenny Ramage
(If) is probably the most important and widely use condition statement.
Unlike other languages though, you can match only one (if) statement with a then statement. The syntax is as follows :

(if xyz
   (then do this)
   (else do this)
)

MSTG007

  • Gator
  • Posts: 2606
  • I can't remeber what I already asked! I need help!
Re: If then statements
« Reply #3 on: November 02, 2017, 10:25:23 AM »
To a degree, that is what I am kinda trying to figure out. So I see this portion that finds the keyword. But I need it to run a lisp routine if that loads custom (Ribbons, Tool Palettes, etc.) loaded.

Am I heading the right direction with this?

Code: [Select]
(vl-load-com)

;If this exists in this path
(setq CurrentTemplatePath (vla-get-TemplateDwgPath *files*))
(if ((vl-string-search "C:\\" CurrentTemplatePath))

;then do load routine

(defun c:Setup_menus()
  (load (findfile ".\\Library\\Setup_menus\\Setup_menus.lsp")))
(C:Setup_menus)
(princ)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(setq *files* (vla-get-files (vla-get-preferences (vlax-get-acad-object))))

(setq CurrentSupportPaths (vla-get-SupportPath *files*))
(if (not (vl-string-search "H:\\Application Data\\" CurrentSupportPaths)) (vla-put-SupportPath *files* (strcat "H:\\Application Data\\" ";" CurrentSupportPaths)))

(vla-put-TemplateDwgPath *files* "H:\\Application Data\\Template\\")


Honestly, it would be nice if I could set this up where once the user clicks the icon shortcut it runs this code above.

Civil3D 2020

huiz

  • Swamp Rat
  • Posts: 919
  • Certified Prof C3D
Re: If then statements
« Reply #4 on: November 02, 2017, 10:42:02 AM »
I think this does it:

Code: [Select]

(if (vl-string-search "C:\\" CurrentTemplatePath) setup_menus)

The conclusion is justified that the initialization of the development of critical subsystem optimizes the probability of success to the development of the technical behavior over a given period.

MSTG007

  • Gator
  • Posts: 2606
  • I can't remeber what I already asked! I need help!
Re: If then statements
« Reply #5 on: November 02, 2017, 10:55:22 AM »
Bare with me, Im trying to simplify this... lol

Code: [Select]
(vl-load-com)

(defun c:Setup_menus()
(Alert "Still Stock and Needs to be Changed.")
(princ)


(if (vl-string-search "C:\\" CurrentTemplatePath) setup_menus)


With this:
Code: [Select]
(if (vl-string-search "C:\\" CurrentTemplatePath) setup_menus)
Error:
Code: [Select]
Command: (if (vl-string-search "C:\\" CurrentTemplatePath) setup_menus)
; error: bad argument type: (or stringp symbolp): nil

ideas?
Civil3D 2020

ronjonp

  • Needs a day job
  • Posts: 7533
Re: If then statements
« Reply #6 on: November 02, 2017, 11:19:40 AM »
Answer HERE too for qnew path. If you want the support paths, you can also use: (getenv "PATH")




Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

MSTG007

  • Gator
  • Posts: 2606
  • I can't remeber what I already asked! I need help!
Re: If then statements
« Reply #7 on: November 02, 2017, 11:48:33 AM »
Thank you Ron for this.

Code: [Select]
(if (and (setq tmp (getenv "LastTemplate")) (wcmatch (strcase tmp) "C:\USERS\*"))
  (code)
)

Below is a sample lisp I want it to run if the "C:\USERS\*" exist.
Setup_menu.lsp
Code: [Select]
(defun c:Setup_menus ()
(setvar "Expert" 0)
(ALERT "Setting Up Menus.")
(princ)
); defun

Code: [Select]
(if (and (setq tmp (getenv "LastTemplate")) (wcmatch (strcase tmp) "C:\USERS\*"))(C:Setup_menus))
Right now, When I run the above it says it works. but I do not see the Alert box popping up.

And if it does not exist, it will do nothing.

Civil3D 2020

ronjonp

  • Needs a day job
  • Posts: 7533
Re: If then statements
« Reply #8 on: November 02, 2017, 12:48:02 PM »
Try ( double backslashes added to users path ):
Code - Auto/Visual Lisp: [Select]
  1. (if (and (setq tmp (getenv "LastTemplate")) (wcmatch (strcase tmp) "C:\\USERS*"))(C:Setup_menus))
« Last Edit: November 02, 2017, 12:55:59 PM by ronjonp »

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

JohnK

  • Administrator
  • Seagull
  • Posts: 10669
Re: If then statements
« Reply #9 on: November 02, 2017, 01:41:35 PM »
...
Code - Auto/Visual Lisp: [Select]
  1. (if (and (setq tmp (getenv "LastTemplate")) (wcmatch (strcase tmp) "C:\\USERS*"))(C:Setup_menus))

That syntax still irks me.

When you are relying upon a return to signal a specific reaction or setting a variable it's "better" to use COND.
Code - Auto/Visual Lisp: [Select]
  1. (cond (expr) (iffalse))

For example, in ronjonp's code above a variable is created, converted, tested, and wrapped in a boole (to comply with IFs requirements). It would be easier in that situation to use COND and return either the results of "expr" or "nil" to set the variable. Like this:
Code - Auto/Visual Lisp: [Select]
  1. (set var (cond (expr) (iffalse)))

In the case of the OPs needs, a COND will also "better" serve you by offering you something easier to maintain (add conditions to) and easier to read. COND will return "nil" if everything fails.
Code - Auto/Visual Lisp: [Select]
  1.    (expr)
  2.    (expr)
  3.    ;| ... |;
  4.    )
  5. )
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

MSTG007

  • Gator
  • Posts: 2606
  • I can't remeber what I already asked! I need help!
Re: If then statements
« Reply #10 on: November 02, 2017, 01:50:44 PM »
Could you show me what that would look like converting Ron’s to this Cond format please?
Civil3D 2020

JohnK

  • Administrator
  • Seagull
  • Posts: 10669
Re: If then statements
« Reply #11 on: November 02, 2017, 01:56:38 PM »
Could you show me what that would look like converting Ron’s to this Cond format please?

Code - Auto/Visual Lisp: [Select]
  1.   ((wcmatch (strcase (getenv "LastTemplate")) "C:\\USERS*")
  2.    (C:Setup_menus)) )
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

JohnK

  • Administrator
  • Seagull
  • Posts: 10669
Re: If then statements
« Reply #12 on: November 02, 2017, 02:03:23 PM »
BTW, I should mention that ANY autolisp code I have not pulled from my archives and reposted should be considered highly suspect; I have not written any Autolisp code in many years. It was my job for many years to (re)write code for a company (it was my only job for many years to just program; I went through 1000's of lines of code from other programmers and streamlined/fixed and wrote new/better) but I would still not trust anything I write new without testing it yourself first.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

ronjonp

  • Needs a day job
  • Posts: 7533
Re: If then statements
« Reply #13 on: November 02, 2017, 03:27:16 PM »
The only reason I set a variable is to check that (getenv "LastTemplate") actually returns something ( don't know why it ever would not but... ) otherwise STRCASE will $h!+ the bed :).


I guess you could write it like this too not setting any variables.
Code - Auto/Visual Lisp: [Select]
  1. (and (= 'str (type (getenv "LastTemplate")))
  2.      (wcmatch (strcase (getenv "LastTemplate")) "C:\\USERS*")
  3.      (c:setup_menus)
  4. )

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

MSTG007

  • Gator
  • Posts: 2606
  • I can't remeber what I already asked! I need help!
Re: If then statements
« Reply #14 on: November 02, 2017, 05:29:52 PM »
Ok guys. This is where I am at.

Code: [Select]
(defun c:Setup_menus ()
(setvar "Expert" 0)
(ALERT "Setting Up Menus.")
(princ)
); defun

;;does not work;;(cond ((wcmatch (strcase (getenv "LastTemplate")) "C:\\USERS\\*")(C:Setup_menus)) )

;;works;;;;;(if (and (setq tmp (getenv "LastTemplate")) (wcmatch (strcase tmp) "\\\\NetworkName*"))(C:Setup_menus))

;;works;;;;;(cond ((wcmatch (strcase (getenv "LastTemplate")) "\\\\NetworkName*")(C:Setup_menus)) )

;;does not work;;(and (= 'str (type (getenv "LastTemplate")))(wcmatch (strcase (getenv "LastTemplate")) "C:\\USERS*")(c:Setup_menus)(Setup_menus) )


I have a Icon Shortcut with the following switch:
Code: [Select]
/b "C:\Test\Startup.scr"
Code: [Select]
(if (and (setq tmp (getenv "LastTemplate")) (wcmatch (strcase tmp) "C:\\USERS\\*"))(ALERT "Setting Up Menus."))"
If I run this from the desktop shortcut I can not get it to work. But if I drag it into CAD Model it works.

Code: [Select]
(if (and (setq tmp (getenv "LastTemplate")) (wcmatch (strcase tmp) "C:\\\\USERS\\\\*"))(ALERT "Setting Up Menus."))"
If I run this from the desktop shortcut, it works, but if I drag it in; it does not.

Weird stuff. lol


Civil3D 2020