Jump to content

lisp to draw line on same angle as line picked


john_

Recommended Posts

Hi All,

Does anyone have a lisp or can help me make one that can draws a line based on the angle of a line that is selected?

 

Example: I have a line that is on a 45 degrees

and I have text at the end of the line

I want to select the line and have a predetermined line

drawn in after the text based on scale of the drawing

 

see attached picture for better clarification.

 

Thank you

draw-line.jpg

Link to comment
Share on other sites

See here, turn on Extension and Parallell.

Then draw your line, use extension to find the first point, move the cursor over your first line and then to the second point to get the Paralell Osnap.

AcadOsnapExtensionParallell.jpg

Link to comment
Share on other sites

not sure if this is what you want, good exercise it was none the less

 

(defun c:angline (/ scale lne selpt pt1 pt2 dist1 dist2 bp bp2 rad)
 (setq scale (getvar "cannoscalevalue"))
 (setq lne (entsel "\nSelect line: "))
 (setq selpt (cadr lne))
 (setq pt1 (cdr (assoc 10 (entget (car lne)))))
 (setq pt2 (cdr (assoc 11 (entget (car lne)))))
 (setq dist1 (/ (distance pt1 pt2) 2))
 (setq dist2 (distance pt1 selpt))
 (if (< dist1 dist2)
   (progn
   (setq rad (angle pt1 pt2))
   (setq bp (polar pt2 rad (* 1 (/ 1 scale))))
   (setq bp2 (polar bp rad 1))
   (command "_.line" bp bp2 "")
   (command "_.lengthen" "dy" bp2  pause)
   )
   (progn
   (setq rad (angle pt2 pt1))
   (setq bp (polar pt1 rad (* 1 (/ 1 scale))))
   (setq bp2 (polar bp rad 1))
   (command "_.line" bp bp2 "")
   (command "_.lengthen" "dy" bp2 pause "" "")
   )
 )
 (princ)
)

 

this is probably what many of you would call 'clumpsy' code, and as I'm very much a learner I'd love some input on this code and how to possibly improve it

Link to comment
Share on other sites

I tried your code in Acad 2010.

It starts Lengthen again it seems.

Maybe you need an extra (Command) to cancel everything?

 

Otherwise it works very good.

Link to comment
Share on other sites

not sure if this is what you want, good exercise it was none the less

 

(defun c:angline (/ scale lne selpt pt1 pt2 dist1 dist2 bp bp2 rad)
 (setq scale (getvar "cannoscalevalue"))
 (setq lne (entsel "\nSelect line: "))
 (setq selpt (cadr lne))
 (setq pt1 (cdr (assoc 10 (entget (car lne)))))
 (setq pt2 (cdr (assoc 11 (entget (car lne)))))
 (setq dist1 (/ (distance pt1 pt2) 2))
 (setq dist2 (distance pt1 selpt))
 (if (< dist1 dist2)
   (progn
   (setq rad (angle pt1 pt2))
   (setq bp (polar pt2 rad (* 1 (/ 1 scale))))
   (setq bp2 (polar bp rad 1))
   (command "_.line" bp bp2 "")
   (command "_.lengthen" "dy" bp2  pause)
   )
   (progn
   (setq rad (angle pt2 pt1))
   (setq bp (polar pt1 rad (* 1 (/ 1 scale))))
   (setq bp2 (polar bp rad 1))
   (command "_.line" bp bp2 "")
   (command "_.lengthen" "dy" bp2 pause "" "")
   )
 )
 (princ)
)

this is probably what many of you would call 'clumpsy' code, and as I'm very much a learner I'd love some input on this code and how to possibly improve it

 

I am by no means an expert but two things I'd suggest to improve your code is error trapping for your entsel and pts, eg:

 (if (setq lne (entsel "\nSelect line: ")) 
   (progn ...)
   (princ "\nNothing Selected."))

The other thing is that you'll notice with other peoples code pretty soon, you can assign multiple variables with setq, eg:

(setq a 1 b 2 
   c 3)

sets a = 1, b = 2 and c = 3

Hope that helps a bit.:)

Link to comment
Share on other sites

Would something like this help?

(defun c:PAR (/ #Obj #Point)
 (vl-load-com)
 (and (setq #Obj (car (entsel "\nSelect line: ")))
      (eq "LINE" (cdr (assoc 0 (entget #Obj))))
      (setq #Point (getpoint "\nSpecify first point: "))
      (vl-cmdf "_.line"
               "_non"
               #Point
               (strcat "<" (angtos (vla-get-angle (vlax-ename->vla-object #Obj)) 0 4))
               PAUSE
               ""
      ) ;_ vl-cmdf
 ) ;_ and
 (princ)
) ;_ defun

When you get to the point to pick a point, just use the osnap override "ext" and mouse-over the other line.

Link to comment
Share on other sites

Oh, and here's a nice addon, perpendicular. :wink:

 

(defun c:Per (/ #Obj #Point #Ang)
 (and (setq #Obj (entsel "\nSelect line: "))
      (eq "LINE" (cdr (assoc 0 (entget (car #Obj)))))
      (or (setq #Point (getpoint "\nSpecify first point <At Selection>: "))
          (setq #Point (vlax-curve-GetClosestPointTo (car #Obj) (cadr #Obj)))
      ) ;_ or
      (setq #Ang (angtos (+ (* 0.5 pi) (vla-get-angle (vlax-ename->vla-object (car #Obj)))) 0 4))
      (vl-cmdf "_.line" "_non" #Point (strcat "<" #Ang) PAUSE "")
 ) ;_ and
 (princ)
) ;_ defun

Link to comment
Share on other sites

I am by no means an expert but two things I'd suggest to improve your code is error trapping for your entsel and pts, eg:

 (if (setq lne (entsel "\nSelect line: ")) 
   (progn ...)
   (princ "\nNothing Selected."))

The other thing is that you'll notice with other peoples code pretty soon, you can assign multiple variables with setq, eg:

(setq a 1 b 2 
   c 3)

sets a = 1, b = 2 and c = 3

Hope that helps a bit.:)

 

thanks a lot for the input. I actually thought I had an error trapping term, but it seems to gone lost somehow. I like to use the while statement however.

 

(defun c:angline (/ scale lne selpt pt1 pt2 dist1 dist2 bp bp2 rad)
 (setq scale (getvar "cannoscalevalue"))
 (while (= lne nil)
 (setq lne (entsel "\nSelect line: "))
 )
 (setq selpt (cadr lne))
 (setq pt1 (cdr (assoc 10 (entget (car lne)))))
 (setq pt2 (cdr (assoc 11 (entget (car lne)))))
 (setq dist1 (/ (distance pt1 pt2) 2))
 (setq dist2 (distance pt1 selpt))
 (if (< dist1 dist2)
   (progn
   (setq rad (angle pt1 pt2))
   (setq bp (polar pt2 rad (* 1 (/ 1 scale))))
   (setq bp2 (polar bp rad 1))
   (command "_.line" bp bp2 "")
   (command "_.lengthen" "dy" bp2  pause)
   )
   (progn
   (setq rad (angle pt2 pt1))
   (setq bp (polar pt1 rad (* 1 (/ 1 scale))))
   (setq bp2 (polar bp rad 1))
   (command "_.line" bp bp2 "")
   (command "_.lengthen" "dy" bp2 pause "" "")
   )
 )
 (princ)
)

 

I tried your code in Acad 2010.

It starts Lengthen again it seems.

Maybe you need an extra (Command) to cancel everything?

 

Otherwise it works very good.

 

I tried it in 2009 and it worked for me. I guees I have on too many "" in the last command for 2010.

Link to comment
Share on other sites

Thank you very much dhl and alanjt.

Both almost do the exact same thing only difference is the program alanjt made, when you pick the start point for the additional line, the point that you pick can be anywhere and not even with the original line.

The program dhl made does exactly what I was looking for.

So again thank you all very much for your help and time.

Link to comment
Share on other sites

Thank you very much dhl and alanjt.

Both almost do the exact same thing only difference is the program alanjt made, when you pick the start point for the additional line, the point that you pick can be anywhere and not even with the original line.

The program dhl made does exactly what I was looking for.

So again thank you all very much for your help and time.

Yeah, I apologize for that, I didn't properly read the request. However, I ended up putting something together that I find useful....not that it helps you.:oops:

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...