Centerline of Rectangle

Centerline of Rectangle

jaimuthu
Advocate Advocate
1,920 Views
7 Replies
Message 1 of 8

Centerline of Rectangle

jaimuthu
Advocate
Advocate

(DEFUN C:RCT()
(COMMAND "-LAYER" "N" "CUTTILE" "")
(COMMAND "CLAYER" "CUTTILE")

(SETQ

P1(GETPOINT "\n PICK A FIRST CORNER :")

P2(GETCORNER P1 "\n PICK A OPPSITE CORNER :")

DI(DISTANCE P1 P2)

AN(ANGLE P1 P2)


P3(POLAR P1 AN (/ DI 2))

 

)
(COMMAND "-COLOR" "CYAN")

(COMMAND "RECTANGLE" P1 P2 )

(COMMAND "-COLOR" "WHITE")

(COMMAND "LINE" P1 P2 "")(SETQ OBJ1(ENTLAST))
)

 

IF i pick any two corner draw the line in midpoint of lines in rectangle, but this is not work if any help ?

 

 

0 Likes
1,921 Views
7 Replies
Replies (7)
Message 2 of 8

Kent1Cooper
Consultant
Consultant

That is not difficult, but:  There are always two possible Lines between the midpoints of opposite sides of a rectangle.  Do you always want the vertical one?  Do you always want the longer one?  [And if so, what if it's a square and they are of equal length?]  Do you want the User to choose which one?

Kent Cooper, AIA
0 Likes
Message 3 of 8

jaimuthu
Advocate
Advocate

i know little knowledge in programin in lisp plse help if you know program for this 

0 Likes
Message 4 of 8

jaimuthu
Advocate
Advocate

(DEFUN C:RD()
(SETQ
P1(GETPOINT "\n PICK IST CORNER :")
P2(GETCORNER P1 "\n PICK OPPSITE CORNER:")
DI(DISTANCE P1 P2)
ANG(ANGLE P1 P2)
P3(POLAR P1 ANG (/ DI 2))
CAL(SIN ANG)
FI(* CAL DI)
CA(ABS FI)
RT(RTOS CA 4 )
CCAL(COS ANG)
CFI(* CCAL DI)
CCA(ABS CFI)
CRT(RTOS CCA 4)
FI(STRCAT CRT "X" RT)

P4(POLAR P1 0 (/ CCA 2))

P5(POLAR P4 (/ PI 2) CA)
)

(COMMAND "RECTANGLE" P1 P2 )

(COMMAND "LINE" P4 P5 "")


)

)

 

I TRY THESE IT WILL BE WORK ON FIRSTCORNER IN LOWER LEFT SIDE  AND OTHER CORNER IS UPPER RIGHT SIDE ITS WORK FINE , BUT I  PICK CHANGE THE CORNERS ITS SHOULD NOT WORK PLSE HELP ANY 

0 Likes
Message 5 of 8

Kent1Cooper
Consultant
Consultant

It looks, if I'm reading your code correctly, as though the answer to my question in Post 2 is "always the vertical line."

 

There are ways to do that without all those angle & trigonometric calculations.  You can also omit asking for the corners beforehand, since the RECTANGLE command will do that.  That command makes a Polyline, always drawn with one of the horizontal edges first.  Even though it is not "curved," it counts as one of the classes of "curve" objects in (vlax-curve-...) functions.  For the purpose of those functions, a Polyline has Parameter values of integers at its vertices, starting with Parameter 0 at the beginning.  So you can do this, which uses no variables at all:

 

(vl-load-com); if not already loaded

(defun C:RD2 ()

  (command

    "_.rectangle" pause pause ; it will ask for the corners

    "_.line"

      "_none" (vlax-curve-getPointAtParam (entlast) 0.5); midpoint of 1st segment

      "_none" (vlax-curve-getPointAtParam (entlast) 2.5); midpoint of 3rd segment

      "" ; end Line

  ); command

); defun

 

If you want the horizontal mid-line instead, change the 0.5 to 1.5, and change the 2.5 to 3.5.

 

But if you need the corners saved for some other purpose, you can get the lengths of the sides much more concisely:

 

(DEFUN C:RD()
  (SETQ
    P1 (GETPOINT "\n PICK IST CORNER: ")
    P2 (GETCORNER P1 "\n PICK OPPOSITE CORNER: ")

    delta (mapcar '- p2 p1); XYZ difference between corners
;    CA (ABS (cadr delta)); Y-direction distance [not needed]
    CCA (ABS (car delta)); X-direction distance

  ); setq

  (COMMAND

    "RECTANGLE" P1 P2

    "LINE"

      "_none" (POLAR P1 (if (minusp (car delta)) pi 0) (/ CCA 2))

      "_none" (POLAR P2 (if (minusp (car delta)) 0 pi) (/ CCA 2))

      ""

  ); command

)

 

Some of your variables are never used [P3, CRT, FI].  If you do need the middle of the rectangle [P3], here's a shorter way to calculate it:

 

(mapcar '/ (mapcar '+ p1 p2) '(2 2 2))

Kent Cooper, AIA
0 Likes
Message 6 of 8

jaimuthu
Advocate
Advocate

It Work Fine .Thanks Kent Cooper Great Explain for this Thanks

0 Likes
Message 7 of 8

jaimuthu
Advocate
Advocate

(defun c:test (/ ss keys which)

(setq keys

"Horizontal / Vertical"
)

(initget keys)

(setq

which(getkword (strcat "(" keys ")" "<Horizontal>")

)

)

(if (= which "Horizontal")

(alert "kumar")

 

)

(if (= which "Vertical")

(alert "jaikumar")

 

)


)

___________________________________________________________

 

now i plan the horizontal or vertical line in if option it did not work the  default value , But i type the Horizontal Or Vertical Its work fine. 

0 Likes
Message 8 of 8

Kent1Cooper
Consultant
Consultant

There should not be a slash / between the keywords in the (initget) string.  Here are two ways to get a default value [also using different ways of reporting the result]:

 

(defun c:test1 (/ which)
  (initget "Horizontal Vertical")
  (setq which (getkword "Line direction [Horizontal/Vertical] <Horizontal>: "))
  (if (not which); User pressed Enter [nil return from (getkword)]
    (setq which "Horizontal"); then -- use default
  ); if
  (alert (if (= which "Horizontal") "kumar" "jaikumar"))
  ); if
); defun

 

(defun c:test2 (/ which)
  (initget "Horizontal Vertical")
  (setq which
    (cond
      ((getkword "Line direction [Horizontal/Vertical] <Horizontal>: ")); User entry [either option]
      ("Horizontal"); User pressed Enter [nil] -- use default
    ); cond
  ); setq
  (alert (cadr (assoc which '(("Horizontal" "kumar") ("Vertical" "jaikumar")))))
); defun

 

You can even have it remember your choice and offer that as the default on later use, rather than always offering the same default:

 

(defun c:test3 ()
  (initget "Horizontal Vertical")
  (setq which
    (cond
      ( (getkword
          (strcat
            "Line direction [Horizontal/Vertical] <"
            (cond (which) ("Horizontal")); offer previous choice if present, otherwise H initial default
            ">: "
          ); strcat
        ); getkword
      ); User-entry condition
      (which); User pressed Enter with previous choice -- use it
      ("Horizontal"); User pressed Enter without previous choice [first use] -- use initial default
    ); cond
  ); setq
  (alert (cadr (assoc which '(("Horizontal" "kumar") ("Vertical" "jaikumar")))))
); defun

 

NOTE that the 'which' variable is not localized as in the others -- it needs to remain when the routine is finished, for later default use.

 

Whichever way you choose to do it, if you're using the first suggested routine in Post 5, when it's drawing the Line, you can do this:

....

      "_none" (vlax-curve-getPointAtParam (entlast) (if (= which "Horizontal") 1.5 0.5))

      "_none" (vlax-curve-getPointAtParam (entlast) (if (= which "Horizontal") 3.5 2.5))

....

Kent Cooper, AIA
0 Likes