harilalmn 发表于 2022-7-6 09:31:49

创建面板-LISP

大家好,
我试图编写一个lisp程序,在给定的矩形区域中创建面板。
程序将要求用户提供矩形区域的宽度和高度。然后它询问面板宽度和面板高度。然后,它将提示输入矩形区域的左下角。完成。。。但有什么事让我踌躇不前。循环没有结束,它只会继续创建第一列。
可能有什么问题?
这是代码

(defun c:pn ()
(setq Width (getdist "\nWidth of area to be filled : "));Getting the total Width of the area to fill
(setq Height (getdist "\nHeight of area to be filled : "));Getting the total Height of the area to fill
(setq PanelWidth (getdist "\nWidth of the panel : "));Getting Width of the panel
(setq PanelHeight (getdist "\nHeight of the panel : "));Getting Height of the panel
(setq XNumberOfPanels (fix (/ Width PanelWidth)));Calculating Number of panels in X Direction
(setq YNumberOfPanels (fix (/ Height PanelHeight)));Calculating Number of panels in Y Direction
(setq Gap (/(- Width (* XNumberOfPanels PanelWidth)) (+ XNumberOfPanels 1)));Calculating Gap between the panels
(setq Pnt (getpoint "\nPick the Bottom Left Corner of the area to be filled : "));Getting the insertion Point
(setq YPnt Pnt)
(setq x 0);Counter for X Direction
(setq y 0);Counter for Y Direction
(setq COSMode (getvar 'OSMODE));Storing the value of Current OSMode
(setvar 'OSMODE 0);Switching Off all Object snaps
(setq Pnt (List (+(car Pnt) Gap) (+ (cadr Pnt) Gap)));Computing the starting point of the first Rectangle, which is away by one Gap distance along X and one Gap distance along Y
(while (<= x XNumberOfPanels)
   (while (<= y YNumberOfPanels)
      (setq Pnt1 (list (car Pnt) (+ (cadr pnt) Gap)))
      (setq Pnt2 (Polar Pnt1 (rad 90) PanelHeight))
      (setq Pnt3 (Polar Pnt2 (rad 0) PanelWidth))
      (setq Pnt4 (Polar Pnt3 (rad 270) PanelHeight))
      (command "PLine" Pnt1 Pnt2 Pnt3 Pnt4 "Close")
      (setq Pnt Pnt2)
      ;(alert (strcat "x is " (itoa x)))
      (setq x (+ x 1))
   );Ending While Loop for Y Direction
(setq Pnt (list (+ (car YPnt) Gap (* PanelWidth y)) (+ (cadr YPnt) Gap)))
(set y (+ y 1))
);Ending While Loop for X Direction

(setvar 'OSMODE COSMode);Setting OSMode back to initial value
(princ);Silent ending
)


;Function to convert Degrees to Radians
(defun rad (Ang)
(* 0.01745329 Ang)
)

MSasu 发表于 2022-7-6 09:49:32

BIGAL 发表于 2022-7-6 10:05:13

Couple of suggestion you can reduce number of picks to two you have 5 now if your panel is always drawn perpendicular etc
 
Pick Point lower left, pick upper right its easy to work out x dist and y dist and you have your start point also.
 
(setq pt1 (getpoint))   (car pt1) = pt1x   (cadr pt1) =pt1y
(setq pt2 (getpoint))    (car pt2) = pt2x   (cadr pt2) =pt2y
L= (distance pt1x pt2x)

harilalmn 发表于 2022-7-6 10:17:03

It is simple great idea... Thankyou BIGAL and MSASU....

harilalmn 发表于 2022-7-6 10:31:56

Atlast....,
Here is the final code which works like a charm...!!!
 

(defun c:dp () (setvar 'OSMODE 16383) (setq pt1 (getpoint "\nPick lower left corner of fill area : ")) (setq pt1x (car pt1)) (setq pt1y (cadr pt1)) (setq pt2 (getpoint"\nPick upper right corner of fill area : ")) (setq pt2x (car pt2)) (setq pt2y (cadr pt2)) (setq Width (abs(- pt2x pt1x))) (setq Height (abs(- pt2y pt1y))) (setq pnpt1 (getpoint "\nPick lower left corner of panel : ")) (setq pnpt1x (car pnpt1)) (setq pnpt1y (cadr pnpt1)) (setq pnpt2 (getpoint"\nPick upper right corner of panel : ")) (setq pnpt2x (car pnpt2)) (setq pnpt2y (cadr pnpt2)) (setq PanelWidth (abs(- pnpt2x pnpt1x))) (setq PanelHeight (abs(- pnpt2y pnpt1y))) (setq XDivisions (fix (/ Width PanelWidth))) (setq xgap (/ (- Width (* XDivisions PanelWidth)) (+ XDivisions 1))) (setq YDivisions (fix (/ Height PanelHeight))) (setq ygap (/ (- Height (* YDivisions PanelHeight)) (+ YDivisions 1))) (setq x 0) (setq y 0) (setvar 'OSMODE 0) (setq IP (List (+ (car pt1) xgap) (+ ygap (cadr pt1)))) (While (< x XDivisions)   (While (< y YDivisions)   (prompt (strcat "\n Number:" (itoa x)))   (setq p1 IP)   (setq p2 (polar p1 (rad 90) PanelHeight))   (setq p3 (polar p2 (rad 0) PanelWidth))   (setq p4 (polar p3 (rad 270) PanelHeight))   (command "PLINE" p1 p2 p3 p4 "CLOSE")   (setq y (+ 1 y))   (setq IP (List (car p2) (+ ygap (cadr p2))))   )   (setq y 0)   (setq x (+ 1 x))   (setq IP (List (+ (car pt1) (+ (* x xgap) xgap) (* x PanelWidth)) (+ ygap (cadr pt1)))) ) (setvar 'OSMODE 16383) (princ));Function to Convert Degrees to Radians(defun rad (Degrees) (* Degrees 0.0174532925))
页: [1]
查看完整版本: 创建面板-LISP