Home > Blockchain >  AutoLISP: Saving user chosen two points into variables when using the AutoCAD “rectangle” command
AutoLISP: Saving user chosen two points into variables when using the AutoCAD “rectangle” command

Time:03-02

Is there a way to save variables of the “first corner point” and “other corner point” when a user picks first point, then types in the length and width (with dynamic input on) when using the “Rectangle” command?

Example:

Command: RECTANGLE
Specify first corner point or [Chamfer/Elevation/Fillet/Thickness/Width]:
Specify other corner point or [Area/Dimensions/Rotation]: @20',15' 

I want to replace what I have below with the “Rectangle” command so user can type in length and width they need. The user will prefer the length and width to be whole numbers (Ex: 13, 15, 20, 23...etc) which can be done if using the rectangle command. Right now they have to draw a rectangle they want in whole numbers first. Then use the routine and snap to the corners. Hoping to combine all in one routine.

What I have now:

 (setq firstpick (getpoint "\nEnter first corner: "))
(setq secondpick (getcorner firstpick "\nEnter cross corner: "))
; Get all four corners of user drawn rectangle
(setq pt1 firstpick)
(setq pt3 secondpick)
(setq pt2 (list (car pt1) (cadr pt3)))
(setq pt4 (list (car pt3) (cadr pt1)))
; Get the Area drawn and save in variable “myrecarea”
 (setq mylength (distance pt1 pt2)); length
(setq mywidth (distance pt1 pt4)); width
(setq myrecarea (* mylength mywidth)); Get area of rectangle (length x width)

I want to replace with using the “Rectangle” command (if possible) so user can type in length and width. Not sure how to replace with rectangle command, extract those corner points and save as variables...

(setq firstpick (command "rectangle"))(?)
(setq secondpick (?)
; Get all four corners of user drawn rectangle
(setq pt1 firstpick)
(setq pt3 secondpick)
(setq pt2 (list (car pt1) (cadr pt3)))
(setq pt4 (list (car pt3) (cadr pt1)))
; Get the Area drawn and save in variable “myrecarea”
 (setq mylength (distance pt1 pt2)); length
(setq mywidth (distance pt1 pt4)); width
(setq myrecarea (* mylength mywidth)); Get area of rectangle (length x width)

*The Area size determines what is drawn later in the routine…

One last question, Is it possible to save everything drawn in a routine in a block? Not sure how the block naming would work if routine run several times in a single drawing without overwriting the original block.

CodePudding user response:

For the rectangle part, I tried this code below written by Kent1Cooper and it seems to work for what I need.

(defun C:RSCP () ; = Rectangle, Save Corner Points
  (command-s "_.rectang"); [must be without Fillet or Chamfer options]
  (setq
    pt1 (vlax-curve-getPointAtParam (entlast) 0)
    pt2 (vlax-curve-getPointAtParam (entlast) 1)
    pt3 (vlax-curve-getPointAtParam (entlast) 2)
    pt4 (vlax-curve-getPointAtParam (entlast) 3)
  ); setq
  (princ)
); defun

Thanks Kent!

  • Related