Author Topic: Get Dynamic Block Parameter  (Read 4600 times)

0 Members and 1 Guest are viewing this topic.

chowter

  • Guest
Get Dynamic Block Parameter
« on: June 30, 2015, 05:17:36 AM »
Hi,

i'm new in lisp. . I have some problems, i wanna  get dynamic blocks parameter (named "Base") from my Dynamic Blocks(in file Switch.dwg) parameters and assign to the variable/list named "x".

kpblc

  • Bull Frog
  • Posts: 396
Re: Get Dynamic Block Parameter
« Reply #1 on: June 30, 2015, 05:40:13 AM »
No testing:
Code - Auto/Visual Lisp: [Select]
  1. (defun set-dynprop (blk prop value / def err)
  2.                    ;|
  3. blk    pointer to block reference
  4. prop   dynmic property name
  5. value  value to set to prop
  6.  
  7. call sample:
  8. (set-dynprop (car (entsel "\nSelect block")) "base" 2)
  9. |;
  10.   (if (and (setq blk (cond
  11.                        ((= (type blk) 'ename)
  12.                         (setq blk (vlax-ename->vla-object blk))
  13.                         )
  14.                        ((= (type blk) 'vla-object) blk)
  15.                        ) ;_ end of cond
  16.                  ) ;_ end of setq
  17.            (= (vla-get-objectname blk) "AcDbBlockReference")
  18.            (vlax-method-applicable-p blk 'getdynamicblockproperties)
  19.            (setq def (car (vl-remove-if-not
  20.                             (function
  21.                               (lambda (x)
  22.                                 (= (strcase prop) (strcase (vla-get-propertyname x)))
  23.                                 ) ;_ end of LAMBDA
  24.                               ) ;_ end of function
  25.                             (vlax-safearray->list (vlax-variant-value (vla-getdynamicblockproperties blk)))
  26.                             ) ;_ end of vl-remove-if-not
  27.                           ) ;_ end of car
  28.                  ) ;_ end of setq
  29.            ) ;_ end of and
  30.           (setq err (vl-catch-all-apply
  31.                       (function
  32.                         (lambda ()
  33.                           (vla-put-value def value)
  34.                           ) ;_ end of LAMBDA
  35.                         ) ;_ end of function
  36.                       ) ;_ end of VL-CATCH-ALL-APPLY
  37.                 ) ;_ end of setq
  38.           ) ;_ end of VL-CATCH-ALL-ERROR-P
  39.       (princ (strcat "\nError setting value "
  40.                      (vl-princ-to-string value)
  41.                      " to property \""
  42.                      prop
  43.                      "\" : "
  44.                      (vl-catch-all-error-message err)
  45.                      ) ;_ end of strcat
  46.              ) ;_ end of princ
  47.       ) ;_ end of if
  48.     ) ;_ end of if
  49.   ) ;_ end of defun
  50.  
Sorry for my English.

Lee Mac

  • Seagull
  • Posts: 12913
  • London, England
Re: Get Dynamic Block Parameter
« Reply #2 on: June 30, 2015, 07:18:10 AM »
My Dynamic Block Functions may help.

For your example, you would call my LM:getdynpropvalue function in the following way:
Code - Auto/Visual Lisp: [Select]
  1. (defun c:test ( / e x )
  2.     (if (and (setq e (car (entsel "\nSelect block: ")))
  3.              (= "INSERT" (cdr (assoc 0 (entget e))))
  4.         )
  5.         (print (setq x (LM:getdynpropvalue (vlax-ename->vla-object e) "base")))
  6.     )
  7.     (princ)
  8. )
« Last Edit: June 30, 2015, 07:21:29 AM by Lee Mac »

chowter

  • Guest
Re: Get Dynamic Block Parameter
« Reply #3 on: July 03, 2015, 07:35:39 AM »
Thanks Lee Mac :)

But i wanna Select block in different way. For example:

I have base address for all blocks and each block have own base address( first = 1, second = 2 etc). So if i enter base address (for example 100: First block would have 101 adress second 102). (look new attached file)

My lisp code:

Code: [Select]
Set dynamic block parameters

(defun myModifyBk ( lstProp / ss index cnt oBkRef oProps i j oSBReferenceProperty)
(vl-load-com)
(SETQ ss (SSGET "_A"))
(setq index 0
      cnt (sslength ss)
)

(while (< index cnt)
(setq e (ssname ss index))
(setq oBkRef (vlax-ename->vla-object e))
   
(setq oProps (vlax-variant-value (vla-GetDynamicBlockProperties oBkRef)))

(setq i (vlax-safearray-get-l-bound oProps 1))
(while (<= i (vlax-safearray-get-u-bound oProps 1))
(setq oSBReferenceProperty (vlax-safearray-get-element oProps i))
(print (strcat (vla-get-PropertyName oSBReferenceProperty) "="))
(princ (vlax-variant-value (vla-get-value oSBReferenceProperty)))
(setq i (1+ i))
)

(setq j 0)
(while (< j (length lstProp))
(setq sProp (strcase (nth j lstProp)))
(setPropValue oProps sProp (nth (+ 1 j) lstProp))
(setq j (+ 2 j))
) ;while < j (length lstProp)

(setq index (1+ index))
) ;while < index cnt


(princ)
)

(defun setPropValue (oProps sProp Val / i oSBReferenceProperty sPName iFound)
(setq i (vlax-safearray-get-l-bound oProps 1))
(setq iFound 0)
(while (and (<= i (vlax-safearray-get-u-bound oProps 1)) (= iFound 0))
(setq oSBReferenceProperty (vlax-safearray-get-element oProps i))
(setq sPName (vla-get-PropertyName oSBReferenceProperty))
(if (= (strcase sPName) sProp)
(progn
(print (strcat "_Old value of " sPName "="))
(princ (vlax-variant-value (vla-get-value oSBReferenceProperty)))
(vla-put-value oSBReferenceProperty
(vlax-make-variant Val
(vlax-variant-type (vla-get-value oSBReferenceProperty))
)
)
(print "New value=")
(princ (vlax-variant-value (vla-get-value oSBReferenceProperty)))
(setq iFound 1)
)
)

(setq i (1+ i))
)
(princ)
)





;--------------------------------
;Get Base adress from block

;; 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)
    )
)

        (if (and (setq e (car (entsel "\nSelect block: "))) ;(entsel "\nSelect block: ")
                 (= "INSERT" (cdr (assoc 0 (entget e))))
            )
            (print (setq x (LM:getdynpropvalue (vlax-ename->vla-object e) "base")))
        )




;----------
;Enter adress

(setq
  adres (getint "\nEnter adress[0-1023]: ")
);setq

;----------
;Set pins

;----------
;9

(if (>= adres 512)
  (progn
    (setq
  adres (- adres 512)
);setq
    (setq x9 1)
  );progn
  (progn ;else
    (setq x9 0)
  );progn
);if

;----------
;8

(if (>= adres 256)
  (progn
    (setq
  adres (- adres 256)
);setq
    (setq x8 1)
  );progn
  (progn ;else
    (setq x8 0)
  );progn
);if

;----------
;7

(if (>= adres 128)
  (progn
    (setq
  adres (- adres 128)
);setq
    (setq x7 1)
  );progn
  (progn ;else
    (setq x7 0)
  );progn
);if

;----------
;6

(if (>= adres 64)
  (progn
    (setq
  adres (- adres 64)
);setq
    (setq x6 1)
  );progn
  (progn ;else
    (setq x6 0)
  );progn
);if

;----------
;5

(if (>= adres 32)
  (progn
    (setq
  adres (- adres 32)
);setq
    (setq x5 1)
  );progn
  (progn ;else
    (setq x5 0)
  );progn
);if

;----------
;4

(if (>= adres 16)
  (progn
    (setq
  adres (- adres 16)
);setq
    (setq x4 1)
  );progn
  (progn ;else
    (setq x4 0)
  );progn
);if

;----------
;3

(if (>= adres 8)
  (progn
    (setq
  adres (- adres 8)
);setq
    (setq x3 1)
  );progn
  (progn ;else
    (setq x3 0)
  );progn
);if

;----------
;2

(if (>= adres 4)
  (progn
    (setq
  adres (- adres 4)
);setq
    (setq x2 1)
  );progn
  (progn ;else
    (setq x2 0)
  );progn
);if

;----------
;1

(if (>= adres 2)
  (progn
    (setq
  adres (- adres 2)
);setq
    (setq x1 1)
  );progn
  (progn ;else
    (setq x1 0)
  );progn
);if

;----------
;0

(if (>= adres 1)
  (progn
    (setq
  adres (- adres 1)
);setq
    (setq x0 1)
  );progn
  (progn ;else
    (setq x0 0)
  );progn
);if

;Print adress
(princ "\n_PROFIsafe Address: ")
(princ x9)
(princ x8)
(princ x7)
(princ x6)
(princ x5)
(princ x4)
(princ x3)
(princ x2)
(princ x1)
(princ x0)




;----------
;0

(if (>= x0 1)
  (progn
(myModifyBk (list "Flip_state0" 1))
  );progn
  (progn ;else
(myModifyBk (list "Flip_state0" 0))
  );progn
);if

;----------
;1

(if (>= x1 1)
  (progn
(myModifyBk (list "Flip_state1" 1))
  );progn
  (progn ;else
(myModifyBk (list "Flip_state1" 0))
  );progn
);if

;----------
;2

(if (>= x2 1)
  (progn
(myModifyBk (list "Flip_state2" 1))
  );progn
  (progn ;else
(myModifyBk (list "Flip_state2" 0))
  );progn
);if
;----------
;3

(if (>= x3 1)
  (progn
(myModifyBk (list "Flip_state3" 1))
  );progn
  (progn ;else
(myModifyBk (list "Flip_state3" 0))
  );progn
);if

;----------
;4

(if (>= x4 1)
  (progn
(myModifyBk (list "Flip_state4" 1))
  );progn
  (progn ;else
(myModifyBk (list "Flip_state4" 0))
  );progn
);if

;----------
;5

(if (>= x5 1)
  (progn
(myModifyBk (list "Flip_state5" 1))
  );progn
  (progn ;else
(myModifyBk (list "Flip_state5" 0))
  );progn
);if

;----------
;6

(if (>= x6 1)
  (progn
(myModifyBk (list "Flip_state6" 1))
  );progn
  (progn ;else
(myModifyBk (list "Flip_state6" 0))
  );progn
);if

;----------
;7

(if (>= x7 1)
  (progn
(myModifyBk (list "Flip_state7" 1))
  );progn
  (progn ;else
(myModifyBk (list "Flip_state7" 0))
  );progn
);if

;----------
;8

(if (>= x8 1)
  (progn
(myModifyBk (list "Flip_state8" 1))
  );progn
  (progn ;else
(myModifyBk (list "Flip_state8" 0))
  );progn
);if

;----------
;9

(if (>= x9 1)
  (progn
(myModifyBk (list "Flip_state9" 1))
  );progn
  (progn ;else
(myModifyBk (list "Flip_state9" 0))
  );progn
);if


;----------
;**************************************************END

I would like to happen in such a way that when entering command (eg . Setprofi ) script automatically adjust the pins in all blocks across the page .

Lee Mac

  • Seagull
  • Posts: 12913
  • London, England
Re: Get Dynamic Block Parameter
« Reply #4 on: July 03, 2015, 08:54:54 AM »
Thanks Lee Mac :)

But i wanna Select block in different way.

You're welcome.

The posted code was only an example - you can pass my LM:getdynpropvalue function a Block Reference vla-object obtained through any means, not just from an entsel selection.

Lee

chowter

  • Guest
Re: Get Dynamic Block Parameter
« Reply #5 on: July 09, 2015, 05:17:02 AM »
I have one more problem. :(

Part of code:
Code: [Select]
(defun myModifyBk ( lstProp / ss index cnt oBkRef oProps i j oSBReferenceProperty)
(vl-load-com)
(SETQ ss (SSGET "_A"))
(setq index 0
       cnt (sslength ss)
)

(while (< index cnt)
  (setq e (ssname ss index))
(setq oBkRef (vlax-ename->vla-object e))
   
(setq oProps (vlax-variant-value (vla-GetDynamicBlockProperties oBkRef)))


 
  (setq i (vlax-safearray-get-l-bound oProps 1))
  (while (<= i (vlax-safearray-get-u-bound oProps 1))
   (setq oSBReferenceProperty (vlax-safearray-get-element oProps i))
   (print (strcat (vla-get-PropertyName oSBReferenceProperty) "="))
   (princ (vlax-variant-value (vla-get-value oSBReferenceProperty)))
   (setq i (1+ i))
  )

  (setq j 0)
  (while (< j (length lstProp))
   (setq sProp (strcase (nth j lstProp)))
   (setPropValue oProps sProp (nth (+ 1 j) lstProp))
   (setq j (+ 2 j))
  ) ;while < j (length lstProp)
 
  (setq index (1+ index))
) ;while < index cnt

(princ)
)

Works fine in first attached file (first post) but gives error in second Switch.dwg

Code: [Select]
error: ActiveX Server returned the error: unknown name: GetDynamicBlockProperties
I would be grateful for help.


roy_043

  • Water Moccasin
  • Posts: 1895
  • BricsCAD 18
Re: Get Dynamic Block Parameter
« Reply #6 on: July 09, 2015, 08:24:39 AM »
Your code assumes that every object in (SSGET "_A") has dynamic block properties. But your drawing also contains text objects which obviously lack those properties. Hence the error you have reported. You can fix this by using (SSGET "_A" '((0 . "INSERT"))). But your code will then still have to account for non-dynamic block references where oProps will be nil.

Two other tips:
Look at the built-in (logand) function.
Try to improve your coding style: Create functions, localize variables etc.