Change system variable, offset, change system variable back

Change system variable, offset, change system variable back

Anonymous
Not applicable
4,078 Views
31 Replies
Message 1 of 32

Change system variable, offset, change system variable back

Anonymous
Not applicable

Hello, 

 

I'm currently trying to write what I think would be a very simple lisp routine. Typically, my OFFSETGAPTYPE system variable is set to 0. On occasion I want to show a buffer around an object which is better shown by changing OFFSETGAPTYPE to 1 and then using the OFFSET command. 

 

I'd like to write a routine called BUFFER that will change OFFSETGAPTYPE to 1, allow me to run the OFFSET command, then change OFFSETGAPTYPE back to 0. Below is what I have at the moment. The script is changing the variable to 1, allowing me to offset whichever objects but after I hit enter to end the offset command it doesn't change the variable back to 0. In other words, it appears that anything after the offset command isn't being executed. 

 

(defun c:buffer()
(setvar "offsetgaptype" 1)
(command "offset")
(setvar "offsetgaptype" 0)
(princ)
)

 

Any help is greatly appreciated!

 

 

 

0 Likes
Accepted solutions (3)
4,079 Views
31 Replies
Replies (31)
Message 2 of 32

dmfrazier
Advisor
Advisor

For a possible "simple" fix, try this:

 

(defun c:buffer()
  (setvar "offsetgaptype" 1)
  (progn

    (command "offset")
    (setvar "offsetgaptype" 0)

  )
  (princ)
)

 

0 Likes
Message 3 of 32

Ranjit_Singh
Advisor
Advisor
Accepted solution

@Anonymous wrote:

............

(defun c:buffer()
(setvar "offsetgaptype" 1)
(command "offset")
(setvar "offsetgaptype" 0)
(princ)
)

 

.........

 


I believe you need to use (command-s) and that should do it.

(defun c:buffer()
(setvar "offsetgaptype" 1)
(command-s "offset")
(setvar "offsetgaptype" 0)
(princ)
)
Message 4 of 32

Kent1Cooper
Consultant
Consultant
Accepted solution

@Anonymous wrote:

.... 

I'd like to write a routine called BUFFER that will change OFFSETGAPTYPE to 1, allow me to run the OFFSET command, then change OFFSETGAPTYPE back to 0. .... 


What you need is to allow for User input to complete the OFFSET command, before going on to re-set the System Variable.  This is done by checking the CMDACTIVE System Variable:

 

(defun c:buffer()
  (setvar "offsetgaptype" 1)
  (command "offset")

  (while (> (getvar 'cmdactive) 0) (command pause))
  (setvar "offsetgaptype" 0)
  (princ)
)

Kent Cooper, AIA
Message 5 of 32

john.uhden
Mentor
Mentor
Accepted solution

I think this is a slight improvement on @Ranjit_Singh's offering:

 

(defun c:buffer( / *error* og)
  (defun *error* (msg)
    (setvar "offsetgaptype" og)
    (princ msg)
    (princ)
  )
  (setq og (getvar "offsetgaptype"))
  (setvar "offsetgaptype" 1)
  (command-s "offset")
  (while (> (getvar "cmdactive") 0)(command pause))
  (*error* "")
)

This will allow you to cancel or exit the command and be sure that offsetgaptype is reset.
I think you needed the cmdactive part anyway.

John F. Uhden

Message 6 of 32

Anonymous
Not applicable

Thank you for the reply! This solution works but I'm not sure you would need cmdactive part as it worked without it. 

0 Likes
Message 7 of 32

Kent1Cooper
Consultant
Consultant

@Anonymous wrote:

.... I'm not sure you would need cmdactive part as it worked without it. 


Really?  It didn't for me:

 

Command: BUFFER
; error: Unknown (command-s) failure.

 

Help for (command-s) says you need to supply everything it will need for a completed command, to quote:

  "The command that is being executed must be started and completed in the same command-s function."

 

That would explain the failure, but if you're not  getting a failure, I'm intrigued -- how can that be?  I'm in Acad2016 where I am at the moment, but that quote is still there in the 2018 AutoLisp Reference, so it doesn't seem like it can be a version-related difference.

Kent Cooper, AIA
Message 8 of 32

Ranjit_Singh
Advisor
Advisor

@john.uhden wrote:

I think this is a slight improvement on @Ranjit_Singh's offering:

 

.......
I think you needed the cmdactive part anyway.

I don't understand. Try the below code on your system.

(defun c:buffer()
(setvar "offsetgaptype" 1)
(command-s "offset")
(setvar "offsetgaptype" 0)
(princ)
)

Does it not allow you to do everything within the command and then exit and still reset the variable? It does at my end.offset_gap_type.gif

In fact trying something a little bit more crazy also works just fine resetting the variable every time.

(defun c:somefunc ( / )
(command-s "._offset")
(setvar 'offsetgaptype 2)
(command-s "._offset")
(setvar 'offsetgaptype 1)
(command-s "._offset")
(setvar 'offsetgaptype 0)
)

offset_gap_type_2.gif

I agree that adding *error* trap is a good habit but it is certainly not needed in the context of this problem. Am I missing something?

 

Message 9 of 32

Anonymous
Not applicable

If you use the script that @Ranjit_Singh posted (copied below) then it works as described and it doesn't contain the cmdactive line. I'm currently working in AutoCAD 2018. Maybe with the error handling you would need the cmdactive? I'm new to lisp routines so I'm not sure. 

 

(defun c:buffer()
(setvar "offsetgaptype" 1)
(command-s "offset")
(setvar "offsetgaptype" 0)
(princ)
)

 

0 Likes
Message 10 of 32

Kent1Cooper
Consultant
Consultant

@Anonymous wrote:

If you use the script that @Ranjit_Singh posted (copied below) then it works as described and it doesn't contain the cmdactive line. I'm currently working in AutoCAD 2018. ....

 


That's what I used, and I got the error I posted earlier.

Kent Cooper, AIA
0 Likes
Message 11 of 32

Kent1Cooper
Consultant
Consultant

@Ranjit_Singh wrote:

 

....  Does it not allow you to do everything within the command and then exit and still reset the variable? It does at my end
.... adding *error* trap is a good habit but it is certainly not needed in the context of this problem. Am I missing something?

 

It doesn't for me.  I've now tried it on two different Acad2106 installations, one on a Windows 7 computer and one Windows 10.  Could it be a version difference?  If so, Help for 2018 needs to be modified to no longer say what I quoted before.

 

As for the *error* handler, what I think you are missing is that someone might start the command and ESCape out of it in mid-command, in which case surely the System Variable would not be reset.  But I can't confirm that, since I can't run the command at all.

Kent Cooper, AIA
0 Likes
Message 12 of 32

Ranjit_Singh
Advisor
Advisor

Kent1Cooper wrote:................

As for the *error* handler, what I think you are missing is that someone might start the command and ESCape ...........


I did that in the previous post (post #8 first video). The video shows how the variable resets after finishing the command, as well as, when the command is simply cancelled. Here I present it again.

offset_gap_type_3.gif

Maybe post a screencast of the error you are encountering.

Message 13 of 32

Kent1Cooper
Consultant
Consultant

Ranjit.Singh wrote

 

.... Maybe post a screencast of the error you are encountering.

I'm not set up to do a screencast here, but it wouldn't reveal anything except what I showed in Post 7 -- I type in the command name and immediately get the error.

 

There must be some new capability and/or restorative feature in 2018, if that's what you're using.  [I haven't been able to get my 2018 to run yet -- a checking-license problem, though it doesn't hang up until the third  time it says it's checking, which also started happening in 2017 that was  working before -- I guess it's time to buckle down and straighten that out.]  Do you still have an older version around to try it on, for comparison?  And it makes me wonder -- what if you write a routine that changes some System Variable and does not  include setting it back?  If you run the routine [with or without cancelling mid-way], does the System Variable get re-set anyway?

 

OR:  Do you have your AutoCAD set up to operate with some special all-purpose restore-everything *error* handler in place of  AutoCAD's own?  Or have you loaded some routine that includes an *error* handler that does something like that, but is not localized, so that AutoCAD's own is not restored as it should be?  Either would certainly be possible, and would explain the whole situation.

Kent Cooper, AIA
0 Likes
Message 14 of 32

john.uhden
Mentor
Mentor

In 2002 I had to change command-s to command, but when I ran Ranjit's version (without the *error* function) and escaped out of the offset, offsetgaptype did not get restored to 0 (yes, it was zero before the c:buffer command).

.

When I ran my version (with *error* and cmdactive) offsetgaptype did get restored on cancelling the offset.

 

If Autodesk has changed the rules with newer releases, they should advise us of it.  Then again, if the older methods still work then just use them.

I don't get hung up on having a few more lines of code, so long as a millisecond doesn't turn into a few seconds

John F. Uhden

0 Likes
Message 15 of 32

Ranjit_Singh
Advisor
Advisor

@Kent1Cooper wrote:

Ranjit.Singh wrote

 

.... Maybe post a screencast of the error you are encountering.

.......   Do you have your AutoCAD set up to operate with some special all-purpose restore-everything *error* handler in place of  AutoCAD's own?.....I


I would request other AutoCAD community members @ВeekeeCZ@dbroad@pbejse@cadffm@pendean@DannyNL ..... to test the below code on their system and report back. All very credible members and I am sure one of them will get the same behavior as you do.

 

(defun c:buffer()
(setvar "offsetgaptype" 1)
(command-s "offset")
(setvar "offsetgaptype" 0)
(princ)
)

I also find it very convenient for you to say “I'm not set up to do a screencast here, but it wouldn't reveal anything ....” and kind of hint that I have customized the system. What do I gain from doing that?

How long does it take to do a screencast? Plus if someone is making a claim that Autodesk needs to correct their hep documents I believe they should provide a little bit more matter than just a statement ; error: Unknown (command-s) failure. Pretty easy to type that. An expert elite member needs to take a little more effort. Don’t you think so? Maybe you can take a screenshot. Maybe you are not setup for that either. 

0 Likes
Message 16 of 32

ВeekeeCZ
Consultant
Consultant

I've made a little bit of testing in Autocad 2015.

 

It works as same as Ranjit's. (command-s "offset") is just fain, no *error* needed - surprisingly ESC does not cause an error - it leaves the command-s and the lisp continues...

 

SCREENCAST

 

 

And the help was kind of supricing as well... unless I am missing something.

 

The following is an invalid use of prompting for user input with the command-s function.
Command: (command-s "._circle" (getpoint "\nSpecify center point: ") "_d" 2.75)

 

No "Pause" command tokens may be used. Expressions that interact with the drawing area or Command Window may be used, but will all be processed before AutoCAD receives and processes any of them.
The following is not valid with the command-s function:
(command-s "._line" "0,0" PAUSE "")

 

The both examples works for me as shown on the screencast.

 

To @john.uhden

See THIS. (command-s) came up wih ACAD 2012 and then the singnificant change in *error* handling came up with 2015.

0 Likes
Message 17 of 32

Ranjit_Singh
Advisor
Advisor

Thanks for testing @ВeekeeCZ. No surprises there as every single member who is running above Acad 2012 will get same results. It is just 4 simple lines of code. I am going to test on 2016 early next week and post a screencast as well.

0 Likes
Message 18 of 32

Kent1Cooper
Consultant
Consultant

@Ranjit_Singh wrote:

....  I also find it very convenient for you to say “I'm not set up to do a screencast here, but it wouldn't reveal anything ....” and kind of hint that I have customized the system. What do I gain from doing that? How long does it take to do a screencast? Plus if someone is making a claim that Autodesk needs to correct their hep documents I believe they should provide a little bit more matter than just a statement ; error: Unknown (command-s) failure. Pretty easy to type that. An expert elite member needs to take a little more effort. Don’t you think so? Maybe you can take a screenshot. ....


You misunderstood.  The question about whether you have customized your system had nothing whatever to do with screencasting.  It was about whether you might have built in something that does what an *error* handler would do [specifically, changes back a System Variable value], without  your having included an *error* handler in the routine.

 

I didn't type in the error message, as easy as that would be -- I copied/pasted it out of the command history.  If you really think a screenshot could possibly reveal anything more about it, I'll put in that little bit more effort -- here you go, including the pasting in of the command definition itself.  A screencast could not reveal anything more -- there would be nothing at all to see beyond this.

 

BufferError.PNG

 

What immediately precedes that is the drawing of a rectangle, to be sure there would be something in the drawing to use  the command on, if it had worked.  That was the only thing I did in the drawing before what you see.

Kent Cooper, AIA
0 Likes
Message 19 of 32

john.uhden
Mentor
Mentor
I see. But the description is functionally no different from the the
command function (unless it really is).

As I will probably never get to 2012, or 2015, or anything past 2002, I
will never really find out.

John F. Uhden

0 Likes
Message 20 of 32

ВeekeeCZ
Consultant
Consultant

One more test - on AutoCAD 2012 - not working.

 

(command-s) does not wrap the OFFSET until it's finished..

 

Command: (defun c:buffer()
(_> (setvar "offsetgaptype" 1)
(_> (command-s "offset")
(_> (setvar "offsetgaptype" 0)
(_> (princ)
(_> )
C:BUFFER

Command: BUFFER
offset
Current settings: Erase source=No  Layer=Source  OFFSETGAPTYPE=1
Specify offset distance or [Through/Erase/Layer] <Through>: ; error: AutoCAD 
variable setting rejected: "offsetgaptype" 0

Specify offset distance or [Through/Erase/Layer] <Through>: 1

Select object to offset or [Exit/Undo] <Exit>:
Specify point on side to offset or [Exit/Multiple/Undo] <Exit>:
Select object to offset or [Exit/Undo] <Exit>:

 

I suspect that this is working on 2015+ versions (except Kent's).

0 Likes