Author Topic: Perp from line?  (Read 4028 times)

0 Members and 1 Guest are viewing this topic.

TimSpangler

  • Water Moccasin
  • Posts: 2010
  • CAD Naked!!
Perp from line?
« on: November 14, 2006, 02:32:37 PM »
OK guys what I am trying to accomplish here is to get a point perpendicular to the start point but i only want it to be a certain distance.  I also want the user to select the direction to get the point.  If the user selects a line (point on a line -My next question) I want them to be prompted for a side of the line.  Then a point will be created perpendicular to the start point on the side of the line that the user selects at a distance of 1 unit.
 The second question is when using nentsel how can I use osnaps such as nearest or mid while in the nentsel command?
 
 below is some of the code that I have thus far.
Code: [Select]
(defun c:foo ()

(setq OldCmdEcho (getvar "CMDECHO"))
(setvar "CMDECHO" 0)
(setq OldOsmode (getvar "OSMODE"))
(setvar "OSMODE" 514)

(setq StartEnt (nentsel "Define start point for duct: "))
(setq StartEnt (entget (car StartEnt)))

;; Get the end points of the selected line
(setq EndPoint1 (cdr (assoc 10 ENT)))
(setq EndPoint2 (cdr (assoc 11 ENT)))

;; Get the angle of the line and add 90 degrees;(radians)
(setq ANG (+ (angle PT1 PT2) (/ pi 2)))

(setvar "OSMODE" OldOsmode)
(setvar "CMDECHO" OldCmdEcho)
)
ACA 2015 - Windows 7 Pro
All Comments and Content by TimSpangler, Copyright © 2016

Dinosaur

  • Guest
Re: Perp from line?
« Reply #1 on: November 14, 2006, 02:41:01 PM »
This routine from r12 era does just about what you describe

Code: [Select]
******************************************************************************
; file  PERPEN.LSP            by Gregory Phillips              November 4, 1990
; Draws a line of entered length perpendicular to a selected line             
; Uncomment and insert the following line in acad.lsp for selfloading         
; (defun c:ppl () (load "perpen") (c:ppl) )                                   
;******************************************************************************
(defun c:ppl (/ a b c d e f g h i j)                       ;only pplsav global
  (setq a (entsel "\nSelect line to draw perpendicular to: "))   ;get object   
  (if a                                                    ;test if select ok 
    (progn                                                 ;if ok proceed     
      (setq b (entget (car a)))                            ;get entity         
      (if (equal (cdr (assoc 0 b)) "LINE")                 ;check if line     
        (progn                                             ;if line continue   
          (setvar "cmdecho" 0)                             ;suppress chatter   
          (setq c (getpoint "\nSelect starting point: "))  ;pnt can be anywhere
          (setq d (getpoint "\nSelect side for line: "))   ;choose direction   
          (if (not pplsav) (setq pplsav 1.0))              ;default length 1.0
          (setq e (strcat "\nEnter length <" (rtos pplsav) ">: "))  ;build msg
          (setq f (getdist e))                             ;prompt for length 
          (if f (setq pplsav f))                           ;save if entered   
          (setq g (trans (cdr (assoc 10 b)) 0 1))          ;start,  current UCS
          (setq h (trans (cdr (assoc 11 b)) 0 1))          ;end pnt current UCS
          (setq i (angle g h))                             ;angle,  current UCS
          (if (minusp (sin (- (angle g d) i)))             ;determine direction
            (setq j (- i (/ pi 2)))                        ;if "below" -90 deg
            (setq j (+ i (/ pi 2)))                        ;or "above" +90 deg
          )                                                ;END IF             
          (command "line" c (polar c j pplsav) "")         ;draw perp. line   
          (princ "\10\10\10   ")                           ;erase trailing "to"
        )                                                  ;END PROGN         
        (prompt "\nOnly lines can be selected")            ;display error cause
      )                                                    ;END IF             
    )                                                      ;END PROGN         
    (prompt "\nNo object selected")                        ;display error cause
  )                                                        ;END IF             
  (princ)                                                  ;leave clean display
)                                                          ;END DEFUN         
;******************************************************************************


TimSpangler

  • Water Moccasin
  • Posts: 2010
  • CAD Naked!!
Re: Perp from line?
« Reply #2 on: November 14, 2006, 02:52:47 PM »
Thanks Stephen,

It was exactly what I was looking for

Tim
ACA 2015 - Windows 7 Pro
All Comments and Content by TimSpangler, Copyright © 2016

Dinosaur

  • Guest
Re: Perp from line?
« Reply #3 on: November 14, 2006, 02:57:35 PM »
You are welcome . . . the guy who gave me a bunch of these told me to use them well and pass them on, so I guess the license has now been extended.  Too bad it only works with lines, but it will draw the line anywhere and doesn't have to terminate at the reference line.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Perp from line?
« Reply #4 on: November 14, 2006, 03:12:14 PM »
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.

LE

  • Guest
Re: Perp from line?
« Reply #5 on: November 14, 2006, 03:18:50 PM »

sinc

  • Guest
Re: Perp from line?
« Reply #6 on: November 15, 2006, 07:46:53 PM »
Too bad it only works with lines, but it will draw the line anywhere and doesn't have to terminate at the reference line.

It might be worth noting that vlax-curve-getFirstDeriv will return the direction of any object at any given point.  If the object is a curved one, the first derivative is the direction of the tangent at that point.  This works with any entity, including splines.

The only place you need to be careful is at the vertex of polylines (angle points).  The first derivative at polyline vertices is the tangent direction of the segment that is departing from the vertex, e.g. the first derivative at parameter 4.0000 is the tangent direction of the segment between params 4 and 5.

So if you get the first derivative, then rotate it 90°, you will have the perpendicular bearing.