Lisp to Draw a line that extends in both directions

Lisp to Draw a line that extends in both directions

timothy_crouse
Collaborator Collaborator
1,546 Views
12 Replies
Message 1 of 13

Lisp to Draw a line that extends in both directions

timothy_crouse
Collaborator
Collaborator

Does anyone have a lisp or have an idea of how to create a lisp that will draw a line from a starting point that extends in both directions the distance of the selected endpoint?    Essentially draw a line as usual in one direction but the other half of the line is mirrored in the opposing direction, more like stretched in the opposite direction.

 

Thanks in Advance

-Tim C.

0 Likes
Accepted solutions (2)
1,547 Views
12 Replies
Replies (12)
Message 2 of 13

Sea-Haven
Mentor
Mentor

Just a suggestion as I am not good at GRDRAW just draw a line 2 pts then enter length line will be correct length based around 1st point as mid point. This has probably been done before did you google, there is another post I think here click a line and change length. The dummy line is really to get the line angle.

 

Very quick and rough

(defun c:l2ways ( / pt1 pt2 dist ang)
(setq pt1 (getpoint "\nPick 1st point ")
pt2 (getpoint pt1"\npick second point"))
(setq ang (angle Pt1 pt2))
(setq dist (/ (getreal "\nenter length ") 2.0))
(command "line" (polar pt1 (- ang pi) dist)(polar pt1 ang dist) "")
)
0 Likes
Message 3 of 13

ВeekeeCZ
Consultant
Consultant

If you need to use this kind of editing a lot, you should consider using a dynamic block instead of just a plain line.

 

Or if you stay with p/lines, try this...

(vl-load-com)

(defun c:LengthenAdvanced ( / *error osm* s i e l f) 
  
  (defun *error* (errmsg)
    (if (not (wcmatch errmsg "Function cancelled,quit / exit abort,console break,end"))
      (princ (strcat "\nError: " errmsg)))
    (if osm (setvar 'OSMODE osm))
    (princ))
  
  (if (and (setq s (ssget "_:L" '((0 . "LINE,LWPOLYLINE,ARC"))))
	   (setq osm (getvar 'osmode))
	   (setvar 'osmode 0)
	   
	   (or *la-opt1* (setq *la-opt1* "Middle"))
	   (not (initget "Start Middle End"))
	   (setq *la-opt1* (cond ((getkword (strcat "\nApply changes from [Start/Middle/End] <" *la-opt1* ">: ")))
				 (*la-opt1*)))
	   
	   (or *la-opt2* (setq *la-opt2* "Total"))
	   (not (initget "Delta Percent Total"))
	   (setq *la-opt2* (cond ((getkword (strcat "\nSpecify type of change [Delta/Percent/Total] <" *la-opt2* ">: ")))
				 (*la-opt2*)))
	   (cond ((= *la-opt2* "Delta")
		  (or *la-delta* (setq *la-delta* 1.))
		  (setq *la-delta* (cond ((getdist (strcat "\nEnter delta length <" (rtos *la-delta*) ">: ")))
					 (*la-delta*))))
		 ((= *la-opt2* "Percent")
		  (or *la-percent* (setq *la-percent* 100.))
		  (setq *la-percent* (cond ((getreal (strcat "\nEnter percentage difference <" (rtos *la-percent*) ">: ")))
					   (*la-percent*))))
		 
		 ((= *la-opt2* "Total")
		  (or *la-total* (setq *la-total* 1.))
		  (setq *la-total* (cond ((getdist (strcat "\nSpecify total length <" (rtos *la-total*) ">: ")))
					 (*la-total*)))))
	   )
    (if (/= *la-opt1* "Middle")
      
      (progn
	(cond ((= *la-opt2* "Delta")	(command "_.lengthen" "_delta" *la-delta*))
	      ((= *la-opt2* "Percent")	(command "_.lengthen" "_percent" *la-percent*))
	      ((= *la-opt2* "Total")	(command "_.lengthen" "_total" *la-total*)))
	(repeat (setq i (sslength s))
	  (setq e (ssname s (setq i (1- i))))
	  (cond ((= *la-opt1* "Start")	(command (list e (vlax-curve-getpointatparam e (vlax-curve-getstartparam e)))))
		((= *la-opt1* "End")	(command (list e (vlax-curve-getpointatparam e (vlax-curve-getendparam e)))))))
	(if (> (getvar 'cmdactive) 0) (command "")))
      
      (if (= *la-opt2* "Delta")
	(progn
	  (command "_.lengthen" "_delta" (/ *la-delta* 2))
	  (repeat (setq i (sslength s))
	    (setq e (ssname s (setq i (1- i))))
	    (command (list e (vlax-curve-getpointatparam e (vlax-curve-getstartparam e)))
		     (list e (vlax-curve-getpointatparam e (vlax-curve-getendparam e)))))
	  (if (> (getvar 'cmdactive) 0) (command "")))
	
	(repeat (setq i (sslength s))   ;; Middle Percent and Total
	  (setq e (ssname s (setq i (1- i)))
		l (vlax-curve-getdistatparam e (vlax-curve-getendparam e))
		f (if (= *la-opt2* "Percent") (* l *la-percent* 0.01) *la-total*))
	  (command "_.lengthen" "_delta" (/ (- f l) 2)
		   (list e (vlax-curve-getpointatparam e (vlax-curve-getstartparam e)))
		   (list e (vlax-curve-getpointatparam e (vlax-curve-getendparam e)))
		   "")))))
  (*error* "end")
  )

 

0 Likes
Message 4 of 13

CADaSchtroumpf
Advisor
Advisor

This ?

(defun c:LINE_by_Mid ( / pt_m tmp pt_e)
  (initget 9)
  (setq pt_m (getpoint "\nMiddle point: "))
  (princ "\nEnd point: ?")
  (while (= 5 (car (setq tmp (grread t 5 0))))
    (cond
      ((= 5 (car tmp))
        (redraw)
        (grdraw (setq pt_e (polar pt_m (angle (cadr tmp) pt_m) (distance pt_m (cadr tmp)))) (cadr tmp) 7)
      )
    )
  )
  (if (and pt_e (listp (cadr tmp)))
    (entmake
      (list
        '(0 . "LINE")
        (if (eq (getvar "CTAB") "Model") '(67 . 0)'(67 . 1))
        (cons 8 (getvar "CLAYER"))
        (cons 10 pt_e)
        (cons 11 (cadr tmp))
      )
    )
  )
  (redraw)
  (prin1)
)
Message 5 of 13

calderg1000
Mentor
Mentor
Accepted solution

Regards @timothy_crouse 

Try this code...

(defun c:mln (/ spm p1 p2 p3 d a)
  (setq
    spm (vla-get-modelspace (vla-get-activedocument (vlax-get-acad-object)))
  )
  (setq p1 (getpoint "\nSpecify First Point: ")
        p2 (getpoint p1 "\nSpecify Next Point: ")
        d  (distance p1 p2)
        a  (angle p1 p2)
        p3 (polar p1 (+ a pi) d)
  )
  (vla-addline spm (vlax-3d-point p3) (vlax-3d-point p2))
)

 


Carlos Calderon G
EESignature
>Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

0 Likes
Message 6 of 13

timothy_crouse
Collaborator
Collaborator

NICE!

 

Could you add functionality to allow Ortho to be toggled?

 

Is it possible to make the object a line so there is no mid-point present?

 

Best Regards

-Tim

0 Likes
Message 7 of 13

Kent1Cooper
Consultant
Consultant

@timothy_crouse wrote:

... an idea of how to create a lisp that will draw a line from a starting point that extends in both directions the distance of the selected endpoint?  ....


The (grread)/(grdraw) approach is probably best, because it can dynamically show you what you'll get in both directions as you're drawing the initial Line.  But if you don't need that preview, here's a super-simple approach -- draw the Line from your starting [ultimately mid-] point to an end, and SCALE it about that end by a factor of 2:

 

(defun C:LBD () ; = Line Both Directions
  (command
    "_.line" pause pause ""
    "_.scale" "_last" "" "_non" "@" 2
  )
  (princ)
)

It honors whatever you have for Osnap modes, Ortho, etc.

 

Kent Cooper, AIA
Message 8 of 13

timothy_crouse
Collaborator
Collaborator

NICE!

 

Could you add functionality to display the line as it is being drawn?

Is it possible to make the object a line so there is no mid-point present?

 

I ask CADaSchtroumpf to add ortho functionality to the code provided.  

 

I wanted to test that code with this code, as they look to be so different.  Curious about the approach you folks took.

 

 

Thanks for you help with this,  I think many folks will find this useful.

 

Best Regards

-Tim C.

 

0 Likes
Message 9 of 13

Kent1Cooper
Consultant
Consultant

I had forgotten about this coming up before, at >this topic<.

Kent Cooper, AIA
0 Likes
Message 10 of 13

john.uhden
Mentor
Mentor

@Sea-Haven 

Bettter with pt2 being (getdist).  He can just point in the right direction and enter the distance, which is the same thing as using just the LINE command.

John F. Uhden

0 Likes
Message 11 of 13

john.uhden
Mentor
Mentor

@ВeekeeCZ 

Umm, how will he know which is the start and the end?

You could give him one of my arrow glyphs but then you have to get into grreads so that the arrow remains as he zooms and pans.

John F. Uhden

0 Likes
Message 12 of 13

timothy_crouse
Collaborator
Collaborator

Sorry I did not reply earlier.  I guess after seeing the various ways of doing the task I realized seeing the line as it was being drawn and having the ability to toggle ortho were also characteristics I was looking for.  As your code had user input I figured I would ask for help from the folk whose code had more of a visual interface.

 

Thanks for your help

Best Regards

-Tim C.

 

 

0 Likes
Message 13 of 13

CADaSchtroumpf
Advisor
Advisor
Accepted solution

@timothy_crouse  a écrit :

NICE!

 

Could you add functionality to allow Ortho to be toggled?

 

Is it possible to make the object a line so there is no mid-point present?

 

Best Regards

-Tim


With "ORTHOMODE" "On" or F8 activated this is possible.

(defun c:LINE_by_Mid ( / pt_m tmp pt_e)
  (initget 9)
  (setq pt_m (getpoint "\nMiddle point: "))
  (princ "\nEnd point: ?")
  (while (= 5 (car (setq tmp (grread t 5 0))))
    (cond
      ((= 5 (car tmp))
        (redraw)
        (if (eq (getvar "ORTHOMODE") 1)
          (if (> (abs (- (car pt_m) (caadr tmp))) (abs (- (cadr pt_m) (cadadr tmp))))
            (setq tmp (list (car tmp) (list (caadr tmp) (cadr pt_m))))
            (setq tmp (list (car tmp) (list (car pt_m) (cadadr tmp))))
          )
        )
        (grdraw (setq pt_e (polar pt_m (angle (cadr tmp) pt_m) (distance pt_m (cadr tmp)))) (cadr tmp) 7)
      )
    )
  )
  (if (and pt_e (listp (cadr tmp)))
    (progn
      (if (eq (getvar "ORTHOMODE") 1)
        (if (> (abs (- (car pt_m) (caadr tmp))) (abs (- (cadr pt_m) (cadadr tmp))))
          (setq tmp (list (car tmp) (list (caadr tmp) (cadr pt_m))))
          (setq tmp (list (car tmp) (list (car pt_m) (cadadr tmp))))
        )
      )
      (entmake
        (list
          '(0 . "LINE")
          (if (eq (getvar "CTAB") "Model") '(67 . 0)'(67 . 1))
          (cons 8 (getvar "CLAYER"))
          (cons 10 pt_e)
          (cons 11 (cadr tmp))
        )
      )
    )
  )
  (redraw)
  (prin1)
)

There are no mid-point present?! Is a simple line with the start and end point.