Jump to content

Cond Statement Problems


Lee Mac

Recommended Posts

OK, so I was experimenting with the LOGAND command, just as a learning exercise.

 

I have tried to build this LISP:

 

(defun c:BXLister (/ BX_Ans BX_Ent BX_List)
   (initget "Blocks Xrefs")
   (setq BX_Ans (getkword "\nList Blocks or XRefs? [blocks/XRefs] <Both> : "))
   (cond
   ((= BX_Ans "Blocks")
    (setq BX_Ent  (tblnext "BLOCK" T)
          BX_List "Blocks: \n"
    ) ;_  end setq
    (while    BX_Ent
        (if (eq 0 (logand 0 (cdr (assoc 70 BX_Ent))))
        (setq BX_List (strcat BX_List (cdr (assoc 2 BX_Ent)) "\n"))
        ) ;_  end if
        (setq BX_Ent (tblnext "BLOCK"))
    ) ;_  end while
   )
   ((= BX_Ans "XRefs")
    (setq BX_Ent  (tblnext "BLOCK" T)
          BX_List "XRefs: \n"
    ) ;_  end setq
    (while    BX_Ent
        (if (eq 4 (logand 4 (cdr (assoc 70 BX_Ent))))
        (setq BX_List (strcat BX_List (cdr (assoc 2 BX_Ent)) "\n"))
        ) ;_  end if
        (setq BX_Ent (tblnext "BLOCK"))
    ) ;_  end while
   )
   ((/= BX_Ans "XRefs" "Blocks")
    (setq BX_Ent  (tblnext "BLOCK" T)
          BX_List "Blocks & XRefs: \n"
    ) ;_  end setq
    (while    BX_Ent
        (setq BX_List (strcat BX_List (cdr (assoc 2 BX_Ent)) "\n"))
        (setq BX_Ent (tblnext "BLOCK"))
    ) ;_  end while
   )
   ) ;_  end cond
   (alert BX_List)
   (princ)
) ;_  end defun

 

I intend it to list either Xref or Block depending on the input of the user.

 

However, its not in fact the logand function I am having problems with - the problem occurs when the user enters to list Xrefs - the program will list blocks AND xrefs.

 

I think there is an error with the cond statement, but I can't see what :?

 

Thanks in advance guys.

Link to comment
Share on other sites

Ok,

 

I have performed the same task in a different manner - posted for everyone's benefit:

 

(defun c:BXLister (/ BX_Ent BX_BList BX_XList)
   (setq BX_Ent   (tblnext "BLOCK" T)
     BX_BList "Blocks: \n"
     BX_XList "Xrefs: \n"
   ) ;_  end setq
   (while BX_Ent
   (if (eq 0 (logand 0 (cdr (assoc 70 BX_Ent))))
       (setq BX_BList (strcat BX_BList (cdr (assoc 2 BX_Ent)) "\n"))
   ) ;_  end if
   (if (eq 4 (logand 4 (cdr (assoc 70 BX_Ent))))
       (setq BX_XList (strcat BX_XList (cdr (assoc 2 BX_Ent)) "\n"))
   ) ;_  end if
   (setq BX_Ent (tblnext "BLOCK"))
   ) ;_  end while
   (alert (strcat BX_BList "\n" BX_XList))
   (princ)
) ;_  end defun

Link to comment
Share on other sites

Lee,

 

The easiest way to step thru a nongraphical table is with a ( while ) call:

 

 

[b][color=BLACK]([/color][/b]defun c:blist [b][color=FUCHSIA]([/color][/b]/ tdef bn al xl bl[b][color=FUCHSIA])[/color][/b]
 [b][color=FUCHSIA]([/color][/b]while [b][color=NAVY]([/color][/b]setq tdef [b][color=MAROON]([/color][/b]tblnext [color=#2f4f4f]"BLOCK"[/color] [b][color=GREEN]([/color][/b]not tdef[b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b][b][color=NAVY])[/color][/b]
        [b][color=NAVY]([/color][/b]setq bn [b][color=MAROON]([/color][/b]cdr [b][color=GREEN]([/color][/b]assoc 2 tdef[b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b][b][color=NAVY])[/color][/b]
        [b][color=NAVY]([/color][/b]cond [b][color=MAROON]([/color][/b][b][color=GREEN]([/color][/b]= [b][color=BLUE]([/color][/b]logand [b][color=RED]([/color][/b]cdr [b][color=PURPLE]([/color][/b]assoc 70 tdef[b][color=PURPLE])[/color][/b][b][color=RED])[/color][/b] 4[b][color=BLUE])[/color][/b] 4[b][color=GREEN])[/color][/b]
               [b][color=GREEN]([/color][/b]setq xl [b][color=BLUE]([/color][/b]cons bn xl[b][color=BLUE])[/color][/b][b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b]
              [b][color=MAROON]([/color][/b][b][color=GREEN]([/color][/b]= [b][color=BLUE]([/color][/b]logand [b][color=RED]([/color][/b]cdr [b][color=PURPLE]([/color][/b]assoc 70 tdef[b][color=PURPLE])[/color][/b][b][color=RED])[/color][/b] 1[b][color=BLUE])[/color][/b] 1[b][color=GREEN])[/color][/b]
               [b][color=GREEN]([/color][/b]setq al [b][color=BLUE]([/color][/b]cons bn al[b][color=BLUE])[/color][/b][b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b]
              [b][color=MAROON]([/color][/b]T
               [b][color=GREEN]([/color][/b]setq bl [b][color=BLUE]([/color][/b]cons bn bl[b][color=BLUE])[/color][/b][b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b][b][color=NAVY])[/color][/b][b][color=FUCHSIA])[/color][/b]

  [b][color=FUCHSIA]([/color][/b]princ [color=#2f4f4f]"\nXREF List = "[/color][b][color=FUCHSIA])[/color][/b]
  [b][color=FUCHSIA]([/color][/b]prin1 xl[b][color=FUCHSIA])[/color][/b]
  [b][color=FUCHSIA]([/color][/b]princ [color=#2f4f4f]"\nANONYMOUS BLOCK List = "[/color][b][color=FUCHSIA])[/color][/b]
  [b][color=FUCHSIA]([/color][/b]prin1 al[b][color=FUCHSIA])[/color][/b]
  [b][color=FUCHSIA]([/color][/b]princ [color=#2f4f4f]"\nSTANDARD BLOCK List = "[/color][b][color=FUCHSIA])[/color][/b]
  [b][color=FUCHSIA]([/color][/b]prin1 bl[b][color=FUCHSIA])[/color][/b]
  [b][color=FUCHSIA]([/color][/b]prin1[b][color=FUCHSIA])[/color][/b][b][color=BLACK])[/color][/b]

-David

Link to comment
Share on other sites

Hi Lee,

Regarding your first code example with the COND, I haven't studied it enough to completely understand what you're doing, but I did notice the line

(/= BX_Ans "XRefs" "Blocks") . My reference says that the NOT EQUAL function is not defined with more than two arguments. This might be a problem. The weird thing is, in the reference manual, the function is written (/= atom atom ...) , suggesting more than two arguments! Typo, I guess.

Link to comment
Share on other sites

I was under the impression that the functions:

 

=, /=, , = could take any number of args.

 

If you type:

 

(/= 4 5 6)

in your command line, it returns T.

 

Thanks for your time though CALCAD. :P

Link to comment
Share on other sites

I like David's.:)

Modified output to this:

   (princ "\n   XREF List = ")
  (mapcar 'print xl)
  (princ "\n   ANONYMOUS BLOCK List = ")
  (mapcar 'print al)
  (princ "\n   STANDARD BLOCK List = ")
  (mapcar 'print bl)
  (prin1))

Link to comment
Share on other sites

Lee,

My little investigation of the NOT EQUAL function resulted in discovering mistakes or omissions in two of my references and

a surprising (to me) non-standard behavior of this function in Autolisp. You may wish to test your Autocad 2004 to see if it

conforms to the following :

Extract from the Autocad 2008 on-line help file :

.... Returns T if no two successive arguments are the same in value; otherwise nil. If only one argument is supplied, /= returns T.

Note that the behavior of /= does not quite conform to other LISP dialects. The standard behavior is to return T if no two arguments in the list have the same value. In AutoLISP, /= returns T if no successive arguments have the same value; see the examples that follow.

Examples

(/= 10 20) returns T

(/= "you" "you") returns nil

(/= 5.43 5.44) returns T

(/= 10 20 10 20 20) returns nil

(/= 10 20 10 20) returns T

Note : In the last example, although there are two arguments in the list with the same value, they do not follow one another; thus /= evaluates to T.

Link to comment
Share on other sites

the (/=) function has always been flakey and the documentation poorly written. I avoid it with multiple arguments it at all costs as that it has very little real world value.

 

Lee,

 

I wrote a program that reads .LSP files and formats it into HTML a long time ago. I redid it to output in BBC when CADTutor changed its format some time ago and just did some tweaking on it. I've always thought color coded paraenthisis was a good proof reading tool. Also a good learning tool.

 

I'm not a big fan of ending comments i.e ); end if

 

I use them from time to time when developing a very lagre rotuine but omit them in the final editions. -David

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...