Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

How to intersect a fixed-length line perfectly between two differently angled lines

21 REPLIES 21
SOLVED
Reply
Message 1 of 22
sean.swiatek
2604 Views, 21 Replies

How to intersect a fixed-length line perfectly between two differently angled lines

Something I've often found myself needing to do is place a line or object of fixed length so that it intersects on both ends perfectly between two other differently angled lines (reference image shows red line is fixed length). If I drag the red line in a direction to try and get close, while snapping it to one of the lines, the intersection at the other end is never perfectly touching the other line. Any help is greatly appreciated.

seanwarnecke_0-1648074188407.png

 

Labels (2)
21 REPLIES 21
Message 2 of 22
jlbelshan
in reply to: sean.swiatek

imagine sliding your new red line along the one that you attached it to.  The other end, top in this case, will slide along a path parallel to the bottom line.  So find this path by copying the bottom line up to the top of the red line.  And where the copied line crosses the top line, is the point where you can move the top of the new line to.  Start with the red line a little  farther away from its final home, to get a clearer visual.  

Message 3 of 22
Sea-Haven
in reply to: jlbelshan

The same for a line length perp to 1 line use offset.

Message 4 of 22
Kent1Cooper
in reply to: sean.swiatek

Do I assume correctly that the fixed-length red Line is also at an angle that needs to remain the same?  If so, the approach of Copying one of the white Lines by the length and direction of the red one, and using the copy's intersection with the other white Line, is good.  If not, Move the red Line so that either end is on one of the white ones, and draw a Circle whose center is that end and whose radius is the red Line.  Where that intersects the other white Line is the solution [2 solutions in most cases].

Kent Cooper, AIA
Message 5 of 22
JBerns
in reply to: sean.swiatek

@sean.swiatek,

 

Would AutoCAD parametrics be a solution?

 

  • Fix constrain the end points of the white lines. Optionally, use dimensional constraints to control position, length, or angle.
  • Vertical constrain the red line.
  • Dimensional length constrain the red line.
  • Coincident constrain the endpoints of the red line to the white lines using the "object" option.

The red line should move to a position that intersects with the white lines.

 

More steps, but offers more control of the line lengths and angles. 

 

I am confident this community of solution providers would be able to offer a LISP solution too.

 

 

Regards,

Jerry 

-----------------------------------------------------------------------------------------
CAD Administrator
Using Inventor 2022
Autodesk Certified Instructor
Autodesk Inventor 2020 Certified Professional
Autodesk AutoCAD 2017 Certified Professional
Message 6 of 22
Sea-Haven
in reply to: sean.swiatek

Waiting for sean so easy to do manually do you really need a lisp ? The hor/ver answer is there as well as perp. Yes an angle would need a lisp as would have to walk along a line checking length. But is it needed ?

Message 7 of 22
sean.swiatek
in reply to: Sea-Haven

I actually accidentally posted this in the LISP forum, and meant it for AutoCAD General, but your point about walking the line and measuring for an angled "red" line could be helpful with a LISP since I wind up needing to do this multiple times in multiple drawings. 

Message 8 of 22
sean.swiatek
in reply to: Kent1Cooper

In some cases, yes the red line would also be at an angle that needs to be consistent, and in some cases is a dynamic block with a nonlinear shape that also needs to find this exact double intersection. Manually doing this isn't going to be a major time-waste for me, but when I have to do it so many times in a single drawing, finding a LISP to automate it would be awesome as well.
Message 9 of 22
john.uhden
in reply to: Sea-Haven

Following @jlbelshan 's lead, I offer the following:

(defun c:Trial ( / *error* vars vals e1 e2 e ent1 ent2 ent p1 p2 p3 p4 p5 p6 p7 ang d)
  (gc)
  (prompt "\nTrial v1.0 (c)2022, John F. Uhden")
  (defun *error* (error)
     (mapcar 'setvar vars vals)
     (vla-endundomark *doc*)
     (cond
       (not error)
       ((wcmatch (strcase error) "*CANCEL*,*QUIT*")
         (vl-exit-with-error "\r                                              ")
       )
       (1 (vl-exit-with-error (strcat "\r*ERROR*: " error)))
     )
     (princ)
  )
  ;;------------------------------------------
  ;; Intitialze drawing and program variables:
  ;;
  (setq *acad* (vlax-get-acad-object))
  (setq *doc* (vlax-get *acad* 'ActiveDocument))
  (vla-endundomark *doc*)
  (vla-startundomark *doc*)
  (setq vars '("cmdecho"))
  (setq vals (mapcar 'getvar vars))
  (mapcar 'setvar vars '(0))
  (command "_.expert" (getvar "expert")) ;; dummy command
  ;; Get on with the action:
  (and
    (setq e1 (car (entsel "\nSelect 1st boundary line: ")))
    (setq e2 (car (entsel "\nSelect 2nd boundary line: ")))
    (setq e (car (entsel "\nSelect line to adjust: ")))
    (setq ent1 (entget e1)
          ent2 (entget e2)
          ent (entget e)
          p1 (cdr (assoc 10 ent1))
          p2 (cdr (assoc 11 ent1))
          p3 (cdr (assoc 10 ent2))
          p4 (cdr (assoc 11 ent2))
          p5 (cdr (assoc 10 ent))
          p6 (cdr (assoc 11 ent))
          ang (angle p5 p6)
          d (distance p5 p6)
          p5 (inters p1 p2 p5 p6 nil)
          p6 (inters p3 p4 p5 p6 nil)
          p6 (polar p5 (angle p5 p6) d)
          p7 (polar p6 (angle p1 p2) 10)
          p6 (inters p3 p4 p6 p7 nil)
          p5 (inters p6 (polar p6 ang 10) p1 p2 nil)
          ent (subst (cons 10 p5)(assoc 10 ent) ent)
          ent (subst (cons 11 p6)(assoc 11 ent) ent)
    )
    (entmod ent)
  )
  (*error* nil)
)

John F. Uhden

Message 10 of 22
JBerns
in reply to: sean.swiatek

Impressive! Well done, @john.uhden!

 

I knew if a challenge or request is placed before this community someone would produce a great solution.

 

Regards,

Jerry 

-----------------------------------------------------------------------------------------
CAD Administrator
Using Inventor 2022
Autodesk Certified Instructor
Autodesk Inventor 2020 Certified Professional
Autodesk AutoCAD 2017 Certified Professional
Message 11 of 22
sean.swiatek
in reply to: john.uhden

Wow thank you, John. I might be doing something wrong, but after loading the application, selecting the first boundary, second boundary, and then the line to adjust, I'm not seeing how the adjustment is to take place, since I'm new to LISP. Would you mind explaining the sequence so that I can then see how it relates to the file you wrote?
Message 12 of 22
JBerns
in reply to: sean.swiatek

@sean.swiatek,

I tested John's code on my system successfully.

I created the three lines similar to your image.

After selecting boundary1 line, boundary2 line, and the line to adjust, then the last selected line is moved.

No other interaction required.

 

- Jerry

-----------------------------------------------------------------------------------------
CAD Administrator
Using Inventor 2022
Autodesk Certified Instructor
Autodesk Inventor 2020 Certified Professional
Autodesk AutoCAD 2017 Certified Professional
Message 13 of 22
Kent1Cooper
in reply to: sean.swiatek


@sean.swiatek wrote:
In some cases, yes the red line would also be at an angle that needs to be consistent, and in some cases is a dynamic block with a nonlinear shape that also needs to find this exact double intersection. ....

So the thing to be Moved isn't always a Line.  That would suggest a routine should ask for the two locations [the endpoints of a Line, the whatever-you-wants on a dynamic Block, etc.] that need to be made to fall on the two other --- wait a minute: would those always be Line entities, or could they also be something else?

Kent Cooper, AIA
Message 14 of 22
john.uhden
in reply to: JBerns

@JBerns 

I would use the word "verbose" rather than "impressive."  But thanks.

John F. Uhden

Message 15 of 22
sean.swiatek
in reply to: Kent1Cooper

In my experience, they are always lines.
Message 16 of 22
john.uhden
in reply to: sean.swiatek

@sean.swiatek 

The primary goal, as I understood from your original post, is to move the "vertical" line to a position where it starts on one of the two boundaries and ends on the other while maintaining its angle (forward or backward) and its length.

So...

1.  Intersect the target (Line) with either of the boundaries, say Line1, because we will slide it along that line to the desired position.

2.  Determine a point relative to the intersection at the same angle and distance as the original Line.

3.  Make an imaginary line through that last point parallel to Line1.

4.  Intersect the imaginary line with the other boundary, Line2.  This will be one endpoint of the Line's new position.

5.  Make another imaginary line from that new end at the angle of the original but at any length.

6.  Intersect the 2nd imaginary line with Line1 to find the other endpoint of the Line's new position.

7.  Modify the Line's entity data to change its geometry to the two new endpoints.

8. Done.

 

I wrote it hoping that it wouldn't matter which boundary line you picked first.  The reason it works is that if you conclude the (inters) function with nil, it will compute the intersection as though one or both of the lines (imaginary or not) overlap one another, even if they don't.

 

Note #1:  It will work with lines at any angle (except in the case of note #3).  And the line to be moved need not be within the boundary limits.

Note #2:  The line may be moved beyond the limits of either or both boundaries.

Note #3:  It will certainly not work if the Line is parallel to either of the boundaries, or if the two boundaries are parallel to each other.

Note #4:  Technically, the angle of the Line's new position might be 180° opposite the original.  If that is an issue, I can add a test for that and swap the two endpoints.

Note #5 {for @Kent1Cooper}  As written it will work with lines only,  that have one code 10 and one code 11.  Yes, I know that a ray has those, but the code 11 is not a point but a vector, so no deal.  Anyway, that was @sean.swiatek 's request... lines.

John F. Uhden

Message 17 of 22
sean.swiatek
in reply to: john.uhden

Thanks very much, this works!
Message 18 of 22
Kent1Cooper
in reply to: sean.swiatek


@sean.swiatek wrote:
In my experience, they are always lines.

That directly contradicts your earlier "...in some cases is a dynamic block."

Kent Cooper, AIA
Message 19 of 22
sean.swiatek
in reply to: Kent1Cooper

To clarify, referencing my example image, the white angled lines are always lines. What's shown as a red line is sometimes a line, other times a block.
Message 20 of 22
JBerns
in reply to: john.uhden

@john.uhden,

I say impressive because few sample code offerings include an error handler. 😉 

-----------------------------------------------------------------------------------------
CAD Administrator
Using Inventor 2022
Autodesk Certified Instructor
Autodesk Inventor 2020 Certified Professional
Autodesk AutoCAD 2017 Certified Professional

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

AutoCAD Inside the Factory


Autodesk Design & Make Report