Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

LISP Routine for Continuous Block Insertion

11 REPLIES 11
SOLVED
Reply
Message 1 of 12
Temssi.d
633 Views, 11 Replies

LISP Routine for Continuous Block Insertion

Hi, I'm looking for help with an AutoCAD LISP routine that does the following:

  1. Creates a custom command (e1) for inserting a specific block (S-MIF3).
  2. Upon activating the command, the block immediately appears at the cursor, ready for placement, similar to AutoCAD's native INSERT command, The routine should run continuously, allowing multiple block insertions without restarting the command.
  3. It should calculate a height value based on the Y-coordinate of the insertion point.
  4. The height value should be formatted as a string (e.g., "+2.49" or "-1.50") and inserted into the block's "MIFLAS" attribute.
  • No prompts for scaling or rotation - use default values (1, 1, 0).
  • The routine should loop continuously until manually terminated.
  • The height calculation is based on a reference Y-value of 0.
  • Height formatting should always show two decimal places and a +/- sign.

Any help would be greatly appreciated!

11 REPLIES 11
Message 2 of 12
pbejse
in reply to: Temssi.d


@Temssi.d wrote:
  • The routine should loop continuously until manually terminated.

 


Q&D

(defun c:Demo ( / theYPoint )
  (while
    (progn
      (command "_insert" "S-MIF3" pause)
      	(while (= 1 (getvar "cmdactive"))(command ""))
 	(setq theYPoint (cadr (Getvar 'LastPoint)))
      	(princ "\nPressed ESC to terminate")
      )
      	(setpropertyvalue (entlast) "MIFLAS"
	  	(strcat (if (minusp theYPoint) "" "+") (rtos theYPoint 2 2)))      
      )(princ)
    )

HTH

 

Message 3 of 12
Temssi.d
in reply to: Temssi.d

wow great thank you very much!!
Is it possible to make a small correction to the measurement output?
The measurement is in centimeters and should be converted to meters with two digits after the point including zeros if necessary.
123 -> +1.23
60 -> +0.60
300 ->+3.00
and so'..

Message 4 of 12
ec-cad
in reply to: Temssi.d

Not tested, but try this one little change.

ECCAD

 

 

(defun c:Demo ( / theYPoint )
  (while
    (progn
      (command "_insert" "S-MIF3" pause)
      	(while (= 1 (getvar "cmdactive"))(command ""))
 	(setq theYPoint (/ (cadr (Getvar 'LastPoint)) 100.00)); <------ replace this line
      	(princ "\nPressed ESC to terminate")
      )
      	(setpropertyvalue (entlast) "MIFLAS"
	  	(strcat (if (minusp theYPoint) "" "+") (rtos theYPoint 2 2)))      
      )(princ)
    )

 

 

Message 5 of 12
pbejse
in reply to: Temssi.d


@Temssi.d wrote:

123 -> +1.23
60 -> +0.60
300 ->+3.00
and so'..


Code at post#2 updated as per your requirement

 

EDIT: Oops @ec-cad  already posted the update

 

 

Message 6 of 12
Temssi.d
in reply to: ec-cad

Thanks!🙏
Dividing by 100 is really the right way. The difficulty is with the zeros.
300 becomes 3
320 becomes 3.2
...it's very close but not exactly 🙂

Message 7 of 12
pbejse
in reply to: Temssi.d

did you try the updated code at post # 2?

Message 8 of 12
komondormrex
in reply to: Temssi.d

hey there,

the one dynamic

(defun c:e1 (/ continue insert grread_data insertion)
  (defun add_plus (string)
    (if (= 'int (type (read string)))
    	(if (/= "-" (substr string 1 1)) (strcat "+" string ".00") (strcat string ".00"))
        (if (/= "-" (substr string 1 1)) (strcat "+" string) string)
    )
  )
  (defun move_insert (insert insertion)
    (vla-move insert (vla-get-insertionpoint insert) (vlax-3d-point insertion))
    (setpropertyvalue (vlax-vla-object->ename insert) "MIFLAS" (add_plus (rtos (/ (cadr (vlax-get insert 'insertionpoint)) 100.0) 2 2))) 
  )
  (setq continue t)
  (while (and continue
	      (null (prompt "\rLeft click to set point, right click to set precise point, Ecs to cancel"))
	      (setq insert (vla-insertblock (vla-get-block (vla-get-activelayout (vla-get-activedocument (vlax-get-acad-object))))
			   		    (vlax-3d-point (setq insertion (cadr (grread t)))) "s-mif3" 1 1 1 0
			   )
	      )
         )
    	 (while (and (null (vl-catch-all-error-p (setq grread_data (vl-catch-all-apply 'grread (list t 15 0)))))
	   	     (not (member (car grread_data) '(3 11 25)))
		)
	   (move_insert insert (cadr grread_data))
	   (setq insertion (cadr grread_data)) 
	 )
    	 (cond
	   ((vl-catch-all-error-p grread_data)
	    	(vla-erase insert)
	    	(setq continue nil)
	   )
    	   ((member (car grread_data) '(11 25))
	       	(setq insertion (getpoint insertion "\nPick point to place block at: "))
	        (move_insert insert insertion)
    	   )
  	 )
  )
  (princ)
)

 

Message 9 of 12
Moshe-A
in reply to: Temssi.d

@Temssi.d  hi,

 

i liked  👍 that you exactly know how this tool should be work but this arise some question in my mind:

 

a. "S-MIF3" to work in cm units so and you need to divide "miflas" value by 100

    ok you notice this and it's corrected 😀

b. there is a need to cover +/- 0.00

c. do you plan to lay all sections and elevations on same row?  cause if you lay a section B-B above section A-A, the

   "miflas" value would not fit or you could create each section\elevation specific ucs?   

 

 

Message 10 of 12
ec-cad
in reply to: ec-cad

Sorry for late posting. Original had 1000.00  DAH, should have been 100.0

And for adjusting the digits to 2 places, try this new version.

Cheers

 

(defun c:Demo ( / theYPoint )
  (while
    (progn
      (command "_insert" "S-MIF3" pause)
      	(while (= 1 (getvar "cmdactive"))(command ""))
 	(setq theYPoint (/ (cadr (Getvar 'LastPoint)) 100.00)); <------ replace this line
      	(princ "\nPressed ESC to terminate")
      )

        (setq value (strcat (if (minusp theYPoint) "" "+") (rtos theYPoint 2 2)))
;; added to set 0 on end
        (setq chk (substr value (- (strlen value) 1) 1)); check 2nd to last character
        (if (= chk ".")(setq value (strcat value "0"))); add a 0
        (if (or (= value "+0")(= value "-0"))
         (setq value "+/-0.00")
        ); if
;; end of added
      	(setpropertyvalue (entlast) "MIFLAS" value)
      )
      (princ)
    )

ECCAD

Message 11 of 12
Temssi.d
in reply to: Moshe-A

You're right, I still don't have a way how to use it if at all, but I wanted to understand if it's even possible. However, it seems useful in certain limited situations.
Message 12 of 12
Temssi.d
in reply to: Temssi.d

Thank you very much everyone! 

👑👑👑👑👑👑

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

AutoCAD Inside the Factory


Autodesk Design & Make Report