Author Topic: Line between 2 endlines  (Read 510 times)

0 Members and 1 Guest are viewing this topic.

FS_AT12

  • Mosquito
  • Posts: 8
Line between 2 endlines
« on: May 16, 2024, 10:16:18 AM »
Hi
I am facing a problem with architectural drawings.
There are missing lines that I have to manually draw.

I have found a lisp that is basically doing the job but not at all. So I wanted to ask if anybody could help me to adapt this lisp with the following functions:

The lisp is asking for 2 Lines, and creates a new (closed) polyline connecting these 2 Lines, and deleting the selected 2 lines


I would need to change:
1: do not delete the source selected lines.
2: do not create a closed Polyline, just 2 Lines
3: the lisp is working just if the selected lines are LINES, is it possible that it is working with all kind of lines? Like POLYlLines, or Arcs or even Splines? Most important would be to work with Polylines, or even just the selected Polyline segments.


Code - Auto/Visual Lisp: [Select]
  1. (defun c:connectLines (/ line1 line2 data1 data2 pt1 pt2 pt3 pt4)
  2.   (and
  3.     (setq line1 (car (entsel "\nSelect first line: ")))
  4.     (= (cdr (assoc 0 (setq data1 (entget line1)))) "LINE")
  5.     (setq line2 (car (entsel "\nSelect second line: ")))
  6.     (= (cdr (assoc 0 (setq data2 (entget line2)))) "LINE")
  7.     (setq pt1 (cdr (assoc 10 data1))
  8.           pt2 (cdr (assoc 11 data1))
  9.           pt3 (cdr (assoc 11 data2))
  10.           pt4 (cdr (assoc 10 data2))
  11.     )
  12.     (or (< (distance pt2 pt3) (distance pt2 pt4))
  13.         (mapcar 'set '(pt3 pt4) (list pt4 pt3))
  14.     )
  15.     (command "_.erase"
  16.              (ssadd line2 (ssadd line1))
  17.              ""
  18.              "_.pline"
  19.              "_non"
  20.              (trans pt1 0 1)
  21.              "_non"
  22.              (trans pt2 0 1)
  23.              "_non"
  24.              (trans pt3 0 1)
  25.              "_non"
  26.              (trans pt4 0 1)
  27.              "_close"
  28.     )
  29.   )
  30.   (princ)
  31. )

https://stackoverflow.com/questions/47230782/draw-line-connecting-ends-of-2-parallel-lines-with-autolisp

It would be great if somebody could help me.



EDIT (John): 1. Fixed subject line spelling error. 2. Added code tags.
« Last Edit: May 16, 2024, 10:57:46 AM by JohnK »

dexus

  • Bull Frog
  • Posts: 213
Re: Line between 2 endlines
« Reply #1 on: May 16, 2024, 11:24:01 AM »
I use something similar to this written by Tharwat:
https://www.cadtutor.net/forum/topic/72563-autolisp-to-create-enclosed-polylines-from-parallel-lines/#comment-578774


If you want lines instead of closed polylines, you could explode the polylines at the end of the routine.

ribarm

  • Gator
  • Posts: 3310
  • Marko Ribar, architect
Re: Line between 2 endlines
« Reply #2 on: May 16, 2024, 11:54:35 AM »
If I understood correctly...

Code - Auto/Visual Lisp: [Select]
  1. (defun c:connect_with_lines ( / curve1 curve2 pt1 pt2 pt3 pt4 )
  2.  
  3.  
  4.   (setq curve1 (car (entsel "\nSelect first curve: ")))
  5.   (setq curve2 (car (entsel "\nSelect second curve: ")))
  6.         pt2 (vlax-curve-getendpoint curve1)
  7.         pt3 (vlax-curve-getstartpoint curve2)
  8.         pt4 (vlax-curve-getendpoint curve2)
  9.   )
  10.   (if (< (distance pt2 pt3) (distance pt2 pt4))
  11.     (mapcar (function set) (list (quote pt3) (quote pt4)) (list pt4 pt3))
  12.   )
  13.   (command "_.line" "_non" (trans pt1 0 1) "_non" (trans pt3 0 1) "" "_.line" "_non" (trans pt2 0 1) "_non" (trans pt4 0 1) "")
  14.   (princ)
  15. )
  16.  

HTH.
« Last Edit: May 16, 2024, 01:06:54 PM by ribarm »
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

FS_AT12

  • Mosquito
  • Posts: 8
Re: Line between 2 endlines
« Reply #3 on: May 16, 2024, 12:20:28 PM »
Dear dexus,
Thank you for the link. I like the behaviour that you can select multiple lines at once, this accelerates the procedure.
I have tested it.
For me, it is not working, since the Lines in the architectural drawings are from "old buildings" based on measurements, none of the lines are parallel to each other, (just a very little number).
The lisp is not creating any line. -(

ribarm

  • Gator
  • Posts: 3310
  • Marko Ribar, architect
Re: Line between 2 endlines
« Reply #4 on: May 16, 2024, 12:37:45 PM »
It could be that I made mistake with (set) function... It requires symbols instead of lists... Corrected now and it should work...
OP, what do you mean - not create any lines at all? It's clearly written in my code : (command "_.line" ... )
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

FS_AT12

  • Mosquito
  • Posts: 8
Re: Line between 2 endlines
« Reply #5 on: May 16, 2024, 12:46:13 PM »
Dear ribarm,

Thank you for your prompt reply.
In 99% of the selections I get this
I get this Error:

(LIST PT4 PT3)
; in file :
; C:\Users\...\connect_with_lines.lsp
;
; error : bad argument type <(301.187543505381 100.629323961606 0.0)> ; expected <SYMBOL> at [set]
:

the lisp is failing if the lines are not parallel, or the lines are  polylines,
I really dont know why it is not working.
The original lisp I have posted, works in every situation. Connect_with_lines & the test lisp (dexus) are failing in nearly every situation, I do not really understand why?


FS_AT12

  • Mosquito
  • Posts: 8
Re: Line between 2 endlines
« Reply #6 on: May 16, 2024, 12:59:43 PM »
It could be that I made mistake with (set) function... It requires symbols instead of lists... Corrected now and it should work...
OP, what do you mean - not create any lines at all? It's clearly written in my code : (command "_.line" ... )

Hi Ribarm, this was a reaction to the post of dexus.

Nevertheless, after your edit, it works fine as I tested it in some cases.
I do not get the error any more, it works on lines and on polylines (great!).
In some situations I get a wrong connection, so I end up with crossing lines instead of parallel lines, and 4 points are created.

Thank you!

ribarm

  • Gator
  • Posts: 3310
  • Marko Ribar, architect
Re: Line between 2 endlines
« Reply #7 on: May 16, 2024, 01:21:40 PM »
...so I end up with crossing lines instead of parallel lines...

I can not replicate this situation... Can you post *.DWG where this occur... Only small segment, not entire *.DWG as it could be forbidden sharing - if you work at such position in your organisation...
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

FS_AT12

  • Mosquito
  • Posts: 8
Re: Line between 2 endlines
« Reply #8 on: May 17, 2024, 03:27:25 AM »
dear ribarm,

If the two lines are not parallel, like this situation, I end up with crossing lines.
The 4 points do not appear again.



A big help would be if the connection of the selected lines are part of a closed or open polyline,
so the connection would filter out just the segments of the whole polyline.
Currently, the connection is made from the first and the last vertex of the polyline, somewhere else than the selected segments.




ribarm

  • Gator
  • Posts: 3310
  • Marko Ribar, architect
Re: Line between 2 endlines
« Reply #9 on: May 17, 2024, 07:06:40 AM »
I see it...
Try this lisp and see if now is all OK...

Code - Auto/Visual Lisp: [Select]
  1. (defun c:connect_with_lines ( / *error* cmd es1 es2 curve1 curve2 pick1 pick2 pt1 pt2 pt3 pt4 )
  2.  
  3.  
  4.   (defun *error* ( m )
  5.     (while (= 8 (logand 8 (getvar (quote undoctl))))
  6.       (if command-s
  7.         (command-s "_.undo" "_e")
  8.         (vl-cmdf "_.undo" "_e")
  9.       )
  10.     )
  11.     (if cmd
  12.       (setvar (quote cmdecho) cmd)
  13.     )
  14.     (if m
  15.       (prompt m)
  16.     )
  17.     (princ)
  18.   )
  19.  
  20.   (setq cmd (getvar (quote cmdecho)))
  21.   (setvar (quote cmdecho) 0)
  22.   (while (= 8 (logand 8 (getvar (quote undoctl))))
  23.     (command "_.undo" "_e")
  24.   )
  25.   (command "_.undo" "_be")
  26.   (if
  27.     (and
  28.       (setq es1 (entsel "\nPick first curve (on desired segment of polyline) : "))
  29.       (setq es2 (entsel "\nPick second curve (on desired segment of polyline) : "))
  30.     )
  31.     (progn
  32.       (setq curve1 (car es1))
  33.       (setq curve2 (car es2))
  34.       (setq pick1 (cadr es1))
  35.       (setq pick2 (cadr es2))
  36.       (if
  37.         (and
  38.           (wcmatch (cdr (assoc 0 (entget curve1))) "*POLYLINE")
  39.           (wcmatch (cdr (assoc 0 (entget curve2))) "*POLYLINE")
  40.         )
  41.         (setq pt1 (vlax-curve-getpointatparam curve1 (float (fix (vlax-curve-getparamatpoint curve1 (vlax-curve-getclosestpointto curve1 (trans pick1 1 0))))))
  42.               pt2 (vlax-curve-getpointatparam curve1 (float (fix (1+ (vlax-curve-getparamatpoint curve1 (vlax-curve-getclosestpointto curve1 (trans pick1 1 0)))))))
  43.               pt3 (vlax-curve-getpointatparam curve2 (float (fix (vlax-curve-getparamatpoint curve2 (vlax-curve-getclosestpointto curve2 (trans pick2 1 0))))))
  44.               pt4 (vlax-curve-getpointatparam curve2 (float (fix (1+ (vlax-curve-getparamatpoint curve2 (vlax-curve-getclosestpointto curve2 (trans pick2 1 0)))))))
  45.         )
  46.         (setq pt1 (vlax-curve-getstartpoint curve1)
  47.               pt2 (vlax-curve-getendpoint curve1)
  48.               pt3 (vlax-curve-getstartpoint curve2)
  49.               pt4 (vlax-curve-getendpoint curve2)
  50.         )
  51.       )
  52.       (if (> (+ (distance pt1 pt3) (distance pt2 pt4)) (+ (distance pt1 pt4) (distance pt2 pt3)))
  53.         (vl-cmdf "_.line" "_non" (trans pt1 0 1) "_non" (trans pt4 0 1) "" "_.line" "_non" (trans pt2 0 1) "_non" (trans pt3 0 1) "")
  54.         (vl-cmdf "_.line" "_non" (trans pt1 0 1) "_non" (trans pt3 0 1) "" "_.line" "_non" (trans pt2 0 1) "_non" (trans pt4 0 1) "")
  55.       )
  56.     )
  57.   )
  58.   (*error* nil)
  59. )
  60.  

Regards, M.R.
« Last Edit: May 17, 2024, 08:08:48 AM by ribarm »
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

FS_AT12

  • Mosquito
  • Posts: 8
Re: Line between 2 endlines
« Reply #10 on: May 17, 2024, 07:49:10 AM »
Hi Marko,

Thank you very very much, for the effort you are making here.
It already help me a lot, so thank you again.


I have tested the new version,
see here a Video of how it works.

-the simple line connections get crossed,
-the more complex polylines are working
-the combination of line and polyline is producing wrong connections.

Regards Franz

ribarm

  • Gator
  • Posts: 3310
  • Marko Ribar, architect
Re: Line between 2 endlines
« Reply #11 on: May 17, 2024, 08:21:03 AM »
HI, it's me again...
I only mod. inputs for getting points - should work and with combinations (poly-poly; line-poly; poly-line; line-line)... But still you should check as I saw your *.mp4... You could mod. something you see and I overlooked...

Code - Auto/Visual Lisp: [Select]
  1. (defun c:connect_with_lines ( / *error* cmd es1 es2 curve1 curve2 pick1 pick2 pt1 pt2 pt3 pt4 )
  2.  
  3.  
  4.   (defun *error* ( m )
  5.     (while (= 8 (logand 8 (getvar (quote undoctl))))
  6.       (if command-s
  7.         (command-s "_.undo" "_e")
  8.         (vl-cmdf "_.undo" "_e")
  9.       )
  10.     )
  11.     (if cmd
  12.       (setvar (quote cmdecho) cmd)
  13.     )
  14.     (if m
  15.       (prompt m)
  16.     )
  17.     (princ)
  18.   )
  19.  
  20.   (setq cmd (getvar (quote cmdecho)))
  21.   (setvar (quote cmdecho) 0)
  22.   (while (= 8 (logand 8 (getvar (quote undoctl))))
  23.     (vl-cmdf "_.undo" "_e")
  24.   )
  25.   (vl-cmdf "_.undo" "_be")
  26.   (if
  27.     (and
  28.       (setq es1 (entsel "\nPick first curve (on desired segment of polyline) : "))
  29.       (setq es2 (entsel "\nPick second curve (on desired segment of polyline) : "))
  30.     )
  31.     (progn
  32.       (setq curve1 (car es1))
  33.       (setq curve2 (car es2))
  34.       (setq pick1 (cadr es1))
  35.       (setq pick2 (cadr es2))
  36.       (cond
  37.         (
  38.           (and
  39.             (wcmatch (cdr (assoc 0 (entget curve1))) "*POLYLINE")
  40.             (wcmatch (cdr (assoc 0 (entget curve2))) "*POLYLINE")
  41.           )
  42.           (setq pt1 (vlax-curve-getpointatparam curve1 (float (fix (vlax-curve-getparamatpoint curve1 (vlax-curve-getclosestpointto curve1 (trans pick1 1 0))))))
  43.                 pt2 (vlax-curve-getpointatparam curve1 (float (fix (1+ (vlax-curve-getparamatpoint curve1 (vlax-curve-getclosestpointto curve1 (trans pick1 1 0)))))))
  44.                 pt3 (vlax-curve-getpointatparam curve2 (float (fix (vlax-curve-getparamatpoint curve2 (vlax-curve-getclosestpointto curve2 (trans pick2 1 0))))))
  45.                 pt4 (vlax-curve-getpointatparam curve2 (float (fix (1+ (vlax-curve-getparamatpoint curve2 (vlax-curve-getclosestpointto curve2 (trans pick2 1 0)))))))
  46.           )
  47.         )
  48.         (
  49.           (wcmatch (cdr (assoc 0 (entget curve1))) "*POLYLINE")
  50.           (setq pt1 (vlax-curve-getpointatparam curve1 (float (fix (vlax-curve-getparamatpoint curve1 (vlax-curve-getclosestpointto curve1 (trans pick1 1 0))))))
  51.                 pt2 (vlax-curve-getpointatparam curve1 (float (fix (1+ (vlax-curve-getparamatpoint curve1 (vlax-curve-getclosestpointto curve1 (trans pick1 1 0)))))))
  52.                 pt3 (vlax-curve-getstartpoint curve2)
  53.                 pt4 (vlax-curve-getendpoint curve2)
  54.           )
  55.         )
  56.         (
  57.           (wcmatch (cdr (assoc 0 (entget curve2))) "*POLYLINE")
  58.           (setq pt1 (vlax-curve-getstartpoint curve1)
  59.                 pt2 (vlax-curve-getendpoint curve1)
  60.                 pt3 (vlax-curve-getpointatparam curve2 (float (fix (vlax-curve-getparamatpoint curve2 (vlax-curve-getclosestpointto curve2 (trans pick2 1 0))))))
  61.                 pt4 (vlax-curve-getpointatparam curve2 (float (fix (1+ (vlax-curve-getparamatpoint curve2 (vlax-curve-getclosestpointto curve2 (trans pick2 1 0)))))))
  62.           )
  63.         )
  64.         ( t
  65.           (setq pt1 (vlax-curve-getstartpoint curve1)
  66.                 pt2 (vlax-curve-getendpoint curve1)
  67.                 pt3 (vlax-curve-getstartpoint curve2)
  68.                 pt4 (vlax-curve-getendpoint curve2)
  69.           )
  70.         )
  71.       )
  72.       (if (> (+ (distance pt1 pt3) (distance pt2 pt4)) (+ (distance pt1 pt4) (distance pt2 pt3)))
  73.         (vl-cmdf "_.line" "_non" (trans pt1 0 1) "_non" (trans pt4 0 1) "" "_.line" "_non" (trans pt2 0 1) "_non" (trans pt3 0 1) "")
  74.         (vl-cmdf "_.line" "_non" (trans pt1 0 1) "_non" (trans pt3 0 1) "" "_.line" "_non" (trans pt2 0 1) "_non" (trans pt4 0 1) "")
  75.       )
  76.     )
  77.   )
  78.   (*error* nil)
  79. )
  80.  
« Last Edit: May 17, 2024, 08:46:27 AM by ribarm »
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

FS_AT12

  • Mosquito
  • Posts: 8
Re: Line between 2 endlines
« Reply #12 on: May 17, 2024, 08:29:54 AM »
 Error: ;-(

; ----- Error around expression -----
; (DEFUN C:CONNECT_WITH_LINES_OLD NIL (AL_LEAVEDEFUN (AL_ENTERDEFUN 'C:CONNECT_WITH_LINES_OLD 'NIL '((PT4) (PT3) (PT2) (PT1) (CURVE2) (CURVE1))) (LET ((CURVE1) (CURVE2) (PT1) (PT2) (PT3) (PT4)) (OR (NOT (VL-CATCH-ALL-ERROR-P (VL-CATCH-ALL-APPLY #'VLAX-GET-ACAD-OBJECT NIL))) (VL-LOAD-COM)) (SETQ CURVE1 (CAR (ENTSEL "
Select first curve: "))) (SETQ CURVE2 (CAR (ENTSEL "
Select second curve: "))) (SETQ PT1 (VLAX-CURVE-GETSTARTPOINT CURVE1) PT2 (VLAX-CURVE-GETENDPOINT CURVE1) PT3 (VLAX-CURVE-GETSTARTPOINT CURVE2) PT4 (VLAX-CURVE-GETENDPOINT CURVE2)) (IF (< (DISTANCE PT2 PT3) (DISTANCE PT2 PT4)) (MAPCAR #'SET (LIST 'PT3 'PT4) (LIST PT4 PT3))) (COMMAND "_.line" "_non" (TRANS PT1 0 1) "_non" (TRANS PT3 0 1) "" "_.line" "_non" (TRANS PT2 0 1) "_non" (TRANS PT4 0 1) "") (PRINC))))
;
; error : malformed list on input at [read] : File <C:/Users/../user/connect_with_lines.lsp>
C:\Users\..\user\connect_with_lines.lsp loading failed.

EDIT:

I think There is a ")" missing in line 80

i restart the lisp but no line is created at all
this appears in the commandline
: CONNECT_WITH_LINES
Pick first curve (on desired segment of polyline) :
Pick second curve (on desired segment of polyline) : (809.073066301129 25427.1548283457 0.0)
« Last Edit: May 17, 2024, 08:38:27 AM by FS_AT12 »

ribarm

  • Gator
  • Posts: 3310
  • Marko Ribar, architect
Re: Line between 2 endlines
« Reply #13 on: May 17, 2024, 08:47:26 AM »
Re-test it now... I missed to close one bracket in 3rd (cond) statement...
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

FS_AT12

  • Mosquito
  • Posts: 8
Re: Line between 2 endlines
« Reply #14 on: May 17, 2024, 08:58:23 AM »
WOW!!!!

 :yay!:

THANK YOU VERY MUCH!

Everything is working as it should.

Franz