TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: pedroantonio on January 23, 2015, 11:52:26 AM

Title: Find Min Y Coordinate on a Polyline and insert block
Post by: pedroantonio on January 23, 2015, 11:52:26 AM
Hi . After a little search i find a lisp code in this page http://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/min-y-coordinate-on-a-polyline/td-p/2601957 (http://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/min-y-coordinate-on-a-polyline/td-p/2601957)

I want a litle help to do some changes. Tis lisp finds  Min Y Coordinate on a Polyline.

I want to find the  Min Y Coordinate on a Polyline and then insert automaticaly a  block in point (0.00,y). The y=  Min Y - 14 meters

Code - Auto/Visual Lisp: [Select]
  1. (defun C:test ()
  2. ; for Minimum Y coordinate of an object
  3. (vl-load-com); just in case
  4. (vlax-ename->vla-object (car (entsel)))
  5. 'LL 'UR
  6. )
  7. (rtos (cadr (vlax-safearray->list LL)) 2 2)
  8. )
  9.  
  10. ;Insert automatocaly block in (0.00,[Minimum Y - 14.00]) <-- i don't now haw to give this !!
  11. (setq dt1 (getpoint "\n inset block  (0.00,[Minimum Y - 14.00]) :"))
  12.  (command "insert" "*block" dt1 "" "0")
  13. )
  14.  
Title: Re: Find Min Y Coordinate on a Polyline and insert block
Post by: pedroantonio on January 23, 2015, 12:16:45 PM
I am almost there but something is missing .I dont't know how to give the coordinate

Code - Auto/Visual Lisp: [Select]
  1. (defun C:test ()
  2. ; for Minimum Y coordinate of an object
  3. (vl-load-com); just in case
  4. (vlax-ename->vla-object (car (entsel)))
  5. 'LL 'UR
  6. )
  7. (rtos (cadr (vlax-safearray->list LL)) 2 2)
  8. (setq y (- 14 (cadr (vlax-safearray->list LL)))))
  9. (setq dt1 (0.00,y))
  10.  (command "insert" "*block" dt1 "" "0")
  11. )
  12.  
  13.  

Thanks
Title: Re: Find Min Y Coordinate on a Polyline and insert block
Post by: Tharwat on January 23, 2015, 12:54:42 PM
Firstly change the Block name as per yours .
Secondly , is this the one you are after ?

Code - Auto/Visual Lisp: [Select]
  1. (princ "\n Select LWPolyline ")
  2. (if (setq o (ssget "_+.:S" '((0 . "LWPOLYLINE"))))
  3.     (list
  4.       '(0 . "INSERT")
  5.       (cons 2 "BlockName")
  6.       (cons
  7.         10
  8.         (list 0.
  9.               (- 14
  10.                  (cadar (vl-sort (mapcar 'cdr
  11.                                          (vl-remove-if-not
  12.                                            '(lambda (p) (eq (car p) 10))
  13.                                            (entget (ssname o 0))
  14.                                          )
  15.                                  )
  16.                                  '(lambda (j k) (< (cadr j) (cadr k)))
  17.                         )
  18.                  )
  19.               )
  20.               0.
  21.         )
  22.       )
  23.     )
  24.   )
  25. )
  26.  
  27.  
  28.  
Title: Re: Find Min Y Coordinate on a Polyline and insert block
Post by: pedroantonio on January 23, 2015, 01:05:31 PM
hi Tharwat . I try your code but i use polyline not LWPOLYLINE !!!
Title: Re: Find Min Y Coordinate on a Polyline and insert block
Post by: Tharwat on January 23, 2015, 01:09:01 PM
hi Tharwat . I try your code but i use polyline not LWPOLYLINE !!!
Remove the LW before the object name Polyline and try again , otherwise upload a sample drawing showing before and after with the block that to be inserted .
Title: Re: Find Min Y Coordinate on a Polyline and insert block
Post by: pedroantonio on January 23, 2015, 01:11:35 PM
i change the code to

Code: [Select]
(defun C:test ()
(princ "\n Select Polyline ")
(if (setq o (ssget "_+.:S" '((0 . "POLYLINE"))))
  (entmake
    (list
      '(0 . "INSERT")
      (cons 2 "*pindiat")
      (cons
        10
        (list 0.
              (- 14
                 (cadar (vl-sort (mapcar 'cdr
                                         (vl-remove-if-not
                                           '(lambda (p) (eq (car p) 10))
                                           (entget (ssname o 0))
                                         )
                                 )
                                 '(lambda (j k) (< (cadr j) (cadr k)))
                        )
                 )
              )
              0.
        )
      )
    )
  )
)
)

i put the lisp in the same folder with the block but the block didn't insert  :-o
Title: Re: Find Min Y Coordinate on a Polyline and insert block
Post by: Tharwat on January 23, 2015, 01:16:24 PM
i put the lisp in the same folder with the block but the block didn't insert  :-o

If you want to insert the block from outside the drawing , you should use the insert command , that's if the insertion point is taken correctly as we have retrieved it with the codes .
Title: Re: Find Min Y Coordinate on a Polyline and insert block
Post by: pedroantonio on January 23, 2015, 01:25:13 PM
How to do it like this !!

Code: [Select]
(defun C:test ()
(princ "\n Select Polyline ")
(if (setq o (ssget "_+.:S" '((0 . "POLYLINE"))))

  (entmake
    (list
      '(0 . "INSERT")
      (cons 2 "pindiat")
      (cons
        10
        (list 0.
              (- 14
                 (cadar (vl-sort (mapcar 'cdr
                                         (vl-remove-if-not
                                           '(lambda (p) (eq (car p) 10))
                                           (entget (ssname o 0))
                                         )
                                 )
                                 '(lambda (j k) (< (cadr j) (cadr k)))
                        )
                 )
              )
              0.
 (command "insert" "*pindiat" dt1 "" "0")
        )
      )
    )
  )
)
)
 
 
Title: Re: Find Min Y Coordinate on a Polyline and insert block
Post by: Tharwat on January 23, 2015, 01:53:12 PM
My program still using LWPOLYLINE .

Code: [Select]
(defun c:Test (/ o path)
  (princ "\n Select LWPolyline ")
  (if (and (setq o (ssget "_+.:S" '((0 . "LWPOLYLINE"))))
           (setq path (findfile (strcat (getvar 'DWGPREFIX) "pindiat.dwg")))
      )
    (progn
      (command
        "_.-insert"
        path
        nil
      )
      (if (tblsearch "BLOCK" "pindiat")
        (entmake
          (list
            '(0 . "INSERT")
            (cons 2 "pindiat")
            (cons
              10
              (list
                0.
                (-
                  14
                  (cadar (vl-sort (mapcar 'cdr
                                          (vl-remove-if-not
                                            '(lambda (p) (eq (car p) 10))
                                            (entget (ssname o 0))
                                          )
                                  )
                                  '(lambda (j k) (< (cadr j) (cadr k)))
                         )
                  )
                )
                0.
              )
            )
          )
        )
      )
    )
  )
  (if (not path)
    (alert "Block name is not found in Directory !!")
  )
  (princ)
)
Title: Re: Find Min Y Coordinate on a Polyline and insert block
Post by: efernal on January 23, 2015, 01:54:57 PM
Code - Auto/Visual Lisp: [Select]
  1. (DEFUN c:test (/ dxf pline ymin)
  2.   (IF (AND (SETQ pline (CAR (ENTSEL "\n-> Select POLYLINE entity : ")))
  3.            (= (CDR (ASSOC 0 (SETQ dxf (ENTGET pline)))) "POLYLINE")
  4.            (MEMBER (CDR (ASSOC 70 dxf)) '(0 1 128 129))
  5.       )
  6.     (IF (TBLSEARCH "BLOCK" "PINDIAT")
  7.       (PROGN (SETQ ymin 0.0)
  8.              (WHILE (SETQ pline (ENTNEXT pline))
  9.                (IF (= (CDR (ASSOC 0 (SETQ dxf (ENTGET pline)))) "VERTEX")
  10.                  (IF (< (CADR (CDR (ASSOC 10 dxf))) ymin)
  11.                    (SETQ ymin (CADR (CDR (ASSOC 10 dxf))))
  12.                  )
  13.                )
  14.              )
  15.              (ENTMAKE (LIST (CONS 0 "INSERT")
  16.                             (CONS 2 "PINDIAT")
  17.                             (CONS 10 (LIST 0.0 ymin 0.0))
  18.                             (CONS 41 1.0)
  19.                             (CONS 42 1.0)
  20.                             (CONS 43 1.0)
  21.                             (CONS 50 0.0)
  22.                             (CONS 210 (LIST 0.0 0.0 1.0))
  23.                       )
  24.              )
  25.       )
  26.       (ALERT "Error: No block PINDIAT definition...")
  27.     )
  28.     (PRINC "\n-> Not a valid POLYLINE...")
  29.   )
  30.   (PRINC)
  31. )
  32.  
Title: Re: Find Min Y Coordinate on a Polyline and insert block
Post by: pedroantonio on January 23, 2015, 02:01:30 PM
I use the two codes and efernal and Tharwat (I change LWPOLYLINE to POLYLINE ) but the block didn't insert .My block is in folder c:\ dwg
Title: Re: Find Min Y Coordinate on a Polyline and insert block
Post by: efernal on January 23, 2015, 02:02:08 PM
Code - Auto/Visual Lisp: [Select]
  1. ;; BETTER...
  2.  
  3. (DEFUN c:test (/ dxf pline ly)
  4.   (IF (AND (SETQ pline (CAR (ENTSEL "\n-> Select POLYLINE entity : ")))
  5.            (= (CDR (ASSOC 0 (SETQ dxf (ENTGET pline)))) "POLYLINE")
  6.            (MEMBER (CDR (ASSOC 70 dxf)) '(0 1 128 129))
  7.       )
  8.     (IF (TBLSEARCH "BLOCK" "PINDIAT")
  9.       (PROGN (SETQ ymin 0.0)
  10.              (WHILE (SETQ pline (ENTNEXT pline))
  11.                (IF (= (CDR (ASSOC 0 (SETQ dxf (ENTGET pline)))) "VERTEX")
  12.                  (SETQ ly (APPEND ly (LIST (CADR (CDR (ASSOC 10 dxf))))))
  13.                )
  14.              )
  15.              (ENTMAKE (LIST (CONS 0 "INSERT")
  16.                             (CONS 2 "PINDIAT")
  17.                             (CONS 10 (LIST 0.0 (APPLY 'MIN ly) 0.0))
  18.                             (CONS 41 1.0)
  19.                             (CONS 42 1.0)
  20.                             (CONS 43 1.0)
  21.                             (CONS 50 0.0)
  22.                             (CONS 210 (LIST 0.0 0.0 1.0))
  23.                       )
  24.              )
  25.       )
  26.       (ALERT "Error: No block PINDIAT definition...")
  27.     )
  28.     (PRINC "\n-> Not a valid POLYLINE...")
  29.   )
  30.   (PRINC)
  31. )
  32.  
Title: Re: Find Min Y Coordinate on a Polyline and insert block
Post by: pedroantonio on January 23, 2015, 02:05:25 PM
no give me this

-> Select POLYLINE entity :
-> Not a valid POLYLINE...
Title: Re: Find Min Y Coordinate on a Polyline and insert block
Post by: pedroantonio on January 23, 2015, 02:09:20 PM
Here is the files i use
Title: Re: Find Min Y Coordinate on a Polyline and insert block
Post by: ronjonp on January 23, 2015, 02:13:12 PM


hi Tharwat . I try your code but i use polyline not LWPOLYLINE !!!

no give me this

-> Select POLYLINE entity :
-> Not a valid POLYLINE...

First off you have to know what type of objects you are selecting .. you say one thing then your drawing contradicts it.  :?
Title: Re: Find Min Y Coordinate on a Polyline and insert block
Post by: efernal on January 23, 2015, 02:13:52 PM
your test.dwg contains two LWPOLYLINE, not POLYLINE...

Title: Re: Find Min Y Coordinate on a Polyline and insert block
Post by: pedroantonio on January 23, 2015, 02:16:24 PM
when in properties is polyline  !!! :-o
Title: Re: Find Min Y Coordinate on a Polyline and insert block
Post by: Tharwat on January 23, 2015, 02:18:03 PM
Are you sure that you have the block name in the same directory of the current opened drawing ?

Code: [Select]
(defun c:Test (/ o x e l path)
  (princ "\n Select Polyline ")
  (if (and (setq o (ssget "_+.:S" '((0 . "POLYLINE"))))
           (setq x (ssname o 0))
           (while (and (setq x (entnext x))
                       (member '(0 . "VERTEX") (setq e (entget x)))
                  )
             (setq l (cons (cdr (assoc 10 e)) l))
           )
           (setq path (findfile (strcat (getvar 'DWGPREFIX) "pindiat.dwg")))
      )
    (progn
      (command
        "_.-insert"
        path
        nil
      )
      (if (tblsearch "BLOCK" "pindiat")
        (entmake
          (list
            '(0 . "INSERT")
            (cons 2 "pindiat")
            (cons
              10
              (list
                0.
                (- 14
                   (cadar (vl-sort l
                                   '(lambda (j k) (< (cadr j) (cadr k)))
                          )
                   )
                )
                0.
              )
            )
          )
        )
      )
    )
  )
  (if (not path)
    (alert "Block name is not found in Directory !!")
  )
  (princ)
)
Title: Re: Find Min Y Coordinate on a Polyline and insert block
Post by: pedroantonio on January 23, 2015, 02:24:55 PM
Gives me the message   (alert "Block name is not found in Directory !!") ,but the lisp is with the block in the same folder ... i can't  understand !
Title: Re: Find Min Y Coordinate on a Polyline and insert block
Post by: Tharwat on January 23, 2015, 02:29:21 PM
You need to read what users advise you with every thread and not just copy and paste the codes to use .
Read what I have written above the last routine that I posted .
Title: Re: Find Min Y Coordinate on a Polyline and insert block
Post by: pedroantonio on January 23, 2015, 02:31:19 PM
I read it .Yes i am sure that i have the block name in the same directory of the current opened drawing
Title: Re: Find Min Y Coordinate on a Polyline and insert block
Post by: ronjonp on January 23, 2015, 02:36:46 PM
You need to read what users advise you with every thread and not just copy and paste the codes to use .
Read what I have written above the last routine that I posted .
It's easier to just make the block rather than travel down the rabbit hole LOL.

Code - Auto/Visual Lisp: [Select]
  1. (defun c:foo (/ o)
  2.   (if (not (tblobjname "block" "pindiat"))
  3.     (progn (entmake '((0 . "BLOCK")
  4.                       (100 . "AcDbEntity")
  5.                       (67 . 0)
  6.                       (8 . "0")
  7.                       (100 . "AcDbBlockReference")
  8.                       (2 . "pindiat")
  9.                       (10 0.0 0.0 0.0)
  10.                       (70 . 0)
  11.                      )
  12.            )
  13.            (entmake '((0 . "LWPOLYLINE")
  14.                       (100 . "AcDbEntity")
  15.                       (67 . 0)
  16.                       (8 . "0")
  17.                       (100 . "AcDbPolyline")
  18.                       (90 . 2)
  19.                       (70 . 128)
  20.                       (43 . 0.0)
  21.                       (38 . 0.0)
  22.                       (39 . 0.0)
  23.                       (10 -8.660870552062988 0.0)
  24.                       (40 . 0.0)
  25.                       (41 . 0.0)
  26.                       (42 . 0.0)
  27.                       (91 . 0)
  28.                       (10 9.859517574310302 0.0)
  29.                       (40 . 0.0)
  30.                       (41 . 0.0)
  31.                       (42 . 0.0)
  32.                       (91 . 0)
  33.                      )
  34.            )
  35.            (entmake '((0 . "LWPOLYLINE")
  36.                       (100 . "AcDbEntity")
  37.                       (67 . 0)
  38.                       (8 . "0")
  39.                       (100 . "AcDbPolyline")
  40.                       (90 . 2)
  41.                       (70 . 128)
  42.                       (43 . 0.0)
  43.                       (38 . 0.0)
  44.                       (39 . 0.0)
  45.                       (10 -8.660870552062988 -2.40)
  46.                       (40 . 0.0)
  47.                       (41 . 0.0)
  48.                       (42 . 0.0)
  49.                       (91 . 0)
  50.                       (10 9.859517574310302 -2.40)
  51.                       (40 . 0.0)
  52.                       (41 . 0.0)
  53.                       (42 . 0.0)
  54.                       (91 . 0)
  55.                      )
  56.            )
  57.            (entmake '((0 . "LWPOLYLINE")
  58.                       (100 . "AcDbEntity")
  59.                       (67 . 0)
  60.                       (8 . "0")
  61.                       (100 . "AcDbPolyline")
  62.                       (90 . 2)
  63.                       (70 . 128)
  64.                       (43 . 0.0)
  65.                       (38 . 0.0)
  66.                       (39 . 0.0)
  67.                       (10 -8.660870552062988 -4.80)
  68.                       (40 . 0.0)
  69.                       (41 . 0.0)
  70.                       (42 . 0.0)
  71.                       (91 . 0)
  72.                       (10 9.859517574310302 -4.80)
  73.                       (40 . 0.0)
  74.                       (41 . 0.0)
  75.                       (42 . 0.0)
  76.                       (91 . 0)
  77.                      )
  78.            )
  79.            (entmake '((0 . "LWPOLYLINE")
  80.                       (100 . "AcDbEntity")
  81.                       (67 . 0)
  82.                       (8 . "0")
  83.                       (100 . "AcDbPolyline")
  84.                       (90 . 2)
  85.                       (70 . 128)
  86.                       (43 . 0.0)
  87.                       (38 . 0.0)
  88.                       (39 . 0.0)
  89.                       (10 -8.660870552062988 -7.20)
  90.                       (40 . 0.0)
  91.                       (41 . 0.0)
  92.                       (42 . 0.0)
  93.                       (91 . 0)
  94.                       (10 9.859517574310302 -7.20)
  95.                       (40 . 0.0)
  96.                       (41 . 0.0)
  97.                       (42 . 0.0)
  98.                       (91 . 0)
  99.                      )
  100.            )
  101.            (entmake '((0 . "LWPOLYLINE")
  102.                       (100 . "AcDbEntity")
  103.                       (67 . 0)
  104.                       (8 . "0")
  105.                       (100 . "AcDbPolyline")
  106.                       (90 . 2)
  107.                       (70 . 128)
  108.                       (43 . 0.0)
  109.                       (38 . 0.0)
  110.                       (39 . 0.0)
  111.                       (10 -8.660870552062988 -9.60)
  112.                       (40 . 0.0)
  113.                       (41 . 0.0)
  114.                       (42 . 0.0)
  115.                       (91 . 0)
  116.                       (10 9.859517574310302 -9.60)
  117.                       (40 . 0.0)
  118.                       (41 . 0.0)
  119.                       (42 . 0.0)
  120.                       (91 . 0)
  121.                      )
  122.            )
  123.            (entmake '((0 . "ENDBLK") (100 . "AcDbBlockEnd") (8 . "0")))
  124.     )
  125.   )
  126.   (princ "\n Select LWPolyline ")
  127.   (if (setq o (ssget "_+.:S" '((0 . "LWPOLYLINE"))))
  128.     (entmake
  129.       (list
  130.         '(0 . "INSERT")
  131.         (cons 2 "pindiat")
  132.         (cons
  133.           10
  134.           (car (vl-sort
  135.                  (mapcar 'cdr (vl-remove-if-not '(lambda (p) (eq (car p) 10)) (entget (ssname o 0))))
  136.                  '(lambda (j k) (< (cadr j) (cadr k)))
  137.                )
  138.           )
  139.         )
  140.       )
  141.     )
  142.   )
  143.   (princ)
  144. )
Title: Re: Find Min Y Coordinate on a Polyline and insert block
Post by: Tharwat on January 23, 2015, 02:43:12 PM
You need to read what users advise you with every thread and not just copy and paste the codes to use .
Read what I have written above the last routine that I posted .
It's easier to just make the block rather than travel down the rabbit hole LOL.

 :-D

Although that I did not open the attached drawings , I think you are using the wrong entity name as I have did in my first post LOL
Title: Re: Find Min Y Coordinate on a Polyline and insert block
Post by: ronjonp on January 23, 2015, 02:45:21 PM
You need to read what users advise you with every thread and not just copy and paste the codes to use .
Read what I have written above the last routine that I posted .
It's easier to just make the block rather than travel down the rabbit hole LOL.

 ;D

Although that I did not open the attached drawings , I think you are using the wrong entity name as I have did in my first post LOL

The objects are LWPOLYLINE so your code should work.
Title: Re: Find Min Y Coordinate on a Polyline and insert block
Post by: pedroantonio on January 23, 2015, 02:55:45 PM
Nothing is working and i don't know how. The lisp and the block is in tha same folder but nothing insert in the drawing .. i don't  know  :-o :-o :-o :-o :-o

i use autocad 2010
Title: Re: Find Min Y Coordinate on a Polyline and insert block
Post by: ronjonp on January 23, 2015, 02:58:30 PM
Nothing is working and i don't know how. The lisp and the block is in tha same folder but nothing insert in the drawing .. i don't  know  :o :o :o :o :o

i use autocad 2010
Works here (http://www.theswamp.org/index.php?topic=48717.msg538140#msg538140)  :whistling:
Title: Re: Find Min Y Coordinate on a Polyline and insert block
Post by: pedroantonio on January 23, 2015, 03:11:17 PM
ronjonp i ask to insert the block in the point (0,y) y= 14-Min Poly y
Title: Re: Find Min Y Coordinate on a Polyline and insert block
Post by: pedroantonio on January 23, 2015, 03:17:31 PM
with this command the block is insert with the other lisp in not working i can not  understand why

Code: [Select]
(Defun c:PINDIAT ()
 (setq dt1 (getpoint "\n insert point :"))
 (command "insert" "*PINDIAT" dt1 "" "0")
)
Title: Re: Find Min Y Coordinate on a Polyline and insert block
Post by: ronjonp on January 23, 2015, 03:18:00 PM
I know what you asked for and Tharwat's code does that. Mine was just an example.

You have all the code to fix this yourself.

Take THIS (http://www.theswamp.org/index.php?topic=48717.msg538112#msg538112) and replace the same section in the code HERE (http://www.theswamp.org/index.php?topic=48717.msg538140#msg538140).

I know I've already said this but you really have to start applying yourself....