Author Topic: wrote this program...what can i do to make it better  (Read 4456 times)

0 Members and 1 Guest are viewing this topic.

dubb

  • Swamp Rat
  • Posts: 1105
wrote this program...what can i do to make it better
« on: June 08, 2005, 04:32:36 PM »
i wrote this program, however i think i can make it better. i want to make the line all as one peice. and then prompt me to add dtext as a part of the leader. as time goes im getting a little more comforable about writing programs but i dont want to get comfortable with the wrong practices.

Code: [Select]

;;Wynn Engineering Inc.
;;leaderloop.lsp file CREATED BY SOUK
;;souksavanh nomichith
;;last update 06-07-05

(defun c:ll (/ pt1 pt2 scl pt3)
  (princ "Leader loop.....")
  (princ)
  (setq scl (getvar "dimscale"))
  (setq pt1 (getpoint "\nPick first point: ")
  pt2 (getpoint pt1 "\nPick second point: ")
pt3 (getpoint pt2 "\nPick third point: ")

)
  (command "pline" pt1 pt2 pt3 "" "insert" "loop" pt1 scl scl (* 180.0 (/ (angle pt1 pt2) pi)))
  (princ)
  )


here is the file associated with this program
http://www.theswamp.org/lilly_pond/dubb/loop.dwg?nossi=1

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
wrote this program...what can i do to make it better
« Reply #1 on: June 08, 2005, 04:51:10 PM »
a few things I would do is capture a few setvars, mod them and put them back when you are done.  For instance, is the second line "supposed" to be horizontal as it goes into the text?
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
wrote this program...what can i do to make it better
« Reply #2 on: June 08, 2005, 04:52:41 PM »
This is what I did
Code: [Select]
(defun C:LOOP (/ COLOR P1 P2 P3 SIZE)
 (setq ortho  (getvar "orthomode")
cecho (getvar "cmdecho")
osm    (getvar "osmode")
  )
  (setvar "orthomode" 0)
  (setvar "cmdecho" 0)
  (setvar "OSMODE" 0)
  (setq SIZE (getvar "dimscale"))
  (setq P1 (getpoint "\nSelect first point "))
  (setvar "orthomode" 1)
  (command "pline" P1)
  (setq P2 (getpoint P1 "\nNext point "))
  (command "w" 0 0 P2)
  (setvar "orthomode" 0)
  (setq P3 (getpoint P2 "\nLast point "))
  (command P3 "")
  (command "insert" "loop" P3 SIZE "" P2)
  (setvar "orthomode" ortho)
  (setvar "cmdecho" cecho)
  (setvar "OSMODE" osm)
  (princ)
)
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

dubb

  • Swamp Rat
  • Posts: 1105
wrote this program...what can i do to make it better
« Reply #3 on: June 08, 2005, 05:35:53 PM »
Quote from: CmdrDuh
a few things I would do is capture a few setvars, mod them and put them back when you are done.  For instance, is the second line "supposed" to be horizontal as it goes into the text?


yes the second line should be horizontal with a dtext beside it. is it possible to have the loop with the line as one object or polyline?

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
wrote this program...what can i do to make it better
« Reply #4 on: June 08, 2005, 06:17:34 PM »
Ask yourself questions like "What happens if the user doesn't pick the first point, or subsequent points etc." Does the program handle it gracefully or do a face plant? What impacts do various settingd, like units, cmdecho etc. have on performance / display? What can I do to bomb / PEBKAC proof it? Can any portions be rewritten so they could be re-used? Have I written this in a manner which would make it a no brainer for me to quickly understand the program a year from now? Etc.

:)
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

dubb

  • Swamp Rat
  • Posts: 1105
wrote this program...what can i do to make it better
« Reply #5 on: June 15, 2005, 01:20:12 PM »
Quote from: MP
Ask yourself questions like "What happens if the user doesn't pick the first point, or subsequent points etc." Does the program handle it gracefully or do a face plant? What impacts do various settingd, like units, cmdecho etc. have on performance / display? What can I do to bomb / PEBKAC proof it? Can any portions be rewritten so they could be re-used? Have I written this in a manner which would make it a no brainer for me to quickly understand the program a year from now? Etc.

:)


exactly, now i just need to find out how to trap the program so that it will have a positive output no matter what happens rather than an error. btw what the hell is PEBKAC?...ill just google it.

JohnK

  • Administrator
  • Seagull
  • Posts: 10605
wrote this program...what can i do to make it better
« Reply #6 on: June 15, 2005, 02:13:38 PM »
Dubb,

In keeping with MP words of wisdom...

One thing I like to do in my progys is to make 'stronger' support procedures for myself. I love to take the "preventive error traping" approach to alot of my apps. This dosent mean that I dont supply an error trap it just means that I have that much less to do when I do add the trap.

So that being said; I will toss this up here and hope you will ask T O N S of questions about it. When you think you got a good handle on it we can apply this "method" to some other aspects of your program if you would like.

Code: [Select]
(defun getpoin7 ()
  ;; Start a loop to keep going untill test condition evaluates to NIL
  (while
    ;; Make a TRUE eval to nil. -- Otherwise tell me the oposite of what really happnens
    (not
       ;; Create a variable and fill it with the results of a getpoint.
      (setq x (getpoint "\nSelect Point: "))
      );_ end not
    ;; If the user didnt pic the point let them know.
     (princ "\nYou did not select a point, please try again. ")
    );_ end while
  ;; When the loop ends make sure we return the good results back to them.
  x
 );_ end defun
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

dubb

  • Swamp Rat
  • Posts: 1105
wrote this program...what can i do to make it better
« Reply #7 on: June 15, 2005, 03:14:09 PM »
where is there an "x" here
Code: [Select]

;;When the loop ends make sure we return the good results back to them.
x
);_ end defun


if the user selects a point then the program ends..right?but if the user hits "enter" or doesnt select a point then the program will continue following the (princ) line. by using "while" and "not"...it allows the the program to evaluate a nil?

CADaver

  • Guest
wrote this program...what can i do to make it better
« Reply #8 on: June 15, 2005, 03:51:40 PM »
And to throw in my 2 cents:

Let the routine setup a child dimstyle for leaders with a user block rather than using a separate pline and block.

JohnK

  • Administrator
  • Seagull
  • Posts: 10605
wrote this program...what can i do to make it better
« Reply #9 on: June 15, 2005, 05:17:21 PM »
Quote from: dubb
where is there an "x" here
Code: [Select]

;;When the loop ends make sure we return the good results back to them.
x
);_ end defun


if the user selects a point then the program ends..right?but if the user hits "enter" or doesnt select a point then the program will continue following the (princ) line. by using "while" and "not"...it allows the the program to evaluate a nil?


The "x" comes from us creating a variable and filling it up with the result of the "getpoint" function. (Its in the middle of the While-not loop.)

Yes, Would you like to see how you can do this same thing without a "loop"?
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

dubb

  • Swamp Rat
  • Posts: 1105
wrote this program...what can i do to make it better
« Reply #10 on: June 15, 2005, 08:14:06 PM »
Quote from: CADaver
And to throw in my 2 cents:

Let the routine setup a child dimstyle for leaders with a user block rather than using a separate pline and block.


thanks man..ill try this way too



Quote from: se7en
The "x" comes from us creating a variable and filling it up with the result of the "getpoint" function. (Its in the middle of the While-not loop.)

Yes, Would you like to see how you can do this same thing without a "loop"?


i just thought the "x" was in an odd place.
yes it would koo to see this without the a "loop"[/quote]

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
wrote this program...what can i do to make it better
« Reply #11 on: June 15, 2005, 09:24:26 PM »
Looking at John's code without comments, it looks like this
Code: [Select]
(defun getpoin7 (/ x)
  (while (not (setq x (getpoint "\nSelect Point: ")))
     (princ "\nYou did not select a point, please try again. ")
  )
  x
)


As you can see the WHILE loop is waiting for a nil value so it can quit.
It gets that value when the user picks a point.
The GETPOINT returns a point value to the SETQ which in turn returns that value
to the NULL. Because the value is not null the NULL returns nil.
This is the null the WHILE was waiting for to exit the loop.
But the WHILE returns the most recent value of the last expression, which is nil.

You want the getpoin7 function to return the point list not the nil value from the WHILE loop.
So you put the x and it does nothing more that return its value, which is the point list
we were looking for.

I hope that was clear enough to follow.

Each lisp function returns something and sometimes it is meaningless like PROMPT or COMMAND always
return nil and has no meaning. But most return a value that can be useful to you as a programmer.
Pay attention to these return values & think creatively. :)

Not very eloquent but I think you will see what I was trying to say.
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

JohnK

  • Administrator
  • Seagull
  • Posts: 10605
wrote this program...what can i do to make it better
« Reply #12 on: June 15, 2005, 10:38:15 PM »
Good. This is gonna be very kool! (I think your gonna really like this!)

What we are gonna do is test the same way we did in the previous procedure, but this time instead of re-looping if the condition isnt met, we just start over at the begining. Souds simple huh? Well guess what; people tend to get real confused about this method. In realty its a very simple and easy way to accomplish a task. "If it didnt work this time ...try it again!"

This is a basic programing method called recursion. There are several diff methods and ways to classify recursion, but for now we are gonna concentrate on the simplest form.

For now, we are gonna define recursion as: If a test condition isnt met, call the procedure again. (Later on we can develop a more deeper understanding and definition of recursion. But right now we are just gonna keep it simple. k? good. *lol*)


Code: [Select]
;;; g3tpoin7
;;;
;;; A simple recursive example
;;;
(defun g3tpoin7 ( / x)
  (if
    (setq x (getpoint "\nSelect Point: "))
    x
    (g3tpoin7)
   )
 )
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
wrote this program...what can i do to make it better
« Reply #13 on: June 16, 2005, 01:31:02 AM »
Before this thread is finished make sure you mention initget too.

/suggestion

:)
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

dubb

  • Swamp Rat
  • Posts: 1105
wrote this program...what can i do to make it better
« Reply #14 on: June 16, 2005, 12:01:28 PM »
so this recursion thing....doesnt give any options but to make sure the user does something when the program expects an input. cool....i can really use this method in alot of my routines...since you brought up the word "recursion" i decided to google it....heres what i found

http://mars.cs.unp.ac.za/lisp/lisp5.html

so there is more to you recursion method?

JohnK

  • Administrator
  • Seagull
  • Posts: 10605
wrote this program...what can i do to make it better
« Reply #15 on: June 16, 2005, 02:02:04 PM »
**Real quick here**

There is T O N S more to recursion, but for now just focus on the basics. (I will say that you can --and will at sometime-- get into serious trouble for using recursion in your apps. Although its a very "efficient" way to accomplish a task on your part its not an "efficient" method for the computer. Recursion can, more often then not, become a HOG. It would be like driving a big ol cadalic; Its very nice and perdy, but its bad for gas millage.)

Okay, Thats enough about recursion. We learned something new and had fun playing arround with it, lets keep our attention on the task at hand. (Working on a better app.) And besides, we've actualy got a perdy cool procedure in the first one we created so we can use that one in your app if you like. But MP has a good point. And since we are just poking about trying to develop a neat lil app we should try to explore our options and see if we cant "beef up" a bit. So im gonna give you some homework now.

Look up "initget" in the help file and return to me the "bit codes". I also want you to explain to me what "initget" does.  

Now let's see if we cant get MP to share some more of his thoughts on the issue.

Sorry gotta run now. I'll be back in a bit.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
wrote this program...what can i do to make it better
« Reply #16 on: June 16, 2005, 02:10:12 PM »
Quote from: MP
Before this thread is finished make sure you mention initget too.

/suggestion

:)

You already did. :)

INITGET is a great function but with points it falls short.
Unless someone proves me wrong, you can still crash this code.
Code: [Select]
(defun c:test (/ pt)
  (initget 1)
  (setq pt (getpoint "\nPlease select a point: "))
  (princ)
)


by entering a number. If you enter a point like this 211,315
all is well and the initget will prevent you from just pressing ENTER.
But if you enter a single number 22 for example a point is returned that
is random, I think it is random.

Anyway it is fine for preventing the user from pressing enter only.
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

dubb

  • Swamp Rat
  • Posts: 1105
wrote this program...what can i do to make it better
« Reply #17 on: June 16, 2005, 02:23:00 PM »
Quote from: Se7en

Look up "initget" in the help file and return to me the "bit codes". I also want you to explain to me what "initget" does.  .......................
....Sorry gotta run now. I'll be back in a bit.


thats great...ill do some research and do some coding with this new "initget" function...thanks.

i seem to like this "intget" function a lil more...brb...with some code..thanks cab!

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
wrote this program...what can i do to make it better
« Reply #18 on: June 16, 2005, 04:21:57 PM »
OK, how about some overkill with the GETPOINT.
This will not return without a point whether picked or entered.
Entered points must be numbers separated with commas only, 2d or 3d OK.

Code: [Select]
;;;=======================[ getpoint_only.lsp ]=======================
;;; Author: Charles Alan Butler
;;; Version:  1.0 Jun. 16, 2005
;;; Purpose: Only get a point, do not return without one
;;; Sub_Routines: -None
;;; Arguments: -msg  string containing the prompt message
;;; Usage:  (getpoint_only msg)
;;; Returns: -the point picked
;;; Note: -User may enter a point at the command line 10,10 or 10,10,0
;;;====================================================================
(defun getpoint_only (msg / pt loop fuzz lastp)
  (setq loop  t
        fuzz  0.0001
        lastp (getvar "lastpoint")
  )
  (setq msg (strcat "\n" (cond (msg) ("Please select a point: "))))
  ;;  in case the user pressed escape last time here
  (if (< fuzz (distance '(1.9999 1.9999 0.0) (getvar "lastpoint")))
    (setvar "lastpoint" '(1.9999 1.9999 0.0))
    (setvar "lastpoint" '(1.8999 1.8999 0.0))
  )
  (while loop
    ;;  pt must meet the following requirements
    (if (and (setq pt (getpoint msg))    ; not nil
             (listp pt)                  ; must be a list
             ;;  do not allow bad numeric entry
             (< fuzz (distance pt (getvar "lastpoint")))
        )
      (setq loop nil) ; ok to exit, else
      (prompt "\nYou must select a point, please try again. ")
    )
  )
  (setvar "lastpoint" lastp)
  pt
)
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.