### Author Topic: Trim and Replace Line with One Pick  (Read 6683 times)

0 Members and 1 Guest are viewing this topic.

• Bull Frog
• Posts: 305
##### Re: Trim and Replace Line with One Pick
« Reply #15 on: September 26, 2013, 09:38:17 AM »
I cant get it to work on closed polylines

but let me know if this works any better:

Code - Auto/Visual Lisp: [Select]
1. ;;
2. ;;chlh_jd - http://www.theswamp.org/index.php?topic=40873.msg461337#msg461337
3. ;;
4. ;;20/09/13 Patrick Bourke 360geomatics.co.uk - modified to work with polylines
5. ;;
6.
7. (defun c:wt (/ e pt el l a b c p1 p2 p3 p4 p10 p11 oce pointlistb pointlistc)
8. ;(defun c:wt (/)
9.
10.   ;;trim and add new line for trim-space
11.   ;; by GSLS(SS) 2012-2-9
12.   (defun dxf (a l)
13.     (cdr (assoc a l))
14.   )
15.   (defun mindis-pt (l pt)
16.     (car
17.       (vl-sort l
18.                (function (lambda (e1 e2)
19.                            (< (distance pt e1) (distance pt e2))
20.                          )
21.                )
22.       )
23.     )
24.   )
25.   (setq oce (getvar "cmdecho"))
26.   (setvar "cmdecho" 0)
27.   (setq e (entlast))
28.   (prompt "Select a line :")
29.   (command "Trim" "")
30.   (while (setq pt (entsel "\nSelect a line :"))
31.     (command pt)
32.     (if (not (eq e (entlast)))
33.         (setq e (entlast)
34.               b   (entget (car pt))
35.               c   (entget e)
36.               PointListb (mapcar 'cdr (vl-remove-if-not '(lambda (x) (= 10 (car x))) b))
37.               PointListc (mapcar 'cdr (vl-remove-if-not '(lambda (x) (= 10 (car x))) c))
38.               p1  (car PointListb)
39.               p2  (car (reverse PointListb))
40.               p3  (car PointListc)
41.               p4  (car (reverse PointListc))
42. ;             p11 (mindis-pt (list p3 p4) p1)
43. ;             p10 (mindis-pt (list p1 p2) p3)
44.         )
45. ;       (entmake
46. ;         (list (cons 0 "LINE")
47. ;               (cons 10 p10)
48. ;               (cons 11 p11)
49. ;               (cons 6 "DASHED");_LineType must be loaded
50. ;               (cons 62 251)
51.
52.
53.
54.           (list (cons 0 "LINE") ;***
55.                 (cons 6 "BYLAYER")
56.                 (cons 8 "Walls")
57.                 (cons 10 p2) ;***
58.                 (cons 11 p3) ;***
59.                 (cons 39 0.0)
60.                 (cons 210 (list 0.0 0.0 1.0))
61.           )
62.         )
63.         )
64.       )
65.   )
66.   (command "")
67.
68.   (setvar "cmdecho" oce)
69.   (princ)
70. )

#### martinle

• Mosquito
• Posts: 13
##### Re: Trim and Replace Line with One Pick
« Reply #16 on: September 27, 2013, 12:26:09 AM »

Thanks for your help.
It works with the neck of the polyline. There is no line, but then pulled the Dashed linetype.
Why is that?

Greetings Martin

• Bull Frog
• Posts: 305
##### Re: Trim and Replace Line with One Pick
« Reply #17 on: September 27, 2013, 12:56:19 PM »
for the dashed line type just change lines 46 to 62 to this:

Code - Auto/Visual Lisp: [Select]
1.   (list (cons 0 "LINE")
2.  (cons 10 p10)
3.  (cons 11 p11)
4.  (cons 6 "DASHED");_LineType must be loaded
5.  (cons 62 251)
6.
7.
8.
9. ;       (entmake
10. ;        (list (cons 0 "LINE") ;***
11. ;               (cons 6 "BYLAYER")
12. ;               (cons 8 "Walls")
13. ;               (cons 10 p2) ;***
14. ;               (cons 11 p3) ;***
15. ;               (cons 39 0.0)
16. ;              (cons 210 (list 0.0 0.0 1.0))

#### Peter2

• Swamp Rat
• Posts: 556
##### Re: Trim and Replace Line with One Pick
« Reply #18 on: January 13, 2014, 09:54:55 AM »

Code - Auto/Visual Lisp: [Select]
1. ;;
2. ...
3.               PointListb (mapcar 'cdr (vl-remove-if-not '(lambda (x) (= 10 (car x))) b))
4.               PointListc (mapcar 'cdr (vl-remove-if-not '(lambda (x) (= 10 (car x))) c))
5. ...
6.
Hi Patrick
at the moment I have no idea what's the algorithm behind these lines. If you have a little bit of time it would be great if you add some comments to your code

Thanks
Peter

AutoCAD Map 3D 2018 German (so some technical terms will be badly retranslated to English)

#### Lee Mac

• Seagull
• Posts: 12265
• London, England
##### Re: Trim and Replace Line with One Pick
« Reply #19 on: January 14, 2014, 03:44:34 PM »
Code - Auto/Visual Lisp: [Select]
1.               PointListb (mapcar 'cdr (vl-remove-if-not '(lambda (x) (= 10 (car x))) b))
2.               PointListc (mapcar 'cdr (vl-remove-if-not '(lambda (x) (= 10 (car x))) c))
If you have a little bit of time it would be great if you add some comments to your code

Assuming variables 'b' & 'c' point to DXF data for an LWPolyline entity, the above expressions will return a list of vertices for the polylines described by the supplied DXF data lists.

You can see this by stepping through the expressions one expression at a time:

In the following example, assume that the variable 'elist' contains the following LWPolyline DXF data:
Code - Auto/Visual Lisp: [Select]
1. (
2.   (-1 . <Entity name: 7ef03b88>)
3.   (0 . "LWPOLYLINE")
4.   (330 . <Entity name: 7ef01cf8>)
5.   (5 . "359")
6.   (100 . "AcDbEntity")
7.   (67 . 0)
8.   (410 . "Model")
9.   (8 . "0")
10.   (100 . "AcDbPolyline")
11.   (90 . 4)
12.   (70 . 0)
13.   (43 . 0.0)
14.   (38 . 0.0)
15.   (39 . 0.0)
16.   (10 17.3428 14.1773)
17.   (40 . 0.0)
18.   (41 . 0.0)
19.   (42 . 0.0)
20.   (91 . 0)
21.   (10 19.951 16.9304)
22.   (40 . 0.0)
23.   (41 . 0.0)
24.   (42 . 0.0)
25.   (91 . 0)
26.   (10 23.6196 14.0079)
27.   (40 . 0.0)
28.   (41 . 0.0)
29.   (42 . 0.0)
30.   (91 . 0)
31.   (10 26.652 17.0151)
32.   (40 . 0.0)
33.   (41 . 0.0)
34.   (42 . 0.0)
35.   (91 . 0)
36.   (210 0.0 0.0 1.0)
37. )

Evaluating the vl-remove-if-not expression leaves only the DXF group 10 entries (you can read this literally from the expression: "remove if the first element of each list item is not equal to 10"):
Code - Auto/Visual Lisp: [Select]
1. _\$ (vl-remove-if-not '(lambda ( x ) (= 10 (car x))) elist)
2. (
3.   (10 17.3428 14.1773)
4.   (10 19.951 16.9304)
5.   (10 23.6196 14.0079)
6.   (10 26.652 17.0151)
7. )

Now, the mapcar expression evaluates the cdr function on each of these DXF group 10 items, returning each list with the first item (the DXF group code) removed, thus returning the vertex coordinates:
Code - Auto/Visual Lisp: [Select]
1. _\$ (mapcar 'cdr (vl-remove-if-not '(lambda ( x ) (= 10 (car x))) elist))
2. (
3.   (17.3428 14.1773)
4.   (19.951 16.9304)
5.   (23.6196 14.0079)
6.   (26.652 17.0151)
7. )

#### Peter2

• Swamp Rat
• Posts: 556
##### Re: Trim and Replace Line with One Pick
« Reply #20 on: January 16, 2014, 01:24:28 PM »
Thanks Lee

now I have a good base to study it (and later the rest of the code ..).
Peter

AutoCAD Map 3D 2018 German (so some technical terms will be badly retranslated to English)

#### Lee Mac

• Seagull
• Posts: 12265
• London, England
##### Re: Trim and Replace Line with One Pick
« Reply #21 on: January 16, 2014, 06:07:16 PM »
Thanks Lee

now I have a good base to study it (and later the rest of the code ..).

Excellent - you're welcome