Visual LISP, AutoLISP and General Customization

Reply
Active Member
klosow
Posts: 10
Registered: ‎01-05-2009
Message 1 of 25 (1,116 Views)
Accepted Solution

Skew lines -shortest distance

1116 Views, 24 Replies
12-05-2010 03:29 AM

Hello I create the LISP script (attached) to find the shortest distance between two skew lines.

Finnally, I want to draw a segment which represents this shortest distance. It should be a line between apperen intersections of:  PR1 and PR2 lines and PR2 and PR1 lines (on the screen they will be visible in the same point). Lines were indicated before, so I want to write proper LISP expression without indicating them again. What is proper form of line command?

(command "_line"  ???????? "")

Pawel Klosowski

 

*Expert Elite*
pbejse
Posts: 2,441
Registered: ‎11-24-2009
Message 2 of 25 (1,072 Views)

Re: Skew lines -shortest distance

12-05-2010 08:23 PM in reply to: klosow

klosow wrote:

Hello I create the LISP script (attached) to find the shortest distance between two skew lines.

Finnally, I want to draw a segment which represents this shortest distance. It should be a line between apperen intersections of:  PR1 and PR2 lines and PR2 and PR1 lines (on the screen they will be visible in the same point). Lines were indicated before, so I want to write proper LISP expression without indicating them again. What is proper form of line command?

(command "_line"  ???????? "")

Pawel Klosowski

 


Hi Klosow.

looking at your code, Its not really clear what you want to accomplish, since you prompted for two points on screen, it defeats the purpose of computing for the shortest distance., those two points will be enough information for you to draw the line.

if you can show an illustration maybe we can better suggest.

 

if you're tryng to get the start point and endpoint of a line, you can use

 

(setq XX1      (entsel "\nIndicate first line:")
		      start_pt1 (cdr (assoc 10 (entget (car xx1))))
		      end_pt1   (cdr (assoc 11 (entget (car xx1))))
		)
(setq XX2      (entsel "\nIndicate second line:")
        start_pt2 (cdr (assoc 10 (entget (car xx2))))
        end_pt2   (cdr (assoc 11 (entget (car xx2))))
  )

 then you can compute from that list the nearest distance wihtout prompting for points on the screen

 

 hope this helps

 

 

 

 

 

*Expert Elite*
pbejse
Posts: 2,441
Registered: ‎11-24-2009
Message 3 of 25 (1,063 Views)

Re: Skew lines -shortest distance

12-05-2010 09:19 PM in reply to: pbejse

then you use this

 

 

(command "_.line" "none" (*your_point1*) "none" (*your_point2*))

;;	or VL	


(vla-addline (vla-get-modelspace (vla-get-activedocument (vlax-get-acad-object)))
(vlax-3d-point (*your_point1*));<----		result of computation
(vlax-3d-point (*your_point2*));<----		result of computation
			  
				 )

 


Notice the "None" on there? This will avoid errors on dealing with osnaps

hope this helps

 

Active Member
klosow
Posts: 10
Registered: ‎01-05-2009
Message 4 of 25 (1,056 Views)

Re: Skew lines -shortest distance

12-05-2010 10:47 PM in reply to: klosow

This way I can comput the shortest distance, but iI also want to draw proper segment.

Active Member
klosow
Posts: 10
Registered: ‎01-05-2009
Message 5 of 25 (1,053 Views)

Re: Skew lines -shortest distance

12-05-2010 10:59 PM in reply to: pbejse

I improved my algorithm (see new version of nwek_En.lsp file) and try two proposed versions of solution. Non of them work. Works line

(command "_line" "_appint" pause "_appint" pause "")

but in this wariant I need to pick up twice both segments again.

*Expert Elite*
pbejse
Posts: 2,441
Registered: ‎11-24-2009
Message 6 of 25 (1,049 Views)

Re: Skew lines -shortest distance

12-05-2010 11:37 PM in reply to: klosow

geez klosow, :smileysad:

 

I did not mean for you to paste it on your code on its face value

 

This line tells you to put your variable point after processing the list.

 

 (vlax-3d-point (*your_point1*));<----  result of computation

 

*yourpoint1* means the result of your computation. I dont fully understand your intent thats why i cant tell you how exaclty the code will be written.

 

if you can describe it better it will help us here a lot (and help you)

  • :smileyhappy:
  • btw your code deals with math functions, i.e.. -,+,*... be careful with negative values as this will affect your results

    thats why i suggest ed that you work with point list, the function "distance" can be use for your test routine

     

     

     

     

     

     

     

    *Expert Elite*
    Kent1Cooper
    Posts: 5,294
    Registered: ‎09-13-2004
    Message 7 of 25 (1,024 Views)

    Re: Skew lines -shortest distance

    12-06-2010 07:48 AM in reply to: klosow

    I haven't figured out the mathematics behind the vector calculations, but I'll assume they're valid, and that they're establishing only a direction, not any specific points.  A couple of things:
     

    As you have it, it seems the User could choose the end point of a Line that is the same as the end nearer to the point by which the Line itself was selected.  In that case, P1 and P2 would be the same, and the routine would fail.  You should probably get the endpoints from the entity data as pbejse suggested, rather than ask the User to designate an end point.  But if you really want the User to pick and end for some reason, you should prompt them for the end farther from where they selected the Line.

     

    After establishing a UCS view looking along the shortest-distance vector and with line 1 as its basis, the start point for the shortest-distance Line uses APPINT and has a pause, apparently for the User to select the apparent intersection.  That will use the point in the current UCS, which will lie on line 1.  For the second point, rather than use APPINT again and another pause [which will find the same point as the first one], you should probably use PERpendicular Osnap and a pause, but how you can ensure that the User will snap to the second Line, I'm not sure.

     

    Maybe the (vlax-curve-getClosestPointToProjection) function could be helpful, but I'm not sure about that, either.

    Kent Cooper
    Active Member
    klosow
    Posts: 10
    Registered: ‎01-05-2009
    Message 8 of 25 (1,019 Views)

    Re: Skew lines -shortest distance

    12-06-2010 08:21 AM in reply to: Kent1Cooper

    Probably I haven't described problem clear enough. In fact I have two skew lines and I want to construct the segment which links them and has the shortest length. At the beginning I had to pick my two lines (XX1 and XX2) then according to math formulas I put the local coordinate system is such position that Z axis is normal to XX1 and XX2 (command ucs) and I take a view from the top of Z axis (command plan). Now the segment which I want can be constructed as linking apparent intersection of XX1 versus XX2 and XX2 versus XX1 (as perpendicular to XX2 - it also correct). I can easy make is using command

     (command "_line" "_appint" pause "_appint" pause "")

    but in this situation I will have to pick both lines once more (for a large drawing it can be difficult as the plan command will change the scale of the actual view).

    The problem is how to make it without picking lines once more. I want to draw the segment  using information which I provided picking them at the beginning of the algorithm (XX1 and XX2).

    The problem is general: how to make reference (e.g. in line command) to objects which have been selected before?

    *Expert Elite*
    Kent1Cooper
    Posts: 5,294
    Registered: ‎09-13-2004
    Message 9 of 25 (1,016 Views)

    Re: Skew lines -shortest distance

    12-06-2010 09:08 AM in reply to: klosow

    klosow wrote:

    ....

    The problem is general: how to make reference (e.g. in line command) to objects which have been selected before?


    The general answer is that you use the variables in which you have stored the objects' entity names [in this case, PR1 and PR2].  HOWEVER, in this particular situation, you can't use them for the apparent intersection, because APPINT doesn't accept entity names as input.  [I assume Osnap in general doesn't, but I haven't tested the possibilities.]

     

    I had assumed that your "appint" followed by one pause would be for the User to position the aperture box over the apparent intersection itself [one pick] rather than pick on the two Lines separately.  That's the approach that would find the same location for both Osnaps.  But I was pleased to find, in experimenting a little, that if you pick on an object at a place where nothing appears to intersect it, it allows you to pick another object within the same pause, and that if you do that with two Lines in opposite order between the two APPINT calls, it correctly finds the two different locations, rather than using the projection onto the current plane that it does if you do one pick on an apparent intersection.

     

    Since you can't use entity names as input for APPINT, but you can use point locations, you can just use your P1 and P3, which are each an end of the two different Lines.  This seems to work if there's nothing else around that it might snap to instead of the two intended Lines:

     

    (command "_.line" "appint" P1 P3 "appint" P3 P1 "")

     

    But I can't imagine you can count on having nothing else around that might confuse things.  To overcome that problem, I suspect there's a way to do the entire thing without using Osnap, all by calculation.  I will play with that and get back if I figure it out.

    Kent Cooper
    *Expert Elite*
    Kent1Cooper
    Posts: 5,294
    Registered: ‎09-13-2004
    Message 10 of 25 (1,014 Views)

    Re: Skew lines -shortest distance

    12-06-2010 09:57 AM in reply to: Kent1Cooper

    Kent1Cooper wrote:
    ....I suspect there's a way to do the entire thing without using Osnap, all by calculation.  I will play with that and get back if I figure it out.

    This seems to do that, in limited testing:

     

    (defun C:LSBSL ; = Line - Shortest Between Skewed Lines
      (/ L1 L2 1data 2data 1A 1B 2A 2B 2Y)
      (setq
        L1 (car (entsel "\nSelect a Line: "))
        L2 (car (entsel "\nSelect another Line: "))
        1data (entget L1)
        2data (entget L2)
        1A (cdr (assoc 10 1data)); start of first Line
        1B (cdr (assoc 11 1data)); end of first Line
        2A (cdr (assoc 10 2data)); start of second Line
        2B (cdr (assoc 11 2data)); end of second Line
      )
      (setvar 'osmode 0)
      (command
        "_.ucs" "_za" 1A 1B; looking along first Line
        "_.ucs" "_z" (trans 2A 0 1) (trans 2B 0 1)
          ; make second Line horizontal in first-Line view
      ); end command
      (setq 2Y (cadr (trans 2A 0 1)))
        ; Y position of second Line relative to first, current UCS
      (command
        "_line"
          (inters ; closest point on second Line to first
             (trans 2A 0 1); ends of second Line
             (trans 2B 0 1)
             (list 0 2Y 0); parallel to first Line, in plane of second
             (list 0 2Y 1)
             nil
          ); end inters
          (list 0 0 (caddr (getvar 'lastpoint)))
            ; on first Line at current-Z location of closest point
          ""
        "_.ucs" "_previous"
        "_.ucs" "_previous"
      ); end command
    )

    It does not yet have error handling, or save or reset OSMODE, or have object-type selection controls, or turn off UCSFOLLOW, or any of the other usual stuff, but if it does what you want, those can be included easily enough.  It could even be made to work with Polyline line segments, or even with those or Lines that are parts of Blocks/Xrefs, if that's desirable.

     

    It might also be desirable to build something in to alert the User if the two Lines are parallel, or if they actually intersect, or maybe some other possibilities.

    Kent Cooper

    You are not logged in.

    Log into access your profile, ask and answer questions, share ideas and more. Haven't signed up yet? Register

    Announcements
    Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.

    Need installation help?

    Start with some of our most frequented solutions to get help installing your software.

    Ask the Community