TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: Shady_Shiba on October 12, 2017, 06:43:02 PM

Title: program is only returning -1
Post by: Shady_Shiba on October 12, 2017, 06:43:02 PM
Greetings,

I've been trying to debug my program that detects the cfm attribute in my dynamic block and based on the cfm range it picks the sort value from it's property table. so far it keeps returning an integer and does nothing to the block. any ideas on how to correct it? Im sure it may be the way I had set up my loop if not my condition statement, just not certain if it is the root cause. any advice will be greatly appreciated.

here is the code i have so far and I have attached my block:

Code: [Select]
(defun c:AS3 (/ ent val cfm)
  ;; Define function, declare local variables
  (if ;; If the following expression returns a non-nil value
      (setq ent ;; Assign the value returned by the following expression to the symbol 'sel'
(ssget ;; Prompt the user to make a selection and return the selection set if successful
       '
((0 . "INSERT"))
       ;; Filter the selection to block references only (INSERTs)
)
;; end ssget
      )
    ;; end setq

    (Setq I -1)

    (While (setq ent (ssname val (setq I (1+ I))))



      (setq val (LM:Tag_Val (vlax-ename->vla-object ent) "CFM"))


      (Cond
;If
((>= val 51)
(<= val 0)
(LM:reprop "sort" "01" ent)
)
;Else
((>= val 76)
(<= val 52)
(LM:reprop "sort" "02" ent)
)
;Else
((>= val 121)
(<= val 77)
(LM:reprop "sort" "03" ent)
)
((>= val 176)
(<= val 122)
(LM:reprop "sort" "04" ent)
)
((>= val 177)
(LM:reprop "sort" "05" ent)
)
      ) ;End cond


      (defun LM:reprop (blk lst / itm)
(setq
  lst (mapcar '(lambda (x) (cons (strcase (car x)) (cdr x)))
      lst
      )
)
(foreach x (vlax-invoke blk 'getdynamicblockproperties)
  (if (setq itm (assoc (strcase (vla-get-propertyname x)) lst))
    (vla-put-value
      x
      (vlax-make-variant
(cdr itm)
(vlax-variant-type (vla-get-value x))
      )
    )
  )
)
      )

      (defun LM:Tag_Val (blk tag)
(setq tag (strcase tag))
(vl-some '(lambda (att)
    (if (= tag (strcase (vla-get-tagstring att)))
      (atoi (vla-get-TextString att))
    )
  )
(vlax-invoke blk 'getattributes)
)
      )
    )
  )
)
Title: Re: program is only returning -1
Post by: kdub_nz on October 12, 2017, 10:43:17 PM
Without trying your code ( or really looking too hard) ;

You appear to be defining your internal functions inside the while loop ... so every time through the look they are redefined.
and as a side issue ; the first time through they will NOT be defined when you try to call them from inside the conditional test block.

Remedy: relocate the function definitions to be outside the scope of the main application and locate or load them before you run the main app.
     alternatively: define them inside the main app at the top of the code and make the functions local scope by adding the names to the variables list.

Further:
Why are you using the LM:xxx naming convention for the functions ?
If you are going to use functions from other sources it's best to leave the names as originally designed.
If they are your functions name them accordingly.


Regards,
Title: Re: program is only returning -1
Post by: Shady_Shiba on October 12, 2017, 10:55:16 PM
I Understand what you are saying about the "LM: naming" , I rewrote it so I could make note for myself what the module does. At the same time I didn't want to completely overwrite the name in respect to Leemac's good work. So I made a weird compromise, but I will take into consideration on your remedy.
Title: Re: program is only returning -1
Post by: CAB on October 13, 2017, 12:34:41 AM
You need a PROGN in the IF statement to isolate code.
Also moved your functions.

I did not test
Code: [Select]
(defun c:as3 (/ ent val cfm)  ; Define function, declare local variables

      (defun lm:tag_val (blk tag)
        (setq tag (strcase tag))
        (vl-some '(lambda (att)
                    (if (= tag (strcase (vla-get-tagstring att)))
                      (atoi (vla-get-textstring att))
                    )
                  )
                 (vlax-invoke blk 'getattributes)
        )
      )

      (defun lm:reprop (blk lst / itm)
        (setq
          lst (mapcar '(lambda (x) (cons (strcase (car x)) (cdr x)))
                      lst
              )
        )
        (foreach x (vlax-invoke blk 'getdynamicblockproperties)
          (if (setq itm (assoc (strcase (vla-get-propertyname x)) lst))
            (vla-put-value
              x
              (vlax-make-variant
                (cdr itm)
                (vlax-variant-type (vla-get-value x))
              )
            )
          )
        )
      )
 
  (if ;; If the following expression returns a non-nil value
      (setq ent ;; Assign the value returned by the following expression to the symbol 'sel'
                (ssget ;; Prompt the user to make a selection and return the selection set if successful
                       '
                        ((0 . "INSERT"))
                       ;; Filter the selection to block references only (INSERTs)
                )
                ;; end ssget
      )
    ;; end setq
   
(progn  ;  <---<<<
 
    (setq i -1)

    (while (setq ent (ssname val (setq i (1+ i))))



      (setq val (lm:tag_val (vlax-ename->vla-object ent) "CFM"))


      (cond
  ;If
        ((>= val 51)
         (<= val 0)
         (lm:reprop "sort" "01" ent)
        )
  ;Else
        ((>= val 76)
         (<= val 52)
         (lm:reprop "sort" "02" ent)
        )
  ;Else
        ((>= val 121)
         (<= val 77)
         (lm:reprop "sort" "03" ent)
        )
        ((>= val 176)
         (<= val 122)
         (lm:reprop "sort" "04" ent)
        )
        ((>= val 177)
         (lm:reprop "sort" "05" ent)
        )
      ) ;End cond

    )
      ) ; end progn
  )

)

Title: Re: program is only returning -1
Post by: Grrr1337 on October 13, 2017, 05:33:52 AM
Quick revision:

Code - Auto/Visual Lisp: [Select]
  1. (defun C:AS3 ( / LM:reprop LM:Tag_Val findrange SS i o val )
  2.  
  3.   (defun LM:reprop ( blk lst / itm )
  4.     (setq lst (mapcar '(lambda ( x ) (cons (strcase (car x)) (cdr x))) lst))
  5.     (foreach x (vlax-invoke blk 'getdynamicblockproperties)
  6.       (if (setq itm (assoc (strcase (vla-get-propertyname x)) lst))
  7.         (vla-put-value x (vlax-make-variant (cdr itm) (vlax-variant-type (vla-get-value x))))
  8.       )
  9.     )
  10.   )
  11.  
  12.   (defun LM:Tag_Val ( blk tag )
  13.     (setq tag (strcase tag))
  14.     (vl-some '(lambda (att) (if (= tag (strcase (vla-get-tagstring att))) (atoi (vla-get-TextString att)))) (vlax-invoke blk 'getattributes))
  15.   )
  16.  
  17.   (setq findrange
  18.     (lambda (v L)
  19.       (if (and (numberp v) (listp L))
  20.         (vl-some
  21.           '(lambda (x / mn mx)
  22.             (setq mn (caar x))
  23.             (setq mx (cadar x))
  24.             (if (apply '<= (append (if mn (list mn)) (list v) (if mx (list mx)) ))
  25.               (cadr x)
  26.             ); if
  27.           ); lambda
  28.           L
  29.         ); vl-some
  30.       ); if
  31.     ); lambda (v L)
  32.   ); setq findrange
  33.  
  34.   (if (setq SS (ssget '((0 . "INSERT"))))
  35.     (repeat (setq i (sslength SS))
  36.       (and (setq o (vlax-ename->vla-object (ssname SS (setq i (1- i)))))
  37.         (setq val (LM:Tag_Val o "CFM"))
  38.         (setq val
  39.           (findrange val
  40.             '(
  41.               ( (0 51) "01")
  42.               ( (52 76) "02")
  43.               ( (77 121) "03")
  44.               ( (122 176) "04")
  45.               ( (177 nil) "05")
  46.             )
  47.           )
  48.         )
  49.         (LM:reprop o (list (cons "sort" val)))
  50.       ); and
  51.     ); repeat
  52.   ); if SS
  53.   (princ)
  54. ); defun C:AS3

That code was interesting, and related thread (http://www.cadtutor.net/forum/showthread.php?101924-Error-Message-Too-Few-Arguments).
Title: Re: program is only returning -1
Post by: Shady_Shiba on November 09, 2017, 11:41:34 AM
Update on this lisp.

It works! :uglystupid2:

as it turns out, the main problem was the dynamic table itself. Sort column value was set to "custom". if i change it to *last value*  ,and checkbox  to "match row", then the lisp works without any errors.

here is a forum that further explains the dynamic property table settings.
https://forums.autodesk.com/t5/dynamic-blocks/set-dynamic-block-properties-table-through-lisp-without-quot/td-p/3295893 (https://forums.autodesk.com/t5/dynamic-blocks/set-dynamic-block-properties-table-through-lisp-without-quot/td-p/3295893)