Author Topic: Find known object inside a block  (Read 1548 times)

0 Members and 1 Guest are viewing this topic.

kenkrupa

  • Newt
  • Posts: 84
Find known object inside a block
« on: November 27, 2013, 03:27:29 PM »
Some dynamic blocks each contain a circle. For any insertion of that block, I want to find the center point of that circle. It will always lie in the World plane, but it's relation to the insertion point varies when a dynamic angle changes. I need to find the center of this circle. Any way to do this?

Lee Mac

  • Seagull
  • Posts: 12929
  • London, England
Re: Find known object inside a block
« Reply #1 on: November 27, 2013, 06:48:57 PM »
You should be able to use the center of the circle found within the anonymous block definition that is automatically generated for the dynamic block reference when the dynamic parameters are altered.

You would then transform the center coordinates to account for the block reference position, scale & rotation using an appropriate transformation matrix - for this I would recommend gile's refgeom function.

kenkrupa

  • Newt
  • Posts: 84
Re: Find known object inside a block
« Reply #2 on: November 27, 2013, 10:22:16 PM »
Thank you much Lee, and gile too. That matrix stuff is something I would never figure out myself. Going into Thanksgiving day here, so it may be a few days before I can apply this, but I'm thankful for it already!

Lee Mac

  • Seagull
  • Posts: 12929
  • London, England
Re: Find known object inside a block
« Reply #3 on: November 28, 2013, 09:38:29 AM »
You're most welcome ken  :-)

Here is a quick & simple example program for you to test:
Code - Auto/Visual Lisp: [Select]
  1. (defun c:cc ( / blk bln blx cen ent enx lst )
  2.     (while
  3.         (progn
  4.             (setvar 'errno 0)
  5.             (setq blk (car (entsel "\nSelect block: "))
  6.                   cen nil
  7.             )
  8.             (cond
  9.                 (   (= 7 (getvar 'errno))
  10.                     (princ "\nMissed, try again.")
  11.                 )
  12.                 (   (null blk)
  13.                     nil
  14.                 )
  15.                 (   (/= "INSERT" (cdr (assoc 0 (setq blx (entget blk)))))
  16.                     (princ "\nPlease select a block.")
  17.                 )
  18.                 (   (not
  19.                         (or (setq bln (cdr (assoc 2 blx))
  20.                                   cen (cdr (assoc bln lst))
  21.                             )
  22.                             (progn
  23.                                 (setq ent (tblobjname "block" bln))
  24.                                 (while
  25.                                     (and
  26.                                         (null cen)
  27.                                         (setq ent (entnext ent))
  28.                                     )
  29.                                     (if (= "CIRCLE" (cdr (assoc 0 (setq enx (entget ent)))))
  30.                                         (setq cen (trans (cdr (assoc 10 enx)) (cdr (assoc 210 enx)) 0))
  31.                                     )
  32.                                 )
  33.                                 (if cen (setq lst (cons (cons bln cen) lst)))
  34.                             )
  35.                         )
  36.                     )
  37.                     (princ "\nSelected block does not contain a circle.")
  38.                 )
  39.                 (   (entmake
  40.                         (list
  41.                            '(0 . "POINT")
  42.                             (cons 10 (apply '(lambda ( m v ) (mapcar '+ (mxv m cen) v)) (refgeom blk)))
  43.                         )
  44.                     )
  45.                 )
  46.             )
  47.         )
  48.     )
  49.     (princ)
  50. )
  51.  
  52. ;; RefGeom (gile)
  53. ;; Returns a list whose first item is a 3x3 transformation matrix and
  54. ;; second item the object insertion point in its parent (xref, block or space)
  55.  
  56. (defun refgeom ( ent / ang enx mat ocs )
  57.     (setq enx (entget ent)
  58.           ang (cdr (assoc 050 enx))
  59.           ocs (cdr (assoc 210 enx))
  60.     )
  61.     (list
  62.         (setq mat
  63.             (mxm
  64.                 (mapcar '(lambda ( v ) (trans v 0 ocs t))
  65.                    '(
  66.                         (1.0 0.0 0.0)
  67.                         (0.0 1.0 0.0)
  68.                         (0.0 0.0 1.0)
  69.                     )
  70.                 )
  71.                 (mxm
  72.                     (list
  73.                         (list (cos ang) (- (sin ang)) 0.0)
  74.                         (list (sin ang) (cos ang)     0.0)
  75.                        '(0.0 0.0 1.0)
  76.                     )
  77.                     (list
  78.                         (list (cdr (assoc 41 enx)) 0.0 0.0)
  79.                         (list 0.0 (cdr (assoc 42 enx)) 0.0)
  80.                         (list 0.0 0.0 (cdr (assoc 43 enx)))
  81.                     )
  82.                 )
  83.             )
  84.         )
  85.         (mapcar '- (trans (cdr (assoc 10 enx)) ocs 0)
  86.             (mxv mat (cdr (assoc 10 (tblsearch "block" (cdr (assoc 2 enx))))))
  87.         )
  88.     )
  89. )
  90.  
  91. ;; Matrix Transpose  -  Doug Wilson
  92. ;; Args: m - nxn matrix
  93.  
  94. (defun trp ( m )
  95.     (apply 'mapcar (cons 'list m))
  96. )
  97.  
  98. ;; Matrix x Matrix  -  Vladimir Nesterovsky
  99. ;; Args: m,n - nxn matrices
  100.  
  101. (defun mxm ( m n )
  102.     ((lambda ( a ) (mapcar '(lambda ( r ) (mxv a r)) m)) (trp n))
  103. )
  104.  
  105. ;; Matrix x Vector  -  Vladimir Nesterovsky
  106. ;; Args: m - nxn matrix, v - vector in R^n
  107.  
  108. (defun mxv ( m v )
  109.     (mapcar '(lambda ( r ) (apply '+ (mapcar '* r v))) m)
  110. )
  111.  
The program will generate a POINT entity at the center of the first circle found in the block definition (this could easily be altered to match circles based on criteria such as layer / linetype / colour / handle etc.)

Here is a demonstration using a simple dynamic block with a dynamic move parameter assigned to the circle:

« Last Edit: November 28, 2013, 09:41:34 AM by Lee Mac »

kenkrupa

  • Newt
  • Posts: 84
Re: Find known object inside a block
« Reply #4 on: November 28, 2013, 10:35:31 AM »
Perfect! Thank you!