Center of the screen.

Center of the screen.

adaptacad
Advocate Advocate
4,301 Views
21 Replies
Message 1 of 22

Center of the screen.

adaptacad
Advocate
Advocate

Is there a way to insert a block at the same rotation as another block closer to the center of my screen?

-> I zoomed, I called the command, and the new block will be inserted in the block closest to my zoom.

I got a .dwg with the example.

0 Likes
Accepted solutions (4)
4,302 Views
21 Replies
Replies (21)
Message 2 of 22

Kent1Cooper
Consultant
Consultant

The VIEWCTR System Variable holds the [as you might expect] VIEW CenTeR, i.e. the middle of the current viewing area.  You can use that as an insertion point for a Block with:

  (getvar 'viewctr)

when asked for the insertion point.

 

But to find the already-inserted Block that's closest to that location would probably require a routine to find all  Blocks in the current space, step through them all looking at the distance from there to each one's insertion point, and use the one for which that distance is shortest.  It's probably doable in an AutoLisp routine, if that sounds like what you need to do.

Kent Cooper, AIA
Message 3 of 22

hak_vz
Advisor
Advisor

Command INSERT

Enable option: Insertion point -> specify on screen

 

Miljenko Hatlak

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
Message 4 of 22

adaptacad
Advocate
Advocate

@Kent1Cooper 

(command "Insert" "Blk_Insert" (getvar 'viewctr) 1 1 0)

It Works very well!
Now how to find the nearest block point?

0 Likes
Message 5 of 22

Kent1Cooper
Consultant
Consultant
Accepted solution

@adaptacad wrote:

.... Now how to find the nearest block point?



Something like this [minimally tested]:

(defun C:IBCVC ; = Insert at Block Closest to View Center
  (/ ss dst vc n blk bdst theOne)
  (if (setq ss (ssget "_X" (list '(0 . "INSERT") (cons 410 (getvar 'ctab)))))
    (progn ; then
      (setq
        dst (distance (getvar 'extmin) (getvar 'extmax))
          ; virtually guaranteed farther than some Block will be from ViewCenter
        vc (getvar 'viewctr)
      ); setq
      (repeat (setq n (sslength ss))
        (setq blk (ssname ss (setq n (1- n))))
        (if (< (setq bdst (distance (cdr (assoc 10 (entget blk))) vc)) dst); closer than any other so far
          (setq theOne blk dst bdst); then -- mark as closest; its distance for basis of next comparison
        ); if
      ); repeat
      (command "_.insert" "Blk_Insert" "_none" (cdr (assoc 10 (entget theOne))) 1 1 0)
    ); progn
  ); if
  (princ)
); defun
Kent Cooper, AIA
Message 6 of 22

hak_vz
Advisor
Advisor
(defun c:ntb ( / cp dx dy p1 p2 ss i mindist di mp tp *error*)
(defun *error* () (princ))
(setvar "cmdecho" 0)
(setq 
    cp (getvar "VIEWCTR")
    dy (* 0.5 (getvar "VIEWSIZE"))
    dx (* 0.5 (* vy (/ (car (getvar "SCREENSIZE"))(cadr (getvar"SCREENSIZE")))))
    p1 (mapcar '- cp (list dx dy))
    p2 (mapcar '+ cp (list dx dy))
    ss (ssget "w" p1 p2 '((0 . "INSERT")))    
    i 0
    mindist 1e8
)
    (if ss
        (progn
            (command "Insert" "Blk_Insert" cp 1 1 0)
            (while (< i (sslength ss))
                (setq di (distance cp (setq tp(cdr (assoc 10 (entget (ssname ss i)))))))
                (if (< di mindist)
                    (setq mp tp mindist di)
                )
             (setq i (+ i 1))
            )
            (command "move" (entlast) "" cp mp)
        )
        (princ "\nThere is no block in visible area!")
    )
(setvar "cmdecho" 1)
(princ)
)

Miljenko Hatlak

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
0 Likes
Message 7 of 22

adaptacad
Advocate
Advocate

@Kent1Cooper  Thank you very much!! it Works very well!
How do I rotate at the same angle as "Block_Mult"?
How not to insert in the base of "Block_Mult" but in the place of the example?

0 Likes
Message 8 of 22

adaptacad
Advocate
Advocate

@hak_vz It does not work !!

0 Likes
Message 9 of 22

hak_vz
Advisor
Advisor
Accepted solution
(defun c:ntb ( / cp dx dy p1 p2 ss i mindist di mp tp *error*)
(defun *error* () (princ))
(setvar "cmdecho" 0)
(setq 
    cp (getvar "VIEWCTR")
    dy (* 0.5 (getvar "VIEWSIZE"))
    dx (* 0.5 (* dy (/ (car (getvar "SCREENSIZE"))(cadr (getvar"SCREENSIZE")))))
    p1 (mapcar '- cp (list dx dy))
    p2 (mapcar '+ cp (list dx dy))
    ss (ssget "w" p1 p2 '((0 . "INSERT")))    
    i 0
    mindist 1e8
)
    (if ss
        (progn
            (command "Insert" "Blk_Insert" cp 1 1 0)
            (while (< i (sslength ss))
                (setq di (distance cp (setq tp(cdr (assoc 10 (entget (ssname ss i)))))))
                (if (< di mindist)
                    (setq mp tp mindist di)
                )
             (setq i (+ i 1))
            )
            (command "move" (entlast) "" cp mp)
        )
        (princ "\nThere is no block in visible area!")
    )
(setvar "cmdecho" 1)
(princ)
)


(defun c:ntb2 ( / cp dx dy p1 p2 ss i mindist di mp tp *error*)
(defun *error* () (princ))
(setvar "cmdecho" 0)
(setq
cp (getvar "VIEWCTR")
ss (ssget "x" '((0 . "INSERT")))
i 0
mindist 1e8
)
(if ss
(progn
(while (< i (sslength ss))
(setq di (distance cp (setq tp(cdr (assoc 10 (entget (ssname ss i)))))))
(if (< di mindist)
(setq mp tp mindist di)
)
(setq i (+ i 1))
)
(command "Insert" "Blk_Insert" mp 1 1 0)
)
)
(setvar "cmdecho" 1)
(princ)
)

Miljenko Hatlak

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
Message 10 of 22

Kent1Cooper
Consultant
Consultant

@adaptacad wrote:

….
How do I rotate at the same angle as "Block_Mult"?
How not to insert in the base of "Block_Mult" but in the place of the example?


 

[No AutoCAD here, but I'll give it a shot....]

 

1.  You should be able to replace the 0 at the end of the Insert command with:

  (cdr (assoc 50 (entget theOne)))

 

2.  I'm not sure what you mean by "in the place of the example" -- what would determine where that place is, if it's not the insertion point of the Block closest to the center of the current view?

Kent Cooper, AIA
Message 11 of 22

adaptacad
Advocate
Advocate

@hak_vz It Works very well. Thank you!!!

 

@Kent1Cooper This point with the same rotation as the block.img.PNG

0 Likes
Message 12 of 22

hak_vz
Advisor
Advisor

(defun c:ntb ( / cp dx dy p1 p2 ss i mindist di mp tp e *error*)
(defun *error* () (princ))
(defun rad_to_deg (rad)(* 180.0 (/ rad pi)))
(setvar "cmdecho" 0)
(setq
cp (getvar "VIEWCTR")
dy (* 0.5 (getvar "VIEWSIZE"))
dx (* 0.5 (* dy (/ (car (getvar "SCREENSIZE"))(cadr (getvar"SCREENSIZE")))))
p1 (mapcar '- cp (list dx dy))
p2 (mapcar '+ cp (list dx dy))
ss (ssget "w" p1 p2 '((0 . "INSERT")))
i 0
mindist 1e8
)
(if ss
(progn

(while (< i (sslength ss))
(setq di (distance cp (setq tp(cdr (assoc 10 (entget (ssname ss i)))))))
(if (< di mindist)
(setq mp tp mindist di e (ssname ss i))
)
(setq i (+ i 1))
)
(command "Insert" "Blk_Insert" mp 1 1 0)
(command "rotate" (entlast) "" mp (rad_to_deg(cdr (assoc 50 (entget e)))))
)
(princ "\nThere is no block in visible area!")
)
(setvar "cmdecho" 1)
(princ)
)

Miljenko Hatlak

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
0 Likes
Message 13 of 22

adaptacad
Advocate
Advocate

@hak_vz wrote:

(defun c:ntb ( / cp dx dy p1 p2 ss i mindist di mp tp e *error*)
(defun *error* () (princ))
(defun rad_to_deg (rad)(* 180.0 (/ rad pi)))
(setvar "cmdecho" 0)
(setq
cp (getvar "VIEWCTR")
dy (* 0.5 (getvar "VIEWSIZE"))
dx (* 0.5 (* dy (/ (car (getvar "SCREENSIZE"))(cadr (getvar"SCREENSIZE")))))
p1 (mapcar '- cp (list dx dy))
p2 (mapcar '+ cp (list dx dy))
ss (ssget "w" p1 p2 '((0 . "INSERT")))
i 0
mindist 1e8
)
(if ss
(progn

(while (< i (sslength ss))
(setq di (distance cp (setq tp(cdr (assoc 10 (entget (ssname ss i)))))))
(if (< di mindist)
(setq mp tp mindist di e (ssname ss i))
)
(setq i (+ i 1))
)
(command "Insert" "Blk_Insert" mp 1 1 0)
(command "rotate" (entlast) "" mp (rad_to_deg(cdr (assoc 50 (entget e)))))
)
(princ "\nThere is no block in visible area!")
)
(setvar "cmdecho" 1)
(princ)
)


 

 

@hak_vz  Thank you. Rotation works great, but how to insert at this point?

 

img.PNG

0 Likes
Message 14 of 22

hak_vz
Advisor
Advisor

Sorry but I get lost.

slope4.jpg

I've got this. Problem is probably in some system variables . At first I had problem with rotation. In my case it is base angle direction is East and i use cc direction. I guess you may have something different.

 

In both codes we only work with insertion points, so I don't see a reason that they don't join.

Miljenko Hatlak

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
Message 15 of 22

Kent1Cooper
Consultant
Consultant

@adaptacad wrote:

....

@Kent1Cooper This point with the same rotation as the block.


 

Is "this point" not  the insertion point of that Block?  The blue square down lower looks like a grip, which presumably would be the insertion point.  If that's true, I suggest you redefine the Block to make the point you want at a specific location its insertion point.  Otherwise, the routine will need to do some calculation, affected by the rotation, of the insertion point to use in order to land "this point" at the insertion point of the closest-to-view-center Block.  And you will need to build the distance from this Block's insertion point to "this point" into that calculation.

Kent Cooper, AIA
Message 16 of 22

adaptacad
Advocate
Advocate

@hak_vz  It's working very well, I would just like to move the block to that point.
understand?

Img.PNG

0 Likes
Message 17 of 22

adaptacad
Advocate
Advocate

@Kent1Cooper  I can't change the base of the block! 😕

0 Likes
Message 18 of 22

Kent1Cooper
Consultant
Consultant

If the recently-posted images show the Block at zero rotation, and if you want the location at the freehand arrowhead point to be what's at the insertion point of the closest-to-view-center Block, try this variant [untested]:

(defun C:IBCVC ; = Insert at Block Closest to View Center
  (/ ss dst vc n blk bdst theOne)
  (if (setq ss (ssget "_X" (list '(0 . "INSERT") (cons 410 (getvar 'ctab)))))
    (progn ; then
      (setq
        dst (distance (getvar 'extmin) (getvar 'extmax))
          ; virtually guaranteed farther than some Block will be from ViewCenter
        vc (getvar 'viewctr)
      ); setq
      (repeat (setq n (sslength ss))
        (setq blk (ssname ss (setq n (1- n))))
        (if (< (setq bdst (distance (cdr (assoc 10 (entget blk))) vc)) dst); closer than any other so far
          (setq theOne blk dst bdst); then -- mark as closest; its distance as next comparison
        ); if
      ); repeat
      (setq rot (cdr (assoc 50 (entget theOne))))
      (command "_.insert" "Blk_Insert" "_none"
        (polar (cdr (assoc 10 (entget theOne))) (- rot (/ pi 2)) 2.5)
        1 1 rot
      ); command
    ); progn
  ); if
  (princ)
); defun
Kent Cooper, AIA
0 Likes
Message 19 of 22

hak_vz
Advisor
Advisor

Will write it later.

Miljenko Hatlak

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
0 Likes
Message 20 of 22

hak_vz
Advisor
Advisor
Accepted solution
(defun c:ntb ( / cp dx dy p1 p2 ss i mindist di mp tp e *error*) 
(defun *error* () (princ))
(defun rad_to_deg (rad)(* 180.0 (/ rad pi)))
(setvar "cmdecho" 0)
(setq 
    cp (getvar "VIEWCTR")
    dy (* 0.5 (getvar "VIEWSIZE"))
    dx (* 0.5 (* dy (/ (car (getvar "SCREENSIZE"))(cadr (getvar"SCREENSIZE")))))
    p1 (mapcar '- cp (list dx dy))
    p2 (mapcar '+ cp (list dx dy))
    ss (ssget "w" p1 p2 '((0 . "INSERT")))    
    i 0
    mindist 1e8
)
    (if ss
        (progn
           
            (while (< i (sslength ss))
                (setq di (distance cp (setq tp(cdr (assoc 10 (entget (ssname ss i)))))))
                (if (< di mindist)
                    (setq mp tp mindist di e  (ssname ss i))
                )
             (setq i (+ i 1))
            )
            (command "Insert" "Blk_Insert" mp 1 1 0)
            (command "rotate" (entlast) "" mp (rad_to_deg(cdr (assoc 50 (entget e)))))
            (command "move" (entlast) "" mp (polar mp (+ (cdr (assoc 50 (entget e))) (* 0.5 pi)) 2.5))
        )
        (princ "\nThere is no block in visible area!")
    )
(setvar "cmdecho" 1)
(princ)
)

slope3.jpg

Miljenko Hatlak

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.