Yet Another Cond Statement Issue

Yet Another Cond Statement Issue

The_CADalyst
Contributor Contributor
923 Views
8 Replies
Message 1 of 9

Yet Another Cond Statement Issue

The_CADalyst
Contributor
Contributor

Hello All, 

 

I know there is tons of posts on this topic, and I have done my best to format according to the examples that I see, but I am not getting anything when I run my code snippet below. What I am trying to do is removing layouts according the amount of floors a model home has. The variable "Floor_Count" is set by another function prior to this one running. But, when I run the code below, it does not remove any of the layouts. The options available are 1, 2, 3, and 4. If 1, 2, or 3 are selected, then it will remove the appropriate sheets. If 4 is selected, then no sheets will be removed. I have tried using "(and (= Floor_Count 1))..." but I get the same results as well. Any insight on what I am missing? 

 

 

 

(defun DeleteFloorLayouts ( / cmd FloorLayouts x)

 ;Remove Floor Layouts
	(foreach x FloorLayouts
		(vl-catch-all-apply
			'vla-add
			(list (vla-get-layouts (vla-get-activedocument (vlax-get-acad-object))) x)
		)
		(cond 
			((/= Floor_Count 1)
				(setq FloorLayouts '("E2_AL" "M2_AL" "P2_AL" "E3_AL" "M3_AL" "P3_AL" "E4_AL" "M4_AL" "P4_AL"))
				(command "-LAYOUT" "_d" x)
				(princ "\nExcess floor layouts for the 2nd-4th floors removed\n")
			)
			((/= Floor_Count 2)
				(setq FloorLayouts '("E3_AL" "M3_AL" "P3_AL" "E4_AL" "M4_AL" "P4_AL"))
				(command "-LAYOUT" "_d" x)
				(princ "\nExcess floor layouts for the 3rd And 4th floors removed\n")
			)
			((/= Floor_Count 3)
				(setq FloorLayouts '("E4_AL" "M4_AL" "P4_AL"))
				(command "-LAYOUT" "_d" x)
				(princ "\nExcess floor layouts for the 4th floor removed\n")
			)
			;if 4 floors, remove nothing
			(princ "\nNO floor layouts removed!\n")
		)
	)
  (princ)
);defun
0 Likes
Accepted solutions (2)
924 Views
8 Replies
Replies (8)
Message 2 of 9

paullimapa
Mentor
Mentor

assuming your floor_count is a number & not a string, modify the last section of your cond statement with:

			;if 4 floors, remove nothing
                 (t
			(princ "\nAutoLot> NO floor layouts removed!\n")
                 ) ; else

Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes
Message 3 of 9

paullimapa
Mentor
Mentor

also I think you need to check the conditions based on the actual number like:

((= Floor_Count 1)
)
((= Floor_Count 2)
)
((= Floor_Count 3)
)
(t
)

Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes
Message 4 of 9

paullimapa
Mentor
Mentor

You'll also need to change your delete layout code because in order for the foreach function to work you have to define the list first. But since you're defining the list inside the conditions, then you'll have to place the foreach function inside the condition also like this:

 

 

				(setq FloorLayouts '("E2_AL" "M2_AL" "P2_AL" "E3_AL" "M3_AL" "P3_AL" "E4_AL" "M4_AL" "P4_AL"))
                              (foreach x FloorLayouts
				(command "-LAYOUT" "_d" x)
                              ) ; foreach

 

 


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes
Message 5 of 9

Kent1Cooper
Consultant
Consultant

In addition to what @paullimapa has pointed out:  With only one thing to do in the fallback [4] condition, you don't even need the T "test."  You can just do it, but it still needs to be wrapped into another pair of parentheses for the condition:

 

;if 4 floors, remove nothing
((prompt "\nAutoLot> NO floor layouts removed!\n"))

 

[And I would use (prompt) rather than (princ) -- that's what you're doing, and the (prinX) functions include capabilities like writing to external files that you don't need.]

Kent Cooper, AIA
0 Likes
Message 6 of 9

The_CADalyst
Contributor
Contributor

Thank you both @paullimapa and @Kent1Cooper. I made the updates suggested above, set my floor count to just 1 to test if it would remove floors 2-4 using (setq Floor_Count "1") , and now I only get a "NO floor layouts removed!" as though my floor count is 4. So, it appears to be working halfway, but not reading my floor count variable. Does the code below need further adjustments or did I mess something up?

 

(defun DeleteFloorLayouts ( / cmd FloorLayouts x)

 ;Remove Floor Layouts
	(vl-catch-all-apply
		'vla-add
		(list (vla-get-layouts (vla-get-activedocument (vlax-get-acad-object))) x)
	)
	(cond 
		((= Floor_Count 1)
			(setq FloorLayouts '("E2_AL" "M2_AL" "P2_AL" "E3_AL" "M3_AL" "P3_AL" "E4_AL" "M4_AL" "P4_AL"))
				(foreach x FloorLayouts
					(command "-LAYOUT" "_d" x)
				) ; foreach
			(prompt "\nExcess floor layouts for the 2nd-4th floors removed\n")
		)
		((= Floor_Count 2)
			(setq FloorLayouts '("E3_AL" "M3_AL" "P3_AL" "E4_AL" "M4_AL" "P4_AL"))
				(foreach x FloorLayouts
					(command "-LAYOUT" "_d" x)
				) ; foreach
			(prompt "\nExcess floor layouts for the 3rd And 4th floors removed\n")
		)
		((= Floor_Count 3)
			(setq FloorLayouts '("E4_AL" "M4_AL" "P4_AL"))
				(foreach x FloorLayouts
					(command "-LAYOUT" "_d" x)
				) ; foreach
			(prompt "\nExcess floor layouts for the 4th floor removed\n")
		)
		;if 4 floors, remove nothing
		((prompt "\nNO floor layouts removed!\n"))
	) ; cond
  (princ)
);defun

 

0 Likes
Message 7 of 9

paullimapa
Mentor
Mentor

try this instead:

(setq Floor_Count 1)

or

(setq Floor_Count 2)

or

(setq Floor_Count 3)

or

(setq Floor_Count 4)

 

But if Floor_Count really is a string = "1" or "2" or "3" or "4", then you'll need to convert it to an integer then use atoi function to convert it from ascii to integer like this:

(setq Floor_Count (atoi "1"))


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes
Message 8 of 9

ronjonp
Mentor
Mentor
Accepted solution

@The_CADalyst 

Here's another way to do this using WCMATCH

 

(defun c:foo (/ f i r)
  ;; RJP » 2023-03-16
  ;; Removes 'floor' tabs above a certain input
  (cond	((and (setq i (getint "\nEnter Floors: ")) (<= 1 i 3))
	 (repeat (- 4 i) (setq r (cons (setq i (1+ i)) r)))
	 (foreach n (setq r (mapcar 'itoa (reverse r)))
	   (vlax-for l (vla-get-layouts (vla-get-activedocument (vlax-get-acad-object)))
	     (if (wcmatch (vla-get-name l) (strcat "[E M P]" n "_AL"))
	       (vla-delete l)
	     )
	   )
	 )
	 (princ (strcat "\nFLOORS " (vl-princ-to-string r) " REMOVED!"))
	)
	((princ "\nINPUT RANGE 1-3 !!"))
  )
  (princ)
)
(vl-load-com)

 

 

 

Message 9 of 9

komondormrex
Mentor
Mentor
Accepted solution

hey, keeping stylistics, but slightly reshuffled, without adding layouts.

 

 

 

 

(defun DeleteFloorLayouts ()
 ;Remove Floor Layouts
	(foreach x 
			(cond 
				((= Floor_Count 1)
					(princ "\nExcess floor layouts for the 2nd-4th floors removed\n")
					'("E2_AL" "M2_AL" "P2_AL" "E3_AL" "M3_AL" "P3_AL" "E4_AL" "M4_AL" "P4_AL")
				)
				((= Floor_Count 2)
					(princ "\nExcess floor layouts for the 3rd And 4th floors removed\n")
					'("E3_AL" "M3_AL" "P3_AL" "E4_AL" "M4_AL" "P4_AL")
				)
				((= Floor_Count 3)
					(princ "\nExcess floor layouts for the 4th floor removed\n")
					'("E4_AL" "M4_AL" "P4_AL")
				)
				;if 4 floors, remove nothing
				( t
					(princ "\nNO floor layouts removed!\n")
					nil
				)
			)
			(command "-LAYOUT" "_d" x)
	)
  (princ)
);defun