Doing a nested conditional inside a condition statement

Doing a nested conditional inside a condition statement

stanovb
Advocate Advocate
735 Views
7 Replies
Message 1 of 8

Doing a nested conditional inside a condition statement

stanovb
Advocate
Advocate

My company has a lisp routine that copies over files from our network based on a dialog box selection. The first selection is what we refer to as the project type. We create a variable called PJT that is inside of a large condition statement. This is written to an asci file along with some other variables &  is copied into the project directory. I have been trying to consolidate this routine by creating some additional selections that are in a dialog box dcl file. I am trying to figure out how I can use either an if statement or an additional condition statement so when the user selects on project type, base, & class it will run a specific block of code. I will upload my routine along with my dialog box file but it is a rather large amount of code. Here is what it looks like:

 

stanovb_0-1636467355544.png

((= PJT "IA")

(COND
(AND((= BASE "SMART30")(= CLASS "NARROW"))
;(alert "Hello and welcome to AutoLISP!")
(PROGN
(VL-LOAD-COM)
(VL-LOAD-REACTORS)
;(setq PJTX PJT)
;(setq PJT "IA")

(setq pno (strcat curyear pjt pno))
(setq dirname (STRCAT pno " " pad "," " " city "," " " state))
(subdirlist)

(setq filenm (STRCAT "P:\\" 4CHYEAR " WENDY'S\\" dirname "\\TITLE.ASC"))

(writelist)

);end PROGN
(setq new-dwg (STRCAT stn " " CITY " " STATE "_ARCH" ".DWG"))
(setq filepath (STRCAT "P:\\" 4CHYEAR " WENDY'S\\" dirname "\\" new-dwg))
(vl-file-copy "H:\\Overlays\\IA Wendys\\_New Bldg\\Smart 30 Narrow 2-0\\00000_STORENAME_ARCH.dwg" filepath)

(setq new-dwg (STRCAT stn " " CITY " " STATE "_COVER" ".DWG"))
(setq filepath (STRCAT "P:\\" 4CHYEAR " WENDY'S\\" dirname "\\" new-dwg))
(vl-file-copy "H:\\Overlays\\IA Wendys\\_New Bldg\\Smart 30 Narrow 2-0\\00000_STORENAME_COVER.dwg" filepath)

(setq new-dwg (STRCAT stn " " CITY " " STATE "_ELEC" ".DWG"))
(setq filepath (STRCAT "P:\\" 4CHYEAR " WENDY'S\\" dirname "\\" new-dwg))
(vl-file-copy "H:\\Overlays\\IA Wendys\\_New Bldg\\Smart 30 Narrow 2-0\\00000_STORENAME_ELEC.dwg" filepath)

(setq new-dwg (STRCAT stn " " CITY " " STATE "_ELEV-EXT.DWG"))
(setq filepath (STRCAT "P:\\" 4CHYEAR " WENDY'S\\" dirname "\\" new-dwg))
(vl-file-copy "H:\\Overlays\\IA Wendys\\_New Bldg\\Smart 30 Narrow 2-0\\00000_STORENAME_ELEV-EXT.dwg" filepath)

(setq new-dwg (STRCAT stn " " CITY " " STATE "_ELEV-INT.DWG"))
(setq filepath (STRCAT "P:\\" 4CHYEAR " WENDY'S\\" dirname "\\" new-dwg))
(vl-file-copy "H:\\Overlays\\IA Wendys\\_New Bldg\\Smart 30 Narrow 2-0\\00000_STORENAME_ELEV-INT.dwg" filepath)

(setq new-dwg (STRCAT stn " " CITY " " STATE "_EQUIP.DWG"))
(setq filepath (STRCAT "P:\\" 4CHYEAR " WENDY'S\\" dirname "\\" new-dwg))
(vl-file-copy "H:\\Overlays\\IA Wendys\\_New Bldg\\Smart 30 Narrow 2-0\\00000_STORENAME_EQUIP.dwg" filepath)

(setq new-dwg (STRCAT stn " " CITY " " STATE "_FINISH.DWG"))
(setq filepath (STRCAT "P:\\" 4CHYEAR " WENDY'S\\" dirname "\\" new-dwg))
(vl-file-copy "H:\\Overlays\\IA Wendys\\_New Bldg\\Smart 30 Narrow 2-0\\00000_STORENAME_FINISH.dwg" filepath)

(setq new-dwg (STRCAT stn " " CITY " " STATE "_MECH.DWG"))
(setq filepath (STRCAT "P:\\" 4CHYEAR " WENDY'S\\" dirname "\\" new-dwg))
(vl-file-copy "H:\\Overlays\\IA Wendys\\_New Bldg\\Smart 30 Narrow 2-0\\00000_STORENAME_MECH.dwg" filepath)

(setq new-dwg (STRCAT stn " " CITY " " STATE "_PLUMB.DWG"))
(setq filepath (STRCAT "P:\\" 4CHYEAR " WENDY'S\\" dirname "\\" new-dwg))
(vl-file-copy "H:\\Overlays\\IA Wendys\\_New Bldg\\Smart 30 Narrow 2-0\\00000_STORENAME_PLUMB.dwg" filepath)

(setq new-dwg (STRCAT stn " " CITY " " STATE "_SECTIONS.DWG"))
(setq filepath (STRCAT "P:\\" 4CHYEAR " WENDY'S\\" dirname "\\" new-dwg))
(vl-file-copy "H:\\Overlays\\IA Wendys\\_New Bldg\\Smart 30 Narrow 2-0\\00000_STORENAME_SECTIONS.dwg" filepath)

(setq new-dwg (STRCAT stn " " CITY " " STATE "_SITE.DWG"))
(setq filepath (STRCAT "P:\\" 4CHYEAR " WENDY'S\\" dirname "\\" new-dwg))
(vl-file-copy "H:\\Overlays\\IA Wendys\\_New Bldg\\Smart 30 Narrow 2-0\\00000_STORENAME_SITE.dwg" filepath)

(setq new-dwg (STRCAT stn " " CITY " " STATE "_SPECS.DWG"))
(setq filepath (STRCAT "P:\\" 4CHYEAR " WENDY'S\\" dirname "\\" new-dwg))
(vl-file-copy "H:\\Overlays\\IA Wendys\\_New Bldg\\Smart 30 Narrow 2-0\\00000_STORENAME_SPECS.dwg" filepath)

(setq new-dwg (STRCAT stn " " CITY " " STATE ".dst"))
(setq filepath (STRCAT "P:\\" 4CHYEAR " WENDY'S\\" dirname "\\" new-dwg))
(vl-file-copy "H:\\Overlays\\IA Wendys\\_New Bldg\\Smart 30 Narrow 2-0\\Smart_30_Narrow_2-0_New_Build.dst" filepath)

(setq new-dwg "XK-BASE.dwg")
(setq filepath (STRCAT "P:\\" 4CHYEAR " WENDY'S\\" dirname "\\" new-dwg))
(vl-file-copy "H:\\Overlays\\IA Wendys\\_New Bldg\\Smart 30 Narrow 2-0\\XK-BASE.DWG" filepath)

(setq new-dwg "XPLAN.dwg")
(setq filepath (STRCAT "P:\\" 4CHYEAR " WENDY'S\\" dirname "\\" new-dwg))
(vl-file-copy "H:\\Overlays\\IA Wendys\\_New Bldg\\Smart 30 Narrow 2-0\\XPLAN.DWG" filepath)

(setq new-dwg "WDTLBK.dwg")
(setq filepath (STRCAT "P:\\" 4CHYEAR " WENDY'S\\" dirname "\\" new-dwg))
(vl-file-copy "H:\\Overlays\\IA Wendys\\_New Bldg\\Smart 30 Narrow 2-0\\WDTLBK.DWG" filepath)
);END Wendy's SMART 30 NARROW 2.0 NEW BUILD COND
))

0 Likes
736 Views
7 Replies
Replies (7)
Message 2 of 8

lkottler
Explorer
Explorer

I'm not entirely sure what you're asking, but you could store a list of each possible filename to reduce number of lines. If your intent is do copy every filetype you could do something like:

(setq dwgtypes (list "ARCH" "COVER" "ELEC" "ELEV-EXT" "ELEV-INT" "EQUIP" "FINISH" "MECH" "PLUMB" "SECTIONS" "SITE" "SPECS"))

(foreach dwgfile dwgtypes
	(setq new-dwg (STRCAT stn " " CITY " " STATE "_" dwgfile ".DWG"))
	(setq filepath (STRCAT "P:\\" 4CHYEAR " WENDY'S\\" dirname "\\" new-dwg))
	(vl-file-copy (strcat "H:\\Overlays\\IA Wendys\\_New Bldg\\Smart 30 Narrow 2-0\\00000_STORENAME_" dwgfile ".dwg") filepath)
)

 
Or if you are only copying one file based on user input you could check

(setq user_input (...)) ; However you are acquiring the user's choice

(setq dwgtypes (list "ARCH" "COVER" "ELEC" "ELEV-EXT" "ELEV-INT" "EQUIP" "FINISH" "MECH" "PLUMB" "SECTIONS" "SITE" "SPECS"))

(if (member user_input dwgtypes) ; If the userinput is a member of the list
	(progn
		(setq new-dwg (STRCAT stn " " CITY " " STATE "_" user_input ".DWG"))
		(setq filepath (STRCAT "P:\\" 4CHYEAR " WENDY'S\\" dirname "\\" new-dwg))
		(vl-file-copy (strcat "H:\\Overlays\\IA Wendys\\_New Bldg\\Smart 30 Narrow 2-0\\00000_STORENAME_" user_input ".dwg") filepath)
	) ;progn

	; Otherwise if userinput is something else...
	
) ;if
0 Likes
Message 3 of 8

john.uhden
Mentor
Mentor

Of course you can nest conditions.

But you might prefer the logic of (and...)

(cond
  ((and (= a 1)(= b 2)(= c 3))
    (abc 1 2 3)
  )
  ((and (= a 2)(= b 2)(= c 3))
    (abc 2 2 3)
  )
  ; etc.
)
;; But of course that's a long way instead of
(abc a b c)

John F. Uhden

0 Likes
Message 4 of 8

stanovb
Advocate
Advocate

Sometimes its difficult for me to explain things on a forum. We have project type prototypes set up that we want to copy over to a project directory & then rename with the new project name when we start a job. The project type could be "IA", "BCK", "BVB", etc. Over the years we created more and more project types & the routine has gotten too big so I am trying to consolidate it. Some of the project types could have another description underneath the project type such as in "IA Smart 30 Narrow. I created those selections in the dialog box so when the user selects

stanovb_0-1636550701488.png

This code would run

(COND
((AND(= PJT "IA")(= BASE "SMART30")(= CLASS "NARROW"))
(PROGN
(VL-LOAD-COM)
(VL-LOAD-REACTORS)
(setq pno (strcat curyear pjt pno))
(setq dirname (STRCAT pno " " pad "," " " city "," " " state))
(subdirlist)

(setq filenm (STRCAT "P:\\" 4CHYEAR " WENDY'S\\" dirname "\\TITLE.ASC"))

(writelist)

);end PROGN
(setq new-dwg (STRCAT stn " " CITY " " STATE "_ARCH" ".DWG"))
(setq filepath (STRCAT "P:\\" 4CHYEAR " WENDY'S\\" dirname "\\" new-dwg))
(vl-file-copy "H:\\Overlays\\IA Wendys\\_New Bldg\\Smart 30 Narrow 2-0\\00000_STORENAME_ARCH.dwg" filepath)

(setq new-dwg (STRCAT stn " " CITY " " STATE "_COVER" ".DWG"))
(setq filepath (STRCAT "P:\\" 4CHYEAR " WENDY'S\\" dirname "\\" new-dwg))
(vl-file-copy "H:\\Overlays\\IA Wendys\\_New Bldg\\Smart 30 Narrow 2-0\\00000_STORENAME_COVER.dwg" filepath)

(setq new-dwg (STRCAT stn " " CITY " " STATE "_ELEC" ".DWG"))
(setq filepath (STRCAT "P:\\" 4CHYEAR " WENDY'S\\" dirname "\\" new-dwg))
(vl-file-copy "H:\\Overlays\\IA Wendys\\_New Bldg\\Smart 30 Narrow 2-0\\00000_STORENAME_ELEC.dwg" filepath)

(setq new-dwg (STRCAT stn " " CITY " " STATE "_ELEV-EXT.DWG"))
(setq filepath (STRCAT "P:\\" 4CHYEAR " WENDY'S\\" dirname "\\" new-dwg))
(vl-file-copy "H:\\Overlays\\IA Wendys\\_New Bldg\\Smart 30 Narrow 2-0\\00000_STORENAME_ELEV-EXT.dwg" filepath)

(setq new-dwg (STRCAT stn " " CITY " " STATE "_ELEV-INT.DWG"))
(setq filepath (STRCAT "P:\\" 4CHYEAR " WENDY'S\\" dirname "\\" new-dwg))
(vl-file-copy "H:\\Overlays\\IA Wendys\\_New Bldg\\Smart 30 Narrow 2-0\\00000_STORENAME_ELEV-INT.dwg" filepath)

(setq new-dwg (STRCAT stn " " CITY " " STATE "_EQUIP.DWG"))
(setq filepath (STRCAT "P:\\" 4CHYEAR " WENDY'S\\" dirname "\\" new-dwg))
(vl-file-copy "H:\\Overlays\\IA Wendys\\_New Bldg\\Smart 30 Narrow 2-0\\00000_STORENAME_EQUIP.dwg" filepath)

(setq new-dwg (STRCAT stn " " CITY " " STATE "_FINISH.DWG"))
(setq filepath (STRCAT "P:\\" 4CHYEAR " WENDY'S\\" dirname "\\" new-dwg))
(vl-file-copy "H:\\Overlays\\IA Wendys\\_New Bldg\\Smart 30 Narrow 2-0\\00000_STORENAME_FINISH.dwg" filepath)

(setq new-dwg (STRCAT stn " " CITY " " STATE "_MECH.DWG"))
(setq filepath (STRCAT "P:\\" 4CHYEAR " WENDY'S\\" dirname "\\" new-dwg))
(vl-file-copy "H:\\Overlays\\IA Wendys\\_New Bldg\\Smart 30 Narrow 2-0\\00000_STORENAME_MECH.dwg" filepath)

(setq new-dwg (STRCAT stn " " CITY " " STATE "_PLUMB.DWG"))
(setq filepath (STRCAT "P:\\" 4CHYEAR " WENDY'S\\" dirname "\\" new-dwg))
(vl-file-copy "H:\\Overlays\\IA Wendys\\_New Bldg\\Smart 30 Narrow 2-0\\00000_STORENAME_PLUMB.dwg" filepath)

(setq new-dwg (STRCAT stn " " CITY " " STATE "_SECTIONS.DWG"))
(setq filepath (STRCAT "P:\\" 4CHYEAR " WENDY'S\\" dirname "\\" new-dwg))
(vl-file-copy "H:\\Overlays\\IA Wendys\\_New Bldg\\Smart 30 Narrow 2-0\\00000_STORENAME_SECTIONS.dwg" filepath)

(setq new-dwg (STRCAT stn " " CITY " " STATE "_SITE.DWG"))
(setq filepath (STRCAT "P:\\" 4CHYEAR " WENDY'S\\" dirname "\\" new-dwg))
(vl-file-copy "H:\\Overlays\\IA Wendys\\_New Bldg\\Smart 30 Narrow 2-0\\00000_STORENAME_SITE.dwg" filepath)

(setq new-dwg (STRCAT stn " " CITY " " STATE "_SPECS.DWG"))
(setq filepath (STRCAT "P:\\" 4CHYEAR " WENDY'S\\" dirname "\\" new-dwg))
(vl-file-copy "H:\\Overlays\\IA Wendys\\_New Bldg\\Smart 30 Narrow 2-0\\00000_STORENAME_SPECS.dwg" filepath)

(setq new-dwg (STRCAT stn " " CITY " " STATE ".dst"))
(setq filepath (STRCAT "P:\\" 4CHYEAR " WENDY'S\\" dirname "\\" new-dwg))
(vl-file-copy "H:\\Overlays\\IA Wendys\\_New Bldg\\Smart 30 Narrow 2-0\\Smart_30_Narrow_2-0_New_Build.dst" filepath)

(setq new-dwg "XK-BASE.dwg")
(setq filepath (STRCAT "P:\\" 4CHYEAR " WENDY'S\\" dirname "\\" new-dwg))
(vl-file-copy "H:\\Overlays\\IA Wendys\\_New Bldg\\Smart 30 Narrow 2-0\\XK-BASE.DWG" filepath)

(setq new-dwg "XPLAN.dwg")
(setq filepath (STRCAT "P:\\" 4CHYEAR " WENDY'S\\" dirname "\\" new-dwg))
(vl-file-copy "H:\\Overlays\\IA Wendys\\_New Bldg\\Smart 30 Narrow 2-0\\XPLAN.DWG" filepath)

(setq new-dwg "WDTLBK.dwg")
(setq filepath (STRCAT "P:\\" 4CHYEAR " WENDY'S\\" dirname "\\" new-dwg))
(vl-file-copy "H:\\Overlays\\IA Wendys\\_New Bldg\\Smart 30 Narrow 2-0\\WDTLBK.DWG" filepath)
);END Wendy's SMART 30 NARROW 2.0 NEW BUILD COND
)

 

The problem is i guess there is an error in this code because when I run the lisp routine i get

stp ; error: bad argument type: stringp nil

 

p.s. stp is the name of the command to run the routine. Also, the routine is supposed to write all of the selections that are made in the dialog box  to an asci file but it only gets to the 15th or so. It doesn't get to the 20th one.

0 Likes
Message 5 of 8

ВeekeeCZ
Consultant
Consultant

You know what, maybe start with Excel.

Make some spreadsheet with possible combinations and required results.

That could help you to structuralize the file.

 

The syntax of cond is following:

(cond
  ((and (= X 1a) (= Y 1b) (= Z 1c))
   (line 1)  ;; see no PROGN !
   (line 2)
   (line 3)
   ) ; end of cond1

  ((and (= X 2a) (= Y 2b) (= Z 2c))
   (line 1)  ;; see no PROGN !
   (line 2)
   (line 3)
   ) ; end of cond2

  ) ; end of cond func

 

Of is you have IF or combinations IF and COND

(if (= x y)
  (single line of then)
  (single line of else)
  )


(if (= x y)
  (progn
    (1st line of then)
    (2nd line of then)
    (3th line of then)
    )
  (single line of else)
  )

(if (= x z)
  (cond ; see that cond could replace progn becouse its a single statement!
    ((= x a)
     (1st line)
     (2nd line)
     )

    ((= y a)  
     (1st line)
     (2nd line)
     )
    ) ; end of cond

  (progn
    (1st line of else)
    (2nd line of else)
    ) ; end of progn
  ) ; end of if
  

 

Your code looks like this....

((= PJT "IA")
  
  (COND
    
    (AND((= BASE "SMART30")(= CLASS "NARROW"))
((and (= BASE "SMART30")(= CLASS "NARROW")) ;cond 1 ;(alert "Hello and welcome to AutoLISP!") ;(PROGN ; no need for progn (VL-LOAD-COM) (VL-LOAD-REACTORS) ;(setq PJTX PJT) ;(setq PJT "IA") (setq pno (strcat curyear pjt pno)) (setq dirname (STRCAT pno " " pad "," " " city "," " " state)) (subdirlist) (setq filenm (STRCAT "P:\\" 4CHYEAR " WENDY'S\\" dirname "\\TITLE.ASC")) (writelist) );end PROGN
; now what is here ... another cond or you finish (progn) early??
(setq new-dwg (STRCAT stn " " CITY " " STATE "_ARCH" ".DWG")) (setq filepath (STRCAT "P:\\" 4CHYEAR " WENDY'S\\" dirname "\\" new-dwg)) (vl-file-copy "H:\\Overlays\\IA Wendys\\_New Bldg\\Smart 30 Narrow 2-0\\00000_STORENAME_ARCH.dwg" filepath) (setq new-dwg (STRCAT stn " " CITY " " STATE "_COVER" ".DWG")) (setq filepath (STRCAT "P:\\" 4CHYEAR " WENDY'S\\" dirname "\\" new-dwg)) (vl-file-copy "H:\\Overlays\\IA Wendys\\_New Bldg\\Smart 30 Narrow 2-0\\00000_STORENAME_COVER.dwg" filepath) ... (setq new-dwg "XPLAN.dwg") (setq filepath (STRCAT "P:\\" 4CHYEAR " WENDY'S\\" dirname "\\" new-dwg)) (vl-file-copy "H:\\Overlays\\IA Wendys\\_New Bldg\\Smart 30 Narrow 2-0\\XPLAN.DWG" filepath) (setq new-dwg "WDTLBK.dwg") (setq filepath (STRCAT "P:\\" 4CHYEAR " WENDY'S\\" dirname "\\" new-dwg)) (vl-file-copy "H:\\Overlays\\IA Wendys\\_New Bldg\\Smart 30 Narrow 2-0\\WDTLBK.DWG" filepath) );END Wendy's SMART 30 NARROW 2.0 NEW BUILD COND ))

 

0 Likes
Message 6 of 8

john.uhden
Mentor
Mentor
That's too much code for me to digest.
I'm lousy with VLIDE, so what I do is place a (setq OK 3) etc. after or
inside every operation, so when it fails I check the value of OK to see
where it failed. It's tedious, but it works.

John F. Uhden

0 Likes
Message 7 of 8

lkottler
Explorer
Explorer

The problem is i guess there is an error in this code because when I run the lisp routine i get

stp ; error: bad argument type: stringp nil


This probably means one of your arguments is getting set to nil. It's anticipating a string, but finding an empty value.  I'd recommend using the Visual Lisp Editor, setting Debug -> Break On Error, and after running your script to error, use Debug -> Last Break Source. It should highlight which variable/command has the nil string.
vlide.png

0 Likes
Message 8 of 8

stanovb
Advocate
Advocate

Thank you for your help everyone. I think I figured out what the problem was. We had created a temporary variable that I was not accounting for in that part of my code,  once I set that temporary variable everything did what it was supposed to. It was not getting through all of the variables so it was not populating the asci file.

0 Likes