Author Topic: line from a point perpendicular to a line  (Read 31072 times)

0 Members and 1 Guest are viewing this topic.

DEVITG

  • Bull Frog
  • Posts: 479
line from a point perpendicular to a line
« on: June 01, 2004, 05:02:49 PM »
I had seen this topic before , but I can not find it.


How can I draw a line from a point perpendicular to a line .

all in Lisp.

pseudocode
select the line by ENTSEL
select the point by GETPOINT
draw a line from point to line perpendicular to the line selected

Thanks in advance.
Location @ Córdoba Argentina Using ACAD 2019  at Window 10

ronjonp

  • Needs a day job
  • Posts: 7526
line from a point perpendicular to a line
« Reply #1 on: June 01, 2004, 06:36:45 PM »
Code: [Select]
(Defun C:PERPL (/ SA SB SNP OM OS PT1 PT2)
;draws lines perpendicular from a starting point
  (setvar "cmdecho" 0)
  (setq
    SA (getvar "snapang")
    SB (getvar "snapbase")
    SNP (getvar "snapmode")
    OM (getvar "orthomode")
    OS (getvar "osmode")
    PT1 (osnap (getpoint
"\nPick point on line to draw perpendicular from: "
      )
      "nea"
)
  )
  (setvar "osmode" 0)
  (setq PT2 (osnap PT1 "end"))
  (if (equal PT1 PT2)
    (setq PT2 (osnap PT1 "MID"))
  )
  (command ".snap" "r" PT1 PT2)
  (setvar "snapmode" 0)
  (setvar "orthomode" 1)
  (prompt "\nto point:")
  (command ".pline" PT1 pause "")
  (setvar "snapang" SA)
  (setvar "snapbase" SB)
  (setvar "snapmode" SNP)
  (setvar "orthomode" OM)
  (setvar "osmode" OS)
  (setvar "cmdecho" 1)
  (princ)
) ; end perpl.lsp
(c:perpl)

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

DEVITG

  • Bull Frog
  • Posts: 479
line from a point perpendicular to a line
« Reply #2 on: June 01, 2004, 07:35:04 PM »
Hi Ronjop, it is not what I need

In the drawing  I pick for the line , and then pick the point
after it I need to draw , no more mouse movement , a line FROM the point to the line in perperndicular
Like I draw from a point to a OSNAP PER to line.

But no pointer movement
 :oops:
Location @ Córdoba Argentina Using ACAD 2019  at Window 10

Jeff_M

  • King Gator
  • Posts: 4087
  • C3D user & customizer
line from a point perpendicular to a line
« Reply #3 on: June 01, 2004, 08:26:06 PM »
Devitg,

Here's one that does this.
Code: [Select]

(defun c:prp2ln (/ ANG INTPT LINE PT1 PT2 PT3 PT4)
  (setq old-os (getvar "osmode"))
  (setvar "osmode" 0)
  (and (setq line (entsel "\nSelect line: "))
       (setq line (entget (car line)))
       (setq pt1 (cdr (assoc 10 line))
    pt2 (cdr (assoc 11 line))
    )
       (setq pt3 (getpoint "\nSelect point to draw perpendicular from: "))
       (setq ang (angle pt1 pt2))
       (setq pt4 (polar pt3 (+ ang (/ pi 2)) 1))
       (setq intPt (inters pt1 pt2 pt3 pt4 nil))
       (command "line" pt3 intPt "")
       )
  (setvar "osmode" old-os)
  (princ)
  )


Jeff

SpeedCAD

  • Guest
line from a point perpendicular to a line
« Reply #4 on: June 01, 2004, 10:45:12 PM »
This too:

Code: [Select]
(defun c:perLinea
       (/ vla-linea punto p1 p2 angulo pPer dist seleccion *osmode)
  (setq *osmode (getvar "osmode"))
  (setq vla-linea (vlax-ename->vla-object
   (car (entsel "\nSelecione línea: "))
 )
punto  (getpoint "\nSelecione punto para perpendicularidad: ")
p1  (vlax-safearray->list
   (vlax-variant-value
     (vla-get-startpoint vla-linea)
   )
 )
p2  (vlax-safearray->list
   (vlax-variant-value
     (vla-get-endpoint vla-linea)
   )
 )
angulo  (angle p1 p2)
pPer  (inters
   p1
   p2
   punto
   (polar punto (+ angulo (/ pi 2)) 1)
   nil
 )
dist  (distance punto Pper)
  )
  (vla-addline
    (vla-objectidtoobject
      (vla-get-document vla-linea)
      (vla-get-ownerid vla-linea)
    )
    (vlax-3d-point punto)
    (vlax-3d-point pPer)
  )
  (setvar "osmode" 512)
  (while (and (setq seleccion (getpoint "\nSeleccione linea: "))
     (equal (ssname (ssget seleccion) 0)
    (vlax-vla-object->ename vla-linea)
     )
)
    (vla-addline
      (vla-objectidtoobject
(vla-get-document vla-linea)
(vla-get-ownerid vla-linea)
      )
      (vlax-3d-point seleccion)
      (vlax-3d-point
(polar seleccion (+ (angle punto pPer) pi) dist)
      )
    )
  ) ;_while
  (setvar "osmode" *osmode)
) ;_defun

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
line from a point perpendicular to a line
« Reply #5 on: June 02, 2004, 12:03:28 AM »
Interesting Jeff... that is just about how I would have done it...
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie

Jeff_M

  • King Gator
  • Posts: 4087
  • C3D user & customizer
line from a point perpendicular to a line
« Reply #6 on: June 02, 2004, 12:07:59 PM »
Actually, Keith, after I posted that I decided that it would be best to entmake the line, removing the need to store, change, reset the osnaps. So here's my revised code:
Code: [Select]

(defun c:perp2ln (/ ANG INTPT LINE PT1 PT2 PT3 PT4)
  (and (setq line (entsel "\nSelect line: "))
       (setq line (entget (car line)))
       (setq pt1 (cdr (assoc 10 line))
    pt2 (cdr (assoc 11 line))
    )
       (setq pt3 (getpoint "\nSelect point to draw perpendicular from: "))
       (setq ang (angle pt1 pt2))
       (setq pt4 (polar pt3 (+ ang (/ pi 2)) 1))
       (setq intPt (inters pt1 pt2 pt3 pt4 nil))
       (entmake (list '(0 . "LINE")
     (cons 10 pt3)
     (cons 11 intPt)
     )
)
       )
  (princ)
  )

Jeff

DEVITG

  • Bull Frog
  • Posts: 479
line from a point perpendicular to a line
« Reply #7 on: June 02, 2004, 12:49:26 PM »
Thanks  all you for your help .

 :D  :)  :lol:  :o  :wink:  :idea:  :!:
Location @ Córdoba Argentina Using ACAD 2019  at Window 10

DEVITG

  • Bull Frog
  • Posts: 479
line from a point perpendicular to a line
« Reply #8 on: June 02, 2004, 02:29:33 PM »
I made this change to draw multiple perpendicular lines to a selected line

Code: [Select]

;;my OS and #oldos FUNcTIONS

(DEFUN OS (VAL)  ; A FUNCTION TO CHANGE OSMODE  BY "VAL" AS FOLLOW
(setq oldos   (getvar "osmode"))
;;;0  NONe
;;;1  ENDpoint
;;;2  MIDpoint
;;;4  CENter
;;;8  NODe
;;;16  QUAdrant
;;;32  INTersection
;;;64  INSertion
;;;128  PERpendicular
;;;256  TANgent
;;;512  NEArest
;;;1024  QUIck
;;;2048  APParent Intersection
;;;4096    EXTension
;;;8192    PARallel
  ;; IF VAL = 1+2+4 = 7 IT WIL DO END MID AND CENT

  (SETVAR "OSMODE" VAL)
) ;_ END OS FUNCTION

(defun #oldos ( )
  (setvar "osmode" oldos)
  )
;;**********************************************************

(defun c:prp2ln (/ ANG INTPT LINE PT1 PT2 PT3 PT4)
  (setq old-os (getvar "osmode"))
  (setvar "osmode" 0)
  (and (setq line (entsel "\nSelect line: "))
       (setq line (entget (car line)))
       (setq pt1 (cdr (assoc 10 line))
        pt2 (cdr (assoc 11 line))
        )
       (os 8)
       
  (WHILE
(OS 8)
    (setq pt3 (getpoint "\nSelect point to draw perpendicular from: "))
       (setq ang (angle pt1 pt2))
       (setq pt4 (polar pt3 (+ ang (/ pi 2)) 1))
       (setq intPt (inters pt1 pt2 pt3 pt4 nil))
(os 0)
       (command "line" pt3 intPt "")
       ))
 
  (setvar "osmode" old-os)
  (princ)
  )
(c:prp2ln)


Add the while to select points until need , and the osmode 8 to osnap on node.
Location @ Córdoba Argentina Using ACAD 2019  at Window 10

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
line from a point perpendicular to a line
« Reply #9 on: June 06, 2004, 06:36:33 PM »
I created a similar routine about 6 months ago, but Jeff's
code is much better so I adopted his code for my routine.
I use it to create elevations, drawing lines from points on the
plan view to a base line. I rotate the ucs to each elevation so
i needed to include a correction for that condition.

 DEVITG
 My routine repeats as yours does. Although if you use the ENTMAKE
 you will not need to switch OSMODE on and off.
 
 Here it is.

Code: [Select]
(defun c:LnRay (/ ang intpt ent p1 p2 p3 p4)
  (if (setq ent (entsel "\nSelect base line: "))
    (if (= "LINE" (cdr (assoc 0 (setq ent (entget (car ent))))))
      (progn
        (setq p1  (cdr (assoc 10 ent))
              p2  (cdr (assoc 11 ent))
              ang (angle p1 p2)
        )
        (while
          (setq p3
                 (getpoint
                   "\nSelect point to draw perpendicular from, Enter to quit: "
                 )
          )
           (setq p3 (trans p3 1 0)) ; adjust for non-World UCS
           (setq p4 (polar p3 (+ ang (/ pi 2)) 100))
           (setq intpt (inters p1 p2 p3 p4 nil))
           (entmake (list '(0 . "LINE")
                          (cons 10 p3)
                          (cons 11 intpt)
                    )
           )
        ) ; end while
      ) ; progn
      (prompt "\nObject is not a LINE.")
    ) ; endif
    (prompt "\nNothing Selected.")
  ) ; endif
  (princ)
)

(prompt "\nElev Lines loaded, type LnRay to run")
(princ)
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

DEVITG

  • Bull Frog
  • Posts: 479
perp line from a point to a line
« Reply #10 on: June 06, 2004, 07:46:57 PM »
Hi Cab , would you please , explain the pourpouse of using
Quote
(setq p3 (trans p3 1 0)) ; adjust for non-World UCS

 :oops:
Location @ Córdoba Argentina Using ACAD 2019  at Window 10

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
line from a point perpendicular to a line
« Reply #11 on: June 06, 2004, 08:01:57 PM »
Well I'll try. :)

I use this
Code: [Select]
^C^Cdview;;tw;-90;;_ucs;w;_ucs;z;90;
to rotate the house plan view so that the right side is at the bottom of the
screen. I then draw the right elevation.
The TRANS function returns the correct coordinates for that UCS. It only comes
into play when then UCS is not World. If you don't rotate the ucs you do not need it.

Here is the thread with more info.
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

SMadsen

  • Guest
line from a point perpendicular to a line
« Reply #12 on: June 07, 2004, 09:30:27 AM »
Here's a more mathematical approach, using a dot product to find the perp point. It has the advantage of working in 3D (which INTERS is a bit picky about).

The parameter r can be used for various information (it's the distance ratio where the perpendicular hits the line).

Code: [Select]
(defun C:P2LINE (/ dist ent entl p1 p2 p3 np up r)
  (and (setq ent (car (entsel "Select line: ")))
       (setq entl (entget ent))
       (= (cdr (assoc 0 entl)) "LINE")
       (setq p1   (trans (cdr (assoc 10 entl)) 0 1)
             p2   (trans (cdr (assoc 11 entl)) 0 1)
             dist (distance p1 p2)
       )
       (while (setq p3 (getpoint "\nPick a point: "))
         (setq r  (/ (apply '+ (mapcar (function (lambda (a b c)
                        (* (- c a) (- b a)))) p1 p2 p3))
                     (expt dist 2.0)
                  )
               np (mapcar (function (lambda (a b) (+ a (* r (- b a))))) p1 p2)
               up (trans np 1 0)
         )
         (entmake (list '(0 . "LINE") (cons 10 (trans p3 1 0))(cons 11 up)))
         (mapcar 'princ (list "\nDistance from point to perp of line: "
                        (rtos (distance p3 np) 2)
                              "\nPerpendicular point on line (WCS): "
                              "\nX = " (car up) "\tY = " (cadr up)
                              "\tZ = " (caddr up)))
         (and (= (getvar "WORLDUCS") 0)
              (mapcar 'princ (list  "\nIn current UCS:\nX = "
                          (car np) "\tY = " (cadr np) "\tZ = " (caddr np))))
       )
  )
  (princ)
)

Mark

  • Custom Title
  • Seagull
  • Posts: 28753
line from a point perpendicular to a line
« Reply #13 on: June 07, 2004, 09:56:55 AM »
that's a thing of beauty Stig.
TheSwamp.org  (serving the CAD community since 2003)

SMadsen

  • Guest
line from a point perpendicular to a line
« Reply #14 on: June 07, 2004, 10:12:05 AM »
You mean it works? Excellent!  :)