;;;series summation ;;; SUMSQR_1.LSP ;;; Sept 25, 2003 Takaya Iwamoto ;;; Copyright 2006 Takaya Iwamoto ;;; sum of squares demo using cubes ;;;**Note** ;;;There is some problem in executing this program. ;;;After all cubes are stacked up, explode command stops. ;;;To continue the operation , do the following steps ;;; Type explode then !ss3 and return ;;; Type finish_up ;;; (defun c:sumsqr_1 ( / last_1 last_2 ) (setup_sumsqr1) (setq nstep 2) (while (= (Yes_or_No "Go to next step ?") "_Y") (nxn_cube nstep) (IF (= JPG_YES 1) (make_jpg) ) (setq nstep (1+ nstep)) ) ;;;end of while loop (setq nstep (1- nstep)) (setq last_1 (entlast)) ;;ss1 is a selection set for the newly created pyramid (setq EL1 (entnext ref_cube1)) (while EL1 (ssadd EL1 ss1) (setq EL1 (entnext EL1)) ) ;;;make a copy of this pyramid at 2 places (alert "\nmake a copy of this pyramid \nto 2 places") (command "_.copy" ss1 "" pnt_z pnt_yz) (setq last_2 (entlast)) (IF (= JPG_YES 1) (make_jpg) ) (setq EL1 (entnext last_1)) (while EL1 (ssadd EL1 ss2) (setq EL1 (entnext EL1)) ) (command "_.copy" ss1 "" pnt_z pnt_xz) (IF (= JPG_YES 1) (make_jpg) ) ;;make a selection set ss3 for pyramid_3 (setq EL1 (entnext last_2)) (while EL1 (ssadd EL1 ss3) (setq EL1 (entnext EL1)) ) (alert "\nrotate Cube set2 90. deg around \nline pnt_yz pnt_xz2" ) (command "rotate3d" ss2 "" "2" pnt_yz pnt_xz2 90.) (command "rotate3d" ss2 "" "2" pnt_z pnt_yz 90.) (IF (= JPG_YES 1) (make_jpg) ) (alert "\nrotate Cube set3 90. deg around \nline pnt_x pnt_xz" ) (command "rotate3d" ss3 "" "2" pnt_x pnt_xz 90.) (alert "\nrotate cube set3 -90. deg\naround line pnt_xz pnt_xz2" ) (command "rotate3d" ss3 "" "2" pnt_xz pnt_xz2 -90.) (IF (= JPG_YES 1) (make_jpg) ) (alert "\nMove set2 to center") ;(command "_.move" ss2 "" pnt_yz '(0 1 15) ) (move_ent_3d ss2 pnt_yz '(0 1 15) 100 5 1 "0") (IF (= JPG_YES 1) (make_jpg) ) (alert "\nMove set3 to center") ;(command "_.move" ss3 "" pnt_xz '(0 0 16) ) (move_ent_3d ss3 pnt_xz '(0. 0. 16.) 100 5 1 "0") (IF (= JPG_YES 1) (make_jpg) ) (setq last_ent (entlast)) (alert "\nRun Explode ss3 \nfrom command line. explode !ss3 return\nThen issue finish_up command" ) ;(command "_.delay" 500) ;(command "explode" ss3 "");;;Why I cannot use "EXPLODE" command at this point ? ;(command "_.explode" SS3) ;(c:finish_up) ;(reset_sysvar) ) ;;;C:sumsqr_1 ;;; ;;; (defun setup_sumsqr1 ( ) (setup_sysvar) (setvar "PDMODE" 32) (setvar "PDSIZE" -3) (if (/= (getvar "UNDOCTL") 0) (command "_.undo" "_C" "_N") ) ;no und (command "shademode" "Gouraud") (setq ref_len 15.) (setq pnt_x (list ref_len 0 0) pnt_xy (list ref_len ref_len 0) pnt_xz (list ref_len 0 ref_len) pnt_y (list 0 ref_len 0) pnt_yz (list 0 ref_len ref_len) pnt_xz2 (list ref_len ref_len ref_len) pnt_z (list 0 0 ref_len) pnt_org '(0 0 0) ss1 nil ss1 (ssadd) ss2 nil ss2 (ssadd) ss3 nil ss3 (ssadd) ss4 nil ss4 (ssadd) ss5 nil ss5 (ssadd) ) (command "_.vpoint" "-18.3967,-26.1979,22.8342") ;;SW isometric view (command "_.point" pnt_x) (command "_.point" pnt_xy) (command "_.point" pnt_xz) (command "_.point" pnt_y) (command "_.point" pnt_yz) (command "_.point" pnt_xz2) (command "_.point" pnt_z) (make_line_1 "0" 8 pnt_org pnt_x) (make_line_1 "0" 8 pnt_org pnt_y) (make_line_1 "0" 8 pnt_org pnt_z) (my_block_insert "white_cube" (list 0 0 (1- ref_len)) 1.0) ;(my_block_insert "red_cube" (list 0 0 (1- ref_len)) 1.0) (setq ref_cube1 (entlast)) ;;this will be used as a reference later (setq ss1 (ssadd)) (ssadd ref_cube1 ss1) (command "_.zoom" "_EXTENT") (command "_.regen") (IF (= JPG_YES 1) (make_jpg) ) ) ;;;SETUP_sumsqr1 ;;; ;;;nxn_cube create N by N cubes using array command ;;; (defun nxn_cube (num / pnt_ins col_id cube_name base_cube) (setq pnt_ins (list 0 0 (- ref_len num))) (setq col_id (rem num 7) cube_name (cond ((= col_id 0) "cyan_cube") ((= col_id 1) "red_cube") ((= col_id 2) "yellow_cube") ((= col_id 3) "green_cube") ((= col_id 4) "white_cube") ((= col_id 5) "blue_cube") ((= col_id 6) "magenta_cube") (t "white_cube") ) ) (princ pnt_ins) (terpri) (my_block_insert cube_name pnt_ins 1.0) (setq base_cube (entlast)) ;;;use ARRAY command to make N x N (command "_.array" base_cube "" "_R" num num 1.0 1.0) ;;(command "_.array" base_cube "" "_R" num num 1.25 1.25);test ) ;;;NXN_CUBE ;;; (defun c:square_pyramid () (setup_ball) (setq nstep 2) (while (= (Yes_or_No "Go to next step ?") "_Y") (nxn_ball nstep) (IF (= JPG_YES 1) (make_jpg) ) (setq nstep (1+ nstep)) ) ;;;end of while loop ;(command "_.3dface" pnt_ll pnt_ur pnt_mid pnt_ll "") (reset_sysvar) ) ;;;C:square_pyramid ;;; ;;;make a division line using 3dface entity (defun c:cut_plate( ) (command "_.3dface" pnt_ll pnt_ur pnt_mid pnt_ll "") ) ;;; (defun setup_ball () (setup_sysvar) ;(setvar "PDMODE" 32) ;(setvar "PDSIZE" -3) (if (/= (getvar "UNDOCTL") 0) (command "_.undo" "_C" "_N") ) ;no und (command "shademode" "Gouraud") (setq s_fac (getreal "\nScale factor for distance between balls <4.0>")) (if (= s_fac nill) (setq s_fac 4.0)) (setq ref_len 15.) (setq z_up '(0 0 15) z_down '(0 0 0) x_left '(-15 0 0) x_right '(20 0 0) y_far '(0 20 0) y_near '(0 -15 0) pnt_ll '(-21.5 -20.5 -3) pnt_ur '(20.5 21.5 -3) pnt_mid '(-0.5 0.5 17) ) (make_line_1 "0" 1 x_left x_right) (setq x_axis (entlast)) (make_line_1 "0" 2 y_near y_far) (setq y_axis (entlast)) (make_line_1 "0" 3 z_down z_up) (setq z_axis (entlast)) ;;;;;SW isometric view "-18.3967,-26.1979,22.8342" (command "_.vpoint" "-64.8580,-52.1979,13.4587") (my_block_insert "white_ball" (list 0 0 ref_len ) 1.5 ) (setq ref_ball1 (entlast)) ;;this will be used as a reference later (command "_.zoom" "_EXTENT") (command "_.regen") (IF (= JPG_YES 1) (make_jpg) ) ) `;;;SETUP_ball ;;; ;;; ;;;nxn_ball create N by N cannon-ball pyramid using array command ;;; (defun nxn_ball (num / nm1 sqrt_2 unit_space unit_height array_space pnt_ins col_id ball_name base_ball) (setq nm1 (1- num) sqrt_2 (sqrt 2.) unit_space (* s_fac nm1) unit_height (* sqrt_2 unit_space) pnt_ins (list (- unit_space) (- unit_space) (- ref_len unit_height)) array_space (* 2. s_fac) ) (setq col_id (rem nm1 7) ball_name (cond ((= col_id 0) "white_ball") ((= col_id 1) "red_ball") ((= col_id 2) "yellow_ball") ((= col_id 3) "green_ball") ((= col_id 4) "cyan_ball") ((= col_id 5) "blue_ball") ((= col_id 6) "magenta_ball") (t "white_ball") ) ) (princ pnt_ins) (terpri) (my_block_insert ball_name pnt_ins 1.5) (setq base_ball (entlast)) ;;;use ARRAY command to make N x N (command "_.array" base_ball "" "_R" num num array_space array_space);;test ) ;;;NXN_ball ;;;************************************************************************ ;;; Function: PLT_3D ;;; ;;; get a point off the line segment defined by two end points and angle. ;;; ; utility routine ;; get a point along the line segment defined by two end points ;; ;; pnt_a pnt_b end points ;; loc_from_a ratio of distance from a to length AB ;; output point data (x,y) (defun PLT_3D (pnt_a pnt_b loc_from_a / xa xb ya yb za zb dist_ab dx dy dz xm ym zm ) (setq xa (car pnt_a) ya (cadr pnt_a) za (caddr pnt_a) xb (car pnt_b) yb (cadr pnt_b) zb (caddr pnt_b) dx (- xb xa) dy (- yb ya) dz (- zb za) xm (+ xa (* loc_from_a dx)) ym (+ ya (* loc_from_a dy)) zm (+ za (* loc_from_a dz)) ) (list xm ym zm) ) ;PLT_3D ;;;************************************************************************ ;;; Function: PLD_3d ;;; ;;; get a point off the line segment defined by two end points and angle. ;;; ; utility routine ;; get a point along the line segment defined by two end points ;; ;; pnt_a pnt_b end points ;; dist_from_a distance from a to b ;; ;; output point data (x,y) ;; (defun PLD_3D (pnt_a pnt_b dist_from_a / xa xb ya yb za zb dx dy dz xm ym zm loc_from_a ) (setq xa (car pnt_a) ya (cadr pnt_a) za (caddr pnt_a) xb (car pnt_b) yb (cadr pnt_b) zb (caddr pnt_b) dx (- xb xa) dy (- yb ya) dz (- zb za) loc_from_a (/ dist_from_a (distance pnt_a pnt_b)) xm (+ xa (* loc_from_a dx)) ym (+ ya (* loc_from_a dy)) zm (+ za (* loc_from_a dz)) ) ;(princ zm) (list xm ym zm) ) ;PLD_3D ;;; ;;;******************************************************************************************* ;;; move_ent_3d move a sinlge entity slowly ;;; (defun move_ent_3d (move_entity pnt_start pnt_end n_speed ndiv ncolor layer_name / nstep pnt_from pnt_to inc ) (setq inc (/ 1. ndiv) nstep 1 pnt_from pnt_start pnt_to (plt_3d pnt_start pnt_end inc) ) (repeat ndiv (slow_line_3d n_speed layer_name ncolor pnt_from pnt_to) (entdel (entlast)) ;;;test delete the trace (command "_.move" move_entity "" pnt_from pnt_to) (setq nstep (1+ nstep) pnt_from pnt_to pnt_to (plt_3d pnt_start pnt_end (* inc nstep)) ) ) ) ;;;move_ent_3d ;;; ;;; (defun c:finish_up () (slice_top) (command "_.delay" 1000) (alert "slice the cubes on the top layer") (copy_top) (IF (= JPG_YES 1) (make_jpg) ) (command "_.delay" 2000) ;(alert "flip over") (rotate_top) (IF (= JPG_YES 1) (make_jpg) ) (command "_.delay" 2000) ;(alert "move the sliced half layer") (display_result) (IF (= JPG_YES 1) (make_jpg) ) );finish_up ;;; ;;; ;;;slice_top (defun slice_top ( / pnt_sx pnt_sy pnt_sxy) (setq EL1 (entnext last_ent)) (while EL1 (ssadd EL1 ss4) (setq EL1 (entnext EL1)) ) (setq last_cube (entlast)) (setq N nstep np1 (1+ N) np3 (+ n 3) fz (- ref_len N) nphalf (+ fz N 0.5) pnt_sx (list 1 0 nphalf) pnt_sy (list 0 1 nphalf) pnt_sxy (list 1 1 nphalf) ) ;;slice this ss1 by a xy-plane defined by pnt_sx pnt_sy & pnt_sxy (command "_.slice" ss4 "" "3" pnt_sx pnt_sy pnt_sxy "_B") ;(command "slice" ss4 "" "3" pnt_sx pnt_sy pnt_sxy (list 0 0 20) ) ;;upper half is ss4, lower half is the previous entities (setq EL1 (entnext last_cube)) (while EL1 (ssadd EL1 ss5) (setq EL1 (entnext EL1)) ) ) ;;;slice_top ;;; ;;;COPY_TOP ;;; Copy ss5 and move it down by the distance 0.5 ;;; (defun copy_top (/ pnt_s pnt_e) (setq pnt_s (list 0 0 (+ 0.5 ref_len)) pnt_e (list 0 0 (+ 0.03 ref_len)) ) (command "_.copy" ss5 "" pnt_s pnt_e) ) ;;;COPY_TOP ;;;ROTATE_TOP ;;; (defun rotate_top ( / pnt_rot_0 pnt_rot_top pnt_end1 pnt_end2) (setq pnt_rot_0 (list n n 0) pnt_rot_top (list n n nphalf) pnt_end1 (list 0 1 (1+ ref_len)) pnt_end2 (list 0 1 ref_len) ) (alert "Rotate the sliced upper half 180 degrees horizontally" ) (command "_.rotate3d" ss5 "" "2" pnt_rot_0 pnt_rot_top 60.) (command "_.delay" 500) (command "_.rotate3d" ss5 "" "2" pnt_rot_0 pnt_rot_top 60.) (command "_.delay" 500) (command "_.rotate3d" ss5 "" "2" pnt_rot_0 pnt_rot_top 60.) (IF (= JPG_YES 1) (make_jpg) ) ;(alert "Rotate_top #1") (command "_.delay" 1000) (alert "move the top piece back to the square") (move_ent_3d ss5 pnt_rot_top pnt_end1 100 4 1 "0") (IF (= JPG_YES 1) (make_jpg) ) (alert "Move the piece down by the half_cube distance") (command "_.delay" 1000) (move_ent_3d ss5 pnt_end1 pnt_end2 100 3 1 "0") ) ;;; ;;;DISPLAY_RESULT ;;; (defun display_result ( / pnt_ytext pnt_xtext pnt_ztext pnt_result pnt_title result_text result_title ) (setq pnt_ytext (list -3 (+ 2.5 (* 0.5 N)) fz) pnt_xtext (list (- (* 0.5 n) 0.5) -3 fz) pnt_ztext (list np3 0 (- ref_len (* 0.5 N))) pnt_result (list 12 35 0) pnt_title (list 15.5 35.8 0) result_text "3 Sum = N (N+1) (N+1/2)" result_title "Sum of N squared:" ) (make_line_1 "0" 8 (list 0 np1 fz) (list -3 np1 fz)) (make_line_1 "0" 8 (list 0 0 fz) (list -3 0 fz)) (make_line_1 "0" 8 (list -3 np1 fz) (list -3 0 fz)) (make_line_1 "0" 8 (list 0 0 fz) (list 0 -3 fz)) (make_line_1 "0" 8 (list n 0 fz) (list n -3 fz)) (make_line_1 "0" 8 (list 0 -3 fz) (list n -3 fz)) (make_line_1 "0" 8 (list n 0 fz) (list np3 0 fz)) (make_line_1 "0" 8 (list n 0 nphalf) (list np3 0 nphalf)) (make_line_1 "0" 8 (list np3 0 fz) (list np3 0 nphalf)) (textdisplay "(N+1)" pnt_ytext 1.0 -90.) (textdisplay "N" pnt_xtext 1.0 0.) (textdisplay "(N+(1/2))" pnt_ztext 1.0 0.) (textdisplay result_title pnt_title 1.5 -35.5) (textdisplay result_text pnt_result 1.5 -35.5) ) ;;DISPLAY_RESULT ;;; Function: SLOW_LINE_3D ;;; ;;;draw a line slowly between two given points ;;; (defun SLOW_LINE_3D (n_section layer_name color_code pnt_begin pnt_end / n_section nstep x0 x1 delta_x y0 y1 delta_y z0 z1 delta_z pnt_cur x_next y_next z_next pnt_next ) (setq sblip (getvar "blipmode")) (setq scmde (getvar "cmdecho")) (setvar "blipmode" 0) (setvar "cmdecho" 0) (setq nstep 1) (setq x0 (car pnt_begin) x1 (car pnt_end) y0 (cadr pnt_begin) y1 (cadr pnt_end) z0 (caddr pnt_begin) z1 (caddr pnt_end) delta_x (/ (- x1 x0) (float n_section)) delta_y (/ (- y1 y0) (float n_section)) delta_z (/ (- z1 z0) (float n_section)) ) (setq pnt_cur pnt_begin) (while (< nstep n_section) (progn (setq x_next (+ x0 (* delta_x nstep)) y_next (+ y0 (* delta_y nstep)) z_next (+ z0 (* delta_z nstep)) pnt_next (list x_next y_next z_next) ) (command "_.line" pnt_begin pnt_next "") (entdel (entlast)) ) (setq nstep (+ 1 nstep)) ) (make_line_1 layer_name color_code pnt_begin pnt_end) ;slow displayed line (setvar "blipmode" sblip) (setvar "cmdecho" scmde) ) ;SLOW_LINE_3d ;;;************************************************************************