Author Topic: Move a point in a dynamic block, with a LSP  (Read 1801 times)

0 Members and 1 Guest are viewing this topic.

Mirko

  • Guest
Move a point in a dynamic block, with a LSP
« on: May 13, 2016, 03:40:07 PM »
Hi everyone!
I've done a dynamic block. To let you know it, I've semplified it (It's to much difficult to exlplain in words...), so:
7
^this is my dynamic block

When i insert my blocks, the point "PT1" of all the block it's never ordered (I must set one by one). So I think i need a LISP that move the point of the dynamic block "FLOOR-DATA" aligned to a point that i set. If I know the height "H" of each frame I can remove the H-value from the Y-value from the point setted.
So a possible operation could be this:
Pick the point (P1): setq P1....
then the lisp, remove the value H (1) from the Y coordinate, from the point (P1). It start from the number "NUM" 1 to the end of the "NUM".
example number 2 will be: P1,Y-1-1 ---> 3 will be: P1,Y-1-1-1 ---> 4 will be: P1,Y-1-1-1-1 etc...

I'm not able to this, because i'm not expert in LISP. I wanted to know if it's possible to do?

Below I made a scheme to make you better understand:



^1


^2


^3

I hope you understand my question...If not ask me

Thanks

(Attached the example in dwg)



Mirko

  • Guest
Re: Move a point in a dynamic block, with a LSP
« Reply #1 on: May 14, 2016, 10:52:31 AM »
Hi everyone,
I've tried to write with my small knowledge in LSP this:

Code - Auto/Visual Lisp: [Select]
  1. (setq STARTPOINT (getpoint "\nSpecify the point to start: "))
  2. (setq H (getreal "\nEnter the value of the height :")) ;For me It's 1 (the height value)
  3. (setq STARTPOINT_X (nth 0 STARTPOINT))
  4. (setq STARTPOINT_Y (nth 1 STARTPOINT))
  5. (setq STARTPOINT_Z (nth 2 STARTPOINT))
  6.  
  7. (setq NUM (attrubute_from_block_called_NUM))    ;I'm not able to do this
  8. (setq SELECTED_BLOCKS (ssget ....))             ;select the blocks
  9. (repeat_for_al_the_blocks_selected (NUM * H))   ;that increment the distance from the others blocks
  10. (setq New_point_for_the_anchor_point_PT1 (list STARTPOINT_X (- STARTPOINT_Y H) STARTPOINT_Z)) ;set the new point
  11. (repeat_for_al_the_blocks_selected)             ;repeat for all the blocks
  12.  
  13. (LM:setdynpropvalue "PT1" (point New_point_for_the_anchor_point_PT1))
  14.  
  15. ;; Set Dynamic Block Property Value  -  Lee Mac
  16. ;; Modifies the value of a Dynamic Block property (if present)
  17. ;; blk - [vla] VLA Dynamic Block Reference object
  18. ;; prp - [str] Dynamic Block property name (case-insensitive)
  19. ;; val - [any] New value for property
  20. ;; Returns: [any] New value if successful, else nil
  21.  
  22. (defun LM:setdynpropvalue ( blk prp val )
  23.     (setq prp (strcase prp))
  24.     (vl-some
  25.        '(lambda ( x )
  26.             (if (= prp (strcase (vla-get-propertyname x)))
  27.                 (progn
  28.                     (vla-put-value x (vlax-make-variant val (vlax-variant-type (vla-get-value x))))
  29.                     (cond (val) (t))
  30.                 )))
  31.         (vlax-invoke blk 'getdynamicblockproperties)
  32.     )
  33. )


My problems are:
  -  I've inserted some notes, and added a function that I took from http://www.lee-mac.com/dynamicblockfunctions.html#setdynamicpropvalue, but i'm not able to integrate in my code (I've tried but I think I'm wrong)
  -  i'm not able to call and modify a parameter from a dynamic block

Someone could help me in this?  :nerdystraight: because more than this I'm not able to do  :reallysad: :reallysad:

Thank you so much in advance

have a great day

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: Move a point in a dynamic block, with a LSP
« Reply #2 on: May 15, 2016, 10:58:24 AM »
Please try the following:
Code - Auto/Visual Lisp: [Select]
  1. (defun c:positionblocks ( / ano blk bpt idx lst obj par sel srt tag val vec )
  2.  
  3.     (setq blk "FLOOR-DATA"
  4.           tag "NUM"
  5.           par '("POSIZIONE1 X" "POSIZIONE1 Y")
  6.           vec '(0.0 -1.0 0.0)
  7.     )
  8.    
  9.     (if
  10.         (and
  11.             (setq sel
  12.                 (ssget "_:L"
  13.                     (list '(0 . "INSERT")
  14.                         (cons 2
  15.                             (strcat blk
  16.                                 (apply 'strcat
  17.                                     (mapcar '(lambda ( ano ) (strcat ",`" ano))
  18.                                         (LM:getanonymousreferences blk)
  19.                                     )
  20.                                 )
  21.                             )
  22.                         )
  23.                     )
  24.                 )
  25.             )
  26.             (setq bpt (getpoint "\nSpecify base point: "))
  27.             (setq bpt (trans bpt 1 0))
  28.         )
  29.         (progn
  30.             (repeat (setq idx (sslength sel))
  31.                 (if (setq obj (vlax-ename->vla-object (ssname sel (setq idx (1- idx))))
  32.                           val (LM:vl-getattributevalue obj tag)
  33.                     )
  34.                     (setq srt (cons (atoi val) srt)
  35.                           lst (cons (cons obj (vlax-get obj 'insertionpoint)) lst)
  36.                     )
  37.                 )
  38.             )
  39.             (foreach idx (vl-sort-i srt '<)
  40.                 (LM:setdynprops (car (nth idx lst)) (mapcar 'cons par (mapcar '- bpt (cdr (nth idx lst)))))
  41.                 (setq bpt (mapcar '+ bpt vec))
  42.             )
  43.         )
  44.     )
  45.     (princ)
  46. )
  47.  
  48. ;; Set Dynamic Block Properties  -  Lee Mac
  49. ;; Modifies values of Dynamic Block properties using a supplied association list.
  50. ;; blk - [vla] VLA Dynamic Block Reference object
  51. ;; lst - [lst] Association list of ((<Property> . <Value>) ... )
  52. ;; Returns: nil
  53.  
  54. (defun LM:setdynprops ( blk lst / itm )
  55.     (setq lst (mapcar '(lambda ( x ) (cons (strcase (car x)) (cdr x))) lst))
  56.     (foreach x (vlax-invoke blk 'getdynamicblockproperties)
  57.         (if (setq itm (assoc (strcase (vla-get-propertyname x)) lst))
  58.             (vla-put-value x (vlax-make-variant (cdr itm) (vlax-variant-type (vla-get-value x))))
  59.         )
  60.     )
  61. )
  62.  
  63. ;; Get Attribute Value  -  Lee Mac
  64. ;; Returns the value held by the specified tag within the supplied block, if present.
  65. ;; blk - [vla] VLA Block Reference Object
  66. ;; tag - [str] Attribute TagString
  67. ;; Returns: [str] Attribute value, else nil if tag is not found.
  68.  
  69. (defun LM:vl-getattributevalue ( blk tag )
  70.     (setq tag (strcase tag))
  71.     (vl-some '(lambda ( att ) (if (= tag (strcase (vla-get-tagstring att))) (vla-get-textstring att)))
  72.         (vlax-invoke blk 'getattributes)
  73.     )
  74. )
  75.  
  76. ;; Get Anonymous References  -  Lee Mac
  77. ;; Returns the names of all anonymous references of a block.
  78. ;; blk - [str] Block name/wildcard pattern for which to return anon. references
  79.  
  80. (defun LM:getanonymousreferences ( blk / ano def lst rec ref )
  81.     (setq blk (strcase blk))
  82.     (while (setq def (tblnext "block" (null def)))
  83.         (if
  84.             (and (= 1 (logand 1 (cdr (assoc 70 def))))
  85.                 (setq rec
  86.                     (entget
  87.                         (cdr
  88.                             (assoc 330
  89.                                 (entget
  90.                                     (tblobjname "block"
  91.                                         (setq ano (cdr (assoc 2 def)))
  92.                                     )
  93.                                 )
  94.                             )
  95.                         )
  96.                     )
  97.                 )
  98.             )
  99.             (while
  100.                 (and
  101.                     (not (member ano lst))
  102.                     (setq ref (assoc 331 rec))
  103.                 )
  104.                 (if
  105.                     (and
  106.                         (entget (cdr ref))
  107.                         (wcmatch (strcase (LM:al-effectivename (cdr ref))) blk)
  108.                     )
  109.                     (setq lst (cons ano lst))
  110.                 )
  111.                 (setq rec (cdr (member (assoc 331 rec) rec)))
  112.             )
  113.         )
  114.     )
  115.     (reverse lst)
  116. )
  117.                        
  118. ;; Effective Block Name  -  Lee Mac
  119. ;; ent - [ent] Block Reference entity
  120.  
  121. (defun LM:al-effectivename ( ent / blk rep )
  122.     (if (wcmatch (setq blk (cdr (assoc 2 (entget ent)))) "`**")
  123.         (if
  124.             (and
  125.                 (setq rep
  126.                     (cdadr
  127.                         (assoc -3
  128.                             (entget
  129.                                 (cdr
  130.                                     (assoc 330
  131.                                         (entget
  132.                                             (tblobjname "block" blk)
  133.                                         )
  134.                                     )
  135.                                 )
  136.                                '("acdbblockrepbtag")
  137.                             )
  138.                         )
  139.                     )
  140.                 )
  141.                 (setq rep (handent (cdr (assoc 1005 rep))))
  142.             )
  143.             (setq blk (cdr (assoc 2 (entget rep))))
  144.         )
  145.     )
  146.     blk
  147. )
  148.  

The above uses the following library functions:

Effective Block Name
Get Anonymous References
Get Attribute Value
Set Dynamic Block Properties

Mirko

  • Guest
Re: Move a point in a dynamic block, with a LSP
« Reply #3 on: May 15, 2016, 02:37:10 PM »
WOOOOWW Thanks very much Lee Mac :) It works perfectly!! :) :)

Your website and the library functions are very very interesting! (although many are complicated to understand, since I'm a beginner in lisp)

I've another little question..
I've tried  to change your lisp, to allow  to work even if I select 2 blocks with different name (with same attributes and parameters)...In ths way I could use it also for an another block for the windows....The problem is that the modify I've done doesen't work...

Code - Auto/Visual Lisp: [Select]
  1. (defun c:positionblocks ( / ano blk bpt idx lst obj par sel srt tag val vec )
  2.  
  3.     (setq tag "NUM"
  4.           par '("POSIZIONE1 X" "POSIZIONE1 Y")
  5.           vec '(0.0 -1.0 0.0)
  6.     )
  7.  
  8.     (if
  9.         (and
  10.             (setq sel
  11.                 (ssget "_:L"
  12.                     (list '(0 . "INSERT")
  13.                         (cons 2
  14.                                 (apply 'strcat
  15.                                     (mapcar '(lambda ( ano ) (strcat ",`" ano))
  16.                                         (LM:getanonymousreferences blk)
  17.                                     )
  18.                                 )
  19.                         )
  20.                     )
  21.                 )
  22.             )
  23.             (setq bpt (getpoint "\nSpecify base point: "))
  24.             (setq bpt (trans bpt 1 0))
  25.         )
  26.         (progn
  27.             (repeat (setq idx (sslength sel))
  28.                 (if (setq obj (vlax-ename->vla-object (ssname sel (setq idx (1- idx))))
  29.                           val (LM:vl-getattributevalue obj tag)
  30.                     )
  31.                     (setq srt (cons (atoi val) srt)
  32.                           lst (cons (cons obj (vlax-get obj 'insertionpoint)) lst)
  33.                     )
  34.                 )
  35.             )
  36.             (foreach idx (vl-sort-i srt '<)
  37.                 (LM:setdynprops (car (nth idx lst)) (mapcar 'cons par (mapcar '- bpt (cdr (nth idx lst)))))
  38.                 (setq bpt (mapcar '+ bpt vec))
  39.             )
  40.         )
  41.     )
  42.     (princ)
  43. )

What I'm wrong?

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: Move a point in a dynamic block, with a LSP
« Reply #4 on: May 15, 2016, 07:23:00 PM »
You're most welcome - I'm glad the code is working well.

Below is a modified version to remove the block name restriction:
Code - Auto/Visual Lisp: [Select]
  1. (defun c:positionblocks ( / bpt idx lst obj par sel srt tag val vec )
  2.  
  3.     (setq tag "NUM"
  4.           par '("POSIZIONE1 X" "POSIZIONE1 Y")
  5.           vec '(0.0 -1.0 0.0)
  6.     )
  7.     (if (and (setq sel (ssget "_:L" '((0 . "INSERT"))))
  8.              (setq bpt (getpoint "\nSpecify base point: "))
  9.              (setq bpt (trans bpt 1 0))
  10.         )
  11.         (progn
  12.             (repeat (setq idx (sslength sel))
  13.                 (if (setq obj (vlax-ename->vla-object (ssname sel (setq idx (1- idx))))
  14.                           val (LM:vl-getattributevalue obj tag)
  15.                     )
  16.                     (setq srt (cons (atoi val) srt)
  17.                           lst (cons (cons obj (vlax-get obj 'insertionpoint)) lst)
  18.                     )
  19.                 )
  20.             )
  21.             (foreach idx (vl-sort-i srt '<)
  22.                 (LM:setdynprops (car (nth idx lst)) (mapcar 'cons par (mapcar '- bpt (cdr (nth idx lst)))))
  23.                 (setq bpt (mapcar '+ bpt vec))
  24.             )
  25.         )
  26.     )
  27.     (princ)
  28. )
  29.  
  30. ;; Set Dynamic Block Properties  -  Lee Mac
  31. ;; Modifies values of Dynamic Block properties using a supplied association list.
  32. ;; blk - [vla] VLA Dynamic Block Reference object
  33. ;; lst - [lst] Association list of ((<Property> . <Value>) ... )
  34. ;; Returns: nil
  35.  
  36. (defun LM:setdynprops ( blk lst / itm )
  37.     (setq lst (mapcar '(lambda ( x ) (cons (strcase (car x)) (cdr x))) lst))
  38.     (foreach x (vlax-invoke blk 'getdynamicblockproperties)
  39.         (if (setq itm (assoc (strcase (vla-get-propertyname x)) lst))
  40.             (vla-put-value x (vlax-make-variant (cdr itm) (vlax-variant-type (vla-get-value x))))
  41.         )
  42.     )
  43. )
  44.  
  45. ;; Get Attribute Value  -  Lee Mac
  46. ;; Returns the value held by the specified tag within the supplied block, if present.
  47. ;; blk - [vla] VLA Block Reference Object
  48. ;; tag - [str] Attribute TagString
  49. ;; Returns: [str] Attribute value, else nil if tag is not found.
  50.  
  51. (defun LM:vl-getattributevalue ( blk tag )
  52.     (setq tag (strcase tag))
  53.     (vl-some '(lambda ( att ) (if (= tag (strcase (vla-get-tagstring att))) (vla-get-textstring att)))
  54.         (vlax-invoke blk 'getattributes)
  55.     )
  56. )
  57.  

Mirko

  • Guest
Re: Move a point in a dynamic block, with a LSP
« Reply #5 on: May 16, 2016, 04:10:56 AM »
Thanks very much Lee Mac :)

have a great day

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: Move a point in a dynamic block, with a LSP
« Reply #6 on: May 16, 2016, 04:14:05 AM »
You too Mirko  :-)