Author Topic: Find Min Y Coordinate on a Polyline and insert block  (Read 6177 times)

0 Members and 1 Guest are viewing this topic.

pedroantonio

  • Guest
Find Min Y Coordinate on a Polyline and insert block
« 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

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.  

pedroantonio

  • Guest
Re: Find Min Y Coordinate on a Polyline and insert block
« Reply #1 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

Tharwat

  • Swamp Rat
  • Posts: 710
  • Hypersensitive
Re: Find Min Y Coordinate on a Polyline and insert block
« Reply #2 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.  

pedroantonio

  • Guest
Re: Find Min Y Coordinate on a Polyline and insert block
« Reply #3 on: January 23, 2015, 01:05:31 PM »
hi Tharwat . I try your code but i use polyline not LWPOLYLINE !!!

Tharwat

  • Swamp Rat
  • Posts: 710
  • Hypersensitive
Re: Find Min Y Coordinate on a Polyline and insert block
« Reply #4 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 .

pedroantonio

  • Guest
Re: Find Min Y Coordinate on a Polyline and insert block
« Reply #5 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

Tharwat

  • Swamp Rat
  • Posts: 710
  • Hypersensitive
Re: Find Min Y Coordinate on a Polyline and insert block
« Reply #6 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 .

pedroantonio

  • Guest
Re: Find Min Y Coordinate on a Polyline and insert block
« Reply #7 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")
        )
      )
    )
  )
)
)
 
 

Tharwat

  • Swamp Rat
  • Posts: 710
  • Hypersensitive
Re: Find Min Y Coordinate on a Polyline and insert block
« Reply #8 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)
)

efernal

  • Bull Frog
  • Posts: 206
Re: Find Min Y Coordinate on a Polyline and insert block
« Reply #9 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.  
e.fernal

pedroantonio

  • Guest
Re: Find Min Y Coordinate on a Polyline and insert block
« Reply #10 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

efernal

  • Bull Frog
  • Posts: 206
Re: Find Min Y Coordinate on a Polyline and insert block
« Reply #11 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.  
e.fernal

pedroantonio

  • Guest
Re: Find Min Y Coordinate on a Polyline and insert block
« Reply #12 on: January 23, 2015, 02:05:25 PM »
no give me this

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

pedroantonio

  • Guest
Re: Find Min Y Coordinate on a Polyline and insert block
« Reply #13 on: January 23, 2015, 02:09:20 PM »
Here is the files i use

ronjonp

  • Needs a day job
  • Posts: 7529
Re: Find Min Y Coordinate on a Polyline and insert block
« Reply #14 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.  :?

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC