Jump to content

List variables to block attributes with lisp


Kris Malen

Recommended Posts

Can someone help me out with changing the attributes of a block using a list. The block is used in a leader.

 

The block has 7 attributes, but my list doesn't always contain 7 variables. I have tried to implement Lee Mac's code dynamicblockfunctions, but I keep getting the error "too few arguments".

 

I would like to import the leader when the calculation is done, and for the block to then automatically change the attributes to the variables in the list. 

 

Here's my current code:

 

(defun c:testKRIS ( / *error* dch dcl des mv d1 d2 d3 d4 d5 d6)
 
    (defun *error* ( msg )
        (if (and (= 'int (type dch)) (< 0 dch))
            (unload_dialog dch)
        )
        (if (= 'file (type des))
            (close des)
        )
        (if (and (= 'str (type dcl)) (findfile dcl))
            (vl-file-delete dcl)
        )
        (if (and msg (not (wcmatch (strcase msg t) "*break,*cancel*,*exit*")))
            (princ (strcat "\nError: " msg))
        )
        (princ)
    )
 
    (cond
        (   (not
                (setq dcl (vl-filename-mktemp nil nil ".dcl")
                      des (open dcl "w")
                )
            )
            (princ "\nUnable to open DCL for writing.")
        )
        (   (progn
                (foreach str
                   '(
                        "ed : edit_box"
                        "{"
                        "    alignment = left;"
                        "    width = 20;"
                        "    edit_width = 10;"
                        "    fixed_width = true;"
                        "}"
                        ""
                        "test : dialog"
                        "{"
                        "    spacer;"
                        "    key = \"dcl\";"
                        "    : ed"
                        "    {"
                        "        key = \"mv\";"
                        "        label = \"MV:\";"
                        "    }"
                        "    :boxed_column { "
                        "     label = \"Diepte vd buizen\";"
                        "    :row {"
                        "    : ed"
                        "    {"
                        "        key = \"d1\";"
                        "        label = \"A:\";"
                        "    }"
                        "    : ed"
                        "    {"
                        "        key = \"d4\";"
                        "        label = \"X:\";"
                        "    }}"
                        "    :row {"
                        "    : ed"
                        "    {"
                        "        key = \"d2\";"
                        "        label = \"B:\";"
                        "    }"
                        "    : ed"
                        "    {"
                        "        key = \"d5\";"
                        "        label = \"Y:\";"
                        "    }}"
                        "    :row {"
                        "    : ed"
                        "    {"
                        "        key = \"d3\";"
                        "        label = \"C:\";"
                        "    }"                      
                        "    : ed"
                        "    {"
                        "        key = \"d6\";"
                        "        label = \"Z:\";"
                        "    }}"
                        "    }"
                        "    : boxed_row"
                        "    { "
                        "     label = \"BOK berekeningen\";"
                        "    :column {"
                        "        : ed { key = \"res\"; label = \"BOK:\"; is_enabled = false; }"
                        "        : ed { key = \"res2\"; label = \"BOK2:\"; is_enabled = false; }"
                        "        : ed { key = \"res3\"; label = \"BOK3:\"; is_enabled = false; }"
                        "         }"
                        "     :column{"
                        "        : ed { key = \"res4\"; label = \"BOK4:\"; is_enabled = false; }"
                        "        : ed { key = \"res5\"; label = \"BOK5:\"; is_enabled = false; }"
                        "        : ed { key = \"res6\"; label = \"BOK6:\"; is_enabled = false; }"
                        "         }"
                        "    }"
                        "        : button"
                        "        {"
                        "            key = \"cal\";" 
                        "            label = \"Calculate\";"
                        "        }"
                        "    spacer;"
                        "    ok_only;"
                        "}"
                    )
                    (write-line str des)
                )
                (setq des (close des)
                      dch (load_dialog dcl)
                )
                (<= dch 0)
            )
            (princ "\nUnable to load DCL file.")
        )
        (   (not (new_dialog "test" dch))
            (princ "\nUnable to display 'test' dialog.")
        )
        (   t
            (set_tile "dcl" "Calculate Area")
            (action_tile "mv" "(setq mv $value)")
            (action_tile "d1" "(setq d1 $value)")
            (action_tile "d2" "(setq d2 $value)")
            (action_tile "d3" "(setq d3 $value)")
            (action_tile "d4" "(setq d4 $value)")
            (action_tile "d5" "(setq d5 $value)")
            (action_tile "d6" "(setq d6 $value)")
            (action_tile "cal"
                (vl-prin1-to-string
                   '(
                        (lambda ( / m x y z a b c lijst_results )
                            (setq lijst_results '())
                            (set_tile "res" "")
                            (set_tile "res2" "")
                            (set_tile "res3" "")
                            (set_tile "res4" "")
                            (set_tile "res5" "")
                            (set_tile "res6" "")
                            (setq m (distof mv))
                            (if m
                              (progn
                                (setq lijst_results (append lijst_results (list (strcat "mv: " (rtos m 2 2)))))
                                (if (and d1 (setq a (distof d1)))
                                  (progn
                                    (setq lijst_results (append lijst_results (list (strcat "A: " (rtos (- m a) 2 2)))))
                                    (set_tile "res" (rtos (- m a) 2 2))))
                                (if (and d2 (setq b (distof d2)))
                                  (progn
                                    (setq lijst_results (append lijst_results (list (strcat "B: " (rtos (- m b) 2 2)))))
                                    (set_tile "res2" (rtos (- m b) 2 2))))
                                (if (and d3 (setq c (distof d3)))
                                  (progn
                                    (setq lijst_results (append lijst_results (list (strcat "C: " (rtos (- m c) 2 2)))))
                                    (set_tile "res3" (rtos (- m c) 2 2))))
                                (if (and d4 (setq x (distof d4)))
                                  (progn
                                    (setq lijst_results (append lijst_results (list (strcat "X: " (rtos (- m x) 2 2)))))
                                    (set_tile "res4" (rtos (- m x) 2 2))))
                                (if (and d5 (setq y (distof d5)))
                                  (progn
                                    (setq lijst_results (append lijst_results (list (strcat "Y: " (rtos (- m y) 2 2)))))
                                    (set_tile "res5" (rtos (- m y) 2 2))))
                                (if (and d6 (setq z (distof d6)))
                                  (progn
                                    (setq lijst_results (append lijst_results (list (strcat "Z: " (rtos (- m z) 2 2)))))
                                    (set_tile "res6" (rtos (- m z) 2 2))))
                                (print lijst_results)
                              )
                              (alert "Vul maaiveld waarden in.")      
                                  
                            )
                        
                        )
                    )
                )
            )
          (start_dialog)
        )
    )
    (*error* nil)
    (princ)
)


 

 

Link to comment
Share on other sites

Posted (edited)

How about something like this.

I used a mapcar with 3 lists to send all the items to a lambda function.

This does the check if the values are there, if not it returns a string with the word "Empty".

So it shouldn't give any errors when you didn't fill in one of the vars.

The apply function joins it into one string you can print.

 

Here is the part of the code to replace:

(action_tile "cal"
  (vl-prin1-to-string
    '(
      (lambda ( / m lijst_results )
        (foreach box '("res" "res2" "res3" "res4" "res5" "res6")
          (set_tile box "")
        )
        (if (setq m (distof mv))
          (progn
            (setq lijst_results
              (mapcar
                '(lambda (box naam var / a v)
                  (if
                    (and
                      var
                      (setq a (distof var))
                      (setq v (rtos (- m a) 2 2))
                    )
                    (progn
                      (set_tile box v)
                      (strcat naam ": " v) ; return to list
                    )
                    (progn
                      (strcat naam ": Empty") ; return to list
                    )
                  )
                )
                (list "res1" "res2" "res3" "res4" "res5" "res6") ; Box
                (list "A" "B" "C" "X" "Y" "Z") ; Naam
                (list d1 d2 d3 d4 d5 d6) ; Var
              )
            )
            (princ (apply 'strcat lijst_results))
          )
          (alert "Vul maaiveld waarden in.")      
        )
      )
    )
  )
)

 

Edited by dexus
Link to comment
Share on other sites

Posted (edited)

And about the dynamic block functions, maybe you can show what you tried.

The error you get is when you don't give enough arguments to a function.

 

So like this:

(defun add (a b)
  (+ a b)
)

(add 1 2) ;  No error
(add 1) ; Error

 

Edited by dexus
Link to comment
Share on other sites

Hey Dexus, thanks for your time to look into my problem. 

I just added this to my code: 

;; Get Dynamic Block Property Value  -  Lee Mac
;; Returns the value of a Dynamic Block property (if present)
;; blk - [vla] VLA Dynamic Block Reference object
;; prp - [str] Dynamic Block property name (case-insensitive)

(defun LM:getdynpropvalue ( blk prp )
    (setq prp (strcase prp))
    (vl-some '(lambda ( x ) (if (= prp (strcase (vla-get-propertyname x))) (vlax-get x 'value)))
        (vlax-invoke blk 'getdynamicblockproperties)
    )
)

then it gave me the error "too few arguments"

 

Can you explain to me what your code does that mine doesn't. I'am still new to this so I don't know how every line of code works.

My code gave me a list that looks like this:

 

Schermafbeelding2024-05-23100656.png.b5ef36c23ea03a047c93ed4c9de40808.png

 

For this example, I only set the value for mv to 50 and a value for d1 to 10 the rest was empty. I don't see the reason to change that part of my program.

Link to comment
Share on other sites

Posted (edited)

I added some comments below to show what it does differently. Mostly its just a loop instead of the same code 6 times.

The Dynamic block functions are not for changing attributes but the different custom properties a dynamic block can have.

For changing attributes you can look at the other link CyberAngel provided on the other post.

 

But since you are trying to fill in a dynamic block that looks just like a mleader, why not just use an actual mleader like this:

(defun c:testKRIS ( / *error* dch dcl des mv d1 d2 d3 d4 d5 d6)
 
    (defun *error* ( msg )
        (if (and (= 'int (type dch)) (< 0 dch))
            (unload_dialog dch)
        )
        (if (= 'file (type des))
            (close des)
        )
        (if (and (= 'str (type dcl)) (findfile dcl))
            (vl-file-delete dcl)
        )
        (if (and msg (not (wcmatch (strcase msg t) "*break,*cancel*,*exit*")))
            (princ (strcat "\nError: " msg))
        )
        (princ)
    )
 
    (cond
        (   (not
                (setq dcl (vl-filename-mktemp nil nil ".dcl")
                      des (open dcl "w")
                )
            )
            (princ "\nUnable to open DCL for writing.")
        )
        (   (progn
                (foreach str
                   '(
                        "ed : edit_box"
                        "{"
                        "    alignment = left;"
                        "    width = 20;"
                        "    edit_width = 10;"
                        "    fixed_width = true;"
                        "}"
                        ""
                        "test : dialog"
                        "{"
                        "    spacer;"
                        "    key = \"dcl\";"
                        "    : ed"
                        "    {"
                        "        key = \"mv\";"
                        "        label = \"MV:\";"
                        "    }"
                        "    :boxed_column { "
                        "     label = \"Diepte vd buizen\";"
                        "    :row {"
                        "    : ed"
                        "    {"
                        "        key = \"d1\";"
                        "        label = \"A:\";"
                        "    }"
                        "    : ed"
                        "    {"
                        "        key = \"d4\";"
                        "        label = \"X:\";"
                        "    }}"
                        "    :row {"
                        "    : ed"
                        "    {"
                        "        key = \"d2\";"
                        "        label = \"B:\";"
                        "    }"
                        "    : ed"
                        "    {"
                        "        key = \"d5\";"
                        "        label = \"Y:\";"
                        "    }}"
                        "    :row {"
                        "    : ed"
                        "    {"
                        "        key = \"d3\";"
                        "        label = \"C:\";"
                        "    }"                      
                        "    : ed"
                        "    {"
                        "        key = \"d6\";"
                        "        label = \"Z:\";"
                        "    }}"
                        "    }"
                        "    : boxed_row"
                        "    { "
                        "     label = \"BOK berekeningen\";"
                        "    :column {"
                        "        : ed { key = \"res\"; label = \"BOK:\"; is_enabled = false; }"
                        "        : ed { key = \"res2\"; label = \"BOK2:\"; is_enabled = false; }"
                        "        : ed { key = \"res3\"; label = \"BOK3:\"; is_enabled = false; }"
                        "         }"
                        "     :column{"
                        "        : ed { key = \"res4\"; label = \"BOK4:\"; is_enabled = false; }"
                        "        : ed { key = \"res5\"; label = \"BOK5:\"; is_enabled = false; }"
                        "        : ed { key = \"res6\"; label = \"BOK6:\"; is_enabled = false; }"
                        "         }"
                        "    }"
                        "        : button"
                        "        {"
                        "            key = \"cal\";" 
                        "            label = \"Calculate\";"
                        "        }"
                        "    spacer;"
                        "    ok_only;"
                        "}"
                    )
                    (write-line str des)
                )
                (setq des (close des)
                      dch (load_dialog dcl)
                )
                (<= dch 0)
            )
            (princ "\nUnable to load DCL file.")
        )
        (   (not (new_dialog "test" dch))
            (princ "\nUnable to display 'test' dialog.")
        )
        (   t
            (set_tile "dcl" "Calculate Area")
            (action_tile "mv" "(setq mv $value)")
            (action_tile "d1" "(setq d1 $value)")
            (action_tile "d2" "(setq d2 $value)")
            (action_tile "d3" "(setq d3 $value)")
            (action_tile "d4" "(setq d4 $value)")
            (action_tile "d5" "(setq d5 $value)")
            (action_tile "d6" "(setq d6 $value)")
            (action_tile "cal"
              (vl-prin1-to-string
                '(
                  (lambda ( / m)
                    (foreach box '("res" "res2" "res3" "res4" "res5" "res6")
                      (set_tile box "")
                    )
                    (if (and mv (setq m (distof mv)))
                      (progn
                        (setq lijst_results
                          (cons ; Add item to start of a list
                            (strcat "MV: " mv "\\P") ; Add MV to the list before saving to lijst_results
                            (mapcar ; Loop function (lambda) over 3 lists
                              '(lambda (box naam var / a v) ; Function
                                (if
                                  (and ; Checks from your code
                                    var
                                    (setq a (distof var))
                                    (setq v (rtos (- m a) 2 2)) ; Save string in a variable since its used twice
                                  )
                                  (progn
                                    (set_tile box v)
                                    (strcat naam ": " v "\\P") ; return to list including enter
                                  )
                                  (progn
                                    ""
                                  )
                                )
                              )
                              (list "res" "res2" "res3" "res4" "res5" "res6") ; List 1 called Box
                              (list "A" "B" "C" "X" "Y" "Z") ; List 2 called Naam
                              (list d1 d2 d3 d4 d5 d6) ; List 3 called Var
                            )
                          )
                        )
                      )
                      (alert "Vul maaiveld waarden in.")      
                    )
                  )
                )
              )
            )
            (start_dialog)
            (if lijst_results
              (command "_mleader"
                "\\" ; Pause for input startpoint
                "\\" ; Pause for position of text
                (apply 'strcat lijst_results) ; Fill in info
              )
            )
        )
     )
    (*error* nil)
    (princ)
)
Edited by dexus
  • Like 1
Link to comment
Share on other sites

Thanks alot dexus!!🤗

 

Your code works perfect, this is exactly what I was looking for. You're right about just using an mleader. But can you add 'mv' to the list? 

 

I am trying my best to understand how your code works, but I find it hard to edit your code without fully understanding it.

At the top of the mleader, it should say "MV: (value)".

 

 

 

 

 

Link to comment
Share on other sites

Posted (edited)

You can add MV to the start of the list using (cons mv-string (mapcar ...))

I edited my post above.

 

You can change the text by editing all the stuff between strcat.

Edited by dexus
  • Thanks 1
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...