How to extract the insertion point of a block

How to extract the insertion point of a block

sohaybMX6R7
Participant Participant
757件の閲覧回数
14件の返信
メッセージ1/15

How to extract the insertion point of a block

sohaybMX6R7
Participant
Participant

How to extract the insertion point of a block, I am using the code bellow but I am getting a different result than the value under Properties->Geometry
the output: Insertion Point: -969.46, 109.50, 936.81

Expected output: 969.46, 936.81, 109.50

the rotation is 0

 

how can I fix it and the right value ?

(defun c:PrintBlockInsertionPoint (/ ent insertion-point wcs-point)
  ;; Prompt the user to select a block
  (setq ent (car (entsel "\nSelect a block: ")))

  ;; Check if the entity is a valid block reference
  (if (and ent (= "INSERT" (cdr (assoc 0 (entget ent))))) 
    (progn
      ;; Extract the insertion point using (cdr (assoc 10 ent)) in WCS
      (setq wcs-point (cdr (assoc 10 (entget ent))))
      
      ;; Print both WCS coordinates
      (princ (strcat "\n Insertion Point: " 
                     (rtos (car wcs-point) 2 2) ", " 
                     (rtos (cadr wcs-point) 2 2) ", " 
                     (rtos (caddr wcs-point) 2 2)))
    )
    (princ "\nSelected entity is not a block reference.")
  )

  (princ) ;; Exit quietly
)

 

0 件のいいね
解決済み
758件の閲覧回数
14件の返信
返信 (14)
メッセージ2/15

Kent1Cooper
Consultant
Consultant

Was the Block inserted when in the WCS?  Can you explain on what basis you get your expectation for the result?

Can you post a small drawing file, containing a Block reference from which you get this kind of incorrect result?

Kent Cooper, AIA
0 件のいいね
メッセージ3/15

komondormrex
Mentor
Mentor

try with trans 0 -> 1 added to line 9

(defun c:PrintBlockInsertionPoint (/ ent insertion-point wcs-point)
  ;; Prompt the user to select a block
  (setq ent (car (entsel "\nSelect a block: ")))

  ;; Check if the entity is a valid block reference
  (if (and ent (= "INSERT" (cdr (assoc 0 (entget ent))))) 
    (progn
      ;; Extract the insertion point using (cdr (assoc 10 ent)) in WCS
      (setq wcs-point (trans (cdr (assoc 10 (entget ent))) 0 1))
      
      ;; Print both WCS coordinates
      (princ (strcat "\n Insertion Point: " 
                     (rtos (car wcs-point) 2 2) ", " 
                     (rtos (cadr wcs-point) 2 2) ", " 
                     (rtos (caddr wcs-point) 2 2)))
    )
    (princ "\nSelected entity is not a block reference.")
  )

  (princ) ;; Exit quietly
)
0 件のいいね
メッセージ4/15

sohaybMX6R7
Participant
Participant

by expected result I mean the coordinates I am seeing in the properties menu

sohaybMX6R7_1-1741285523981.png

 

0 件のいいね
メッセージ5/15

sohaybMX6R7
Participant
Participant

unfortunately didn't work

0 件のいいね
メッセージ6/15

Kent1Cooper
Consultant
Consultant

@sohaybMX6R7 wrote:

by expected result I mean the coordinates I am seeing in the properties menu


Those will be in the current UCS.  Can you say more about the (trans) approach [which I would also have suggested] not working?

Kent Cooper, AIA
0 件のいいね
メッセージ7/15

sohaybMX6R7
Participant
Participant

I got the same result as before using the trans approach, output: Insertion Point: -969.46, 109.50, 936.81

0 件のいいね
メッセージ8/15

paullimapa
Mentor
Mentor

would definitely help if you shared a sample dwg that contains this block at the location you're having trouble getting the right results as others here have already requested.


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 件のいいね
メッセージ9/15

sohaybMX6R7
Participant
Participant

I attached an example of the dwg file containing the block

0 件のいいね
メッセージ10/15

Brock_Olly
Collaborator
Collaborator

Well, your block is 3D and its current orientation is not the same as it's drawn in the block editor. When I insert it again and run your lisp the coordinates are correct.
Context matters!

Brock_Olly_0-1741349359153.png

 

0 件のいいね
メッセージ11/15

sohaybMX6R7
Participant
Participant

Thank you for your reply, I appreciate your support.
How can I get the insertion coordinates without re-inserting it in another orientation ?

0 件のいいね
メッセージ12/15

Brock_Olly
Collaborator
Collaborator

I think with a 3D object rotated the LISP will have to be adjusted to function correctly.

However, if you have full autocad, you can use the built-in data extraction feature.
https://www.autodesk.com/support/technical/article/caas/sfdcarticles/sfdcarticles/Extracting-the-XYZ...

Brock_Olly_0-1741350579284.png

 

0 件のいいね
メッセージ13/15

sohaybMX6R7
Participant
Participant

Isn't it a way to do it with lisp ? what adjustments could be done in order to achieve that ?

0 件のいいね
メッセージ14/15

Brock_Olly
Collaborator
Collaborator
解決済み

Here's a lisp which extracts the coords correctly that my friend chatGPT made with some fiddling around, because it also got the wrong coordinates at first.
It used the same principle as data extraction so it might not work in LT (did not test)

 

(defun c:PrintBlockInsertionPoint ()
  (setq ent (car (entsel "\nSelect a block: ")))  ; Prompt user to select a block
  (if ent
    (progn
      (setq entObj (vlax-ename->vla-object ent))  ; Convert entity name to VLA object
      (setq insPoint (vlax-get entObj 'InsertionPoint))  ; Get insertion point as a list
      (setq xScale (vlax-get entObj 'XScaleFactor))  ; Get scale factor in X
      (setq yScale (vlax-get entObj 'YScaleFactor))  ; Get scale factor in Y
      (setq zScale (vlax-get entObj 'ZScaleFactor))  ; Get scale factor in Z
      (setq rotation (vlax-get entObj 'Rotation))  ; Get rotation in radians

      ;; Directly extract the X, Y, Z from the list (no safearray conversion needed)
      (setq x (car insPoint))
      (setq y (cadr insPoint))
      (setq z (caddr insPoint))

      ;; Apply rotation correction (if needed)
      (setq realX (+ (* x (cos rotation)) (* y (sin rotation))))
      (setq realY (+ (* y (cos rotation)) (- (* x (sin rotation)))))
      (setq realZ z)  ; Blocks in AutoCAD don't rotate around Z normally

      ;; Apply scaling correction
      (setq realX (* realX xScale))
      (setq realY (* realY yScale))
      (setq realZ (* realZ zScale))

      ;; Print the final coordinates
      (princ (strcat "\nBlock WCS Coordinates: " 
                     (rtos realX 2 6) ", " 
                     (rtos realY 2 6) ", " 
                     (rtos realZ 2 6)))
    )
    (princ "\nNo block selected or invalid selection.")
  )
  (princ)
)

 

0 件のいいね
メッセージ15/15

sohaybMX6R7
Participant
Participant

thank you it worked perfectly !!