Lisp that create line

Lisp that create line

Tolearnlisp
Enthusiast Enthusiast
4,804 Views
29 Replies
Message 1 of 30

Lisp that create line

Tolearnlisp
Enthusiast
Enthusiast

Hi fellows, 

would like to have a lisp that create automatic line from center of a square to the center of a circle respectively just by selecting the square and circle after the Lisp command. Thanks in advance.

 Capture.PNG

 

0 Likes
4,805 Views
29 Replies
Replies (29)
Message 2 of 30

Alfred.NESWADBA
Consultant
Consultant

Hi,

 

does that help:

(command "_LINE" "_GCEN" pause "_CEN" pause "")

- alfred -

------------------------------------------------------------------------------------
Alfred NESWADBA
ISH-Solutions GmbH / Ingenieur Studio HOLLAUS
www.ish-solutions.at ... blog.ish-solutions.at ... LinkedIn ... CDay 2026
------------------------------------------------------------------------------------

(not an Autodesk consultant)
0 Likes
Message 3 of 30

pendean
Community Legend
Community Legend
Why not just turn on running osnaps for the task with LINE command?




0 Likes
Message 4 of 30

Kent1Cooper
Consultant
Consultant

It depends in part on what those objects are.  For example, GeometricCENter Object Snap will work on the squares only if they are closed Polylines [whether top-level or nested in a Block], but not  if they're drawn with Lines.

 

An AutoLisp routine could  be built to let you select all  the Circles and squares together in one window selection, and draw all the connecting Lines for you at once, if they're in a "clean" relationship such as in the image, lined up in rows.  But what are they?  Are the circles Circle entities?  Polylines  such as made with the DONUT command?  One or the other inside Blocks?  Are the squares closed Polyline  rectangles?  Four Lines  each?  Blocks?  If either are in Blocks, are their insertion points at their centers where you want the Lines drawn from/to?  Etc., etc.

Kent Cooper, AIA
Message 5 of 30

Alfred.NESWADBA
Consultant
Consultant

Hi,

 

>> It depends in part on what those objects are.  For example,

>> GeometricCENter Object Snap will work on the squares only

>> if they are closed Polylines

Absolutely correct, but as long as we don't get real data I assume at least a bit of structured content.

If DWG-files would be uploaded we could always do more valuable suggestions, but you are correct, I should have mentioned that!

 

- alfred -

 

------------------------------------------------------------------------------------
Alfred NESWADBA
ISH-Solutions GmbH / Ingenieur Studio HOLLAUS
www.ish-solutions.at ... blog.ish-solutions.at ... LinkedIn ... CDay 2026
------------------------------------------------------------------------------------

(not an Autodesk consultant)
0 Likes
Message 6 of 30

dlanorh
Advisor
Advisor
Is the line from the centre of the square to the centre of the circle, or do you want it trimmed at the edge of each entity?

I am not one of the robots you're looking for

0 Likes
Message 7 of 30

Kent1Cooper
Consultant
Consultant

@Kent1Cooper wrote:

It depends in part on what those objects are.  ....


 

I hadn't noticed that over in >the other thread< on this Topic, there's a drawing file attached, but for some reason not in this one.  The circles are in fact Circle entities, and the squares are closed Polylines.  [And @dlanorh, their desired Lines do go to the middle of both shapes, not trimmed at their edges.]

 

But before considering how one might make a routine to do that [which would not be difficult], I would like to hear back from @Tolearnlisp whether the suggestions in Messages 2 & 3 here are not sufficient, and if not, in what way.

Kent Cooper, AIA
Message 8 of 30

dlanorh
Advisor
Advisor
Thanks Kent, I didn't realise that there was a second thread.

I am not one of the robots you're looking for

0 Likes
Message 9 of 30

Tolearnlisp
Enthusiast
Enthusiast

Hi Alfred,

Thank you. I'm sorry I do not know how to your command below. Kindly expand please. thank you.

0 Likes
Message 10 of 30

Tolearnlisp
Enthusiast
Enthusiast

Hi Pendean,

 

"Why not just turn on running osnaps for the task with LINE command?" This what we are doing currently. The reason why I am looking in the possibility of doing in lisp is to fasten the work because we are doing  a hundreds of squares/circles to be connected by line.

0 Likes
Message 11 of 30

Tolearnlisp
Enthusiast
Enthusiast

Hi Alfred,

 

Thank you. I have attached the drawing for your reference. That is the exact properties of the circle and square. Hope to hear from you soon.

0 Likes
Message 12 of 30

Tolearnlisp
Enthusiast
Enthusiast

Hi dlanorh,

Yes, you are correct, the line is from the center of the square to the center of the circle. Hope to hear from you soon. Thank you. See attached sample drawing.

0 Likes
Message 13 of 30

Tolearnlisp
Enthusiast
Enthusiast

Hi Kent1Cooper,

 

Thank you. The suggestions message from 2&3 are not sufficient.

For the message 2, I do not know how will it works.

For the message 3, that's the curent method we are doing but it take us too long when hundreds of circle and squares/rectangle to be connected tha't why I'm looking for LISP because I believed it will fasten our work. Looking forward to your feedback and solution.

0 Likes
Message 14 of 30

ВeekeeCZ
Consultant
Consultant
Accepted solution

Try this code.

The code says to use the Fence type of selection - only that way the selection will be in the order as selected. You CAN use the Window (or other) type of selection only when you're sure, that the objects were CREATED in the correct order.

 

(defun c:MLineConnect (/ ss1 ss2 pt1 pt2)

  (if (and (princ "\nUse Fence selection SQUAREs: ")
           (setq ss1 (ssget '((0 . "LWPOLYLINE") (62 . 256))))
           (princ "\nUse Fence selection to select CIRCLEs: ")
           (setq ss2 (ssget '((0 . "CIRCLE"))))
           )
    (repeat (setq i (min (sslength ss1) (sslength ss2)))
      (if (and (setq lst (vl-remove-if-not '(lambda (x) (= 10 (car x))) (entget (ssname ss1 (setq i (1- i))))))
               (setq pt1 (trans (mapcar '/ (mapcar '+ (cdar lst) (cdaddr lst)) '(2 2)) 0 1))
               (setq pt2 (cdr (assoc 10 (entget (ssname ss2 i)))))
               )
        (command "_.LINE" "_non" pt1 "_non" pt2 ""))))
  (princ)
  )
0 Likes
Message 15 of 30

Kent1Cooper
Consultant
Consultant

@ВeekeeCZ wrote:

.... to use the Fence type of selection - only that way the selection will be in the order as selected. ....


 

... and be sure you run the Fence in the same direction  across the squares as across the Circles, or:

FenceOpposite.PNG

 

If they're always in nice rows like that, a routine could be made to figure out their positional sequence for you, and you could select all the squares and Circles together in one  collective selection, rather than separately, and any  selection method(s) could be used, not just Fence.  Is that a workable approach?  If so, would it always be horizontal rows, or might it sometimes be vertical columns, and/or sometimes in non-orthogonal directions?

Kent Cooper, AIA
0 Likes
Message 16 of 30

Tolearnlisp
Enthusiast
Enthusiast

Hi BeeKeeCZ,

 

Great Code and I appreciate your effort.

I forgot to mention that there are cases that the squares and circles being selected vertically. After trying the code in this case, the wire are crossing even I select it in the same direction using window. It works when I select the square&circles individually. I hope you can still improve the current code. Thank you.

See attached drawing for your reference.

0 Likes
Message 17 of 30

Tolearnlisp
Enthusiast
Enthusiast

Hi Kent1Cooper,

Thank you. My preferred approach would be selecting first the squares then the circles, not selecting them all because sometimes there are cases that not all the squares and circles are to be connected. I replied to @ВeekeeCZ to consider other cases like vertical columns and non-orthogonal directions aside from horizontal rows.

0 Likes
Message 18 of 30

dbhunia
Advisor
Advisor

This is my approach.....Try this....

 

(defun c:csl ( / ss ss_c ss_r lst p1 p2 MP NL N C_lst S_lst Ans)

(defun Poly_Cor_Extr (key cor / val cor_list)
   (foreach val cor
	(if (eq key (car val)) (setq cor_list (cons (cdr val) cor_list)))
   )
(reverse cor_list)
)
(setvar "cmdecho" 0)
(initget "Horizontal Vertical Angular")
(setq Ans (cond ((getkword "\nCircles & Squares Position is [Horizontal/Vertical/Angular] <Horizontal>: ")) ("Horizontal")))
(prompt "\nSelect Circles.......")
(setq ss_c (vl-remove-if 'listp (mapcar 'cadr (ssnamex (ssget '((0 . "CIRCLE")))))))
    (foreach item ss_c
	(setq C_lst (cons (cdr (assoc 10 (entget item))) C_lst))
    )
(if (/= "Vertical" Ans)
    (setq C_lst (vl-sort C_lst '(lambda (e1 e2) (< (car e1) (car e2)))))
    (setq C_lst (vl-sort C_lst '(lambda (e1 e2) (< (cadr e1) (cadr e2)))))
)
(prompt "\nSelect Squares.......")
(setq ss_r (vl-remove-if 'listp (mapcar 'cadr (ssnamex (ssget '((0 . "LWPOLYLINE")(90 . 4)))))))
    (foreach item ss_r
   	(setq lst (Poly_Cor_Extr 10 (entget item)))
	(setq p1 (nth 0 lst) p2 (nth 2 lst))
	(setq MP (polar p1 (angle p1 p2) (/ (distance p1 p2) 2)))
	(setq S_lst (cons MP S_lst))
    )
(if (/= "Vertical" Ans)
    (setq S_lst (vl-sort S_lst '(lambda (e1 e2) (< (car e1) (car e2)))))
    (setq S_lst (vl-sort S_lst '(lambda (e1 e2) (< (cadr e1) (cadr e2)))))
)
(if (< (length S_lst) (length C_lst)) (setq NL (length S_lst)) (setq NL (length C_lst)))
(setq N -1)
(repeat NL
	(command "_.line" (nth (setq N (1+ N)) C_lst) (nth N S_lst) "")
)
(setvar "cmdecho" 0)
(princ)
)

Debashis Bhunia
Co-Founder of Geometrifying Trigonometry(C)
________________________________________________
Walking is the First step of Running, Technique comes Next....
0 Likes
Message 19 of 30

ВeekeeCZ
Consultant
Consultant

You should turn the echo back on and the osnaps off.

0 Likes
Message 20 of 30

ВeekeeCZ
Consultant
Consultant
Accepted solution

@Tolearnlisp wrote:

Hi BeeKeeCZ,

 

Great Code and I appreciate your effort.

I forgot to mention that there are cases that the squares and circles being selected vertically. After trying the code in this case, the wire are crossing even I select it in the same direction using window. It works when I select the square&circles individually. I hope you can still improve the current code. Thank you.

See attached drawing for your reference.


 

OK, the issue fixed! See how it works HERE 

(defun c:LineConnect (/ ss ssp ssc lsp lsc d d0 dir)
  
  (if (and (princ "\nSelect SQUAREs and CIRCLEs to connect: ")
	   (setq ss (ssget '((-4 . "<OR")
			     (0 . "CIRCLE")
			     (-4 . "<AND") (0 . "LWPOLYLINE") (90 . 4) (-4 . "AND>")
			     (-4 . "OR>"))))
	   (setq ssp (acet-ss-ssget-filter ss '((0 . "LWPOLYLINE"))))
	   (setq ssc (acet-ss-ssget-filter ss '((0 . "CIRCLE"))))
	   (setq lsp (mapcar '(lambda (e) (vl-remove-if-not '(lambda (x) (= 10 (car x))) (entget e))) (vl-remove-if 'listp (mapcar 'cadr (ssnamex ssp)))))
	   (setq lsp (mapcar '(lambda (x) (trans (mapcar '/ (mapcar '+ (cdar x) (cdaddr x)) '(2 2)) 0 1)) lsp))
	   (setq lsc (mapcar '(lambda (e) (trans (cdr (assoc 10 (entget e))) 0 1)) (vl-remove-if 'listp (mapcar 'cadr (ssnamex ssc)))))
	   (setq d (abs (/ (-  (apply 'max (mapcar 'car lsp)) (apply 'min (mapcar 'car lsp)))      			;xmax-xmin
			   (if (zerop (setq d0 (- (apply 'max (mapcar 'cadr lsp)) (apply 'min (mapcar 'cadr lsp))))) 	;ymax-ymin
			     0.001
			     d0))))
	   )
    (progn
      (if (< 0.2 d 5)
	(progn
	  (initget "Horizontal Vertical")
	  (setq dir (getkword "\nChoose an option [Horizontal/Vertical]: "))))
      (if (or (= dir "Horizontal")
	      (> d 5))
	(setq lsp (vl-sort lsp '(lambda (p q) (< (car  p) (car  q))))
	      lsc (vl-sort lsc '(lambda (p q) (< (car  p) (car  q)))))
	(setq lsp (vl-sort lsp '(lambda (p q) (< (cadr p) (cadr q))))
	      lsc (vl-sort lsc '(lambda (p q) (< (cadr p) (cadr q))))))
      (mapcar '(lambda (p1 p2)
		 (entmakex (list (cons 0 "LINE")
				 (cons 10 p1)
				 (cons 11 p2)
				 (cons 8 "wire"))))
	      lsp lsc)))
  (princ)
  )

PS. It was really not an issue - you obviously have no idea what the Fence type of selection is. So read about it HERE and HERE in help.

But with the upper code you can select squares and circle anyway you want. Just don't combine horizontal with vertical or upper and lower. See the video linked above.

0 Likes