Author Topic: Start and end points of a Dynamic Block  (Read 2661 times)

0 Members and 1 Guest are viewing this topic.

Coder

  • Swamp Rat
  • Posts: 827
Start and end points of a Dynamic Block
« on: February 04, 2015, 12:35:35 AM »
Hello guys  :-)

I am facing a problem with a Dynamic Block , I need to get the start and the end points of the block .

Anyone idea please ?

Thanks in advance .

Attached a drawing with the dynamic block


Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: Start and end points of a Dynamic Block
« Reply #1 on: February 04, 2015, 07:04:04 AM »
An easy way might be to iterate over the visible entities of the anonymous block definition (i.e. the block with name "*U##") for the dynamic block reference and retrieve the endpoints of the grey centerline (providing it has some defining characteristics such as layer/colour to differentiate it from the other entities in the definition); then use gile's refgeom function to transform these endpoints to suit the position/scale/rotation of the block.

roy_043

  • Water Moccasin
  • Posts: 1895
  • BricsCAD 18
Re: Start and end points of a Dynamic Block
« Reply #2 on: February 04, 2015, 10:01:24 AM »
Alternative approach:
1.
Assume that the scaling of the block is always 1 or -1. And that the block is only used in a 2D context.
2.
The insertion point of the block reference, point p1 and point p2 are on the same line.
3.
Point p1 is a fixed distance from the insertion point of the block reference.
4.
The distance from p1 to p2 is determined by the 'Distance1' dynamic property. You can retrieve this value using code. See example below.
5.
You can now use the insertion point, the sign of the X and Y scaling, and the rotation of the block reference, combined with the values from step 3 and 4, to calculate p1 and p2. Tip: Use the polar function.

Code: [Select]
; (GetPropValue (vlax-ename->vla-object (car (entsel))) "Distance1")
(defun GetPropValue (blockRefObj propName / ret)
  (vl-some
    '(lambda (propObj)
      (if (= (strcase propName) (strcase (vlax-get propObj 'propertyname)))
        (progn
          (setq ret (vlax-get propObj 'value))
          T
        )
      )
    )
    (vlax-invoke blockRefObj 'getdynamicblockproperties)
  )
  ret
)
« Last Edit: February 04, 2015, 10:12:26 AM by roy_043 »

Coder

  • Swamp Rat
  • Posts: 827
Re: Start and end points of a Dynamic Block
« Reply #3 on: February 04, 2015, 11:56:07 AM »
Thank you Lee for your advise , I will search for that routine and follow your instructions and will let you know.  :-)

Thank you roy your example works if the insertion point of the block is on one of the two ends.  :wink:

Many thanks for you all .

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: Start and end points of a Dynamic Block
« Reply #4 on: February 04, 2015, 02:00:25 PM »
Point p1 is a fixed distance from the insertion point of the block reference.

Since the linear stretch parameter has two grips (i.e. both P1 & P2 can be repositioned), I don't think this assumption will be valid.

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: Start and end points of a Dynamic Block
« Reply #5 on: February 04, 2015, 03:10:54 PM »
Here is a quick example using the method I proposed above:

Code - Auto/Visual Lisp: [Select]
  1. (defun c:test ( / lst sel )
  2.     (if (setq sel (ssget "_+.:E:S" '((0 . "INSERT") (2 . "`*U*,block"))))
  3.         (if (setq lst (getlinepoints (ssname sel 0)))
  4.             (foreach pnt lst
  5.                 (entmake (list '(0 . "POINT") (cons 10 pnt)))
  6.             )
  7.             (princ "\nTarget line not found.")
  8.         )
  9.     )
  10.     (princ)
  11. )
  12.  
  13. (defun getlinepoints ( ref / ent enx rtn )
  14.     (setq ent (tblobjname "block" (cdr (assoc 2 (entget ref)))))
  15.     (while (setq ent (entnext ent))
  16.         (if
  17.             (and
  18.                 (setq enx (entget ent))
  19.                 (= "LINE" (cdr (assoc 0 enx)))
  20.                 (/= 1 (cdr (assoc 60 enx)))
  21.                 ;; Add more conditions if necessary...
  22.             )
  23.             (setq rtn (list (cdr (assoc 10 enx)) (cdr (assoc 11 enx))))
  24.         )
  25.     )
  26.     (apply '(lambda ( m v ) (mapcar '(lambda ( x ) (mapcar '+ (mxv m x) v)) rtn)) (refgeom ref))
  27. )
  28.  
  29. ;; RefGeom (gile)
  30. ;; Returns a list whose first item is a 3x3 transformation matrix and
  31. ;; second item the object insertion point in its parent (xref, block or space)
  32.  
  33. (defun refgeom ( ent / ang enx mat ocs )
  34.     (setq enx (entget ent)
  35.           ang (cdr (assoc 050 enx))
  36.           ocs (cdr (assoc 210 enx))
  37.     )
  38.     (list
  39.         (setq mat
  40.             (mxm
  41.                 (mapcar '(lambda ( v ) (trans v 0 ocs t))
  42.                    '(
  43.                         (1.0 0.0 0.0)
  44.                         (0.0 1.0 0.0)
  45.                         (0.0 0.0 1.0)
  46.                     )
  47.                 )
  48.                 (mxm
  49.                     (list
  50.                         (list (cos ang) (- (sin ang)) 0.0)
  51.                         (list (sin ang) (cos ang)     0.0)
  52.                        '(0.0 0.0 1.0)
  53.                     )
  54.                     (list
  55.                         (list (cdr (assoc 41 enx)) 0.0 0.0)
  56.                         (list 0.0 (cdr (assoc 42 enx)) 0.0)
  57.                         (list 0.0 0.0 (cdr (assoc 43 enx)))
  58.                     )
  59.                 )
  60.             )
  61.         )
  62.         (mapcar '- (trans (cdr (assoc 10 enx)) ocs 0)
  63.             (mxv mat (cdr (assoc 10 (tblsearch "block" (cdr (assoc 2 enx))))))
  64.         )
  65.     )
  66. )
  67.  
  68. ;; Matrix Transpose  -  Doug Wilson
  69. ;; Args: m - nxn matrix
  70.  
  71. (defun trp ( m )
  72.     (apply 'mapcar (cons 'list m))
  73. )
  74.  
  75. ;; Matrix x Matrix  -  Vladimir Nesterovsky
  76. ;; Args: m,n - nxn matrices
  77.  
  78. (defun mxm ( m n )
  79.     ((lambda ( a ) (mapcar '(lambda ( r ) (mxv a r)) m)) (trp n))
  80. )
  81.  
  82. ;; Matrix x Vector  -  Vladimir Nesterovsky
  83. ;; Args: m - nxn matrix, v - vector in R^n
  84.  
  85. (defun mxv ( m v )
  86.     (mapcar '(lambda ( r ) (apply '+ (mapcar '* r v))) m)
  87. )

Coder

  • Swamp Rat
  • Posts: 827
Re: Start and end points of a Dynamic Block
« Reply #6 on: February 04, 2015, 03:36:06 PM »
Great and Great Lee .
thank you so much , your program works like a charm   :-)

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: Start and end points of a Dynamic Block
« Reply #7 on: February 04, 2015, 03:38:59 PM »
Great and Great Lee .
thank you so much , your program works like a charm   :-)

Excellent, you're most welcome Coder.

roy_043

  • Water Moccasin
  • Posts: 1895
  • BricsCAD 18
Re: Start and end points of a Dynamic Block
« Reply #8 on: February 05, 2015, 05:09:07 AM »
Point p1 is a fixed distance from the insertion point of the block reference.

Since the linear stretch parameter has two grips (i.e. both P1 & P2 can be repositioned), I don't think this assumption will be valid.
In BricsCAD V14.2.17, which has limited support for dynamic blocks, that is not the case and the point p1 appears to be fixed point. Well, at least I now understand the strange distance between the insertion point and p1.

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: Start and end points of a Dynamic Block
« Reply #9 on: February 05, 2015, 07:49:50 AM »
Point p1 is a fixed distance from the insertion point of the block reference.

Since the linear stretch parameter has two grips (i.e. both P1 & P2 can be repositioned), I don't think this assumption will be valid.
In BricsCAD V14.2.17, which has limited support for dynamic blocks, that is not the case and the point p1 appears to be fixed point. Well, at least I now understand the strange distance between the insertion point and p1.

I see - thank you for illuminating this difference between the AutoCAD/BricsCAD dynamic block functionality, that's good to know.