PDA

View Full Version : Misbehaving lisp.



Cadastrophic
25th Apr 2005, 11:49 pm
Would one of you who has experience with lisp routines mind casting an eye over this lisp for me please? The author didn't include his email address, so I can't contact him.
The problem is, I need to load the lisp afresh every time I want to use it. For some reason it unloads itself.
It also switches off all my OSNAPS and the routine doesn't seem to fully complete.
Maybe I'm doing something wrong.




;;;DJG_RCSprockets.lsp Sprocket Drafting Utility (c) 2005 Don Grauel


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;
;;;
;;; Filename: DJG_RCSprockets.lsp
;;; Author: Don Grauel
;;; First created: 11/22/99
;;; Last updated: 02/03/05
;;; Description: This function automates the creation of roller chain sprockets.
;;;
;;; References: ASME B29.1M-1993
;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;

(defun c:DJG_RCS (/ +*error* +attdia +attreq +blipmode +clayer +cmdecho +dimzin +osmode
txt tmp waiting N P Dr PD OD Ds R A B M U E W V F
pta ptb ptc pt00 pt01 pt02 pt03 pt04 pt05 pt06 pt07 pt08
circle_R circle_F circle_E circle_OD line_01 line_02 pline_01 pline_02 ss01)

(DJG_InitEnv)

(prompt "\nRoller Chain Sprocket Generator...")

(if (not DJG_RCS-chn) (setq DJG_RCS-chn "160")) ; set default, suggesting previous value if it exists
&#40;setq txt &#40;strcat "\nChain Number &#91;25/35/41/40/50/60/80/100/120/140/160/180/200/240&#93; <" DJG_RCS-chn ">&#58; "&#41;&#41;

&#40;initget "25 35 41 40 50 60 80 100 120 140 160 180 200 240"&#41;
&#40;setq tmp &#40;getkword txt&#41;&#41;
&#40;if &#40;/= tmp nil&#41; &#40;setq DJG_RCS-chn tmp&#41;&#41;

&#40;setvar "dimzin" 8&#41; ; suppress trailing zeros in default value prompt
&#40;if &#40;not DJG_RCS-N&#41; &#40;setq DJG_RCS-N 12&#41;&#41;
&#40;setq txt &#40;strcat "\nNumber of teeth <" &#40;rtos DJG_RCS-N&#41; ">&#58; "&#41;&#41;

&#40;setq waiting T&#41;
&#40;while waiting
&#40;initget 6&#41;
&#40;setq tmp &#40;getint txt&#41;&#41;

&#40;cond &#40; &#40;= tmp nil&#41; ; user accepted default value
&#40;setq N DJG_RCS-N waiting nil&#41;
&#41;

&#40; &#40;< 4 tmp 201&#41; ; user provided valid input
&#40;setq DJG_RCS-N tmp N tmp waiting nil&#41;
&#41;

&#40; T
&#40;prompt "\nValue must be between 5 and 200 inclusive."&#41;
&#41;
&#41;
&#41;

&#40;if &#40;not DJG_RCS-frm&#41; &#40;setq DJG_RCS-frm "N"&#41;&#41;
&#40;setq txt &#40;strcat "\nUse pointed-tooth form? &#91;Y/N&#93; <" DJG_RCS-frm ">&#58; "&#41;&#41;

&#40;initget "Y N"&#41;
&#40;setq tmp &#40;getkword txt&#41;&#41;
&#40;if &#40;/= tmp nil&#41; &#40;setq DJG_RCS-frm tmp&#41;&#41;

&#40;if &#40;not DJG_RCS-ipt&#41; &#40;setq DJG_RCS-ipt '&#40;0 0&#41;&#41;&#41;
&#40;setq txt &#40;strcat "&#40;" &#40;rtos &#40;car DJG_RCS-ipt&#41;&#41; "," &#40;rtos &#40;cadr DJG_RCS-ipt&#41;&#41; "&#41;"&#41;
txt &#40;strcat "\nSprocket center point <" txt ">&#58; "&#41;
&#41;

&#40;initget 0&#41;
&#40;setq tmp &#40;getpoint txt&#41;&#41;
&#40;if &#40;/= tmp nil&#41;
&#40;setq DJG_RCS-ipt &#40;list &#40;car tmp&#41; &#40;cadr tmp&#41;&#41; ; discarding z-coordinate from &#40;getpoint&#41;
pt00 DJG_RCS-ipt
&#41;
&#40;setq pt00 DJG_RCS-ipt&#41;
&#41;

&#40;cond &#40; &#40;= DJG_RCS-chn "25"&#41;
&#40;setq P 0.25 ; chain pitch
Dr 0.13 ; roller diameter
&#41;
&#41;

&#40; &#40;= DJG_RCS-chn "35"&#41;
&#40;setq P 0.375
Dr 0.2
&#41;
&#41;

&#40; &#40;= DJG_RCS-chn "41"&#41;
&#40;setq P 0.5
Dr 0.306
&#41;
&#41;

&#40; &#40;= DJG_RCS-chn "40"&#41;
&#40;setq P 0.5
Dr 0.312
&#41;
&#41;

&#40; &#40;= DJG_RCS-chn "50"&#41;
&#40;setq P 0.625
Dr 0.4
&#41;
&#41;

&#40; &#40;= DJG_RCS-chn "60"&#41;
&#40;setq P 0.75
Dr 0.469
&#41;
&#41;

&#40; &#40;= DJG_RCS-chn "80"&#41;
&#40;setq P 1.0
Dr 0.625
&#41;
&#41;

&#40; &#40;= DJG_RCS-chn "100"&#41;
&#40;setq P 1.25
Dr 0.75
&#41;
&#41;

&#40; &#40;= DJG_RCS-chn "120"&#41;
&#40;setq P 1.5
Dr 0.875
&#41;
&#41;

&#40; &#40;= DJG_RCS-chn "140"&#41;
&#40;setq P 1.75
Dr 1.0
&#41;
&#41;

&#40; &#40;= DJG_RCS-chn "160"&#41;
&#40;setq P 2.0
Dr 1.125
&#41;
&#41;

&#40; &#40;= DJG_RCS-chn "180"&#41;
&#40;setq P 2.25
Dr 1.406
&#41;
&#41;

&#40; &#40;= DJG_RCS-chn "200"&#41;
&#40;setq P 2.5
Dr 1.562
&#41;
&#41;

&#40; &#40;= DJG_RCS-chn "240"&#41;
&#40;setq P 3.0
Dr 1.875
&#41;
&#41;
&#41;

; converting to radians when using AutoLISP trig functions
&#40;setq PD &#40;/ P &#40;sin &#40;/ pi N&#41;&#41;&#41; ; pitch diameter
OD &#40;* P &#40;+ 0.6 &#40;/ &#40;cos &#40;/ pi N&#41;&#41; &#40;sin &#40;/ pi N&#41;&#41;&#41;&#41;&#41; ; outside diameter &#40;using cot=cos/sin identity&#41;
Ds &#40;+ &#40;* 1.005 Dr&#41; 0.003&#41; ; seating curve diameter
R &#40;/ Ds 2.0&#41; ; seating curve radius
A &#40;+ 35 &#40;/ 60.0 N&#41;&#41; ; &#91;degrees&#93;
B &#40;- 18 &#40;/ 56.0 N&#41;&#41; ; &#91;degrees&#93;
M &#40;* 0.8 Dr &#40;cos &#40;d2r A&#41;&#41;&#41;
U &#40;* 0.8 Dr &#40;sin &#40;d2r A&#41;&#41;&#41; ; using U in place of T to avoid overwriting AutoCAD symbol T
E &#40;+ &#40;* 1.3025 Dr&#41; 0.0015&#41;
W &#40;* 1.4 Dr &#40;cos &#40;/ pi N&#41;&#41;&#41;
V &#40;* 1.4 Dr &#40;sin &#40;/ pi N&#41;&#41;&#41;
F &#40;- &#40;* Dr
&#40;+ &#40;* 0.8 &#40;cos &#40;d2r B&#41;&#41;&#41;
&#40;* 1.4 &#40;cos &#40;d2r &#40;- 17 &#40;/ 64.0 N&#41;&#41;&#41;&#41;&#41;
-1.3025
&#41;
&#41;
0.0015
&#41;
&#41;

&#40;setq pta &#40;list &#40;car pt00&#41; &#40;+ &#40;cadr pt00&#41; &#40;/ PD 2&#41;&#41;&#41; ; center of arc w/radius R &#40;seating curve&#41;
ptb &#40;list &#40;+ &#40;car pta&#41; W&#41; &#40;- &#40;cadr pta&#41; V&#41;&#41; ; center of arc w/radius F
ptc &#40;list &#40;- &#40;car pta&#41; M&#41; &#40;+ &#40;cadr pta&#41; U&#41;&#41; ; center of arc w/radius E
pt01 &#40;list &#40;car pta&#41; &#40;+ &#40;cadr pta&#41; Ds&#41;&#41;
pt02 &#40;polar pt00
&#40;- &#40;/ pi 2&#41; &#40;/ pi N&#41;&#41;
&#40;+ &#40;/ PD 2&#41; Ds&#41;
&#41;
pt03 &#40;polar ptc &#40;d2r 285&#41; E&#41;
pt04 &#40;polar ptb &#40;d2r 105&#41; F&#41;
pt05 &#40;polar ptb &#40;d2r 270&#41; F&#41;
pt06 &#40;polar ptb &#40;d2r 60&#41; F&#41;
pt07 &#40;polar pta &#40;d2r 150&#41; R&#41;
pt08 &#40;polar pt02
&#40;- &#40;* pi 1.5&#41; &#40;/ pi N&#41;&#41;
&#40;* Ds 1.25&#41;
&#41;
&#41;

&#40;command "zoom" "c" pta &#40;* 4 Ds&#41;&#41;

&#40;DJG_LayerSet "-Visible" "4" "Continuous"&#41;

&#40;setvar "osmode" 0&#41;

&#40;command "circle" pta R&#41; ; draw circle w/radius R &#40;seating curve&#41;
&#40;setq circle_R &#40;entlast&#41;&#41;

&#40;command "circle" ptb F&#41; ; draw circle w/radius F
&#40;setq circle_F &#40;entlast&#41;&#41;

&#40;command "circle" ptc E&#41; ; draw circle w/radius E
&#40;setq circle_E &#40;entlast&#41;&#41;

&#40;setvar "osmode" 256&#41; ; tangent osnap

&#40;command "line" pt03 pt04 ""&#41; ; draw line tangent to circles of radii E & F
&#40;setq line_01 &#40;entlast&#41;&#41;

&#40;setvar "osmode" 0&#41;

&#40;command "trim" line_01 circle_R "" pt03 ""&#41; ; trim circle of radius E

&#40;if &#40;= DJG_RCS-frm "Y"&#41;
&#40;progn
&#40;command "line" pt02 pt08 ""&#41; ; draw tooth centerline
&#40;setq line_02 &#40;entlast&#41;&#41;

&#40;command "trim" line_01 line_02 "" pt05 ""&#41; ; trim circle of radius F

&#40;command "erase" line_02 ""&#41;
&#41;

&#40;progn
&#40;command "circle" pt00 "d" OD&#41; ; draw sprocket OD
&#40;setq circle_OD &#40;entlast&#41;&#41;

&#40;command "trim" line_01 circle_OD "" pt05 pt06 ""&#41; ; trim circle of radius F

&#40;command "mirror" circle_F "" pt00 pt02 ""&#41; ; arc temporarily needed to trim sprocket OD
&#40;setq tmp &#40;entlast&#41;&#41;

&#40;command "trim" circle_F tmp "" circle_OD ""&#41; ; trim sprocket OD
&#40;command "erase" tmp ""&#41; ; erase temporary arc
&#41;
&#41;

&#40;if &#40;= &#40;getvar "peditaccept"&#41; 1&#41;
&#40;command "pedit" circle_E "j" line_01 circle_F "" ""&#41;
&#40;command "pedit" circle_E "y" "j" line_01 circle_F "" ""&#41;
&#41;

&#40;setq pline_01 &#40;entlast&#41;&#41;

&#40;command "mirror" pline_01 "" pt00 pt01 ""&#41;
&#40;setq pline_02 &#40;entlast&#41;&#41;

&#40;command "trim" pline_01 pline_02 "" pt07 ""&#41; ; trim circle of radius R

&#40;if &#40;= DJG_RCS-frm "Y"&#41;
&#40;command "pedit" pline_01 "j" pline_02 circle_R "" ""&#41;
&#40;command "pedit" pline_01 "j" pline_02 circle_R circle_OD "" ""&#41;
&#41;
&#40;setq pline_01 &#40;entlast&#41;&#41;

&#40;command "zoom" "c" pt00 PD&#41;
&#40;command "zoom" ".75x"&#41;

&#40;command "array" pline_01 "" "p" pt00 N "" ""&#41;

&#40;setq tmp pline_01
ss01 &#40;ssadd&#41; ; initialize ss01 to null selection set
&#41;
&#40;repeat &#40;- N 1&#41;
&#40;ssadd &#40;setq tmp &#40;entnext tmp&#41;&#41; ss01&#41; ; add each polyline created by array command to selection set
&#41;
&#40;command "pedit" pline_01 "j" ss01 "" ""&#41;

&#40;command "rotate" pline_01 "" pt00 &#40;/ 180.0 N&#41;&#41; ; rotate sprocket profile so that tooth is at 90°

&#40;DJG_LayerSet "-Center" "5" "Center"&#41;

&#40;command "circle" pt00 "d" PD&#41; ; draw sprocket pitch circle

&#40;DJG_ResetEnv&#41;

&#40;princ&#41;

&#41; ;_c&#58;DJG_RCS

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;

&#40;defun d2r &#40;a&#41; &#40;* &#40;/ a 180.0&#41; pi&#41;&#41; ; convert from degrees to radians

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;

&#40;defun DJG_InitEnv &#40;&#41;
;;
;; Author&#58; Don Grauel
;; First created&#58; 12/02/04
;; Last updated&#58; 01/178/05
;; Description&#58; This function is used to initialize environment prior to running many custom routines.
;;
;;;;;;;;;;;;;;;;;;;;

&#40;setq +*error* *error*
*error* *DJG_Error01*
+attdia &#40;getvar "attdia"&#41;
+attreq &#40;getvar "attreq"&#41;
+blipmode &#40;getvar "blipmode"&#41;
+clayer &#40;getvar "clayer"&#41;
+cmdecho &#40;getvar "cmdecho"&#41;
+dimzin &#40;getvar "dimzin"&#41;
+osmode &#40;getvar "osmode"&#41;
&#41;

&#40;setvar "blipmode" 0&#41;
&#40;setvar "cmdecho" 0&#41;

&#40;command "undo" "mark"&#41;

&#40;princ&#41;

&#41; ;_ DJG_InitEnv

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;

&#40;defun DJG_ResetEnv &#40;&#41;
;;
;; Author&#58; Don Grauel
;; First created&#58; 12/02/04
;; Last updated&#58; 01/17/05
;; Description&#58; This function is used to reset environment after running many custom routines.
;;
;;;;;;;;;;;;;;;;;;;;

&#40;setq *error* +*error*&#41;
&#40;setvar "attdia" +attdia&#41;
&#40;setvar "attreq" +attreq&#41;
&#40;setvar "blipmode" +blipmode&#41;
&#40;setvar "clayer" +clayer&#41;
&#40;setvar "cmdecho" +cmdecho&#41;
&#40;setvar "dimzin" +dimzin&#41;
&#40;setvar "osmode" +osmode&#41;

&#40;princ&#41;

&#41; ;_ DJG_ResetEnv

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;

&#40;defun *DJG_Error01* &#40;msg&#41; ; custom error handler

&#40;command&#41; ; exit from any active commands
&#40;command&#41;
&#40;command&#41;

&#40;command "undo" "back"&#41;

&#40;if &#40;or &#40;= msg "Function cancelled"&#41;
&#40;= msg "quit / exit abort"&#41;
&#41;
&#40;princ&#41;
&#40;princ &#40;strcat "\nError&#58; " msg&#41;&#41;
&#41;

&#40;setq *error* +*error*&#41;
&#40;setvar "attdia" +attdia&#41;
&#40;setvar "attreq" +attreq&#41;
&#40;setvar "blipmode" +blipmode&#41;
&#40;setvar "clayer" +clayer&#41;
&#40;setvar "dimzin" +dimzin&#41;
&#40;setvar "osmode" +osmode&#41;

&#40;setvar "cmdecho" 0&#41;
&#40;command "undo" "mark"&#41;
&#40;setvar "cmdecho" +cmdecho&#41;

&#41; ;_ *DJG_Error01*

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;

&#40;defun DJG_LayerSet &#40;layname laycolor lintype / tblentry laystate tmp&#41;
;;
;; Author&#58; Don Grauel
;; First created&#58; 12/02/04
;; Last updated&#58; 01/28/05
;; Description&#58; This function sets the specified layer current, creating it if it doesn't already exist.
;;
;;;;;;;;;;;;;;;;;;;;

&#40;setq tblentry &#40;tblsearch "LAYER" layname&#41;
laystate 0
&#41;

&#40;if tblentry
&#40;progn
&#40;if &#40;/= &#40;logand &#40;cdr &#40;assoc 70 tblentry&#41;&#41; 1&#41; 0&#41; ; bitcode of 1 indicates layer is frozen
&#40;setq laystate &#40;+ laystate 1&#41;&#41;
&#41;

&#40;if &#40;/= &#40;logand &#40;cdr &#40;assoc 70 tblentry&#41;&#41; 4&#41; 0&#41; ; bitcode of 4 indicates layer is locked
&#40;setq laystate &#40;+ laystate 2&#41;&#41;
&#41;

&#40;if &#40;< &#40;cdr &#40;assoc 62 tblentry&#41;&#41; 0&#41; ; color number is negative when layer is turned off
&#40;setq laystate &#40;+ laystate 4&#41;&#41;
&#41;

&#40;command "layer" "thaw" layname "unlock" layname "on" layname "set" layname ""&#41;
&#41;

&#40;progn
&#40;command "layer" "make" layname "color" laycolor "" "ltype" lintype "" ""&#41;
&#40;setq laystate 8&#41;
&#41;
&#41;

&#40;setq tmp &#40;strcat "\nLayer \"" layname "\" has been "&#41;&#41;

&#40;cond &#40; &#40;= laystate 1&#41;
&#40;setq tmp &#40;strcat tmp "thawed."&#41;&#41;
&#41;

&#40; &#40;= laystate 2&#41;
&#40;setq tmp &#40;strcat tmp "unlocked."&#41;&#41;
&#41;

&#40; &#40;= laystate 3&#41;
&#40;setq tmp &#40;strcat tmp "thawed and unlocked."&#41;&#41;
&#41;

&#40; &#40;= laystate 4&#41;
&#40;setq tmp &#40;strcat tmp "turned on."&#41;&#41;
&#41;

&#40; &#40;= laystate 5&#41;
&#40;setq tmp &#40;strcat tmp "thawed and turned on."&#41;&#41;
&#41;

&#40; &#40;= laystate 6&#41;
&#40;setq tmp &#40;strcat tmp "unlocked and turned on."&#41;&#41;
&#41;

&#40; &#40;= laystate 7&#41;
&#40;setq tmp &#40;strcat tmp "thawed, unlocked, and turned on."&#41;&#41;
&#41;

&#40; &#40;= laystate 8&#41;
&#40;setq tmp &#40;strcat tmp "created."&#41;&#41;
&#41;
&#41;

&#40;if &#40;> laystate 0&#41; &#40;prompt tmp&#41;&#41;

&#40;command "color" "bylayer"&#41;

&#40;princ&#41;

&#41; ;_ DJG_LayerSet

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;

&#40;princ&#41;

Anton
1st Nov 2006, 04:42 pm
(setvar "osmode" 0) turns off osnap, to get it to turn on.

Change (setvar "osmode" 0) to (setvar "osmode" 1)


(setvar "osmode" 0) = Off osnap

(command "circle" pta R) ; draw circle w/radius R (seating curve)
(setq circle_R (entlast))

(command "circle" ptb F) ; draw circle w/radius F
(setq circle_F (entlast))

(command "circle" ptc E) ; draw circle w/radius E
(setq circle_E (entlast))

(setvar "osmode" 256) ; tangent osnap

(command "line" pt03 pt04 "") ; draw line tangent to circles of radii E & F
(setq line_01 (entlast))

(setvar "osmode" 1) = On osnap