Announcements
Attention for Customers without Multi-Factor Authentication or Single Sign-On - OTP Verification rolls out April 2025. Read all about it here.

LISP Routine for Continuous Block Insertion

Temssi.d
Enthusiast

LISP Routine for Continuous Block Insertion

Temssi.d
Enthusiast
Enthusiast

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!

0 Likes
Reply
Accepted solutions (4)
782 Views
11 Replies
Replies (11)

pbejse
Mentor
Mentor
Accepted solution

@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

 

Temssi.d
Enthusiast
Enthusiast

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'..

0 Likes

ec-cad
Advocate
Advocate
Accepted solution

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)
    )

 

 

pbejse
Mentor
Mentor

@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

 

 

0 Likes

Temssi.d
Enthusiast
Enthusiast

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 🙂

0 Likes

pbejse
Mentor
Mentor

did you try the updated code at post # 2?

0 Likes

komondormrex
Advisor
Advisor
Accepted solution

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)
)

 

Moshe-A
Mentor
Mentor

@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?   

 

 

ec-cad
Advocate
Advocate
Accepted solution

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

Temssi.d
Enthusiast
Enthusiast
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.
0 Likes

Temssi.d
Enthusiast
Enthusiast

Thank you very much everyone! 

👑👑👑👑👑👑

0 Likes