Pixel Graphics, Inc. - Litigation & Court Graphics
LITIGATION GRAPHICS - COMPUTER ANIMATION - TECHNICAL ILLUSTRATION - CAD DRAFTING - AUTOLISP PROGRAMMING
3423 Rivers Edge Trail
Houston, Texas 77339
281.359.1187
Copyright 2008 - Pixel Graphics, Inc. All rights reserved.
AutoLisp Tutorial: Drawing a Line Between the Midpoints of Two Lines
Overview of the Method

To draw a line between the midpoint of two lines using Autolisp we need to break the problem down into
manageable pieces.

  1. First we need to select each line in AutoCAD. We'll use the Autolisp "entsel" function for that.
  2. Then get the endpoints from each line
  3. Then we need a function that will calculate the midpoint of a line given two points. We'll scavenge
    that from another tutorial I've already done.
  4. After that, we need to draw the line between the two midpoints we've calculated

NOTE: If you copy and use the program code, please respect my work and the time I've put into it by
keeping my copyright info with the program code, and giving me credit where it's appropriate. A link from
your site would be appreciated as well. If you don't have a website, please help promote my site by passing
along a link to my site to other fellow Autolisp programmer(s) via e-mail. Thanks.

Step 1: Selecting the Lines in AutoCAD

Use the "entsel" Autolisp function to select the lines and get their entity name as follows:
;------------------------------------------------------------------------
;-  Command:  midpts_line ()
;-
;-    Draws a line between the midpoints of two lines.
;-
;-         Copyright 2008 Jeff Winship. All rights reserved.
;----------------------------------------------------------------5/3/2008

(defun c:midpts_line ()
    ;-- Select the lines
    (setq line1 (car (entsel "\nSelect the first line: ")))
    (setq line2 (car (entsel "\nSelect the second line: ")))
)
Jeff Winship - Computer Graphics Consultant                                                                                                                                                    May 8, 2008
HOME  |  3D ANIMATION  |  DIGITAL IMAGES  |  AUTOCAD DRAFTING  |  AUTOLISP PROGRAMMING  |  CONTACT & OTHER INFO  |  F.A.Q.
Step 2: Getting the endpoints for each line from the entity data

In a prior tutorial, I showed you how to get entity data from an entity. Read through that tutorial if you don't
understand the program code that's added to what we've created so far.
;------------------------------------------------------------------------
;-  Command:  midpts_line ()
;-
;-    Draws a line between the midpoints of two lines.
;-
;-         Copyright 2008 Jeff Winship. All rights reserved.
;----------------------------------------------------------------5/3/2008

(defun c:midpts_line ()
    ;-- Select the lines
    (setq line1 (car (entsel "\nSelect the first line: ")))
    (setq line2 (car (entsel "\nSelect the second line: ")))

    ;-- Get the endpoints of the first selected line
    (setq pt1 (cdr (assoc 10 (entget line1))))
    (setq pt2 (cdr (assoc 11 (entget line1))))

    ;-- Get the endpoints of the second selected line
    (setq pt3 (cdr (assoc 10 (entget line2))))
    (setq pt4 (cdr (assoc 11 (entget line2))))
)
Step 3: Writing a function to calculate and return the midpoint of a line

In prior tutorials, I showed you how to write a function and also how to calculate the midpoint of a line.
Read through that tutorial to understand the function I'm adding in this step and see how I've scavenged
the existing code.
;------------------------------------------------------------------------
;-  Function:  midpt ( p1 p2 )
;-    Arguments: p1 is the starting point of the line
;-               p2 is the ending point of the line
;-
;-    Returns the midpoint of a line given two points.
;-
;-         Copyright 2008 Jeff Winship. All rights reserved.
;----------------------------------------------------------------5/3/2008
(defun midpt (p1 p2 / Xavg Yavg Zavg)
    ;-Calculate the X, Y and Z averages
    (setq Xavg (/
    (+ (car p1) (car p2))
    2.0
    ))
    (setq Yavg (/
    (+ (cadr p1) (cadr p2))
    2.0
    ))
    (setq Zavg (/
    (+ (caddr p1) (caddr p2))
    2.0
    ))

    ;-Return the midpoint as a list
    (list Xavg Yavg Zavg)
)
;------------------------------------------------------------------------
;-  Command:  midpts_line ()
;-
;-    Draws a line between the midpoints of two lines.
;-
;-         Copyright 2008 Jeff Winship. All rights reserved.
;----------------------------------------------------------------5/3/2008

(defun c:midpts_line ()
    ;-- Select the lines
    (setq line1 (car (entsel "\nSelect the first line: ")))
    (setq line2 (car (entsel "\nSelect the second line: ")))

    ;-- Get the endpoints of the first selected line
    (setq pt1 (cdr (assoc 10 (entget line1))))
    (setq pt2 (cdr (assoc 11 (entget line1))))

    ;-- Get the endpoints of the second selected line
    (setq pt3 (cdr (assoc 10 (entget line2))))
    (setq pt4 (cdr (assoc 11 (entget line2))))

    ;-- Find the midpoints of the lines
    (setq mid1 (midpt pt1 pt2))
    (setq mid2 (midpt pt3 pt4))

    ;-- Draw the line
    (command "line" mid1 mid2 "")
)
Step 4: Find the midpoints using the new function and draw the line

Back to the midpts_line code, we first pass the two points for each line to the new midpt function above,
and it will return the midpoint of each line to two new variables "mid1" and "mid2". We then simply draw the
line in AutoCAD from mid1 to mid2 using the "command" function.
Step 5: Final Notes

Now that we're done writing the code, your should place the "midpt" function code in the same file as the
midpts_line command program if you already haven't. It should be inserted below the very last line of code.
You can put it above the code, but it's good practice to put all of your function code blocks below the
command program code.

The final tested LSP file for this tutorial can be downloaded here.

          
Download the Draw a Line Between the Midpoints of Two Lines Autolisp Program

If you have any comments or questions regarding this technique or code, feel free to e-mail me at:
support@pixelgraphicsinc.com