Author Topic: Select text and close polylines inside close polyline  (Read 1327 times)

0 Members and 1 Guest are viewing this topic.

mhy3sx

  • Newt
  • Posts: 166
Select text and close polylines inside close polyline
« on: June 06, 2024, 08:52:52 AM »
Hi, I have multi close polyline on "layer1".

I want to select only the close polylines on "layer2"  inside " layer1"  and move them to layer "movelayer".

In "layer2"  close polyline exist text on "layer3" .I want to select this text (only for close polylines of "layer2" inside layer1) and move them to layer "movelayer".

Look the dwg gile to understand.

The code I have select  all text and lot polylines on layers2

If it helps to find the inside of polyline all the close polylines are ccw

Code - Auto/Visual Lisp: [Select]
  1. (defun c:TEST ( / ss s1 s2 s3 i e x e1 e2 ii p1 p2 lst inside? get-polyline-vertices)
  2.  
  3.  
  4.  
  5.   (setq ss (ssget "_A" (list (cons 8 "layer1,layer2,layer3"))))
  6.  
  7.   (progn
  8.     (setq s1 (ssadd))
  9.     (setq s2 (ssadd))
  10.     (setq s3 (ssadd))
  11.  
  12.     (repeat (setq i (sslength ss))
  13.       (setq e (ssname ss (setq i (1- i))))
  14.       (setq x (entget e))
  15.       (cond
  16.         ((= (strcase (cdr (assoc 8 x))) "layer1")
  17.           (ssadd e s1))
  18.         ((= (strcase (cdr (assoc 8 x))) "layer2")
  19.           (ssadd e s2))
  20.         ((= (strcase (cdr (assoc 8 x))) "layer3")
  21.           (ssadd e s3))
  22.       )
  23.     )
  24.  
  25.  
  26.     (defun inside? (pt pl / minx maxx miny maxy)
  27.  
  28.       (setq minx (apply 'min (mapcar 'car pl)))
  29.       (setq maxx (apply 'max (mapcar 'car pl)))
  30.       (setq miny (apply 'min (mapcar 'cadr pl)))
  31.       (setq maxy (apply 'max (mapcar 'cadr pl)))
  32.  
  33.       (and (>= (car pt) minx) (<= (car pt) maxx)
  34.            (>= (cadr pt) miny) (<= (cadr pt) maxy))
  35.     )
  36.  
  37.  
  38.     (defun get-polyline-vertices (e / obj len inc pt pts)
  39.       (setq obj (vlax-ename->vla-object e))
  40.       (setq inc 0.0)
  41.       (setq pts '())
  42.       (while (<= inc len)
  43.         (setq pt (vlax-curve-getPointAtDist obj inc))
  44.         (setq pts (cons pt pts))
  45.         (setq inc (+ inc (vlax-curve-getDistAtParam obj 1.0)))
  46.       )
  47.       (reverse pts)
  48.     )
  49.                   (progn   (if (= (tblsearch "layer" "MOVELAYER") nil)
  50.               (command "_layer" "_m" "MOVELAYER" "_c" "10" "" ""))
  51.     (foreach e1 (vl-remove-if 'listp (mapcar 'cadr (ssnamex s1)))
  52.       (setq pl (get-polyline-vertices e1))
  53.       (foreach e2 (vl-remove-if 'listp (mapcar 'cadr (ssnamex s2)))
  54.         (if (inside? (vlax-curve-getStartPoint (vlax-ename->vla-object e2)) pl)
  55.           (entmod (subst (cons 8 "MOVELAYER") (assoc 8 (entget e2)) (entget e2)))
  56.         )
  57.       )
  58.       (foreach e3 (vl-remove-if 'listp (mapcar 'cadr (ssnamex s3)))
  59.         (if (inside? (cdr (assoc 10 (entget e3))) pl)
  60.           (entmod (subst (cons 8 "MOVELAYER") (assoc 8 (entget e3)) (entget e3)))
  61.         )
  62.              )
  63.      )
  64.     )
  65.   )
  66.   (princ)
  67. );end defun
  68.  
  69.  

Thanks
« Last Edit: June 06, 2024, 01:50:42 PM by mhy3sx »

mhy3sx

  • Newt
  • Posts: 166
Re: Select text and close polylines inside close polyline
« Reply #1 on: June 06, 2024, 01:50:20 PM »
is it possible to be done?

Thanks

ribarm

  • Gator
  • Posts: 3327
  • Marko Ribar, architect
Re: Select text and close polylines inside close polyline
« Reply #2 on: June 06, 2024, 02:09:43 PM »
Study this function :

Code - Auto/Visual Lisp: [Select]
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

mhy3sx

  • Newt
  • Posts: 166
Re: Select text and close polylines inside close polyline
« Reply #3 on: June 06, 2024, 04:23:54 PM »
Hi ribarm . Can you help to fix the code?

Thanks

mhy3sx

  • Newt
  • Posts: 166
Re: Select text and close polylines inside close polyline
« Reply #4 on: June 07, 2024, 03:22:41 AM »
I try this,  but select everything only inside the polyline and not overlap and inside. I have problem to make it select automatic the layer I want and not everything in the polyline

Code - Auto/Visual Lisp: [Select]
  1. (defun C:test( / i elist at cmde cen rad p1 impl)
  2.  (setq cmde (getvar "cmdecho"))
  3.  (setvar "cmdecho" 0)
  4.  (setq i 0 elist (entget (car (entsel "\nPick a bounding circle or polyline: "))))
  5.  (setvar "OSMODE" (boole 7 (getvar "OSMODE") 16384))
  6.  (if (zerop (getvar "CMDACTIVE")) (progn (setq impl T)(command "_select")))
  7.  (command "_wp") ; or _CP
  8.  (if (= (cdr(assoc 0 elist)) "CIRCLE")
  9.   (progn
  10.   (setq cen (cdr (assoc 10 elist))
  11.         rad (cdr (assoc 40 elist))
  12.   )
  13.   (repeat 90 ; 360/4  0.06981317=4*pi/180
  14.    (setq p1 (polar cen (* i 0.06981317) rad)  i (1+ i))
  15. ;   (command "_POINT" (trans p1 0 1))
  16.    (command (trans p1 0 1))
  17.   )); else
  18.   (repeat (length elist)
  19.    (setq at (nth i elist) i (1+ i))
  20. ;   (if (= (car at) 10) (command (cdr at)))
  21.    (if (= (car at) 10) (command (trans (cdr at) 0 1)))
  22.   )
  23.  );if CIRCLE
  24.  (command "")
  25.  (setvar "OSMODE" (boole 2 (getvar "OSMODE") 16384))
  26.  (setvar "cmdecho" cmde)
  27.  (if impl (progn (command "")(sssetfirst nil (ssget "_P"))))
  28.  (princ)
  29. )
  30.  


Thanks

BIGAL

  • Swamp Rat
  • Posts: 1471
  • 40 + years of using Autocad
Re: Select text and close polylines inside close polyline
« Reply #5 on: June 07, 2024, 08:59:13 PM »
If you look at Lee-mac.com  SSGET functions you will see that there is a select single object method, the advantage of ssget over entsel is that you can filter objects so in simple terms.

Code: [Select]
(setq ss (ssget '((0 . "LWPOLYLINE,CIRCLE"))))
(setq elist (entget (ssname ss 0)))

Not sure what your doing with osmode.
Code: [Select]
(setq oldsnap (getvar 'osmode))
(setvar 'osmode 0) ; snaps off or 16384
A man who never made a mistake never made anything

Marco Jacinto

  • Newt
  • Posts: 50
Re: Select text and close polylines inside close polyline
« Reply #6 on: June 10, 2024, 03:37:12 PM »
Code: [Select]
Some time without been here, hope this code helps you.

[code
(defun c:changeInsidePolys (/ EDATA LSTPOLSINSIDE POLSS SSPOLSINSIDE VPOL VTX)
  (IF
    (SETQ Polss (SSGET "_+.:E:S" '((0 . "LWPOLYLINE"))))
     (PROGN
       (SETQ edata (ENTGET (SSNAME Polss 0)))
       (SETQ vtx (massoc 10 edata))
       (SETQ ssPolsInside
      (SSGET "CP"
     vtx
     '((0 . "LWPOLYLINE") (8 . "Layer2") (70 . 1))
      )
       )
       (SETQ LstPolsInside
      (VL-REMOVE-IF 'LISTP (MAPCAR 'CADR (SSNAMEX ssPolsInside)))
       )
       (FOREACH pol LstPolsInside
(IF (AND (inside_p (mu:centroide (SETQ vpol (massoc 10 (ENTGET pol))))
    vtx
  )
  (SETQ ssPolsInside
(SSGET "WP"
vpol
'((0 . "*TEXT") (8 . "Layer3"))
)
  )
     )
   (PROGN
     (VLA-PUT-LAYER (VLAX-ENAME->VLA-OBJECT pol) "MOVELAYER")
     (VLA-PUT-LAYER
       (VLAX-ENAME->VLA-OBJECT (SSNAME ssPolsInside 0))
       "MOVELAYER"
     )
   )
)
       )
     )
    (alert "Nothing selected...")
  )
  (PRINC)
)


;defun from https://www.theswamp.org/index.php?topic=48399.30
(defun inside_p (:p :Lst / far_p cross on)
    (setq far_p (mapcar '+ '(1.0 1.0 0.0) (getvar "extmax")))
    (setq cross 0)
    (if (not (member :p :Lst))
        (mapcar
            '(lambda (a b)
                 (if (inters :p far_p a b) (setq cross (1+ cross)))
                 (if (equal (+ (distance :p a) (distance :p b)) (distance a b) 1e-8) (setq on t))
             )
            :Lst (cdr :Lst)
        )
        (setq on t)
    )
    (or (not (zerop (rem cross 2))) on)
)

;;;This function in some scenarios gives a point outside the pline
(DEFUN mu:centroide (ptLst / sumx sumy centroide)
  (SETQ sumx   0
sumy   0
centroide (MAPCAR
    '(LAMBDA (x)
       (SETQ sumx (+ (CAR x) sumx))
       (SETQ sumy (+ (CADR x) sumy))
     )
    ptLst
  )
centroide (LIST (/ sumx (LENGTH ptLst)) (/ sumy (LENGTH ptLst)))
  )
  centroide
)

(DEFUN massoc (key edata / vtx)
  (IF (AND key
   edata
   (SETQ vtx (VL-REMOVE-IF-NOT '(LAMBDA (x) (= key (CAR x))) edata))
      )
    (MAPCAR 'CDR vtx)
  )
)


mhy3sx

  • Newt
  • Posts: 166
Re: Select text and close polylines inside close polyline
« Reply #7 on: June 12, 2024, 03:12:11 AM »
Thanks Marco Jacinto !!

mhy3sx

  • Newt
  • Posts: 166
Re: Select text and close polylines inside close polyline
« Reply #8 on: June 13, 2024, 05:50:45 AM »
Hi. I  update the code to select automatic more than one polylines in layer1. I test it and work fine but in this example  give me this error (look the dwg file)

Code: [Select]
Error: incorrect type - nil
Can any one help?

Code - Auto/Visual Lisp: [Select]
  1. (defun c:changeInsidePolys (/ EDATA LSTPOLSINSIDE POLSS SSPOLSINSIDE VPOL VTX)
  2.   (if
  3.     (setq Polss (ssget "_X" '((0 . "LWPOLYLINE") (8 . "layer1") (70 . 1))))
  4.     (progn
  5.       (foreach pl (vl-remove-if 'listp (mapcar 'cadr (ssnamex Polss)))
  6.         (setq edata (entget pl))
  7.         (setq vtx (massoc 10 edata))
  8.         (setq ssPolsInside
  9.           (ssget "CP"
  10.             vtx
  11.             '((0 . "LWPOLYLINE") (8 . "layer2") (70 . 1))
  12.           )
  13.         )
  14.         (setq LstPolsInside
  15.           (vl-remove-if 'listp (mapcar 'cadr (ssnamex ssPolsInside)))
  16.         )
  17.         (foreach pol LstPolsInside
  18.           (if (and (inside_p (mu:centroide (setq vpol (massoc 10 (entget pol))))
  19.               vtx
  20.             )
  21.             (setq  ssPolsInside
  22.               (ssget  "WP"
  23.                 vpol
  24.                 '((0 . "*TEXT") (8 . "layer3"))
  25.               )
  26.             )
  27.           )
  28.             (progn
  29.               (if (= (tblsearch "layer" "BLD") nil)
  30.                 (command "_layer" "_m" "BLD" "_c" "10" "" ""))
  31.               (vla-put-layer (vlax-ename->vla-object pol) "BLD")
  32.               (vla-put-layer
  33.                 (vlax-ename->vla-object (ssname ssPolsInside 0))
  34.                 "BLD"
  35.               )
  36.             )
  37.           )
  38.         )
  39.       )
  40.     )
  41.       (prompt (strcat "\n No layer1 found."))
  42.   )
  43.   (princ)
  44. )
  45.  


Thanks

Marco Jacinto

  • Newt
  • Posts: 50
Re: Select text and close polylines inside close polyline
« Reply #9 on: June 27, 2024, 05:56:08 PM »
see if this helps

Code - Auto/Visual Lisp: [Select]
  1. (DEFUN c:changeInsidePolys (/ EDATA LSTPOLSINSIDE POLSS SSPOLSINSIDE VPOL VTX)
  2.   (IF (= (TBLSEARCH "layer" "BLD") nil)
  3.     (COMMAND "_layer" "_m" "BLD" "_c" "10" "" "")
  4.   )
  5.   (COND
  6.     ((NOT
  7.        (SETQ
  8.          Polss (SSGET "_X" '((0 . "LWPOLYLINE") (8 . "layer1") (70 . 1)))
  9.        )
  10.      )
  11.      (alert "\nNothing selected...")
  12.     )
  13.     (T
  14.      (FOREACH pl (VL-REMOVE-IF 'LISTP (MAPCAR 'CADR (SSNAMEX Polss)))
  15.        (SETQ edata (ENTGET pl))
  16.        (SETQ vtx (massoc 10 edata))
  17.        (COND
  18.          ((NOT
  19.             (AND (SETQ ssPolsInside
  20.                         (SSGET "CP"
  21.                                vtx
  22.                                '((0 . "LWPOLYLINE") (8 . "layer2") (70 . 1))
  23.                         )
  24.                  )
  25.                  (SETQ LstPolsInside
  26.                         (VL-REMOVE-IF 'LISTP (MAPCAR 'CADR (SSNAMEX ssPolsInside)))
  27.                  )
  28.             )
  29.           )
  30.           (princ "\n There's not polyline inside")
  31.          )
  32.          (T
  33.           (FOREACH pol LstPolsInside
  34.             (IF
  35.               (AND (inside_p (mu:centroide (SETQ vpol (massoc 10 (ENTGET pol))))
  36.                              vtx
  37.                    )
  38.                    (SETQ ssPolsInside
  39.                           (SSGET "WP"
  40.                                  vpol
  41.                                  '((0 . "*TEXT") (8 . "layer3"))
  42.                           )
  43.                    )
  44.               )
  45.                (PROGN
  46.                  (VLA-PUT-LAYER (VLAX-ENAME->VLA-OBJECT pol) "BLD")
  47.                  (VLA-PUT-LAYER
  48.                    (VLAX-ENAME->VLA-OBJECT (SSNAME ssPolsInside 0))
  49.                    "BLD"
  50.                  )
  51.                )
  52.             )
  53.           )
  54.          )
  55.        )
  56.      )
  57.     )
  58.   )
  59.   (PRINC)
  60. )

edit @kerry : add AutoLisp code formatting tag
Code - Auto/Visual Lisp: [Select]
  1.  
« Last Edit: July 01, 2024, 11:22:16 PM by kdub_nz »