Jump to content
trmg

Lisp that draws lines

Recommended Posts

trmg

Hello everyone! I would like some help with a lisp that draws lines from multiple starting points to multiple endingpoints.

 

It would be beneficial if lines did not intersect, and if lines were drawn in 45 angles.

 

I will attach a drawing example of what i want.

 

I never made a lisp before so i dont know how hard this is. I know some scripting (autohotkeys).

 

Nice to find this forum. Hope you can help me / direct me.

 

I am willing to learn.

 

Best regards /T

example.dwg

example.dwg

Share this post


Link to post
Share on other sites
BIGAL

You want a bent line lisp others may come up with a better idea I would pick start pt, pick a approx horizontal midpoint then end point this is a enough then to draw a line. You need to calc the intersection point of the horizontal line with the 45 line then basicly line pt1 pt2 pt3. You have 4 quadrants to work within so your 45 goes the right way, you need to work out 2 imaginary lines one at 45 degrees the other 0 then you can use the command "inters" to calculate the middle join point.

 

 

Its not hard but if you have never done lisp. keep watching post bit busy at moment but others will probably help.

 

(setq pt1 (getpoint))
(setq pt2 (getpoint))
(setq pt3 (getpoint))
(setq ang (angle pt1p2))
(cond 
(angle 0-1.5706 qaud 1) 45
(angle 1.5707-3.1416 quad2)315
(angle 3.1417-4.7123 quad 3)225
(angle 4.712371-6.283185 quad4)135
after cond can now work out inters
)

Share this post


Link to post
Share on other sites
trmg

How would the script you posted trigger? I mean what key do i use?

Share this post


Link to post
Share on other sites
BIGAL

Give this a try

 

; lisp to draw hor lines plus 45's between 2 points
; By BIGAL Jan 2012
(defun c:horbend ()
(setq pt1 (getpoint "\npick 1st point on terminal bar"))
(setq pt2 (getpoint "\npick 2nd point "))
; ang2 is horiz lines ang3 is 45 lines
(setq ang1 (angle pt1 pt2))
(cond
 ((> ang1 4.412388)(setq ang2 0.0)(setq ang3 (* 0.75 pi)))
 ((> ang1 pi)(setq ang2 pi)(setq ang3 (* 0.25 pi))) 
 ((> ang1 (/ pi 2.0))(setq ang2 PI)(setq ang3 (* 0.75 pi)))
 ((> ang1 0.0)(setq ang2 0.0)(setq ang3 (* 0.25 pi)))
)
(setq pt3 (polar pt1 ang2 20.0)) 
(setq pt4 (polar pt2 ang3 20.0))
(setq pt5 (inters pt1 pt3 pt2 pt4 nil))
(command "line" pt1 pt5 pt2 "")
)
(princ)

Share this post


Link to post
Share on other sites
trmg

Thank you! That works when i do it sideways. What doesn't seem to work is if i want to draw something upwards / downwards.

 

 

AutoCAD%20MEP%202010%20-%20%5BDrawing1.dwg%5D_2012-01-23_07-48-57.jpg

 

 

Is it possible to select multiple starting points and multiple ending points?

 

Thank you for your time.

Share this post


Link to post
Share on other sites
fixo

Try this too, mostly borrowed from code above

 
(defun C:HORBEND(/ acsp adoc allset ang cr cs_lst da en end_lst i ip p1 p2 p3 pt sset ta tmp tp)
; credits to BIGAL
;local defuns

; Convert value in radians to degrees
(defun rtd (a)
(* 180.0 (/ a pi))
)

(prompt "\n\t Select number of circles within the terminal port: ")
(if (setq sset (ssget '((0 . "circle") (40 . 100.0))))
(progn
(setq cs_lst nil
end_lst nil
)
(or adoc (setq adoc (vla-get-activedocument (vlax-get-acad-object))))
(or acsp (setq acsp (vla-get-block (vla-get-activelayout adoc))))
(while
(setq en (ssname sset 0))
(setq pt (reverse (cdr (reverse (cdr (assoc 10 (entget en))))))
tmp (list pt en)
cs_lst (cons tmp cs_lst)
)
(ssdel en sset)
)
(setq cs_lst (vl-sort cs_lst '(lambda (a b) (> (cadar a) (cadar b)))))

(prompt "\n\t Select circles to connect to selected just at one side: ")

(setq allset (ssget '((0 . "circle") (40 . 100.0))))
(foreach item cs_lst
(ssdel (cadr item)
allset
)
)

(while
(setq cr (ssname allset 0))
(setq pt (reverse (cdr (reverse (cdr (assoc 10 (entget cr))))))
tmp (list pt cr)
end_lst (cons tmp end_lst)
)
(ssdel cr allset)
)
(if (/= (length cs_lst) (length end_lst))
(progn
(alert "Different numbers of start and and points")
(exit)
(princ)
)
)
(setq end_lst (vl-sort end_lst '(lambda (a b) (> (cadar a) (cadar b)))))
(setq i 0)
(repeat (length cs_lst)
(setq p1 (car (nth i cs_lst))
p3 (car (nth i end_lst))
p2 (list (car p3) (cadr p1))
ang (angle p3 p1)
da (rtd ang)
ta
(cond ((< 0. da 90.)
(/ pi 4))
((< 90. da 180.)
(* 0.75 pi))
((< 180. da 270.)
(* pi 1.25))
((< 270. da 360.)
(* pi 1.75))
)
tp (polar p3 ta (* 2 (getvar 'viewsize)))
ip (inters p1 p2 p3 tp nil)
)
(vlax-invoke acsp 'addlightweightpolyline (apply 'append (list p1 ip p3)))
(setq i (1+ i))
)
)
)
(princ)
)
(prompt "\n   ---   Start command with HORBEND   ---")
(prin1)
(or (vl-load-com) )
(princ)

 

~'J'~

Share this post


Link to post
Share on other sites
trmg

Thank you for your time. Im not sure how to use that one? It says 0 found when i mark circles.

Share this post


Link to post
Share on other sites
fixo

I used your attached drawings for the test

Share this post


Link to post
Share on other sites
trmg

Oh it works when i use the example. Maybe circle diameter matters? Where do i change it?

 

And also, how do i make it so it works vertical also? Or is that not possible?

Share this post


Link to post
Share on other sites
fixo

(setq sset (ssget '((0 . "circle") (40 . 100.0))))

100.0 is radius, change on whatever you need

 

For verticals you have to create separate lisp,

otherwise the code will be too complicated,

i'm kinda busy with my own, sorry

Share this post


Link to post
Share on other sites
trmg

Ok, i totally understand. I am really trying to follow your lisp but it is hard for me. Would it be possible to do the same job in all directions if you didn't care about the 45 angles? Just straight lines?

Share this post


Link to post
Share on other sites
trmg

(defun c:vv15 (/ pt)
 
   (command "TEXT" "S" "ISO" "0,25,0" "125" "0" "KV5-15")
     (command "TEXT" "S" "ISO" "0,-150,0" "125" "0" "VV5-15")
       (command "LINE" "0,0,0" "540,0,0" "")
     (command "-BLOCK" "v15" "270,0" "BOX" "700,700,0" "-700,-700,0" "")
(princ)
)

(defun c:vv18 (/ pt)
 
 
   (command "TEXT" "S" "ISO" "0,25,0" "125" "0" "KV5-15")
     (command "TEXT" "S" "ISO" "0,-150,0" "125" "0" "VV5-15")
       (command "LINE" "0,0,0" "540,0,0" "")
     (command "-BLOCK" "v18" "270,0" "BOX" "700,700,0" "-700,-700,0" "")
(princ)
  
)


(defun c:ss50 (/ pt)
 
   (command "TEXT" "S" "ISO" "0,25" "125" "0" "S1-50")
     
       (command "LINE" "0,0,0" "540,0" "")
     (command "-BLOCK" "ss50" "270,0" "BOX" "700,700,0" "-700,-700,0" "")
(princ)
)

(defun c:ss75 (/ pt)
 
   (command "TEXT" "S" "ISO" "0,25" "125" "0" "S1-75")
     
       (command "LINE" "0,0,0" "540,0" "")
     (command "-BLOCK" "ss50" "270,0" "BOX" "700,700,0" "-700,-700,0" "")
(princ)
)

(defun c:ss110 (/ pt)
 
   (command "TEXT" "S" "ISO" "0,25" "125" "0" "S1-110")
     
       (command "LINE" "0,0,0" "540,0" "")
     (command "-BLOCK" "ss50" "270,0" "BOX" "700,700,0" "-700,-700,0" "")
(princ)
)


(defun c:dd110 (/ pt)
 
   (command "TEXT" "S" "ISO" "0,25" "125" "0" "D2-110")
     
       (command "LINE" "0,0,0" "540,0" "")
     (command "-BLOCK" "ss50" "270,0" "BOX" "700,700,0" "-700,-700,0" "")
(princ)
)

(defun c:skapablock (/ pt)
 
   (command "vv15" "vv18" "ss50" "ss75" "ss110" "dd110")
     
      
(princ)
)

 

Hijacking my own thread for something else:

 

All functions work except the last one which is supposed to call all other functions. How do i go on with that? It just says unknown command on all of them.

Share this post


Link to post
Share on other sites
BIGAL

(VV15) (VV1) etc

Share this post


Link to post
Share on other sites
trmg

Thanks alot. Can anyone post the line script working on vertical instead of horizontional? I am having no clue how to do it. I would be very thankful if someone did.

 

Thanks again for all the help. Means alot

Share this post


Link to post
Share on other sites
BIGAL

Just change the code where it uses the 0.0 angle to (/ pi 2.0)

Share this post


Link to post
Share on other sites
trmg
Just change the code where it uses the 0.0 angle to (/ pi 2.0)

 

Original code:

; lisp to draw hor lines plus 45's between 2 points
; By BIGAL Jan 2012
(defun c:horbend ()
(setq pt1 (getpoint "\npick 1st point on terminal bar"))
(setq pt2 (getpoint "\npick 2nd point "))
; ang2 is horiz lines ang3 is 45 lines
(setq ang1 (angle pt1 pt2))
(cond
 ((> ang1 4.412388)(setq ang2 0.0)(setq ang3 (* 0.75 pi)))
 ((> ang1 pi)(setq ang2 pi)(setq ang3 (* 0.25 pi))) 
 ((> ang1 (/ pi 2.0))(setq ang2 PI)(setq ang3 (* 0.75 pi)))
 ((> ang1 0.0)(setq ang2 0.0)(setq ang3 (* 0.25 pi)))
)
(setq pt3 (polar pt1 ang2 20.0)) 
(setq pt4 (polar pt2 ang3 20.0))
(setq pt5 (inters pt1 pt3 pt2 pt4 nil))
(command "line" pt1 pt5 pt2 "")
)
(princ)

 

 

My edit:

 

; lisp to draw hor lines plus 45's between 2 points
; By BIGAL Jan 2012
(defun c:hg ()
(setq pt1 (getpoint "\npick 1st point on terminal bar"))
(setq pt2 (getpoint "\npick 2nd point "))
; ang2 is horiz lines ang3 is 45 lines
(setq ang1 (angle pt1 pt2))
(cond
 ((> ang1 4.412388)(setq ang2 (/ pi 2.0))(setq ang3 (* 0.75 pi)))
 ((> ang1 pi)(setq ang2 pi)(setq ang3 (* 0.25 pi))) 
 ((> ang1 (/ pi 2.0))(setq ang2 PI)(setq ang3 (* 0.75 pi)))
 ((> ang1 (/ pi 2.0))(setq ang2 (/ pi 2.0))(setq ang3 (* 0.25 pi)))
)
(setq pt3 (polar pt1 ang2 20.0)) 
(setq pt4 (polar pt2 ang3 20.0))
(setq pt5 (inters pt1 pt3 pt2 pt4 nil))
(command "line" pt1 pt5 pt2 "")
)
(princ)

 

 

I must be really stupid, but i can't make it work.

 

 

only works in one direction, and its down and to the right.

 

Screenshot:

 

 

AutoCAD%20MEP%202010%20-%20%5BDrawing11.dwg%5D_2012-01-25_07-30-28.jpg

Share this post


Link to post
Share on other sites
BIGAL

(setq ang2 (/ pi 2.0)) I would suggest do some reading up about LISP programming so you understand what the lines of code are doing rather than just have people write the code.

Share this post


Link to post
Share on other sites
maksolino

hello

I like the second code but i would like to have the possibility

to input the angle in degrees

Thanks

Share this post


Link to post
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
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  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...