Jump to content

Add in an undo in these sections while lisp is running


EricDevault

Recommended Posts

This lisp is intended to setup a grid system for measuring purposes. The While loop goes until an Enter and then advances to the next set. If the user keys the wrong value then need to be able to undo back to re-enter the correct value. I have put in the "Undo" "Begin" and "Undo" "End" where I thought they should go but they do not work as intended.

The line that reads: (setq vsn (getdist "\Distance to next Vert Strap (Enter when done): ")) should also have an undo option in the verbiage. Like (setq vsn (getdist "\Distance to next Vert Strap (Enter when done or Undo to go back): "))

 

Any help is much appreciated. Also if any clarification is needed please let me know, I'm no expert as you will see from this coding. Which I should also mention, I am not really trying to rewrite this code as it works as is now just need the undo portion.

 

 

(defun c:grid1 (vs tvs lnty hs)
;;Written by Eric DeVault 2015
;********************************************************
	
(setvar "CLAYER" "PENME")

(setq entvs (entsel "\Select the Vert Strap measured from: ")

) 

Need an undo to start here

;original strap
(setq vs (getdist "\Distance to next Vert Strap: ")
)


(setvar "CLAYER" "DIMBAD")
(command ".text"  (list (/ (/ vs 2) 12) -1 0) 0.45 0 (rtos vs 2 0))
(setvar "CLAYER" "PENME")
(command "._offset" (/ vs 12) entvs "_non" '(70 0. 0.) "")

 

End and start one here

 


(while

(setq vsn (getdist "\Distance to next Vert Strap (Enter when done): "))
(setq vlnty (entlast))
(setq vstart (entget (entlast)))
(setq vspt (cdr (assoc 10 vstart)))
(setq xspt (car vspt))

(setvar "CLAYER" "DIMBAD")
(command ".text"  (list (+ xspt (/ (/ vsn 2) 12)) -1 0) 0.45 0 (rtos vsn 2 0))
(setvar "CLAYER" "PENME")
(command "._offset" (/ vsn 12) vlnty "_non" '(70 0. 0.) "")

)

 

End and start one here

 

	

(setq enths (entsel "\Select the Horiz Strap measured from: ")) 

;original strap	
(setq hs (getdist "\Distance to next Horiz Strap: "))
;distance to next vert strap

(setvar "CLAYER" "DIMBAD")
(command ".text"  (list -1 (/ (/ hs 2) 12) 0) 0.45 0 (rtos hs 2 0))
(setvar "CLAYER" "PENME")
(command "._offset" (/ hs 12) enths "_non" '(0. 70 0.) "")

 

End and start one here

 

	

(while
 
(setq hsn (getdist "\Distance to next Horiz Strap (Enter when done): "))
(setq hlnty (entlast))
(setq hstart (entget (entlast)))
(setq hspt (cdr (assoc 10 hstart)))
(setq yspt (cadr hspt))

(setvar "CLAYER" "DIMBAD")
(command ".text"  (list -1 (+ yspt (/ (/ hsn 2) 12)) 0) 0.45 0 (rtos hsn 2 0))
(setvar "CLAYER" "PENME")
(command "._offset" (/ hsn 12) hlnty "_non" '(0. 70 0.) "")

)

 

End here

 


(setq tedge (ssget)); "\Select the last Vert and Horiz strap created: "))
(command "._trim" tedge)

(princ)
)

Link to comment
Share on other sites

A suggestion use a while then check the answer like (setq hsn (getdist "\Distance to next Horiz Strap (Enter when done): ")) enter A "this is not a number please redo" you can use to force a exit as it returns a value of Nil

 

eg (setq tedge (ssget)); "\Select the last Vert and Horiz strap created: ")) check sslength if not 2 do again

 

; a preset example
(if (= vert nil)
   (progn (setq vert 50)
   (prompt "\nEnter Vertical scale:<")
   (prin1 vert)
   (prompt ">:")
   (setq newvert (getint))
   (if (= newvert nil)
     (setq vert vert)
     (setq vert newvert)
   )
   )
 )

Link to comment
Share on other sites

  • 2 years later...

I have modified this pretty extensively, and I still cannot get the undo to produce the results that I am looking for. I now get an error because I believe the old reference is no longer available (entlast). The undo works perfectly, however, the next offset does not work.

 

Basically, there is a vertical line present on the starter drawing. The user selects that line first after the lisp is initiated and then enters the distance to the next vertical line. The line is offset and a dimension is placed in the drawing. Next, the user simply enters the distances to the next lines and they are offset and a dimension is placed and this repeats until enter is pressed. If they enter the wrong value I would like the previous offset and dimension deleted and then they can continue as they did before, entering the distance to the next one. But, it errors

 

(defun c:grid1 ()
(progn
(setq s (car (entsel "\nSelect the Vertical Strap measured from:")))
(setq d (getdist "\nDistance to next Vertical Strap: "))
     (command "._offset"
              (/ d 12.)
              s
              "_non"
              (polar (cdr (assoc 10 (entget s))) 0. 1.)
              ""
     )
     (if (not (eq s (setq s (entlast))))
       (progn
         (setq p1 (cdr (assoc 10 (entget s)))
               p2 (cdr (assoc 11 (entget s)))
               pt (if (< (cadr p1) (cadr p2))
                    p1
                    p2
                  )
         )
	  (setq dx1 (car p1)   ;ent x value
			dx2 (cadr p1)  ;ent y value
			dx3 (caddr p1) ;ent z value
			dd1 (+ dx1 (/ d 12)) ;dimension distance
			sdx (- dx1 (/ d 12))  ;start point x
			dmx (/ (+ dd1 dx1) 2) ;middle 
		
	  )
(if (>= d 18)	
	(setq doff -1.6)
	(setq doff -3.2)
	)
         (entmake (list (cons 0 "DIMENSION") 
	  (cons 100  "AcDbEntity")
	  (cons 67 0)	 
      (cons 8  "DIMBAD")
      (cons 100  "AcDbDimension") 
	  (cons 10 (trans (list dd1 doff 0.) 1 0))
	  (cons 11 (trans (list dmx doff 0.) 1 0)) 
	  (cons 12 (list 0. 0. 0.))
	  (cons 70  33)
      (cons 1  "")
      (cons 71  5)
      (cons 72  1)
      (cons 41  1.0)
	  (cons 52 0)
      (cons 53 0)
      (cons 54  0)
	  (cons 3  "TEMPLATE_DRAWING")
	  (cons 100  "AcDbAlignedDimension") 
	  (cons 13 (trans (list sdx 0. 0.) 1 0))
	  (cons 14 (trans (list dx1 0. 0.) 1 0))
	  (cons 15 (list 0. 0. 0.))
      (cons 16 (list 0. 0. 0.))
	  ))
       )
     )
(command "._undo" "._mark")
   (while

(progn

(initget "U")
(setq d (getdist (strcat "\Distance to next Vert Strap (Enter when done; U to Undo)"))))

(cond
((= d "U") (command "._undo" "._back"))
((/= d 0)

(progn
     (command "._offset"
              (/ d 12.)
              s
              "_non"
              (polar (cdr (assoc 10 (entget s))) 0. 1.)
              ""
     )
     (if (not (eq s (setq s (entlast))))
       (progn
         (setq p1 (cdr (assoc 10 (entget s)))
               p2 (cdr (assoc 11 (entget s)))
               pt (if (< (cadr p1) (cadr p2))
                    p1
                    p2
                  )
         )
	  (setq dx1 (car p1)   ;ent x value
			dx2 (cadr p1)  ;ent y value
			dx3 (caddr p1) ;ent z value
			dd1 (+ dx1 (/ d 12)) ;dimension distance
			sdx (- dx1 (/ d 12))  ;start point x
			dmx (/ (+ dd1 dx1) 2) ;middle 
	  )
	  (if (>= d 18)	
	(setq doff -1.6)
	(setq doff -3.2)
	)
         (entmake (list (cons 0 "DIMENSION") 
	  (cons 100  "AcDbEntity")
	  (cons 67 0)	 
      (cons 8  "DIMBAD")
      (cons 100  "AcDbDimension")
	  (cons 10 (trans (list dd1 doff 0.) 1 0))
	  (cons 11 (trans (list dmx doff 0.) 1 0)) 
	  (cons 12 (list 0. 0. 0.))
	  (cons 70  33)
      (cons 1  "")
      (cons 71  5)
      (cons 72  1)
      (cons 41  1.0) 
	  (cons 52 0)
      (cons 53 0)
      (cons 54  0)
	  (cons 3  "TEMPLATE_DRAWING")
	  (cons 100  "AcDbAlignedDimension") 
	  (cons 13 (trans (list sdx 0. 0.) 1 0))
	  (cons 14 (trans (list dx1 0. 0.) 1 0))
	  (cons 15 (list 0. 0. 0.))
      (cons 16 (list 0. 0. 0.))
	  ))
       )
     )
   )
)))
)
(princ)
)

Link to comment
Share on other sites

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