LISP-BREAK MULTIPLE LINES AT MIDPOINT

alicia.stewart
Explorer

LISP-BREAK MULTIPLE LINES AT MIDPOINT

alicia.stewart
Explorer
Explorer

Hi all, 
FYI: I do not know how to write Autolisp scripts, but I do know how to load them. 

 

I have hundreds of lines that I need to break into two segments at the midpoint.  In other words, I need each line to become two separate lines with the gap being at the midpoint of the original line. I don't care how big the gap is. But I would like for the lisp to work on multiple lines at once.  It's too many lines for me to do this to each one. 

I know someone is going to ask why, so I've attached a pic. On the left is the goal. On the right is how the rest of the lines are. Long story short, I need each intersection to have lines that extend 9" in 6 different directions, 95% of these intersections are unique. The requested lisp will cut the work in half for me. 

 

 

0 Likes
Reply
Accepted solutions (1)
2,683 Views
10 Replies
Replies (10)

CodeDing
Advisor
Advisor

@alicia.stewart ,

 

Welcome to the forums with your first post!

Can you post an example dwg please? This will help.

Will the lines always be one type of line? i.e. a LINE, or a POLYLINE, or..?

 

Best,

~DD

0 Likes

alicia.stewart
Explorer
Explorer

Hi thanks for the warm welcome, 
I've attached a sample file. Also, yes, these are all regular lines. 

0 Likes

Sea-Haven
Mentor
Mentor

Try this

 

 

; break lines at midpoint
; By Alan H March 2020

(defun c:brkl ( / obj pt start end ang dist rad)
(vl-load-com)
(setq rad 1.0) ; change to getreal for a nominated radius
(setq oldsnap (getvar 'osmode))
(setvar 'osmode 0)
(setq ss (ssget (list (cons 0 "LINE"))))
(repeat (setq x (sslength ss))
(setq obj (vlax-ename->vla-object (ssname ss (setq x (- x 1)))))
(setq end (vlax-get Obj 'EndPoint))
(setq start (vlax-get Obj 'StartPoint))
(setq ang (angle start end))
(setq dist (/ (distance start end) 2.0))
(setq pt1 (polar start ang (- dist rad)))
(setq pt2 (polar start ang (+ rad dist)))
(command "Break" pt1 pt2)
)
(setvar 'osmode oldsnap)
(princ)
)
(c:brkl)

 

Sea-Haven
Mentor
Mentor

The code posted was for a 2d line, need different approach for 3d.

0 Likes

CodeDing
Advisor
Advisor
Accepted solution

@alicia.stewart ,

 

My lisp takes a different approach but gets the job done quick. Hopefully it works for you.

Let me know. I commented out a line, it's only there if the (vla-...) commands don't work.

 

(defun c:BML ( / ssL osm e1 e2 p1 p2)
;Break My Lines
(vl-load-com)
(prompt "\nSelect LINES: ")
(if (not (setq ssL (ssget '((0 . "LINE")))))
  (progn (prompt "\nNo Lines were selected.") (exit))
);if
(setq osm (getvar 'OSMODE))
(setvar 'CMDECHO 0) (setvar 'OSMODE (logior 16384 osm))
;do work
(repeat (setq cnt (sslength ssL))
  (setq e1 (ssname ssL (setq cnt (1- cnt))))
  (setq e2 (entmakex (cons '(0 . "LINE") (member (assoc 100 (entget e1)) (entget e1)))))
  (setq p1 (cdr (assoc 10 (entget e1))))
  (setq p2 (cdr (assoc 11 (entget e2))))
  (vla-ScaleEntity (vlax-ename->vla-object e1) (vlax-3d-point p1) 0.45)
  (vla-ScaleEntity (vlax-ename->vla-object e2) (vlax-3d-point p2) 0.45)
  ;(command "_.SCALE" e1 "" p1 "0.45" "_.SCALE" e2 "" p2 "0.45")
);repeat
(setvar 'CMDECHO 1) (setvar 'OSMODE osm)
(setq ssL nil)
(prompt "\nBML Complete.")
(alert "BML Complete.")
(princ)
);defun

 

Best,

~DD

0 Likes

alicia.stewart
Explorer
Explorer

Thanks, this is faster than my other options. Not totally what I was thinking tho because I'd like to not have to touch every line. I'd like to just draw a window around a set of lines and have them broken into two lines. 

I'll try this one out for a while, 

Thanks again. 🙂

0 Likes

alicia.stewart
Explorer
Explorer

YOU ARE AN ABSOLUTE LIFESAVER!!!!!! THANK YOOOOOU! 

Sea-Haven
Mentor
Mentor

The 1st version only does flat lines and you have lines in 3d overlapping, so this makes two lines for each line on a new layer.

 

 

; break lines at midpoint and makes 2 lines
; By Alan H March 2020

(defun c:brkl2 ( / obj pt start end ang dist rad)
(vl-load-com)
(setq oldsnap (getvar 'osmode))
(setvar 'osmode 0)
(setq oldlay (getvar 'clayer))
(command "-layer" "m" "Temp" "C" 1 "Temp" "Lt" "Continuous" "Temp" "")
(setq ss (ssget (list (cons 0 "LINE"))))
(repeat (setq x (sslength ss))
(setq obj (vlax-ename->vla-object (ssname ss (setq x (- x 1)))))
(setq end (vlax-get Obj 'EndPoint))
(setq start (vlax-get Obj 'StartPoint))
(setq mpt  (mapcar '+ end start) )
(setq mpt (mapcar '(lambda (x) (/ x 2.0)) mpt))
(entmake (list (cons 0 "LINE")
              (cons 8 "Temp")
              (cons 10 start)
              (cons 11 mpt)
			  )
)
(entmake (list (cons 0 "LINE")
              (cons 8 "Temp")
              (cons 10 mpt)
              (cons 11 end)
			  )
)
)
(setvar 'osmode oldsnap)
(princ)
)
(c:brkl2)

 

0 Likes

Kent1Cooper
Consultant
Consultant

@alicia.stewart wrote:

.... I have hundreds of lines that I need to break into two segments at the midpoint.  I need each line to become two separate lines with the gap being at the midpoint of the original line. I don't care how big the gap is. ....


To literally Break at the midpoint  as described, and since you don't care about the gap size, how about a gap of zero?  That simplifies things.  [The lengths of your sample-drawing Lines don't accord with your description of extending 9"  in all those directions, so I'm not sure what the intent really is.]

 

(vl-load-com)
(defun C:LBH ; = Lines Break in Half
  (/ ss n lin)
  (prompt "\nTo Break Lines in Half,")
  (if (setq ss (ssget '((0 . "LINE"))))
    (repeat (setq n (sslength ss))
      (setq lin (ssname ss (setq n (1- n))))
      (command "_.break" lin "_non"
        (mapcar '/ (mapcar '+ (vlax-curve-getStartPoint lin) (vlax-curve-getEndPoint lin)) '(2 2 2))
        "@"
      ); command
    ); repeat
  ); if
  (princ)
); defun

 

 

Kent Cooper, AIA
0 Likes

CodeDing
Advisor
Advisor

If we're trying to get a 'midpoint break'. Mine could also be altered to accomplish this by just changing the scale factors to 0.5.

  (vla-ScaleEntity (vlax-ename->vla-object e1) (vlax-3d-point p1) 0.5)
  (vla-ScaleEntity (vlax-ename->vla-object e2) (vlax-3d-point p2) 0.5)
0 Likes