Author Topic: How to reset variables in the get midpoint functions  (Read 9482 times)

0 Members and 1 Guest are viewing this topic.

jaydee

  • Guest
How to reset variables in the get midpoint functions
« on: August 04, 2011, 08:25:05 AM »
There are tones of ways to get the midpoint between 2points.
The link below show a few of them
http://www.theswamp.org/index.php?topic=2587.msg32773#msg32773

I used these function from a accelerator pop menu for transparent function call.

My question is how to code these functions to reset variables after getmidpoints, i have tried
(while (> (getvar "cmdactive") 0) (command pause))
and it not working.

The code below always go straight to osmode=0 instead of 131
What i would like to do is to provide *error* trapping just incase cancel out before picking the 2 points so all the vars can be reset.

Code: [Select]
;;;this one from ronjonp
(defun c:mm (/ pt1 pt2)

[color=red](setvar "osmode" 131)[/color]

  (setq pt1 (getpoint)
        pt2 (getpoint)
  )
  (polar pt1 (angle pt1 pt2) (/ (distance pt1 pt2) 2.))
[color=red](setvar "osmode" 0)[/color]
)

Code: [Select]
(defun c:GETMID ( / pt1 pt2 inspt)
[color=red](setvar "osmode" 131)[/color]
(setq pt1 (getpoint "\nSelect First Point : ")
         pt2 (getpoint pt1 "\nSelect Second Point : "))
(setvar "osmode" osm)
(setq inspt
   (mapcar '+ pt1
      (mapcar '/
         (mapcar '- pt2 pt1)
         '(2.0 2.0 2.0)
      )
   )
)
[color=red](setvar "osmode" 0)[/color]
)

Thankyou. Your advice are much appriciated
« Last Edit: August 04, 2011, 08:42:28 AM by jaydee »

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: How to reset variables in the get midpoint functions
« Reply #1 on: August 04, 2011, 08:52:56 AM »
I think the "best" way to do it is to save the OSMode before changing it into some variable, e.g.:
Code: [Select]
(defun c:whatever (/ other-args osmode)
  (setq osmode (getvar "OSMODE"))
  (setvar "OSMODE" 131)
  ;; The rest of the code
  (setvar "OSMODE" osmode)
  (princ)
)
Of course you might then also want some error handling for in case the user presses ESC.
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

Lee Mac

  • Seagull
  • Posts: 12921
  • London, England
Re: How to reset variables in the get midpoint functions
« Reply #2 on: August 04, 2011, 09:12:12 AM »
Irneb's already pointed you in the right direction  8-)

Below is a brief explanation of an example which includes an error handler:

Code: [Select]
(defun c:test ( / *error* osm p1 p2 pm )

  ;; Redefine *error* function to reset value of
  ;; OSMODE System Variable should the routine error or
  ;; the user hit Esc.
  ;;
  ;; Notice that the *error* symbol is localised so that
  ;; it assumes its previous value after program evaluation is
  ;; complete.

  (defun *error* ( msg )
    (if osm (setvar 'OSMODE osm))
    (if (not (member (strcase msg t) '("function cancelled" "quit / exit abort")))
      (princ (strcat "\nError: " msg))
    )
    (princ)
  )

  ;; Store value of OSMODE System Variable before
  ;; changing it, so that it may be reset either in the
  ;; error handler or at the end of the program.

  (setq osm (getvar 'OSMODE))

  ;; Set OSMODE to a desired setting. I use the
  ;; (+ bit bit bit) arrangement to make it clearer
  ;; as to which bits are being set.
 
  (setvar 'OSMODE (+ 1 2 128))

  (if

    ;; If the following expression returns non-nil

    (and

      ;; AND will continue evaluating expressions supplied to it
      ;; until an expression returns nil.
      ;; If all expressions return non-nil, AND will return T
      ;; otherwise AND will return nil.

      (setq p1 (getpoint "\nSpecify First Point: "))
      (setq p2 (getpoint "\nSpecify Second Point: "))

    ) ;; End AND

    (progn

      ;; Use the progn function to wrap the following statements into a single
      ;; expression that we can pass to the IF function to constitute the 'Then' argument
     
      (setq pm (mapcar '(lambda ( a b ) (/ (+ a b) 2.)) p1 p2))

      ;; Calculate Midpoint.

      (setvar 'OSMODE 0)

      ;; Set OSMODE to zero, ready for the rest of your code...

      (command "_.point" pm)

      ;; ^^ Just as an example.

    ) ;; End Progn
   
  ) ;; End IF

  (setvar 'OSMODE osm)

  ;; Reset OSMODE

  (princ) ;; Exit Quietly
)

I have also written a small tutorial on error handling here which may be of help.

ribarm

  • Gator
  • Posts: 3296
  • Marko Ribar, architect
Re: How to reset variables in the get midpoint functions
« Reply #3 on: August 04, 2011, 09:27:11 AM »
Irneb, why do you use variable osmode as local variable when it is sysvar... Just posted Lee is correct - osm is OK...

Or this has no sense?

M.R.
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: How to reset variables in the get midpoint functions
« Reply #4 on: August 04, 2011, 09:33:15 AM »
Irneb, why do you use variable osmode as local variable when it is sysvar... Just posted Lee is correct - osm is OK...

Or this has no sense?

M.R.
It doesn't matter what you call it, it's a localized lisp variable. You could call it by any name you wish. I just used a name which looks the same as the system variable. I could have used o or os or osm or just-another-variable-which-i-dont-want-to-think-about  ;)
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: How to reset variables in the get midpoint functions
« Reply #5 on: August 04, 2011, 09:44:55 AM »
It doesn't matter what you call it, it's a localized lisp variable. You could call it by any name you wish. I just used a name which looks the same as the system variable. I could have used o or os or osm or just-another-variable-which-i-dont-want-to-think-about  ;)

... and I personally think it's good practice, self documenting code et al.
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

Lee Mac

  • Seagull
  • Posts: 12921
  • London, England
Re: How to reset variables in the get midpoint functions
« Reply #6 on: August 04, 2011, 09:49:29 AM »
It doesn't matter what you call it, it's a localized lisp variable. You could call it by any name you wish. I just used a name which looks the same as the system variable. I could have used o or os or osm or just-another-variable-which-i-dont-want-to-think-about  ;)

... and I personally think it's good practice, self documenting code et al.

1+  And I'm just a lazy typist  :lol:

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: How to reset variables in the get midpoint functions
« Reply #7 on: August 04, 2011, 09:53:25 AM »
I'm just a lazy typist  :lol:

Refuted by thousands of lines of code me thinks.
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

Lee Mac

  • Seagull
  • Posts: 12921
  • London, England
Re: How to reset variables in the get midpoint functions
« Reply #8 on: August 04, 2011, 09:56:12 AM »
I'm just a lazy typist  :lol:

Refuted by thousands of lines of code me thinks.

 :-P

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: How to reset variables in the get midpoint functions
« Reply #9 on: August 04, 2011, 10:24:29 AM »
Refuted by thousands of lines of code me thinks.
But that's why he's a lazy typist!  ;)
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: How to reset variables in the get midpoint functions
« Reply #10 on: August 04, 2011, 10:34:49 AM »
But that's why he's a lazy typist!  ;)

Should have said: "Refuted by thousands of lines of carefully crafted code me thinks."

Subtitle: The dude is anal about his coding. I say that affectionately as it looks very much like my coding style, which (acknowledging hubris) is not a bad thing imo.
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

Lee Mac

  • Seagull
  • Posts: 12921
  • London, England
Re: How to reset variables in the get midpoint functions
« Reply #11 on: August 04, 2011, 10:42:17 AM »
Subtitle: The dude is anal about his coding. I say that affectionately as it looks very much like my coding style, which (acknowledging hubris) is not a bad thing imo.

I'll confess that I spend far too much time on code formatting and tend to sacrifice program functionality over having 'clean' code. But I would say that a great deal has been gleaned from your plentiful examples Michael, I hope this is evident without demonstrating plagiarism.

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: How to reset variables in the get midpoint functions
« Reply #12 on: August 04, 2011, 10:51:12 AM »
I'll confess that I spend far too much time on code formatting and tend to sacrifice program functionality over having 'clean' code.

Nonsense, to be succinct it will make you employable.

But I would say that a great deal has been gleaned from your plentiful examples Michael

It has been my great pleasure and honor to be able to share some of my coding over the years. While thanks are not needed it's truly great when someone acknowledges it. Thank you Lee. The only thing better is when one grabs the baton and reciprocates in kind -- which you have done in spades my friend. You have produced an enviable library of work; kudos.

I hope this is evident without demonstrating plagiarism.

I've never had a sense of plagiarism. You're doing your own thing, and well, as noted above.

PS: My own style was the result of programming in many different languages and finding a style that worked for all of them. Reviewing work from masters like Nesterovsky, Petzold, Tanzillo et all did not hurt one bit either.
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

Lee Mac

  • Seagull
  • Posts: 12921
  • London, England
Re: How to reset variables in the get midpoint functions
« Reply #13 on: August 04, 2011, 11:06:18 AM »
Thanks for your kind words & advice Michael, I feel flattered.  :-)

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: How to reset variables in the get midpoint functions
« Reply #14 on: August 04, 2011, 11:09:07 AM »
You're very welcome Lee. Apologies to everyone else for the tangent. :)
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst