Jump to content

Insert / Rotate block in sequence of chosen points


Recommended Posts

Posted (edited)

I have this routine where I select a block, the routine asks for an insertion point and allows the block to be rotated to orthomode angles. The block becomes visible and rotates following the mouse. The routine requests other points and the blocks are inserted and rotated according to the project's needs. What I need is for the routine to also accept free angles, but accept osnap and F8 for orthomode. There is an error in the input of the name of the last block used, but I think I can fix that.
 

(defun c:insrot0 (/ *error* blk oldosmode oldortho blkname pt1 pt2 ang tmp_blk dyn_ang continue)
    ;; Error handler
    (defun *error* (msg)
        (if (and tmp_blk (entget tmp_blk)) (entdel tmp_blk))
        (if (not (member msg '("Function cancelled" "quit / exit abort")))
            (princ (strcat "\nError: " msg))
        )
        (if oldosmode (setvar 'osmode oldosmode))
        (if oldortho (setvar 'orthomode oldortho))
        (princ)
    )

    ;; Get block name
    (setq blkname (getstring "\nNome do bloco (Enter para último usado): "))
    
    (if (or (not blkname) (eq blkname ""))
        (setq blk (getvar 'insname))
        (setq blk blkname)
    )

    (while (not (tblsearch "block" blk))
        (alert (strcat "Bloco \"" blk "\" não encontrado."))
        (setq blkname (getstring "\nDigite o nome do bloco válido: "))
        (if (or (not blkname) (eq blkname ""))
            (setq blk (getvar 'insname))
            (setq blk blkname)
        )
    )

    (setq oldosmode (getvar 'osmode))
    (setq oldortho (getvar 'orthomode))
    (setvar 'osmode (boole 7 oldosmode 514)) ; 512+2=514

    ;; Main insertion loop
    (while (setq pt1 (getpoint "\nPonto de inserção (Enter para terminar): "))
        ;; Create temporary block
        (entmake (list (cons 0 "INSERT") (cons 2 blk) (cons 10 pt1) (cons 50 0)))
        (setq tmp_blk (entlast))
        (setq continue T)
        
        
        ;; Dynamic rotation
        
        (princ "\nSelecione o ponto para determinar o ângulo (F8 para ortogonal): ")
        (setq dyn_ang 0.0)
        (redraw)
        
        (while continue
            (setq pt2 (grread t 15 0))
            (cond
                ((= (car pt2) 5) ; Mouse movement
                 (setq ang (angle pt1 (cadr pt2)))
                 (if (= (getvar 'orthomode) 0)
                     (setq dyn_ang (* (fix (+ (/ ang (/ pi 2)) 0.5)) (/ pi 2))) ; Ortho angle
                     (setq dyn_ang ang) ; Free angle
                 )
                 (entmod (subst (cons 50 dyn_ang) (assoc 50 (entget tmp_blk)) (entget tmp_blk)))
                 (redraw)
                )
                ((or (= (car pt2) 3) (= (car pt2) 25)) ; Left click or right click
                 (entdel tmp_blk)
                 (command "_.-insert" blk "_non" pt1 "" "" (angtos dyn_ang))
                 (setq continue nil)
                )
                ((= (car pt2) 2) ; Keyboard
                 (if (member (cadr pt2) '(13 32)) ; Enter or Space
                     (progn
                         (entdel tmp_blk)
                         (command "_.-insert" blk "_non" pt1 "" "" "0")
                         (setq continue nil)
                     )
                 )
                )
                ((null pt2) ; User pressed Enter
                 (entdel tmp_blk)
                 (setq continue nil)
                )
            )
        )
    )

    (setvar 'osmode oldosmode)
    (setvar 'orthomode oldortho)
    (princ)
)




 

Edited by Laranjeira
Posted

Hi @Laranjeira

'(grread T 15 0)' won't let you toggle ORTHOMODE or OSMODE at runtime.
If you want that option to work, you'll have to use '(grread nil...)'
And then you'll have to 'manufacture' the object reference yourself.

Posted (edited)

Although, really, once you dive into '(grread nil...)' the state of these variables no longer directly affects you.

It's like finding a corner where you're safe from the 'Matrix' 😎

Edited by GLAVCVS
Posted

I think for this you are going to have to go full 'Lee Mac' - if you want snaps as well then look at Lee Macs website and grsnaps where he has this, though you'll need to read, understand and put into your LISP.

 

I'm not sure why you are doing it that way, this should do the job just as well, where xscale is the scale factor (1?), first pause could be pt1 "_non"

 

(command "-insert" -Blockame- pause "" xscale pause)

 

Posted
  On 3/26/2025 at 8:43 PM, GLAVCVS said:

Although, really, once you dive into '(grread nil...)' the state of these variables no longer directly affects you.

It's like finding a corner where you're safe from the 'Matrix' 😎

Expand  

Good point. I'll check  grsnap from Lee.

Posted
  On 3/26/2025 at 9:13 PM, Steven P said:

I think for this you are going to have to go full 'Lee Mac' - if you want snaps as well then look at Lee Macs website and grsnaps where he has this, though you'll need to read, understand and put into your LISP.

 

I'm not sure why you are doing it that way, this should do the job just as well, where xscale is the scale factor (1?), first pause could be pt1 "_non"

 

(command "-insert" -Blockame- pause "" xscale pause)

 

Expand  

The problem is not inserting the block. It is inserted provisionally and remains visible and rotates following the mouse while I look for an angle to insert it definitively. What happens is that it is not possible to select orthogonal points or osnap as perpendicular, endpoint, midpoint and others. I work with projects and when I am going to insert electrical outlets for example, with this routine I insert a series of them without having to interrupt the command and start over. I navigate through the rooms, offices, and insert the outlets. It speeds up the distribution. I have another routine that inserts, rotates at any angle, accepts F8 and osnap, but the block is not visible, it stays at zero angle waiting for the point to be defined. 

Posted

Maybe you should get it working without snap first. And leave that for last.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...