Author Topic: Please check my new program door & suggestion me how to improve this program  (Read 3324 times)

0 Members and 1 Guest are viewing this topic.

Sam

  • Bull Frog
  • Posts: 201
dear all
Please check my new program door & suggestion me how to improve this program

& one problem on this code is only working in mm drg

Quote
(DEFUN C:DR (/ lu a b c d e f g pt pp pt1 pt2 p ang1 tz wl ds wt sw ang2
 pt3 pt4 ang3 pl2 pl3 pl4 pl5 pl6 pt5 pt6 pt7 pt8 pt9 pt10 pt11 pt13 pt14 pt15)
 (SETVAR "BLIPMODE" 0)
 (SETVAR "OSMODE" 512)
 (setq lu (getvar "lunits"))
 (if (= lu 2) (setq a 75 b 82 c 15 d 38 e 60 f 120 g 150)
 (setq a 3 b 3.5 c 0.5 d 1.5 e 2.5 f 5 g 6))
   (if (null DRlay)
    (progn
       (setq DRlay "DR")
       (setq DRlayer (tblsearch "layer" DRlay))
     (if (null DRlayer)
         (progn
           (setq DRlay (getstring "\nLayer name for TEXT : "))
           (setq DRclr (getstring (strcat "\nColor for " DRlay " layer: ")))
           (command "layer" "m" DRlay "c" DRclr "" "")
         )
       (prompt "\nDOOR ON DR LAYER")
     )
    )
   )
 (SETQ PT (entsel "\nPICK WALL LINE:"))
 (setq pp (CDR (ASSOC 11 (ENTGET (CAR PT)))))
 (setvar "osmode" 128)
 (SETQ PT1 (GETPOINT PP "\nEnter Insertion Point:"))
 (SETQ PT2 (GETPOINT PT1 "\nPick Opposite Wall Line:"))
 (SETVAR "OSMODE" 512)
 (SETQ p (GETpoint PT2 "\nPICK THE SIDE FOR OPENING:"))
 (setq ang1 (angle pt2 p))
 (IF (null DZ) (SETQ DZ "900"))
 (SETQ TZ (STRCASE (GETSTRING (STRCAT "\nENTER SIZE OF OPENING <" DZ ">: ")) t))
 (IF (/= TZ "") (SETQ DZ TZ))
 (SETVAR "BLIPMODE" 0)
 (setq wl (cdr (assoc 8 (entget (car pt)))))
 (SETQ DS (Atof DZ))
 (SETQ WT (DISTANCE PT1 PT2))
 (SETQ SW (- DS f))
 (SETQ ANG2 (ANGLE PT1 PT2))
 (SETQ PT3 (POLAR PT1 ANG1 DS))
 (SETQ PT4 (POLAR PT3 ANG2 WT))
 (SETQ ANG3 (ANGLE PT3 PT1))
 (SETQ PL2 (POLAR PT1 ANG1 a))
 (SETQ PL3 (POLAR PL2 ANG2 b))
 (SETQ PL4 (POLAR PL3 ANG3 c))
 (SETQ PL5 (POLAR PL4 ANG2 d))
 (SETQ PL6 (POLAR PL5 ANG3 e))
 (SETQ PT5 (POLAR PT1 ANG1 (/ DS 2)))
 (SETQ PT6 (POLAR PT5 ANG2 f))
 (SETQ PT7 (POLAR PL5 ANG1 SW))
 (SETQ PT12 PT7 PT13 PL5)  
 (SETVAR "OSMODE" 0)
 (COMMAND "BREAK" Pt "F" PT1 PT3)
 (COMMAND "BREAK" P "F" PT2 PT4)
 (COMMAND "LAYER" "s" WL "")
 (COMMAND "LINE" PT1 PT2 "")
 (COMMAND "LINE" PT3 PT4 "")
 (COMMAND "COLOR" "BYLAYER")
 (COMMAND "LAYER"  "t" drlay "on" drlay "s" drlay "")
 (COMMAND "PLINE" PT1 PL2 PL3 PL4 PL5 PL6 "")
 (COMMAND "MIRROR" PL2 "" PT5 PT6 "")
 (SETVAR "ORTHOMODE" 0)
 (SETQ PT11 (GETPOINT PT5 "\nPICK THE SIDE FOR SHUTTER:"))
 (IF (> (DISTANCE PT11 PL5)(DISTANCE PT7 PT11)) (SETQ PL5 PT7 PT12 PT13))
 (SETQ PT8 (POLAR PL5 (ANGLE PL5 PT6) d))
 (SETQ PT9 (POLAR PT8 ANG2 SW))
 (SETQ PT10 (POLAR PT9 (ANGLE PT6 PL5) d))
 (COMMAND "PLINE" PL5 PT8 PT9 PT10 PL5 "")
 (COMMAND "ARC" PT12 PT9 PT10)
 (COMMAND "CHPROP" "L" "" "LT" "HIDDEN2" "")
 (setq pt14 (polar pl3 ang1 (- ds g)))
 (setq pt15 (polar pl2 ang1 (- ds g)))
 (if (and (>= ds 100) (<= ds 750)) (command "line" pl3 pt14 ""))
 (if (>= ds 910) (command "line" pl2 pt15 ""))
 (if (<= ds 32) (command "line" pl3 pt14 ""))
 (if (and (>= ds 37) (< ds 100)) (command "line" pl2 pt15 ""))
)
« Last Edit: July 08, 2010, 08:17:59 AM by Sam »
Every time we waste electricity, we put our planet's future in the dark. Let's turn around our attiude and start saving power and our planet, before it's too late
http://www.theswamp.org/donate.html

Krushert

  • Seagull
  • Posts: 13679
  • FREE BEER Tomorrow!!
Re: Please check my new program door
« Reply #1 on: July 08, 2010, 08:25:36 AM »
For my uneducated-not a gruru opinion.

I would add at the begining of your code
Code: [Select]

  (vl-cmdf ".undo" "BEGIN")

and add this at the end of your code.
Code: [Select]

  (vl-cmdf ".undo" "END")
The reason for this is that all if the lisp can be undone at once instead of the user having to enter multiple undos.  

Also reset your variables at the end of your lisp. Like resetting the layer back to whatever it was prior to firing the Door lisp.
IN the begining
Code: [Select]
(setq clay (getvar "clayer"))and at the end
Code: [Select]
  (setvar "clayer" clay)  Ideally you should also have a error handler ( I don't but I should) to reset stuff your variables in case of bad user input

My two cent hacker opinion, however a guru will be along show you the right way.
I + XI = X is true ...  ... if you change your perspective.

I no longer CAD or Model, I just hang out here picking up the empties beer cans

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
Re: Please check my new program door
« Reply #2 on: July 08, 2010, 08:55:17 AM »
The code appears to work as expected, however, I might suggest several things to make it better ...

First there are several things you should do to improve the code:
block all the setq's into one call like so
Code: [Select]
  (setq wl (cdr (assoc 8 (entget (car pt))))
        DS (Atof DZ)
WT (DISTANCE PT1 PT2)
SW (- DS f)
ANG2 (ANGLE PT1 PT2)
PT3 (POLAR PT1 ANG1 DS)
PT4 (POLAR PT3 ANG2 WT)
ANG3 (ANGLE PT3 PT1)
PL2 (POLAR PT1 ANG1 a)
PL3 (POLAR PL2 ANG2 b)
PL4 (POLAR PL3 ANG3 c)
PL5 (POLAR PL4 ANG2 d)
PL6 (POLAR PL5 ANG3 e)
PT5 (POLAR PT1 ANG1 (/ DS 2))
PT6 (POLAR PT5 ANG2 f)
PT7 (POLAR PL5 ANG1 SW)
PT12 PT7 PT13 PL5)

Reduce the occurence of COMMAND in the code. The structure of the calls to the commands have changed considerably over the many releases and will likely change again in the future. This means that your code will likely cease to work as expected at some point in the future. Instead, it might be better to utilize ENTMAKE where practical and leave the command calls to those areas where coding adds considerable work.

I noticed you change several system variables in the program. It is advisable to store the original values of these variables and restore them at the end of the function. i.e.
Code: [Select]
(setq bmode (getvar "BLIPMODE")
        omode (getvar "OSMODE"))
(mapcar 'SETVAR '("BLIPMODE" "OSMODE") '(0 512))
;do stuff here
(mapcar 'SETVAR '("BLIPMODE" "OSMODE") '(bmode omode))

I noticed that during the running of the code, you set OSMODE to 512 and 128 respectively. This might work for you, but in my experience, that is very limiting on the user. I suggest changing the OSMODE only if it is imperitive to the function of the code. In this case, it does not appear to be. Perhaps some error checking to see if the user has any running osnaps on and if not, turn them on only where desired.

You don't have an error handler, this will leave the system in an unfamiliar state for the user if the program crashes or the user terminates it unexpectedly.

The code is currently only designed to work in mm, that is fine if that is all you are expecting to use it for. To determine if the drawing is indeed metric or imperial, you should check the value of MEASUREMENT, if that is 0, check the value of MEASUREINIT, if that is 0, then it is likely imperial and you can adjust your measurements accordingly. If MEASUREMENT is 1 then it is metric. Remember that MEASUREMENT overrides MEASUREINIT, so you will need to check both to be reasonably sure of the units. Alternately, if the user has set LUNITS to 4, it is likely an architectural drawing and probably will be imperial as metric drawings typically set LUNITS to 2, although that isn't necessarily the case all the time.

You have some variables that are global. I suggest making them either sufficiently obscure so they won't be mistakenly overwritten by another bit of code or change other code's expected operation. You can do this by storing the variables in the registry of by storing them in the ini file .. there are functions for both of these .. you could then make the function default to these layers between drawings and the user can simply click through (press enter or space bar) if they don't want to change them.

For ease of reading the code, I also suggest using a format that makes the variables readily identifiable, i.e. caps for lisp functions and lowercase for vars or vice-versa .. or whatever happens to suit you. There are some pretty comprehensive code formatting options in the vlide editor.

Good luck!
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie

Sam

  • Bull Frog
  • Posts: 201
Re: Please check my new program door
« Reply #3 on: July 08, 2010, 09:34:01 AM »
The code appears to work as expected, however, I might suggest several things to make it better ...

First there are several things you should do to improve the code:
block all the setq's into one call like so
Code: [Select]
  (setq wl (cdr (assoc 8 (entget (car pt))))
        DS (Atof DZ)
WT (DISTANCE PT1 PT2)
SW (- DS f)
ANG2 (ANGLE PT1 PT2)
PT3 (POLAR PT1 ANG1 DS)
PT4 (POLAR PT3 ANG2 WT)
ANG3 (ANGLE PT3 PT1)
PL2 (POLAR PT1 ANG1 a)
PL3 (POLAR PL2 ANG2 b)
PL4 (POLAR PL3 ANG3 c)
PL5 (POLAR PL4 ANG2 d)
PL6 (POLAR PL5 ANG3 e)
PT5 (POLAR PT1 ANG1 (/ DS 2))
PT6 (POLAR PT5 ANG2 f)
PT7 (POLAR PL5 ANG1 SW)
PT12 PT7 PT13 PL5)

I noticed you change several system variables in the program. It is advisable to store the original values of these variables and restore them at the end of the function. i.e.
Code: [Select]
[b](setq bmode (getvar "BLIPMODE")
        omode (getvar "OSMODE"))
(mapcar 'SETVAR '("BLIPMODE" "OSMODE") '(0 512))
;do stuff here
(mapcar 'SETVAR '("BLIPMODE" "OSMODE") '(bmode omode))[/b]

dear sir
thx for replay & u r time & suggestion
change code as per u suggest
how to my old osmode return
error r come in
Quote
dr ; error: bad argument value: AutoCAD variable value: BMODE

Quote
(DEFUN C:DR (/ lu a b c d e f g pt pp pt1 pt2 p ang1 tz wl ds wt sw ang2
 pt3 pt4 ang3 pl2 pl3 pl4 pl5 pl6 pt5 pt6 pt7 pt8 pt9 pt10 pt11 pt13 pt14 pt15)


 ;|(setq oldCM (getvar "CMDECHO")
      oldos (getvar "OSMODE")
)
(defun *error* (msg)
(if oldCM (setvar "CMDECHO" oldCM))
(if oldos (setvar "OSMODE" oldos))
(princ msg)
(princ)
)|;

    ;|(SETVAR "BLIPMODE" 0)
   (SETVAR "OSMODE" 512)|;
(setq bmode (getvar "BLIPMODE")
        omode (getvar "OSMODE"))
(mapcar 'SETVAR '("BLIPMODE" "OSMODE") '(0 512))
;do stuff here
(mapcar 'SETVAR '("BLIPMODE" "OSMODE") '(bmode omode))
 
 (setq lu (getvar "lunits"))
 (if (= lu 2) (setq a 75 b 82 c 15 d 38 e 60 f 120 g 150)
 (setq a 3 b 3.5 c 0.5 d 1.5 e 2.5 f 5 g 6))
   (if (null DRlay)
    (progn
       (setq DRlay "DR")
       (setq DRlayer (tblsearch "layer" DRlay))
     (if (null DRlayer)
         (progn
           (setq DRlay (getstring "\nLayer name for TEXT : "))
           (setq DRclr (getstring (strcat "\nColor for " DRlay " layer: ")))
           (command "layer" "m" DRlay "c" DRclr "" "")
         )
       (prompt "\nDOOR ON DR LAYER")
     )
    )
   )
 (SETQ PT (entsel "\nPICK WALL LINE:"))
 (setq pp (CDR (ASSOC 11 (ENTGET (CAR PT)))))
 (setvar "osmode" 128)
 (SETQ PT1 (GETPOINT PP "\nEnter Insertion Point:"))
 (SETQ PT2 (GETPOINT PT1 "\nPick Opposite Wall Line:"))
 (SETVAR "OSMODE" 512)
 (SETQ p (GETpoint PT2 "\nPICK THE SIDE FOR OPENING:"))
 (setq ang1 (angle pt2 p))
 (IF (null DZ) (SETQ DZ "900"))
 (SETQ TZ (STRCASE (GETSTRING (STRCAT "\nENTER SIZE OF OPENING <" DZ ">: ")) t))
 (IF (/= TZ "") (SETQ DZ TZ))
 (SETVAR "BLIPMODE" 0)
;----------old one
  ;|(setq wl (cdr (assoc 8 (entget (car pt)))))
 (SETQ DS (Atof DZ))
 (SETQ WT (DISTANCE PT1 PT2))
 (SETQ SW (- DS f))
 (SETQ ANG2 (ANGLE PT1 PT2))
 (SETQ PT3 (POLAR PT1 ANG1 DS))
 (SETQ PT4 (POLAR PT3 ANG2 WT))
 (SETQ ANG3 (ANGLE PT3 PT1))
 (SETQ PL2 (POLAR PT1 ANG1 a))
 (SETQ PL3 (POLAR PL2 ANG2 b))
 (SETQ PL4 (POLAR PL3 ANG3 c))
 (SETQ PL5 (POLAR PL4 ANG2 d))
 (SETQ PL6 (POLAR PL5 ANG3 e))
 (SETQ PT5 (POLAR PT1 ANG1 (/ DS 2)))
 (SETQ PT6 (POLAR PT5 ANG2 f))
 (SETQ PT7 (POLAR PL5 ANG1 SW))
 (SETQ PT12 PT7 PT13 PL5) |;
;---------------------------------------------

  ;----Keith suggestion
  (setq wl (cdr (assoc 8 (entget (car pt))))
        DS (Atof DZ)
   WT (DISTANCE PT1 PT2)
   SW (- DS f)
   ANG2 (ANGLE PT1 PT2)
   PT3 (POLAR PT1 ANG1 DS)
   PT4 (POLAR PT3 ANG2 WT)
   ANG3 (ANGLE PT3 PT1)
   PL2 (POLAR PT1 ANG1 a)
   PL3 (POLAR PL2 ANG2 b)
   PL4 (POLAR PL3 ANG3 c)
   PL5 (POLAR PL4 ANG2 d)
   PL6 (POLAR PL5 ANG3 e)
   PT5 (POLAR PT1 ANG1 (/ DS 2))
   PT6 (POLAR PT5 ANG2 f)
   PT7 (POLAR PL5 ANG1 SW)
   PT12 PT7 PT13 PL5)
  ;-------------------------------------------

 
 (SETVAR "OSMODE" 0)
 (COMMAND "BREAK" Pt "F" PT1 PT3)
 (COMMAND "BREAK" P "F" PT2 PT4)
 (COMMAND "LAYER" "s" WL "")
 (COMMAND "LINE" PT1 PT2 "")
 (COMMAND "LINE" PT3 PT4 "")
 (COMMAND "COLOR" "BYLAYER")
 (COMMAND "LAYER"  "t" drlay "on" drlay "s" drlay "")
 (COMMAND "PLINE" PT1 PL2 PL3 PL4 PL5 PL6 "")
 (COMMAND "MIRROR" PL2 "" PT5 PT6 "")
 (SETVAR "ORTHOMODE" 0)
 (SETQ PT11 (GETPOINT PT5 "\nPICK THE SIDE FOR SHUTTER:"))
 (IF (> (DISTANCE PT11 PL5)(DISTANCE PT7 PT11)) (SETQ PL5 PT7 PT12 PT13))
 (SETQ PT8 (POLAR PL5 (ANGLE PL5 PT6) d))
 (SETQ PT9 (POLAR PT8 ANG2 SW))
 (SETQ PT10 (POLAR PT9 (ANGLE PT6 PL5) d))
 (COMMAND "PLINE" PL5 PT8 PT9 PT10 PL5 "")
 (COMMAND "ARC" PT12 PT9 PT10)
 (COMMAND "CHPROP" "L" "" "LT" "HIDDEN2" "")
 (setq pt14 (polar pl3 ang1 (- ds g)))
 (setq pt15 (polar pl2 ang1 (- ds g)))
 (if (and (>= ds 100) (<= ds 750)) (command "line" pl3 pt14 ""))
 (if (>= ds 910) (command "line" pl2 pt15 ""))
 (if (<= ds 32) (command "line" pl3 pt14 ""))
 (if (and (>= ds 37) (< ds 100)) (command "line" pl2 pt15 ""))
)
Every time we waste electricity, we put our planet's future in the dark. Let's turn around our attiude and start saving power and our planet, before it's too late
http://www.theswamp.org/donate.html

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
Ah, the error was my fault ... the line in error should have been ...
Code: [Select]
  (mapcar 'setvar '("BLIPMODE" "OSMODE") (list (BMODE OMODE)))

Also, that needs to be one of the last things in the code ... I'll post a revision in a bit
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
After further review, I have noticed a potential problem ... you ask for a color, however, if the user enters an invalid color, the program will fail. i.e. misspelling the color name.

It would be good to look at the CSTOCI lisp function used almost universally and see if that will suit your needs to ensure the color is correct, also, you can look into using the ACAD_COLORDLG to return the color .. and show it according to the value of FILEDIA.
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie

Sam

  • Bull Frog
  • Posts: 201
Ah, the error was my fault ... the line in error should have been ...
Code: [Select]
  (mapcar 'setvar '("BLIPMODE" "OSMODE") (list (BMODE OMODE)))

Also, that needs to be one of the last things in the code ... I'll post a revision in a bit
dear sir
thx
vlide is checking is ok
error is come
command: dr ; error: bad function: 0
Every time we waste electricity, we put our planet's future in the dark. Let's turn around our attiude and start saving power and our planet, before it's too late
http://www.theswamp.org/donate.html

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Haven't looked at any code but,

Code: [Select]
(mapcar 'setvar '("BLIPMODE" "OSMODE") (list (BMODE OMODE)))

Should perhaps be:

Code: [Select]
(mapcar 'setvar '("BLIPMODE" "OSMODE") (list BMODE OMODE))

Sam

  • Bull Frog
  • Posts: 201
Haven't looked at any code but,

Code: [Select]
(mapcar 'setvar '("BLIPMODE" "OSMODE") (list (BMODE OMODE)))

Should perhaps be:

Code: [Select]
[b](mapcar 'setvar '("BLIPMODE" "OSMODE") (list BMODE OMODE))[/b]
dear sir
thx for reply
Command: ; error: malformed list on input
Every time we waste electricity, we put our planet's future in the dark. Let's turn around our attiude and start saving power and our planet, before it's too late
http://www.theswamp.org/donate.html

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
That means your are missing a right paren ")".

Sam

  • Bull Frog
  • Posts: 201
That means your are missing a right paren ")".
THX SIR
ANY SUGGESTION ME TO IMPROVE THIS CODE
Every time we waste electricity, we put our planet's future in the dark. Let's turn around our attiude and start saving power and our planet, before it's too late
http://www.theswamp.org/donate.html

dgorsman

  • Water Moccasin
  • Posts: 2437
1. Use blank lines to block your code into logical groups of functions.  This will greatly improve readability.

2. Comment your code.  It may be months or years before you have to look at it again, comments that outline the program flow means you won't have to figure it out line-by-line every time.  It also makes it easier when requesting help from others who are not familiar with what its doing.

3. Use descriptive variable names.  "a" "b" etc. doesn't mean much but "start_angle" and "end_angle" would.  This used to be done to save on memory but on even remotely modern computers its a non-issue.

4. Comment your code.

5. Develop a standard style and stick with it, like writing everyting in lower case except for variable names, command strings, etc.

6. Comment your code.

7. Use a descriptive command name rather than "DR".  Some other users might want to use DR for a different command.  Use the PGP to associate "DR" to the command you create.  You will also run short of logical 2 or 3 letter combinations in short order.

8. Comment your code (see a pattern?)

9. Add additional comments above your function call that outlines what the function is, what it does, and any assumptions it makes or additional settings it relies on.  Like other comments, this will make it easier to work with later.
If you are going to fly by the seat of your pants, expect friction burns.

try {GreatPower;}
   catch (notResponsible)
      {NextTime(PlanAhead);}
   finally
      {MasterBasics;}

Sam

  • Bull Frog
  • Posts: 201
1. Use blank lines to block your code into logical groups of functions.  This will greatly improve readability.

2. Comment your code.  It may be months or years before you have to look at it again, comments that outline the program flow means you won't have to figure it out line-by-line every time.  It also makes it easier when requesting help from others who are not familiar with what its doing.

3. Use descriptive variable names.  "a" "b" etc. doesn't mean much but "start_angle" and "end_angle" would.  This used to be done to save on memory but on even remotely modern computers its a non-issue.

4. Comment your code.

5. Develop a standard style and stick with it, like writing everyting in lower case except for variable names, command strings, etc.

6. Comment your code.

7. Use a descriptive command name rather than "DR".  Some other users might want to use DR for a different command.  Use the PGP to associate "DR" to the command you create.  You will also run short of logical 2 or 3 letter combinations in short order.

8. Comment your code (see a pattern?)

9. Add additional comments above your function call that outlines what the function is, what it does, and any assumptions it makes or additional settings it relies on.  Like other comments, this will make it easier to work with later.
dear sir
thx for help
Every time we waste electricity, we put our planet's future in the dark. Let's turn around our attiude and start saving power and our planet, before it's too late
http://www.theswamp.org/donate.html