offset by point

offset by point

Anonymous
Not applicable
2,620 Views
18 Replies
Message 1 of 19

offset by point

Anonymous
Not applicable

hi
anyone know how to edit this code? as I want it show previous offset distance every time common start. 
like this

off.png

Thank you

 

(defun c:aa (/ off pt1 pt2 pt3 ang)
  ;; Tharwat - 20.Sep.2016	;;
  (if (setq off (getdist "\nSpecify offset distance :"))
    (while (and (setq pt1 (getpoint "\n1st point :"))
                (setq pt2 (getpoint "\n2nd point :" pt1))
                (setq pt3 (getpoint "\nSpecify offset side :"))
                (setq ang (angle pt1 pt2))
                )
      (setq ang (if (minusp (sin (- ang (angle pt2 pt3))))
                  (+ ang (* pi 0.5))
                  (- ang (* pi 0.5))
                  )
            )
      (entmakex (list '(0 . "LINE")
                      (cons 10 (trans (polar pt1 ang off) 1 0))
                      (cons 11 (trans (polar pt2 ang off) 1 0)))))
    )
  (princ)
  )
0 Likes
Accepted solutions (1)
2,621 Views
18 Replies
Replies (18)
Message 2 of 19

DannyNL
Advisor
Advisor

Here you go

 

(defun c:aa (/ offprev off pt1 pt2 pt3 ang)
  ;; Tharwat - 20.Sep.2016	;;
   (if
      (> (setq offprev (getvar "OFFSETDIST")) 0)
      (progn
         (initget 6)
         (setq off (getdist (strcat "\nSpecify offset distance <" (rtos offprev) ">:")))
         (if
            (not off)
            (setq off offprev)
         )
      )
      (progn
         (initget 7)
         (setq off (getdist "\nSpecify offset distance :"))
      )
   )
   (while
      (and
         (setq pt1 (getpoint "\n1st point :"))
         (setq pt2 (getpoint "\n2nd point :" pt1))
         (setq pt3 (getpoint "\nSpecify offset side :"))
         (setq ang (angle pt1 pt2))
      )
      (setq ang
         (if
            (minusp (sin (- ang (angle pt2 pt3))))
            (+ ang (* pi 0.5))
            (- ang (* pi 0.5))
         )
      )
      (entmakex
         (list '(0 . "LINE")
         (cons 10 (trans (polar pt1 ang off) 1 0))
         (cons 11 (trans (polar pt2 ang off) 1 0)))
      )
   )  
   (princ)
)
0 Likes
Message 3 of 19

Anonymous
Not applicable

hi @DannyNL, thanks for reply.

code you post look like nothing change, I have to key in offset distance every time common start.Untitled.png

0 Likes
Message 4 of 19

DannyNL
Advisor
Advisor

Yes, sorry....my bad.

Forget to add one line of code. Tested in a development drawing, but forgot to reset some manual variables so it seemed like everything worked ok.

 

But below the modified code that should work. It uses the same system variable as the regular OFFSET command, so in both the OFFSET as the AA command you will get the same previous offset value.

 

PointOffset.gif

 

(defun c:aa (/ offprev off pt1 pt2 pt3 ang)
  ;; Tharwat - 20.Sep.2016	;;
   (if
      (> (setq offprev (getvar "OFFSETDIST")) 0)
      (progn
         (initget 6)
         (setq off (getdist (strcat "\nSpecify offset distance <" (rtos offprev) ">:")))
         (if
            (not off)
            (setq off offprev)
         )
      )
      (progn
         (initget 7)
         (setq off (getdist "\nSpecify offset distance :"))
         (setvar "OFFSETDIST" off)
      )
   )
   (while
      (and
         (setq pt1 (getpoint "\n1st point :"))
         (setq pt2 (getpoint "\n2nd point :" pt1))
         (setq pt3 (getpoint "\nSpecify offset side :"))
         (setq ang (angle pt1 pt2))
      )
      (setq ang
         (if
            (minusp (sin (- ang (angle pt2 pt3))))
            (+ ang (* pi 0.5))
            (- ang (* pi 0.5))
         )
      )
      (entmakex
         (list '(0 . "LINE")
         (cons 10 (trans (polar pt1 ang off) 1 0))
         (cons 11 (trans (polar pt2 ang off) 1 0)))
      )
   )  
   (princ)
)
0 Likes
Message 5 of 19

ronjonp
Mentor
Mentor

FWIW .. you could accomplish this with a global variable like so:

(defun c:aa (/ off pt1 pt2 pt3 ang)
  ;; Tharwat - 20.Sep.2016	;;
  ;; RJP added global default 06-01-2018
  (or *aaglobal* (setq *aaglobal* 1.0))
  (if (setq *aaglobal*
	     (cond ((getdist (strcat "\nSpecify offset distance <" (rtos *aaglobal*) ">:")))
		   (*aaglobal*)
	     )
      )
    (while (and	(setq pt1 (getpoint "\n1st point :"))
		(setq pt2 (getpoint "\n2nd point :" pt1))
		(setq pt3 (getpoint "\nSpecify offset side :"))
		(setq ang (angle pt1 pt2))
	   )
      (setq ang	(if (minusp (sin (- ang (angle pt2 pt3))))
		  (+ ang (* pi 0.5))
		  (- ang (* pi 0.5))
		)
      )
      (entmakex	(list '(0 . "LINE")
		      (cons 10 (trans (polar pt1 ang off) 1 0))
		      (cons 11 (trans (polar pt2 ang off) 1 0))
		)
      )
    )
  )
  (princ)
)
0 Likes
Message 6 of 19

Kent1Cooper
Consultant
Consultant

@Anonymous wrote:

anyone know how to edit this code? as I want it show previous offset distance every time common start. ....

 


You can do the whole thing, including setting the Offset distance before  giving the starting-position Line endpoints [as in your original routine], and  offering the previous Offset distance every time as default, with a whole lot less code -- just this:

 

(defun C:AA ()

  (command "_.offset" "_erase" "_yes" pause "" "_.line" pause pause "" "_.offset" "" "@" pause "")

)

 

[If you don't mind the Offset-distance setting being after  the starting-position Line endpoints are specified, rather than before, it can be even shorter.]

 

That's if you don't mind its leaving the Offset command in the Erase-the-source mode when it's done.  I'm not sure without some research how AutoCAD remembers the Erase-or-not setting in Offset, so that it could be saved first and restored afterwards, but I imagine it could be, still in a much shorter routine.  Or if you always want it set [or set back] to don't-Erase mode, that could be built in:

(defun C:AA ()

  (command

    "_.offset" "_erase" "_yes" pause ""

    "_.line" pause pause ""

    "_.offset" "" "@" pause ""

    "_.offset" "_erase" "_no" "" ""

  )

  (princ)

)

Kent Cooper, AIA
0 Likes
Message 7 of 19

john.uhden
Mentor
Mentor

The major advantage I see of using the OFFSET command, or vla-offset, is that the new object gets all the properties of its parent.  Plus, it can be used on objects other than lines.

John F. Uhden

0 Likes
Message 8 of 19

Anonymous
Not applicable

@DannyNL thanks for help.

After tried it, i find out command always remember 1st time distance but not previous distance, can change it to remember previous distance? thank you.

 

0 Likes
Message 9 of 19

john.uhden
Mentor
Mentor

Let's say your variable name for the offset distance is OFF.

Each time you set a new value for OFF, (setvar "offsetdist" OFF).

John F. Uhden

0 Likes
Message 10 of 19

Anonymous
Not applicable

@john.uhden thanks for reply

so have to add in (setvar "offsetdist" OFF) ? sorry, i'm not really understand what you means.

now the command always remember 1st offset distance value

example:

1st value = 2

2nd value = 4

3rd value = 5

4th value = 8

command always remember 1st value 2 even i already change to 2nd, 3rd ... value. 

 

0 Likes
Message 11 of 19

DannyNL
Advisor
Advisor
Accepted solution

Just move the setting of the variable outside the IF as in the code below.

That way it will always save the last used setting.

 

(defun c:aa (/ offprev off pt1 pt2 pt3 ang)
  ;; Tharwat - 20.Sep.2016	;;
   (if
      (> (setq offprev (getvar "OFFSETDIST")) 0)
      (progn
         (initget 6)
         (setq off (getdist (strcat "\nSpecify offset distance <" (rtos offprev) ">:")))
         (if
            (not off)
            (setq off offprev)
         )
      )
      (progn
         (initget 7)
         (setq off (getdist "\nSpecify offset distance :"))
      )
   )
   (setvar "OFFSETDIST" off)
   (while
      (and
         (setq pt1 (getpoint "\n1st point :"))
         (setq pt2 (getpoint "\n2nd point :" pt1))
...
...
0 Likes
Message 12 of 19

DannyNL
Advisor
Advisor

@ronjonp wrote:

FWIW .. you could accomplish this with a global variable like so:

Yes, this is also possible but why use a global LISP variable if AutoCAD has a dedicated offset variable for this on his own?

It will save you a small amount of code, but using OFFSETDIST in this case is better.

0 Likes
Message 13 of 19

Anonymous
Not applicable

thank you very much.

0 Likes
Message 14 of 19

DannyNL
Advisor
Advisor

You're welcome & glad I could help Smiley Happy

0 Likes
Message 15 of 19

Kent1Cooper
Consultant
Consultant

@DannyNL wrote:

.... why use a global LISP variable if AutoCAD has a dedicated offset variable for this on his own? ....

 

{and}

 

Just move the setting of the variable outside the IF as in the code below.

That way it will always save the last used setting.

 

(defun c:aa (/ offprev off pt1 pt2 pt3 ang)
  ;; Tharwat - 20.Sep.2016	;;
   (if
      (> (setq offprev (getvar "OFFSETDIST")) 0)
      (progn
         (initget 6)
         (setq off (getdist (strcat "\nSpecify offset distance <" (rtos offprev) ">:")))
         (if
            (not off)
            (setq off offprev)
         )
      )
      (progn
         (initget 7)
         (setq off (getdist "\nSpecify offset distance :"))
      )
   )
   (setvar "OFFSETDIST" off)
   (while
      (and
         (setq pt1 (getpoint "\n1st point :"))
         (setq pt2 (getpoint "\n2nd point :" pt1))
...
...

I agree with the first quote above, which makes me wonder about the second -- I don't understand going to all the trouble to build all of that, when the Offset command itself handles it without any additional code.  See Message 6, which allows it to do so.  Only if you want to keep track of an Offset distance for use with this command, separate from  regular Offset's distance, as a global variable, would it seem worth doing.

Kent Cooper, AIA
0 Likes
Message 16 of 19

ronjonp
Mentor
Mentor

@DannyNL wrote:

@ronjonp wrote:

FWIW .. you could accomplish this with a global variable like so:

Yes, this is also possible but why use a global LISP variable if AutoCAD has a dedicated offset variable for this on his own?

It will save you a small amount of code, but using OFFSETDIST in this case is better.


Agreed .. I'd change it to something like this:

(defun c:aa (/ off pt1 pt2 pt3 ang)
  ;; Tharwat - 20.Sep.2016	;;
  (setq off (getvar 'offsetdist))
  (if (setq off	(cond ((getdist (strcat "\nSpecify offset distance <" (rtos off) ">:")))
		      (off)
		)
      )
    (while (and	(setq pt1 (getpoint "\n1st point :"))
		(setq pt2 (getpoint "\n2nd point :" pt1))
		(setq pt3 (getpoint "\nSpecify offset side :"))
		(setq ang (angle pt1 pt2))
	   )
      (setq ang	(if (minusp (sin (- ang (angle pt2 pt3))))
		  (+ ang (* pi 0.5))
		  (- ang (* pi 0.5))
		)
      )
      (entmakex	(list '(0 . "LINE")
		      (cons 10 (trans (polar pt1 ang off) 1 0))
		      (cons 11 (trans (polar pt2 ang off) 1 0))
		)
      )
    )
  )
  (princ)
)
0 Likes
Message 17 of 19

DannyNL
Advisor
Advisor

All roads lead to Rome @Kent1Cooper, you should know that Smiley Happy

I've not built anything, I've just modified the already existing code of the OP to suite his feature request.

0 Likes
Message 18 of 19

DannyNL
Advisor
Advisor

That would be a way to go, but there is still missing something.

 

OFFSETDIST always has a value, but it will be -1.00 when it has not been previously used. So you'll need to check for that value as well and only give the value as an option if it's greater than 0.0.

And the last OFF value will need to be saved back to OFFSETDIST, so it can be used as the default value the next time.

 

0 Likes
Message 19 of 19

ronjonp
Mentor
Mentor

You are correct. I need more coffee this morning LOL. Smiley LOL

 

Take three:

(defun c:aa (/ off pt1 pt2 pt3 ang)
  (and (<= (getvar 'offsetdist) 0) (setq off (setvar 'offsetdist 1)))
  (initget 6)
  (setq	off (abs (cond ((getdist (strcat "\nSpecify offset distance <" (rtos off) ">:")))
		       (off)
		 )
	    )
  )
  (setvar 'offsetdist off)
  (while (and (setq pt1 (getpoint "\n1st point :"))
	      (setq pt2 (getpoint "\n2nd point :" pt1))
	      (setq pt3 (getpoint "\nSpecify offset side :"))
	      (setq ang (angle pt1 pt2))
	 )
    (setq ang (if (minusp (sin (- ang (angle pt2 pt3))))
		(+ ang (* pi 0.5))
		(- ang (* pi 0.5))
	      )
    )
    (entmakex (list '(0 . "LINE")
		    (cons 10 (trans (polar pt1 ang off) 1 0))
		    (cons 11 (trans (polar pt2 ang off) 1 0))
	      )
    )
  )
  (princ)
)