Author Topic: Delete variable that contains 0  (Read 898 times)

0 Members and 1 Guest are viewing this topic.

Rufusff7

  • Mosquito
  • Posts: 8
Delete variable that contains 0
« on: February 03, 2023, 03:39:08 AM »
Hello,

I have a lisp that extract properties from a dynamic block, put them in to variables and creates mLeader with it's values (example in attachment).

The problem is, i want to exclude variables that contains "0" in it. Variables HL2 in my example (SL2 also, because it is connected to HL2, you will see it in ";Mleader command" part of a code). Right now i testing only with 6 properties, and i could just wright down all scenarios, but in my final version there will be 20 properties and it will be impossible to brute-force it.

At first i wanted to put all variables in to list, then vl-remove "0" from it, but i failed. Please guide me in witch direction should i dig.


Code - Auto/Visual Lisp: [Select]
  1. (defun c:test ( / )
  2.   ;;;;;Take properties of dynblocks and save they values to variables
  3. (defun LM:getdynpropvalue1 ( blk prp )
  4.     (setq prp (strcase prp))
  5.     (vl-some '(lambda ( hl1 ) (if (= prp (strcase (vla-get-propertyname hl1))) (vlax-get hl1 'value)))
  6.         (vlax-invoke blk 'getdynamicblockproperties))
  7.   )  
  8.     (if (and (setq e (car (entsel "\nSelect block: ")))
  9.              (= "INSERT" (cdr (assoc 0 (entget e))))
  10.         )
  11.      
  12.            (setq hl1 (LM:getdynpropvalue1 (vlax-ename->vla-object e) "HL1"))
  13.       ) ;if end
  14.  
  15.   (defun LM:getdynpropvalue2 ( blk prp )
  16.     (setq prp (strcase prp))
  17.     (vl-some '(lambda ( sl1 ) (if (= prp (strcase (vla-get-propertyname sl1))) (vlax-get sl1 'value)))
  18.         (vlax-invoke blk 'getdynamicblockproperties))
  19.   )  
  20.          (setq sl1 (LM:getdynpropvalue2 (vlax-ename->vla-object e) "SL1"))
  21.  
  22.   (defun LM:getdynpropvalue3 ( blk prp )
  23.     (setq prp (strcase prp))
  24.     (vl-some '(lambda ( hl2 ) (if (= prp (strcase (vla-get-propertyname hl2))) (vlax-get hl2 'value)))
  25.         (vlax-invoke blk 'getdynamicblockproperties))
  26.   )  
  27.          (setq hl2 (LM:getdynpropvalue3 (vlax-ename->vla-object e) "HL2"))
  28.  
  29.   (defun LM:getdynpropvalue4 ( blk prp )
  30.     (setq prp (strcase prp))
  31.     (vl-some '(lambda ( sl2 ) (if (= prp (strcase (vla-get-propertyname sl2))) (vlax-get sl2 'value)))
  32.         (vlax-invoke blk 'getdynamicblockproperties))
  33.   )  
  34.          (setq sl2 (LM:getdynpropvalue4 (vlax-ename->vla-object e) "SL2"))
  35.  
  36.   (defun LM:getdynpropvalue5 ( blk prp )
  37.     (setq prp (strcase prp))
  38.     (vl-some '(lambda ( hl3 ) (if (= prp (strcase (vla-get-propertyname hl3))) (vlax-get hl3 'value)))
  39.         (vlax-invoke blk 'getdynamicblockproperties))
  40.   )  
  41.          (setq hl3 (LM:getdynpropvalue5 (vlax-ename->vla-object e) "HL3"))
  42.  
  43.   (defun LM:getdynpropvalue6 ( blk prp )
  44.     (setq prp (strcase prp))
  45.     (vl-some '(lambda ( sl3 ) (if (= prp (strcase (vla-get-propertyname sl3))) (vlax-get sl3 'value)))
  46.         (vlax-invoke blk 'getdynamicblockproperties))
  47.   )  
  48.          (setq sl3 (LM:getdynpropvalue6 (vlax-ename->vla-object e) "SL3"))
  49.    
  50. ;;;;;Mleader command
  51.   (setq pointA (getpoint "\nSelect first point"))
  52.   (setq pointB (getpoint "\nSelect second point"))
  53.  
  54.   (setq a (strcat "+"(rtos hl1) " ("sl1")"))
  55.   (setq b (strcat "\n+" (rtos hl2) " (" sl2")"))
  56.   (setq c (strcat "\n+" (rtos hl3) " (" sl3")"))
  57.  
  58.   (setq mText (strcat a b c))
  59.  
  60.   (command "_mleader" pointA pointB mText)
  61.            
  62.     (princ)
  63. )
  64.  


 
« Last Edit: February 07, 2023, 04:34:20 AM by Rufusff7 »

mhupp

  • Bull Frog
  • Posts: 250
Re: Delete variable that contains 0
« Reply #1 on: February 03, 2023, 09:37:13 AM »
If your using AutoCAD their is a simpler way to pull values. but it doesn't work with my version of BricsCAD. Give this a try and see if it works.
https://help.autodesk.com/view/OARX/2022/ENU/?guid=GUID-8E5913FC-09ED-4C70-AFB7-2431C062E899

Code - Auto/Visual Lisp: [Select]
  1. (defun c:test (/ blk HL1 HL2 HL3 SL1 SL2 SL3 lst mtext)
  2.   (setq blk (car (entsel "\nSelect block: ")))  
  3.   (setq HL1 (getpropertyvalue blk "HL1") HL2 (getpropertyvalue blk "HL2") HL3 (getpropertyvalue blk "HL3")
  4.         SL1 (getpropertyvalue blk "SL1") SL2 (getpropertyvalue blk "SL2") SL3 (getpropertyvalue blk "SL3")
  5.   )
  6.   ;(setq HL1 1000 HL2 0 HL3 2000 SL1 "P" SL2 "CIP" SL3 "R")  ;for testing erase if you want
  7.   (if (and HL1 HL2 HL3)
  8.     (progn
  9.       (if (> HL1 0)
  10.         (setq lst (list (strcat "+" (itoa HL1) " (" SL1 ")")))
  11.       )
  12.       (if (> HL2 0)
  13.         (setq lst (append lst (list (strcat "+" (itoa HL2) " (" SL2 ")"))))
  14.       )
  15.       (if (> HL3 0)
  16.         (setq lst (append lst (list (strcat "+" (itoa HL3) " (" SL3 ")"))))
  17.       )
  18.       (setq mtext (car lst)) ;builds a string from the list generated above with \\P inbetween them
  19.       (foreach txt (cdr lst) ;"+1000 (P)\\P+2000 (R)"
  20.         (setq mtext (strcat mtext "\\P" txt))
  21.       )
  22.       (command "_Mleader" pause pause mtext)
  23.     )
  24.     (prompt "\nNot a Block or Missing one of the HL Values")
  25.   )
  26.   (princ)
  27. )
« Last Edit: February 03, 2023, 09:46:56 AM by mhupp »

Rufusff7

  • Mosquito
  • Posts: 8
Re: Delete variable that contains 0
« Reply #2 on: February 06, 2023, 03:07:10 AM »
If your using AutoCAD their is a simpler way to pull values. but it doesn't work with my version of BricsCAD. Give this a try and see if it works.
https://help.autodesk.com/view/OARX/2022/ENU/?guid=GUID-8E5913FC-09ED-4C70-AFB7-2431C062E899

Hello, i got "error: ads request error" when i tried to extract values from dynblock with your method.

But your method to exclude variables that contains 0 works perfectly. I only changed itoa to rtos, without that i got "error: fixnump: 100.0".

One question: why you added this "(setq mtext (strcat mtext "\\P" txt))"? What is the benefit doing it like this instead of my version?

mhupp

  • Bull Frog
  • Posts: 250
Re: Delete variable that contains 0
« Reply #3 on: February 06, 2023, 10:29:46 AM »
See if this new code works.

Code - Auto/Visual Lisp: [Select]
  1. (defun c:test (/ blk lst val HL1 HL2 HL3 SL1 SL2 SL3 tlst mtext)
  2.   (setq blk (vlax-ename->vla-object (car (entsel "\nSelect block: "))))
  3.   (setq lst (vlax-invoke blk 'getdynamicblockproperties)) ;list dynamic properties
  4.   (foreach val lst
  5.     (cond
  6.       ((eq (vla-get-propertyname val) "HL1")
  7.         (setq HL1 (vla-get val 'value))
  8.       )
  9.       ((eq (vla-get-propertyname val) "HL2")
  10.         (setq HL2 (vla-get val 'value))
  11.       )
  12.       ((eq (vla-get-propertyname val) "HL3")
  13.         (setq HL3 (vla-get val 'value))
  14.       )
  15.       ((eq (vla-get-propertyname val) "SL1")
  16.         (setq SL1 (vla-get val 'value))
  17.       )
  18.       ((eq (vla-get-propertyname val) "SL2")
  19.         (setq SL2 (vla-get val 'value))
  20.       )
  21.       ((eq (vla-get-propertyname val) "SL3")
  22.         (setq SL3 (vla-get val 'value))
  23.       )
  24.     )
  25.   )
  26.   ;(setq HL1 1000.0 HL2 0 HL3 2000 SL1 "P" SL2 "CIP" SL3 "R")  ;for testing erase if you want
  27.   (if (and HL1 HL2 HL3)
  28.     (progn
  29.       (if (> HL1 0)
  30.         (setq tlst (list (strcat "+" (rtos HL1 2 0) " (" SL1 ")")))
  31.       )
  32.       (if (> HL2 0)
  33.         (setq tlst (append tlst (list (strcat "+" (rtos HL2 2 0) " (" SL2 ")"))))
  34.       )
  35.       (if (> HL3 0)
  36.         (setq tlst (append tlst (list (strcat "+" (rtos HL3 2 0) " (" SL3 ")"))))
  37.       )
  38.       (setq mtext (car lst))  ;builds a string from the list generated above with \\P inbetween them
  39.       (foreach txt (cdr lst)  ;"+1000 (P)\\P+2000 (R)"
  40.         (setq mtext (strcat mtext "\\P" txt))
  41.       )
  42.       (command "_Mleader" pause pause mtext)
  43.     )
  44.     (prompt "\nNot a Block or Missing one of the HL Values")
  45.   )
  46.   (princ)
  47. )

thought itoa would act like (fix real number) guess not.

The not really a benefit its just needed because we are processing the data differently.  I'm using one list to store all three strings. its basically saying for each item in lst join them together into one big string but put a "//P" between them.
the code would be the same if the list was 30 strings.

Since your using three variables using (strcat a b c) would error if any of those are nil. and since you wanted to exclude any that where set to 0 you would have to make a cond to test all variables. then use the right strcat to add them together.
if you where to add more variables the conditional would grow.

Also since you have the newline call already added into the hl2 string if hl1=0 the mtext created would have a blank first line then hl2.

« Last Edit: February 07, 2023, 02:08:10 PM by mhupp »

BIGAL

  • Swamp Rat
  • Posts: 1411
  • 40 + years of using Autocad
Re: Delete variable that contains 0
« Reply #4 on: February 06, 2023, 05:39:25 PM »
To strcat an unknown number of variables use (setq str "") then in a loop you can (setq str (strcat str (nth x lst))) (setq x (1+x)) then (setq str (strcat str (nth x lst))) and so on if any nth is nil  then dont do the strcat.
A man who never made a mistake never made anything

Rufusff7

  • Mosquito
  • Posts: 8
Re: Delete variable that contains 0
« Reply #5 on: February 07, 2023, 04:30:21 AM »
See if this new code works.

Hello, again. This time i got this "error: bad argument type for compare: #<variant 5 1000> 0". But dont worry, i combined our codes and now it is working like i wanted. Also i didn understand how LeeMac's "Get Dynamic Block Property Value" works and messed up my code. In new revision i wrote it like it should be. Now it looks much more better, see below:
Code - Auto/Visual Lisp: [Select]
  1. ;;;;; Creates Mleader from information that you get from Dynblock
  2.  
  3. (defun c:test ( / HL1 HL2 HL3 SL1 SL2 SL3 mText lst txt pointA pointB eData getdynpropvalue)
  4.  
  5.   ;;;;;Take custom properties from Dynblocks and save its values to variables
  6.  
  7. (defun LM:getdynpropvalue ( blk prp )
  8.     (setq prp (strcase prp))
  9.     (vl-some '(lambda ( x ) (if (= prp (strcase (vla-get-propertyname x))) (vlax-get x 'value)))
  10.         (vlax-invoke blk 'getdynamicblockproperties))
  11.   ) ; end defun LM
  12.  
  13.     (setq e (car (entsel "\nSelect block")))    
  14.      
  15.     (setq hl1 (LM:getdynpropvalue (vlax-ename->vla-object e) "HL1"))
  16.     (setq sl1 (LM:getdynpropvalue (vlax-ename->vla-object e) "SL1"))    
  17.     (setq hl2 (LM:getdynpropvalue (vlax-ename->vla-object e) "HL2"))    
  18.     (setq sl2 (LM:getdynpropvalue (vlax-ename->vla-object e) "SL2"))    
  19.     (setq hl3 (LM:getdynpropvalue (vlax-ename->vla-object e) "HL3"))  
  20.     (setq sl3 (LM:getdynpropvalue (vlax-ename->vla-object e) "SL3"))
  21.    
  22. ;;;;;Take data for first point of Mleader
  23.  
  24.   (setq eData (entget e))
  25.   (setq pointA (cdr (assoc 10 eData)))
  26.   (setq pointB (getpoint "\nSelect Place for you mleader"))
  27.  
  28. ;;;;; Create Mleader, form its text order, exclude HL variables that contains "0"
  29.  
  30.   (if (and HL1 HL2 HL3)
  31.     (progn
  32.       (if (> HL1 0)
  33.         (setq lst (list (strcat "+" (rtos HL1) " (" SL1 ")")))
  34.       )
  35.       (if (> HL2 0)
  36.         (setq lst (append lst (list (strcat "+" (rtos HL2) " (" SL2 ")"))))
  37.       )
  38.       (if (> HL3 0)
  39.         (setq lst (append lst (list (strcat "+" (rtos HL3) " (" SL3 ")"))))
  40.       )
  41.       (setq mtext (car lst)) ;builds a string from the list generated above with \\P inbetween them
  42.       (foreach txt (cdr lst) ;"+1000 (P)\\P+2000 (R)"
  43.         (setq mtext (strcat mtext "\\P" txt))
  44.       )
  45.       (command "_Mleader" pointA pointB mtext)
  46.     )
  47.     (prompt "\nWrong DynBlock")
  48.   )
  49.   (princ)
  50. )
  51.  
Thank you for your help and for clarifying logic behind your method.
« Last Edit: February 07, 2023, 04:36:05 AM by Rufusff7 »

ronjonp

  • Needs a day job
  • Posts: 7529
Re: Delete variable that contains 0
« Reply #6 on: February 07, 2023, 10:15:21 AM »
It's because of how the value is being extracted .. they return different data types.
Code - Auto/Visual Lisp: [Select]
  1. ;; Mhupp
  2. ;; LM
  3. (vlax-get x 'value)

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC