(defun c:ml() (princ "\n**** 二姊的第四個心願: 中央平行線 **** (測試階段)") ;Memorizing osnap status into the variable ostatus before we clear osnap (setq ostatus (getvar "osmode") ) (setvar "osmode" 512) ;setting osnap to capture nearest 512 (setq p1 (getpoint "\n1st point: _NEArest")) ; capture points (setq p2 (getpoint "\n2nd point: _NEArest")) ; (princ "\nInput point 1 : p1 ") (princ p1) ; just shows the data ; (princ "\nInput point 2 : p2 ") (princ p2) (Rectify) ; force perpendicular by rectifying the two input points (setq d (distance p1 p2)) ; distance between p1 and p2 (setq slope (angle p1 p2)) ; gradient of line p1p2 (setq x1 (car p1)) (setq y1 (cadr p1)) ; extract abcessae and ordinates (setq x2 (car p2)) (setq y2 (cadr p2)) ; ie. (x1,y1) and (x2,y2) are the ; two user input points. (setq midx (/ (+ x1 x2) 2)) ; calculate midpoint (setq midy (/ (+ y1 y2) 2)) ;Under the condition that the output line will ; (1) be half the distance p1p2 ; (2) be perpendicular to p1p2 ; (3) has its midpoint coincide with the midpoint of p1p2 ;the following calculation applies. ;The slope of p1 to one end of the output line (setq slopePlus (+ (atan 0.5) slope)) ;The slope of p1 to the other end of the output line (setq slopeMinus (- slope (atan 0.5))) ;Distance between p1 and either end of output line (setq hypotinuse (* d (/ (sqrt 5) 4))) ;Locate the coordinates of the two ends of the output line. ;A and B are the two ends of the output line. (setq A (polar p1 slopePlus hypotinuse)) (setq B (polar p1 slopeMinus hypotinuse)) (setvar "osmode" 0) ;shut off osnap to avoid interference (princ "\nDrawing mid parallel line into layer ") (princ (getvar "clayer")) (princ "\n") ;Finally, drawing the output line (command "line" A B "") ;Print out the data ; (princ "\nRectified point 1: p1 ") (princ p1) ; (princ "\nRectified point 2: p2 ") (princ p2) ; (princ "\nOutput line end 1: A ") (princ A) ; (princ "\nOutput line end 2: B ") (princ B) ; (princ "\np1p2 gradient = ") (princ slope) ; (princ "\np1A gradient = ") (princ slopePlus) ; (princ "\np2A gradient = ") (princ slopeMinus) (setvar "osmode" ostatus) ;Returning initial osnap settings (princ) ; to get rid of end nil ) ; end of defun m (defun Rectify() (setq layerStat (getvar "clayer")) ; stores user current layer ;Search the Layer section in the table for the layer _temp (setq checkExist (tblsearch "LAYER" "_temp")) ;Creates _temp layer if it doesn't already exist (if (= checkExist nil) (command "layer" "n" "_temp" "")) (command "layer" "ON" "_temp" "") ; open up temp layer (setvar "clayer" "_temp") ; set temp as current (command "line" "nea" p1 "per" p2 "") ; temp construction line (setq cLs (ssget "c" p1 p2)) ; construction line included in selection ; Screen for that line we have drawn into temp layer _temp (setq i 0) (repeat (sslength cLs) (progn (setq cL (entget (ssname cLs i) )) ; extract entity by entity (setq Lay (cdr (assoc 8 cL) )) ; obtain the layer of individual entity ; please note, although we created layer _temp in lower ; case, autoCAD automatically converts it into upper case, and so ; it is important that in our if statement to use the upper case. ; If we use the same small case _temp, autoCAD won't pick it up. (if (= Lay "_TEMP") (setq j i)) ; trap entity number of _TEMP line (setq i (+ i 1)) ) ;end progn ) ;end repeat (setq cL (entget (ssname cLs j) )) ; extract the _temp line entity (setq x1 (cadr (assoc 10 cL))) ; starting x (setq y1 (cadr (cdr (assoc 10 cL)))) ; starting y (setq x2 (cadr (assoc 11 cL))) ; ending x (setq y2 (cadr (cdr (assoc 11 cL)))) ; ending y (setq p1 (list x1 y1 0.0)) ; feed rectified values back into p1 and p2 (setq p2 (list x2 y2 0.0)) ;Search the Layer section in the table for the layer _pleasekillme ;(setq checkExist (tblsearch "LAYER" "_pleasekillme")) ;Creates _pleasekillme temp layer if it doesn't already exist ;(if (= checkExist nil) (command "layer" "n" "_pleasekillme" "")) ;Erase the temp line to prevent interference with future temp construction line. ;I tried (command "erase" cL "") at first but it didn't work. Apparently ;erase wanted the name of the entity rather than the content of the entity, so ;when I then tried (command "erase" (ssname cLs j) "") , it worked. (command "erase" (ssname cLs j) "") (setvar "clayer" layerStat) ; returns user layer ; Closing off temp layer ;(if (= layerStat (getvar "clayer")) (command "layer" "OFF" "_temp" "y" "") ) ;(if (/= layerStat (getvar "clayer")) (command "layer" "OFF" "_temp" "") ) (command "layer" "OFF" "_temp" "") ) ;end rectify ; M.LSP ; ; Input: ; User selects two points on two parallel walls (one point on each wall) ; ; Output: ; A line of half the length between the two input points will be drawn. ; Provided that the two walls are parallel, the output line will be ; parallel to both walls and runs through the mid point between these two walls. ; The mid point of the output line coincides with the mid point between ; input p1 p2. ; ; Note in the case when wall1 is not parallel to wall2 to start off with, ; the output line will be parallel to wall2 ; ; Future development: ; (1) Make the line fall between p1 and p2 rather than originating from p1 ; (2) Fix up the two ifs in the last closing temp section