Error: Bad Function: 7 when trying to add local variables

Error: Bad Function: 7 when trying to add local variables

mspears53MAZ
Explorer Explorer
815 Views
9 Replies
Message 1 of 10

Error: Bad Function: 7 when trying to add local variables

mspears53MAZ
Explorer
Explorer

New to coding autolisp, I'm trying to make a simple lisp to add a user input number to a block attribute so far I have the input and the attribute value assigned to variables but the problem comes when I try to add them. I get error bad function: 7 as a response to running this.

 

(defun C:IterBlock (/ ent blk tag, old, val, inc, new)
  (setq ent (car (entsel)))
  (setq blk (vlax-ename->vla-object ent))
  (setq tag "TAG")
  (setq old (vl-some '(lambda ( att ) (if (= tag (strcase (vla-get-tagstring att))) (vla-get-textstring att))) (vlax-invoke blk 'getattributes)))
  (setq inc (getint "Increment "))
  (setq val (read old))
  (setq new (+ val inc))
)
 
Am I just doing the summation wrong?
0 Likes
816 Views
9 Replies
Replies (9)
Message 2 of 10

Kent1Cooper
Consultant
Consultant

(vl-some) [if it succeeds] returns a list, which is what will be in the 'old' variable [not an item like a text string or a number].  Assuming that will be a list of one Attribute value, does it work if you do this to read the value in the list, rather than the list itself [untested]?
(setq val (read (car old)))

Kent Cooper, AIA
0 Likes
Message 3 of 10

john.uhden
Mentor
Mentor

@mspears53MAZ 

I think the problem is that (vl-some) returns a list, which could be nil.

So (read old) is probably your pothole.

John F. Uhden

0 Likes
Message 4 of 10

mspears53MAZ
Explorer
Explorer

I tried using car like that and even in front of vl-some when setting old and it throws a different error this time saying bad argument type: consp. From looking at (type val) it comes out as an int and if i print it, it has the attributes value correct.

0 Likes
Message 5 of 10

Kent1Cooper
Consultant
Consultant

I'm guessing it must be something in the (lambda) function structure, but haven't dug into what it's doing [and can't take the time right now].  Evaluate that in detail....

Kent Cooper, AIA
0 Likes
Message 6 of 10

GeeHaa
Collaborator
Collaborator

It is a little more complicated than it needs to be. I would do something like this. Just select the attribute.

(DEFUN C:iterBlock (/ inc ent att text int sum)
  (setq inc (getint "\nIncrememt"))
  (while (setq ent (nentsel))
    (setq att (entget (car ent)))
    (setq text (cdr(assoc 1 att)))
    (setq int (atoi text))
    (setq sum (+ int inc))
    (setq att (subst (cons 1 (itoa sum)) (assoc 1 att) att))
    (entmod att)
    (entupd (car ent))
   )
  (princ)
)

would do something like this.

0 Likes
Message 7 of 10

Moshe-A
Mentor
Mentor

@Kent1Cooper , 

 


@Kent1Cooper wrote:

(vl-some) [if it succeeds] returns a list, which is what will be in the 'old' variable [not an item like a text string or a number].  Assuming that will be a list of one Attribute value, does it work if you do this to read the value in the list, rather than the list itself [untested]?
(setq val (read (car old)))


what i'm missing here? 🤔

 

it's true some autolisp documentation says (vl-some) return list or nil and some says it returns:

 

Return Values

The predicate value, if predicate-function returned a value other than nil; otherwise nil.

 

so it could be an atom, a list or nil depending on the predicate-function.

 

am i wrong?

 

Moshe

 

0 Likes
Message 8 of 10

john.uhden
Mentor
Mentor

@mspears53MAZ 

Well, I tested and it works just fine if the attribute value is a numeric string like "123."

Depending on what you're trying to do, maybe you oughta check the attribute value before continuing with the rest of the code.

Like: (if (wcmatch old "#*") ; continue)

John F. Uhden

0 Likes
Message 9 of 10

ВeekeeCZ
Consultant
Consultant

@mspears53MAZ wrote:

New to coding autolisp, I'm trying to make a simple lisp to add a user input number to a block attribute so far I have the input and the attribute value assigned to variables but the problem comes when I try to add them. I get error bad function: 7 as a response to running this.

 

(defun C:IterBlock (/ ent blk tag, old, val, inc, new)
  (setq ent (car (entsel)))
  (setq blk (vlax-ename->vla-object ent))
  (setq tag "TAG")
  (setq old (vl-some '(lambda ( att ) (if (= tag (strcase (vla-get-tagstring att))) (vla-get-textstring att))) (vlax-invoke blk 'getattributes)))
  (setq inc (getint "Increment "))
  (setq val (read old))
  (setq new (+ val inc))
)
 
Am I just doing the summation wrong?

 

Are you still experiencing the error? I don't think so. The code is fine (besides these commas that shouldn't be there but does not do any harm...  (/ ent blk tag, old, val, inc, new) 

 

Where is the number 7 coming from? Is it a value of the attribute that you are using to test?

 

I think that error is some remnant of the previous try-and-see process...

You get that error if you run this expression (7 "whatever"). 7 or a variable with the value of 7 is placed right behind the initial parent where a function is expected.

 

So if you previously had something like (setq new (val + inc)) or (setq new + val inc)... that could lead to that error.

0 Likes
Message 10 of 10

kerry_w_brown
Advisor
Advisor

Error No. 7 indicates  "Object selection: pick failed" , but that isn't the only issue.

 

If you run an error trap which includes  (vl-bt)

you will get something like this :

 

 

 

[0.168] (VL-BT)
[1.164] (*ERROR* "bad argument type: stringp nil") LAP+228
[2.158] (_call-err-hook #<USUBR @0000021e3b386520 *ERROR*> "bad argument type: stringp nil")
[3.152] (sys-error "bad argument type: stringp nil")
[4.147] (#<SUBR @0000021e3b00d2f0 nil> "bad argument type: stringp nil")
:ERROR-BREAK.143 "bad argument type: stringp nil"
[5.140] (READ nil)
[6.135] (#<USUBR @0000021e3b386a70 -top->) LAP+11
[7.131] (al-top-funcall #<USUBR @0000021e3b386a70 -top->)
[8.126] (alide-load-stream #<FILE internal> nil)

 

 

 

 

note the error occurs on this statement :

[5.140] (READ nil) 
is the result at your code statement (setq val (read old))

which indicates that the tagstring was not found in any of the attributes you tested.

 

If you test < if/else > that old is not nil before you read all should be cool.

Regards,

 

added  : I use something like this for a trap :

 

 

 

  (defun *error* (msg)
    ;;----- Cancel any Active Commands --
    (while (< 0 (getvar 'cmdactive)) (command-s nil))
    (setvar 'menuecho 1)
    ;;----- Display error message if applicable
    (cond ((not msg))
	  ((member (strcase msg t)
		   '("console break"
		     "function cancelled"
		     "quit / exit abort"
		    )
	   )
	   (princ "\nFunction Cancelled.")
	  )
	  ((princ (strcat "\nApplication Error: "
			  (itoa (getvar 'errno))
			  " :- "
			  msg
		  )
	   )
	   ;;----- Display backtrace ------------------------------------------
	   (vl-bt)
	  )
    )
    (setvar 'errno 0)
    (vla-endundomark
      (vla-get-activedocument (vlax-get-acad-object))
    )
    (princ)
  )

 

 

 

 


// Called Kerry or kdub in my other life.

Everything will work just as you expect it to, unless your expectations are incorrect. ~ kdub
Sometimes the question is more important than the answer. ~ kdub

NZST UTC+12 : class keyThumper<T> : Lazy<T>;      another  Swamper
0 Likes