how to get the display coordinates of a block inside a block

how to get the display coordinates of a block inside a block

johnlwilliamsjr2013
Contributor Contributor
749 Views
11 Replies
Message 1 of 12

how to get the display coordinates of a block inside a block

johnlwilliamsjr2013
Contributor
Contributor

I have a list of entity ids of blocks inside a parent block. However, the child block locations, using code 10 of the child block, list the child block location relative to the parent block and not the display coordinate as seen from the drawing. How do I get the child blocks location as seen from the drawing? The parent block can be rotated, flipped, etc.

 

Thanks,

 

John W.

0 Likes
Accepted solutions (2)
750 Views
11 Replies
Replies (11)
Message 2 of 12

Kent1Cooper
Consultant
Consultant

Does (trans) work, translating from the Block's coordinate system to the WCS or the current UCS?

Kent Cooper, AIA
0 Likes
Message 3 of 12

johnlwilliamsjr2013
Contributor
Contributor

I tried this
(foreach term terms
(princ "term: ") (princ term) (princ "\n")
(setq locationB (GetInstLocation term))
(setq cs_from 2)
(setq cs_to 0)
(setq location (trans locationB cs_from cs_to 0)) ; disp
(princ "location: ") (princ location) (princ "\n")
)

 

The translated location is always the same as the child blocks location relative to the parent block, and not the child blocks location within the drawing.

0 Likes
Message 4 of 12

Kent1Cooper
Consultant
Consultant

Not knowing how you're arriving at your 'terms' list, nor what the things in it are, nor what the (GetInstLocation) function does, I can't be sure I understand what's going on.  But I suspect you are not using the correct 'from' argument in the (trans) function, and you shouldn't have the fourth optional 'disp' argument [0] in it, which [being present and not nil] means the result should be treated as a displacement rather than a location.

 

Doesn't the translation need to be relative to a particular insertion of a containing Block?  If not, what can it really mean, and what would it matter if the Block is rotated, flipped, etc.?

 

If you want it translated into WCS coordinates, I think maybe you want something more like:
(setq location (trans locationB A-Block-Insertion's-Entity-Name 0))

Kent Cooper, AIA
0 Likes
Message 5 of 12

johnlwilliamsjr2013
Contributor
Contributor

Here is the output I see in the window. It gives details about what terms are.

term: <Entity name: 1e5f6c00e00>
((-1 . <Entity name: 1e5f6c00e00>) (0 . INSERT) (330 . <Entity name: 1e5e59131d0>) (5 . 2878) (100 . AcDbEntity) (67 . 0) (8 . 0) (100 . AcDbBlockReference) (66 . 1) (2 . terminal) (10 0.0 0.0 0.0) (41 . 1.0) (42 . 1.0) (43 . 1.0) (50 . 0.0) (70 . 0) (71 . 0) (44 . 0.0) (45 . 0.0) (210 0.0 0.0 1.0))
locationB: (0.0 0.0 0.0)
location: (0.0 0.0 0.0)

When used with the trans function as shown.

(setq location (trans locationB device 0))

Where device is device: <Entity name: 1e5f6c00f20>, and is the parent block of the term objects.

0 Likes
Message 6 of 12

johnlwilliamsjr2013
Contributor
Contributor

Here is a simple translate function I wrote to give me the location I need

 

(defun TranslateChildToParent ( child parent / locationChild locationParent location )
(setq locationChild (GetInstLocation child))
(setq locationParent (GetInstLocation parent))
(setq location (AddPts locationChild locationParent))
location
)

 

However, this only works if the parent if not rotated or flipped. Looking for something similar that can handle the rotation and flipping of the parent block.

0 Likes
Message 7 of 12

Kent1Cooper
Consultant
Consultant
Accepted solution

Completely untested [I don't have your sub-routine], but does this work, or at least give you a start?

 

(defun TranslateChildToParent ( child parent / parentData parentX parentY parentRot locationChild locationParent location )
  (setq
    parentData (entget parent)
    parentX (cdr (assoc 41 parentData)
    parentY (cdr (assoc 42 parentData)
    parentRot (cdr (assoc 50 parentData)
    locationChild (GetInstLocation child)
    locationParent (GetInstLocation parent)
    location
      (polar ; Y-direction position relative to:
        (polar ; X-direction position [point argument for Y]
          locationParent ; point
          parentRot ; angle
          (* (car locationChild) parentX) ; distance
        ); polar [X]
        (+ parentRot (/ pi 2)) ; angle [perpendicular]
        (* (cadr locationChild) parentY) ; distance
      ); polar [Y]
  ); setq
)

 

 

Kent Cooper, AIA
0 Likes
Message 8 of 12

johnlwilliamsjr2013
Contributor
Contributor
Accepted solution

Added a couple of missing parens and then it worked! Perfect! Thanks!

 

(defun TranslateChildToParent ( child parent / parentData parentX parentY parentRot locationChild locationParent location )
(setq
parentData (entget parent)
parentX (cdr (assoc 41 parentData))
parentY (cdr (assoc 42 parentData))
parentRot (cdr (assoc 50 parentData))
locationChild (GetInstLocation child)
locationParent (GetInstLocation parent)
location
(polar ; Y-direction position relative to:
(polar ; X-direction position [point argument for Y]
locationParent ; point
parentRot ; angle
(* (car locationChild) parentX) ; distance
); polar [X]
(+ parentRot (/ pi 2)) ; angle [perpendicular]
(* (cadr locationChild) parentY) ; distance
); polar [Y]

); setq
)

0 Likes
Message 9 of 12

johnlwilliamsjr2013
Contributor
Contributor

(defun GetInstLocation ( inst / ent )
(setq ent (entget inst))
;;(princ ent) (princ "\n")
(cdr (assoc 10 ent))
)

0 Likes
Message 10 of 12

Kent1Cooper
Consultant
Consultant

@johnlwilliamsjr2013 wrote:

Added a couple of missing parens and then it worked! Perfect! Thanks!

....


Sorry about those missing parentheses [I would have noticed if I had tested it...], but I'm kind of pleasantly surprised that it actually worked.  It may not in other-than-World Coordinate System situations, so if you need that, something would need to be adjusted.

Kent Cooper, AIA
0 Likes
Message 11 of 12

johnlwilliamsjr2013
Contributor
Contributor

I also confirmed that this works for flipped or mirrored blocks as well as rotated blocks;)

0 Likes
Message 12 of 12

Kent1Cooper
Consultant
Consultant

@johnlwilliamsjr2013 wrote:

I also confirmed that this works for flipped or mirrored blocks as well as rotated blocks;)


That's the use of the parent Block's scale factors as multiplier, since one or both of those will be negative in a mirrored Block, which will send the (polar) result off in the opposite direction.

 

Maybe you've already determined this, but I'd be interested in confirmation of whether it works right with an unequally-scaled parent Block.

Kent Cooper, AIA
0 Likes