Author Topic: if statement throwing in a error: bad function: 1  (Read 5773 times)

0 Members and 1 Guest are viewing this topic.

zride91

  • Guest
if statement throwing in a error: bad function: 1
« on: January 12, 2012, 09:58:50 PM »
I have a lisp which I have pieced together from help on other forums and numerous other web sites to find block references with a 803000 prefix, then explode them.  It was working well, but I had to add an if statement to cancel the lisp if blocks with out the prefix were not found.  With the if statement in here, everything seems to work fine in files with and without the searched block, but I am getting error: bad function: 1 after the lisp is ran, otherwise everything seems to be working.  Any suggestions?

I've been diggin around on this for a while now, and I've tried a few other ways to include the IF, but this is as close as I've got so far.  If anyone with a good eye can see where I'm going wrong, some direction would be appreciated.

Code - Auto/Visual Lisp: [Select]
  1.  (defun c:xrefexplode ( / lastent flag ss)
  2.   (setq lastent (entlast)
  3.         flag (getvar "qaflags"))
  4.   (setq ss (ssget "_X" '((0 . "INSERT" ) (2 . "803000*") (410 . "Model")))) ;;place all blocks in model with 803000 prefix in selection set
  5.   (if(/= ss nilnil) ;;if no blocks found, end
  6.   ((setvar "qaflags" 1)
  7.   (command "._EXPLODE" ss "") ;;explode all in selection set
  8.   (setvar "qaflags" flag)
  9.   (setq ss (ssadd))
  10.   (while (entnext lastent)
  11.   (setq lastent (entnext lastent))
  12.   (ssadd lastent ss)
  13.   )
  14.   )
  15.   )
  16.   (princ)
  17. )
Thank you.
« Last Edit: January 12, 2012, 10:21:26 PM by zride91 »

T.Willey

  • Needs a day job
  • Posts: 5251
Re: if statement throwing in a error: bad function: 1
« Reply #1 on: January 12, 2012, 11:58:43 PM »
For multiple calls within an if statement, you need to call ' progn '

(if <something>
  (progn
    <some stuff>
    <some other stuff>
    <some other stuff>
  )
  <single call>
)

Hope that helps.
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: if statement throwing in a error: bad function: 1
« Reply #2 on: January 13, 2012, 06:47:15 AM »
You are receiving the error "error: bad function: 1" because of this expression following the IF function:

Code - Auto/Visual Lisp: [Select]
  1. (if (/= ss nilnil)
  2.     (
  3.         (setvar "qaflags" 1)
  4.         (command "._EXPLODE" ss "")
  5.         (setvar "qaflags" flag)
  6.         (setq ss (ssadd))
  7.         (while (entnext lastent)
  8.             (setq lastent (entnext lastent))
  9.             (ssadd lastent ss)
  10.         )
  11.     )
  12. )

Firstly, 'nil' is a protected symbol meaning just that (nil); 'nilnil' is just an symbol which happens to not be used as a variable at this point, and so also evaluates to nil; better to avoid the risk of 'nilnil' being something other than nil, and just use 'nil' instead.

Now, back to your error, if 'ss' is non-nil,

Code - Auto/Visual Lisp: [Select]
  1. (setvar "qaflags" 1)

is being evaluated first, and, since setvar returns the value that the System Variable is set to, this expression returns 1.

Now, the value 1 returned is following an un-quoted open parenthesis:

Code - Auto/Visual Lisp: [Select]
  1.     (
  2.         1
  3.         (command "._EXPLODE" ss "")
  4.         (setvar "qaflags" flag)
  5.         (setq ss (ssadd))
  6.         (while (entnext lastent)
  7.             (setq lastent (entnext lastent))
  8.             (ssadd lastent ss)
  9.         )
  10.     )

And so is now interpreted as a function (any symbol following an un-quoted open parenthesis is evaluated as a function - this is how LISP is interpreted). And, of course, '1' is not a function - hence your error.


Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: if statement throwing in a error: bad function: 1
« Reply #3 on: January 13, 2012, 06:52:10 AM »
T.Willey is correct that a 'progn' expression should be used to wrap multiple expressions so that they may be passed to the IF function as a single argument.

Try something like this (untested code):

Code - Auto/Visual Lisp: [Select]
  1. (defun c:xrefexplode ( / flag lastent ss )
  2.     (setq lastent (entlast)
  3.           flag    (getvar 'QAFLAGS)
  4.     )
  5.     (if (setq ss (ssget "_X" '((0 . "INSERT") (2 . "803000*") (410 . "Model"))))
  6.         (progn
  7.             (setvar 'QAFLAGS 1)
  8.             (command "_.explode" ss "")
  9.             (setvar 'QAFLAGS flag)
  10.             (setq ss (ssadd))
  11.             (while (setq lastent (entnext lastent))
  12.                 (ssadd lastent ss)
  13.             )
  14.             (sssetfirst nil ss)
  15.         )
  16.         (princ "\nNo Blocks Found!")
  17.     )
  18.     (princ)
  19. )

zride91

  • Guest
Re: if statement throwing in a error: bad function: 1
« Reply #4 on: January 13, 2012, 10:41:40 AM »
Hi T.Willey and Lee Mac, thank you for the response.

I did try progn, but I was struggling to get it to work.  I think at the time I was using it, I was using the IF statement a little differently and it was the other part of the IF statement that was tripping, looks like I gave up on it too early.  I appreciate the direction.

Lee Mac, thank you very much for the very detailed explaination and for the example.  This should be enough to get this lisp and a few others I have wrapped up!

zride91

  • Guest
Re: if statement throwing in a error: bad function: 1
« Reply #5 on: January 13, 2012, 10:52:04 AM »
I just noticed in the sample code you left Lee Mac, that all the commands are hyperlinks to definations/explainations, that is a HUGE help!   8-)  I am definately liking this site!

Thanks again!

grandhougday

  • Guest
Re: if statement throwing in a error: bad function: 1
« Reply #6 on: April 16, 2012, 11:10:34 AM »
for those that upper comments have not solved their problem.
look for extra "()" in your code like "((your code block))" and remove it to be like "(your code)"

BlackBox

  • King Gator
  • Posts: 3770
Re: if statement throwing in a error: bad function: 1
« Reply #7 on: April 16, 2012, 11:22:15 AM »
I just noticed in the sample code you left Lee Mac, that all the commands are hyperlinks to definations/explainations, that is a HUGE help!   8-)  I am definately liking this site!

Thanks again!

[OffTopic]

1+

I think that is one of my favorite features to the enhanced code tags that Se7en implemented here at TheSwamp.

Hrm... I wonder if this can be linked to actual online help, instead of text file...? :?
*wonders*

[/OffTopic]
"How we think determines what we do, and what we do determines what we get."

dgorsman

  • Water Moccasin
  • Posts: 2437
Re: if statement throwing in a error: bad function: 1
« Reply #8 on: April 16, 2012, 01:55:16 PM »
I just noticed in the sample code you left Lee Mac, that all the commands are hyperlinks to definations/explainations, that is a HUGE help!   8-)  I am definately liking this site!

Thanks again!

[OffTopic]

1+

I think that is one of my favorite features to the enhanced code tags that Se7en implemented here at TheSwamp.

Hrm... I wonder if this can be linked to actual online help, instead of text file...? :?
*wonders*

[/OffTopic]

Hate to say it, but given the current state of AutoCAD Help thats probably not a good idea.   :|
If you are going to fly by the seat of your pants, expect friction burns.

try {GreatPower;}
   catch (notResponsible)
      {NextTime(PlanAhead);}
   finally
      {MasterBasics;}

BlackBox

  • King Gator
  • Posts: 3770
Re: if statement throwing in a error: bad function: 1
« Reply #9 on: April 16, 2012, 01:57:00 PM »
I just noticed in the sample code you left Lee Mac, that all the commands are hyperlinks to definations/explainations, that is a HUGE help!   8-)  I am definately liking this site!

Thanks again!

[OffTopic]

1+

I think that is one of my favorite features to the enhanced code tags that Se7en implemented here at TheSwamp.

Hrm... I wonder if this can be linked to actual online help, instead of text file...? :?
*wonders*

[/OffTopic]

Hate to say it, but given the current state of AutoCAD Help thats probably not a good idea.   :|

... Good point. *kicks dirt*
"How we think determines what we do, and what we do determines what we get."

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: if statement throwing in a error: bad function: 1
« Reply #10 on: April 16, 2012, 03:36:07 PM »
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

BlackBox

  • King Gator
  • Posts: 3770
Re: if statement throwing in a error: bad function: 1
« Reply #11 on: April 16, 2012, 03:37:51 PM »
 :lol:
"How we think determines what we do, and what we do determines what we get."