Author Topic: object to matrix  (Read 7205 times)

0 Members and 1 Guest are viewing this topic.

reltro

  • Guest
Re: object to matrix
« Reply #15 on: June 10, 2014, 07:34:25 AM »
reltro

Thank you so much reltro for your help  :-)

Your codes after selecting a block give a list of matrix values , does that mean we can use these values with any entity name in the same block  to transfer to model space for example ?

Should work...
The function returns a transformationMatrix wich transforms objects from the origin of the WCS to the CoordinateSystem of the BlockReference...
if u (entget ...) an object wich is inside the BlockReference u get the Coordinates realtive to it, then (entmake ...) it in the ModelSpace (is an other Block) an transform it with the matrix

Code: [Select]
(defun C:Test (/ sel num ent tbl-obj mat)
    (if (setq sel (ssget "_:L" '((0 . "INSERT"))))
        (repeat (setq num (sslength sel))
            (setq ent (ssname sel (setq num (1- num))))
            (setq tbl-obj (tblobjname "BLOCK" (cdr (assoc 2 (entget ent)))))
            (setq mat (vlax-tmatrix (TMatrix ent)))
           
            (while (setq tbl-obj (entnext tbl-obj))
                 (vla-transformby (vlax-ename->vla-object (entmakex (entget tbl-obj))) mat)
            )
        )
    )
)

Take care and test it well! not sure it works on any BlockReferences, but should do ;)

reltro

Coder

  • Swamp Rat
  • Posts: 827
Re: object to matrix
« Reply #16 on: June 10, 2014, 07:40:55 AM »
reltro

Thank you so much reltro for your help  :-)

Your codes after selecting a block give a list of matrix values , does that mean we can use these values with any entity name in the same block  to transfer to model space for example ?

Should work...
The function returns a transformationMatrix wich transforms objects from the origin of the WCS to the CoordinateSystem of the BlockReference...
if u (entget ...) an object wich is inside the BlockReference u get the Coordinates realtive to it, then (entmake ...) it in the ModelSpace (is an other Block) an transform it with the matrix

Code: [Select]
(defun C:Test (/ sel num ent tbl-obj mat)
    (if (setq sel (ssget "_:L" '((0 . "INSERT"))))
        (repeat (setq num (sslength sel))
            (setq ent (ssname sel (setq num (1- num))))
            (setq tbl-obj (tblobjname "BLOCK" (cdr (assoc 2 (entget ent)))))
            (setq mat (vlax-tmatrix (TMatrix ent)))
           
            (while (setq tbl-obj (entnext tbl-obj))
                 (vla-transformby (vlax-ename->vla-object (entmakex (entget tbl-obj))) mat)
            )
        )
    )
)

Take care and test it well! not sure it works on any BlockReferences, but should do ;)

reltro

Fantastic  work :-D
It copies every object to current space but I think I can take care of filtering the required object that I want .

Many many thanks for your time and nice work .

reltro

  • Guest
Re: object to matrix
« Reply #17 on: June 10, 2014, 07:45:50 AM »
It copies every object to current space but I think I can take care of filtering the required object that I want .

reltro

Thank you so much reltro for your help  :-)

Your codes after selecting a block give a list of matrix values , does that mean we can use these values with any entity name in the same block  to transfer to model space for example ?

It was the question  8-) and I had to test it ;)
but u gave urself allready the code do filter some out...
Code: [Select]
(if (or (equal (cdr (assoc 0 (entget tbl-obj))) "CIRCLE")
                (equal (cdr (assoc 0 (entget tbl-obj))) "LWPOLYLINE")
                )
.
.
.

reltro

roy_043

  • Water Moccasin
  • Posts: 1895
  • BricsCAD 18
Re: object to matrix
« Reply #18 on: June 10, 2014, 09:23:33 AM »
roy_043

I tried your codes but nothing happened , maybe I missed something or did not use it should be . sorry  :oops:
My functions only return a matrix.

Coder

  • Swamp Rat
  • Posts: 827
Re: object to matrix
« Reply #19 on: June 10, 2014, 11:14:37 AM »
My functions only return a matrix.

I got it , thank you so much .  :-)

reltro

  • Guest
Re: object to matrix
« Reply #20 on: June 10, 2014, 11:57:16 AM »
roy_043

I tried your codes but nothing happened , maybe I missed something or did not use it should be . sorry  :oops:
My functions only return a matrix.

Hey roy_043
I think ur matrix is only valid if the GroupCode 210 of the BlockRef is '(210 0.0 0.0 1.0) or am I wrong?

roy_043

  • Water Moccasin
  • Posts: 1895
  • BricsCAD 18
Re: object to matrix
« Reply #21 on: June 11, 2014, 04:42:12 AM »
roy_043

I tried your codes but nothing happened , maybe I missed something or did not use it should be . sorry  :oops:
My functions only return a matrix.

Hey roy_043
I think ur matrix is only valid if the GroupCode 210 of the BlockRef is '(210 0.0 0.0 1.0) or am I wrong?
You are wrong but my code may fail in certain cases so I am interested in your testing.
Attached is a test dwg. My test:
Code: [Select]
: (setq myMatrix (c:test1))
Select entity: ((0.346423 -0.533448 0.771637 23.0678) (0.938078 0.196997 -0.284958 -8.5187) (-2.01472e-016 0.822572 0.568661 17.1853) (0.0 0.0 0.0 1.0))
: (equal myMatrix (caddr (nentselp)) 1e-8)
Select entity: T

roy_043

  • Water Moccasin
  • Posts: 1895
  • BricsCAD 18
Re: object to matrix
« Reply #22 on: June 13, 2014, 09:33:40 AM »
The code in one of my previous posts actually does have an issue. It does not take the base point of the block definition into account. Gile's code does.

My improved and cleaned up code:
Code: [Select]
;;; 20140613
;;; Get a matrix from an insert.
;;; See: http://www.theswamp.org/index.php?topic=47227.0
;;; (equal (KGA_Geom_MatrixMakeInsert (car (entsel))) (caddr (nentselp)) 1e-8)
(defun KGA_Geom_MatrixMakeInsert (ename / ang elist normal xVector yVector zVector)
  (setq elist (entget ename))
  (setq normal (cdr (assoc 210 elist)))
  (setq ang (cdr (assoc 50 elist)))
  (KGA_Geom_MatrixMake
    (setq xVector (KGA_Geom_VectorScale (trans (list (cos ang) (sin ang) 0.0) normal 0 T)     (cdr (assoc 41 elist))))
    (setq yVector (KGA_Geom_VectorScale (trans (list (- (sin ang)) (cos ang) 0.0) normal 0 T) (cdr (assoc 42 elist))))
    (setq zVector (KGA_Geom_VectorScale (trans '(0.0 0.0 1.0) normal 0 T)                     (cdr (assoc 43 elist))))
    (mapcar
      '-
      (trans (cdr (assoc 10 elist)) normal 0) ; Origin (insertion point).
      (mapcar
        '(lambda (lst) (apply '+ lst))
        (KGA_Geom_MatrixTranspose
          (mapcar
            'KGA_Geom_VectorScale
            (list xVector yVector zVector)
            (cdr (assoc 10 (tblsearch "block" (cdr (assoc 2 elist))))) ; Base point of the block definition.
          )
        )
      )
    )
  )
)

;;; 20140613: Revised. See (KGA_Geom_MatrixTranspose).
;;; 20120201
;;; Get a 4x4 matrix from three vectors and an origin.
(defun KGA_Geom_MatrixMake (xVector yVector zVector origin)
  (append
    (mapcar 'list xVector yVector zVector origin)
    '((0.0 0.0 0.0 1.0))
  )
)

;;; 20140613
;;; Transpose a matrix.
;;; By Doug Wilson (via gile, www.theswamp.org). Original name 'trp'.
(defun KGA_Geom_MatrixTranspose (matrix)
  (apply 'mapcar (cons 'list matrix))
)

; 20120201
; (KGA_Geom_VectorScale '(1 2 3) 3) => (3.0 6.0 9.0)
(defun KGA_Geom_VectorScale (vector scalar)
  (mapcar '(lambda (a) (* a (float scalar))) vector)
)