Author Topic: Get coordinates of point in a block insertion  (Read 1618 times)

0 Members and 1 Guest are viewing this topic.

kenkrupa

  • Newt
  • Posts: 84
Get coordinates of point in a block insertion
« on: March 20, 2013, 04:30:17 PM »
The blocks in question contains two point entities. The block may be inserted at any orientation. I need to get the x,y,z of both, in any given instance of the block. So far, I can step through the block definition and get the two points, but I don't know how to translate them to an insertion (if that's even the way to go about this). Any help with this?

Lee Mac

  • Seagull
  • Posts: 12926
  • London, England
Re: Get coordinates of point in a block insertion
« Reply #1 on: March 20, 2013, 06:58:34 PM »
gile's RefGeom function is incredibly useful for such applications, e.g.:

Code - Auto/Visual Lisp: [Select]
  1. (defun c:test ( / ent )
  2.     (while
  3.         (progn (setvar 'errno 0) (setq ent (car (entsel "\nSelect Block Reference: ")))
  4.             (cond
  5.                 (   (= 7 (getvar 'errno))
  6.                     (princ "\nMissed, try again.")
  7.                 )
  8.                 (   (= 'ename (type ent))
  9.                     (if (= "INSERT" (cdr (assoc 0 (entget ent))))
  10.                         (apply
  11.                            '(lambda ( mat vec )
  12.                                 (
  13.                                     (lambda ( ent / enx )
  14.                                         (while (setq ent (entnext ent))
  15.                                             (if (= "POINT" (cdr (assoc 0 (setq enx (entget ent)))))
  16.                                                 (entmake
  17.                                                     (list '(0 . "POINT")
  18.                                                         (cons 10
  19.                                                             (mapcar '+ (mxv mat (cdr (assoc 10 enx))) vec)
  20.                                                         )
  21.                                                     )
  22.                                                 )
  23.                                             )
  24.                                         )
  25.                                     )
  26.                                     (tblobjname "BLOCK" (cdr (assoc 2 (entget ent))))
  27.                                 )
  28.                             )
  29.                             (refgeom ent)
  30.                         )
  31.                         (princ "\nObject is not a block.")
  32.                     )
  33.                 )
  34.             )
  35.         )
  36.     )
  37.     (princ)
  38. )
  39.  
  40. ;; RefGeom (gile)
  41. ;; Returns a list which first item is a 3x3 transformation matrix (rotation,
  42. ;; scales, normal) and second item the object insertion point in its parent
  43. ;; (xref, block or space)
  44. ;;
  45. ;; Argument : an ename
  46.  
  47. (defun RefGeom ( ent / ang enx mat ocs )
  48.     (setq enx (entget ent)
  49.           ang (cdr (assoc 050 enx))
  50.           ocs (cdr (assoc 210 enx))
  51.     )
  52.     (list
  53.         (setq mat
  54.             (mxm
  55.                 (mapcar '(lambda ( v ) (trans v 0 ocs t))
  56.                    '(
  57.                         (1.0 0.0 0.0)
  58.                         (0.0 1.0 0.0)
  59.                         (0.0 0.0 1.0)
  60.                     )
  61.                 )
  62.                 (mxm
  63.                     (list
  64.                         (list (cos ang) (- (sin ang)) 0.0)
  65.                         (list (sin ang) (cos ang)     0.0)
  66.                        '(0.0 0.0 1.0)
  67.                     )
  68.                     (list
  69.                         (list (cdr (assoc 41 enx)) 0.0 0.0)
  70.                         (list 0.0 (cdr (assoc 42 enx)) 0.0)
  71.                         (list 0.0 0.0 (cdr (assoc 43 enx)))
  72.                     )
  73.                 )
  74.             )
  75.         )
  76.         (mapcar '- (trans (cdr (assoc 10 enx)) ocs 0)
  77.             (mxv mat (cdr (assoc 10 (tblsearch "BLOCK" (cdr (assoc 2 enx))))))
  78.         )
  79.     )
  80. )
  81.  
  82. ;; Matrix Transpose  -  Doug Wilson
  83. ;; Args: m - nxn matrix
  84.  
  85. (defun trp ( m )
  86.     (apply 'mapcar (cons 'list m))
  87. )
  88.  
  89. ;; Matrix x Matrix  -  Vladimir Nesterovsky
  90. ;; Args: m,n - nxn matrices
  91.  
  92. (defun mxm ( m n )
  93.     ((lambda ( a ) (mapcar '(lambda ( r ) (mxv a r)) m)) (trp n))
  94. )
  95.  
  96. ;; Matrix x Vector  -  Vladimir Nesterovsky
  97. ;; Args: m - nxn matrix, v - vector in R^n
  98.  
  99. (defun mxv ( m v )
  100.     (mapcar '(lambda ( r ) (apply '+ (mapcar '* r v))) m)
  101. )

kenkrupa

  • Newt
  • Posts: 84
Re: Get coordinates of point in a block insertion
« Reply #2 on: March 21, 2013, 04:49:53 PM »
Awesome! Thank you very much.

Lee Mac

  • Seagull
  • Posts: 12926
  • London, England
Re: Get coordinates of point in a block insertion
« Reply #3 on: March 21, 2013, 05:22:57 PM »
You're welcome, credit to gile for the useful function  :-)