Author Topic: selection of blocks  (Read 4961 times)

0 Members and 1 Guest are viewing this topic.

TAIKI

  • Guest
selection of blocks
« on: May 10, 2014, 03:56:05 PM »
Hello

First I’m sorry for my English, I’m French.

Just i want to pick a polyline and the result is a selection of all blocks who have the insertion point on this polyline.

After all bloc selected move on the layer of the polyline.

And do it on all polyline on the same layer.

Thanks

ymg

  • Guest
Re: selection of blocks
« Reply #1 on: May 10, 2014, 07:48:59 PM »
TAIKI,

Use your polyline as a fence for a selection and filter for block only.

Code: [Select]
(defun c:test (/ fence)
   (setq fence (listpol (car (entsel "\nSelectionnez le polyligne: "))))
   (sssetfirst nil (ssget "_F" fence '((0 . "INSERT"))))
)

;;; listpol   by Gille Chanteau                                               ;
;;; Returns the vertices list of any type of polyline (WCS coordinates)       ;
;;;                                                                           ;
;;; Argument                                                                  ;
;;; en, a polyline (ename or vla-object)                                      ;

(defun listpol (en / i p l) 
  (setq i (if (vlax-curve-IsClosed en)
       (vlax-curve-getEndParam en)
     (+ (vlax-curve-getEndParam en) 1)
  )
  )      
  (while (setq p (vlax-curve-getPointAtParam en (setq i (1- i))))
      (setq l (cons (trans p 0 1 ) l))
  )
)

ymg

TAIKI

  • Guest
Re: selection of blocks
« Reply #2 on: May 11, 2014, 02:10:07 AM »
Thank you

It’s a good start, just it is possible that filtered more to not take attributes on blocks but just the insertion point or all object.

And the most important do it on all polyline on the same layer, so when lisp prompt to select a polyline the code take all polyline in the same layer that the polyline you pick. To displace all blocks on all polyline on the same layers.

Thanks for your time.

snownut2

  • Swamp Rat
  • Posts: 971
  • Bricscad 22 Ultimate
Re: selection of blocks
« Reply #3 on: May 11, 2014, 07:41:44 AM »
Another drive-thru lisp order.

mailmaverick

  • Bull Frog
  • Posts: 493
Re: selection of blocks
« Reply #4 on: May 11, 2014, 01:03:24 PM »
Assuming your polyline is 2D, you can use (vla-getboundingbox ....) method to find out the bounding box of the polyline.
Then you can do (ssget "_CP" (list Pt1 Pt2 Pt3 Pt4) (list (cons 0 "INSERT"))) .... of all block entities within this bounding box.
where Pt1, Pt2, Pt3 and Pt4 are the vertices of this bounding box. Note to do  (vla-zoomwindow (vlax-get-acad-object) (vlax-3d-point pt1) (vlax-3d-point pt3)) before you do above ssget because it is important that the vertices of the polygon within which you want to get entities must be zoomed in on screen. Here Pt1 and Pt3 are (minx, miny) and (maxx, maxy) coordinates of the bounding box.

Then you can iterate through all the objects to find their insertion point and check whether the point lies on the polyline or not using (vlax-curve-getDistAtPoint..... method.


ymg

  • Guest
Re: selection of blocks
« Reply #5 on: May 11, 2014, 03:06:05 PM »
TAIKI,

Assuming you assign the Selection Set to variable ss.

Now iterate through it, the insertion point will be given by:

Code: [Select]
(repeat (setq i (sslength ss))
    (setq pl (cons (cdr (assoc 10 (entget (ssname ss (setq i (1- i)))))) pl))
)

ymg

TAIKI

  • Guest
Re: selection of blocks
« Reply #6 on: May 11, 2014, 03:44:49 PM »
thanks a lot

Sorry i'm starting with autolisp.

YMG your first code worked fine, but can you explain me how you create a variable ss to intergrate the code who give the insertion point for filtered.

if it take too time i would know if it possible to intergate a code like that:
Code - Auto/Visual Lisp: [Select]
  1. [/  (cond
  2.     (js
  3.       (sssetfirst nil js)
  4.       (initget "Existant Nouveau _Existent New")
  5.       (if (eq (getkword "\nTraiter jeu de sélection [Existant/Nouveau] <Existant>: ") "New")
  6.         (progn (sssetfirst nil nil) (setq js (ssadd) js (ssget)))
  7.       )
  8.     )
  9.     (T (setq js (ssget)))
  10.   )
  11. ]
  12.  

ymg

  • Guest
Re: selection of blocks
« Reply #7 on: May 11, 2014, 04:04:59 PM »

Code: [Select]
(defun c:test (/ fence)
   (setq fence (listpol (car (entsel "\nSelectionnez le polyligne: "))))
   (setq ss (ssget "_F" fence '((0 . "INSERT"))))
   (repeat (setq i (sslength ss))
       (setq pl (cons (cdr (assoc 10 (entget (ssname ss (setq i (1- i)))))) pl))
   )
)

;;; listpol   by Gille Chanteau                                               ;
;;; Returns the vertices list of any type of polyline (WCS coordinates)       ;
;;;                                                                           ;
;;; Argument                                                                  ;
;;; en, a polyline (ename or vla-object)                                      ;

(defun listpol (en / i p l) 
  (setq i (if (vlax-curve-IsClosed en)
       (vlax-curve-getEndParam en)
     (+ (vlax-curve-getEndParam en) 1)
  )
  )      
  (while (setq p (vlax-curve-getPointAtParam en (setq i (1- i))))
      (setq l (cons (trans p 0 1 ) l))
  )
)

(setq ss (ssget "_F" fence '((0 . "INSERT")))) you are assigning your selection set to variable ss

TAIKI

  • Guest
Re: selection of blocks
« Reply #8 on: May 11, 2014, 04:27:05 PM »
okay

it work fine but not easy to use. For me if your first code can do the job on many polyline at once it would be fantastique.

thank you for your help.

ymg

  • Guest
Re: selection of blocks
« Reply #9 on: May 11, 2014, 04:44:11 PM »
TAIKI,

All the ingredients are there.

Now you need to create a selection set of all the polylines.

Iterates the solution for as many polyline as you have.
Basically the same thing as when we go through the block.

Wrap your head around it, try to come up with the solution.

Post your code and If you need help, will do.

ymg

Bhull1985

  • Guest
Re: selection of blocks
« Reply #10 on: May 12, 2014, 02:21:04 PM »
Hint:
'((0 . "LINE,LWPOLYLINE"))))

TAIKI

  • Guest
Re: selection of blocks
« Reply #11 on: May 12, 2014, 04:13:25 PM »
i think i have a bug:

(
Code - C#: [Select]
  1. defun c:test (/ js fence ss i pl)
  2. (or
  3.     (setq js (ssget "_I"))
  4.     (setq js (ssget "_P"))
  5.   )
  6.   (cond
  7.     (js
  8.       (sssetfirst nil js)
  9.       (initget "Existant Nouveau _Existent New")
  10.       (if (eq (getkword "\nTraiter jeu de sélection [Existant/Nouveau] <Existant>: ") "New")
  11.         (progn (sssetfirst nil nil) (setq js (ssadd) js (ssget)))
  12.       )
  13.     )
  14.  (cond
  15.     (js
  16. (setq fence (listpol(car js)))
  17.          setq ss (ssget "_F" fence '((0 . "INSERT"))))
  18.   (repeat (setq i (sslength ss))
  19.       (setq pl (cons (cdr (assoc 10 (entget (ssname ss (setq i (1- i)))))) pl))
  20.    )
  21.    )     
  22. )
  23.  
  24. ;;; listpol   by Gille Chanteau                                               ;
  25. ;;; Returns the vertices list of any type of polyline (WCS coordinates)       ;
  26. ;;;                                                                           ;
  27. ;;; Argument                                                                  ;
  28. ;;; en, a polyline (ename or vla-object)                                      ;
  29.  
  30. (defun listpol (en / i p l)  
  31.   (setq i (if (vlax-curve-IsClosed en)
  32.              (vlax-curve-getEndParam en)
  33.              (+ (vlax-curve-getEndParam en) 1)
  34.           )
  35.   )          
  36.  (while (setq p (vlax-curve-getPointAtParam en (setq i (1- i))))
  37.      (setq l (cons (trans p 0 1 ) l))
  38.  )
  39. )
  40.  
  41.  
  42.  
  43.  
« Last Edit: May 16, 2014, 02:52:36 PM by TAIKI »

ymg

  • Guest
Re: selection of blocks
« Reply #12 on: May 12, 2014, 04:38:50 PM »
TAIKI,

You got more than one bug.

First and foremost subfunction listpol is expecting
an ename as argument.  You are supplying (car js).

Variable js is a selection set, you have to iterate through it,
or transform it in a list of ename.

ymg

TAIKI

  • Guest
Re: selection of blocks
« Reply #13 on: May 13, 2014, 03:16:51 PM »
it's better but idon't understand;

Code - Auto/Visual Lisp: [Select]
  1. (defun c:test (/ js fence ss i pl)
  2.     (setq js (ssget "_I"))
  3.     (setq js (ssget "_P"))
  4.   )
  5.   (cond
  6.     (js
  7.       (sssetfirst nil js)
  8.       (initget "Existant Nouveau _Existent New")
  9.       (if (eq (getkword "\nTraiter jeu de sélection [Existant/Nouveau] <Existant>: ") "New")
  10.         (progn (sssetfirst nil nil) (setq js (ssadd) js (ssget)))
  11.       )
  12.     )
  13.     (T (setq js (ssget)))
  14.   )
  15.   (cond
  16.     (js
  17. (setq fence (listpol(ssname js (setq n (1- n)))))
  18.          setq ss (ssget "_F" fence '((0 . "INSERT"))))
  19.    (repeat (setq i (sslength ss))
  20.        (setq pl (cons (cdr (assoc 10 (entget (ssname ss (setq i (1- i)))))) pl))
  21.     )
  22.    )     
  23. )
  24.  
  25. ;;; listpol   by Gille Chanteau                                               ;
  26. ;;; Returns the vertices list of any type of polyline (WCS coordinates)       ;
  27. ;;;                                                                           ;
  28. ;;; Argument                                                                  ;
  29. ;;; en, a polyline (ename or vla-object)                                      ;
  30.  
  31. (defun listpol (en / i p l)  
  32.              (vlax-curve-getEndParam en)
  33.              (+ (vlax-curve-getEndParam en) 1)
  34.           )
  35.   )          
  36.       (setq l (cons (trans p 0 1 ) l))
  37.   )
  38. )
« Last Edit: May 16, 2014, 02:59:41 PM by TAIKI »

ymg

  • Guest
Re: selection of blocks
« Reply #14 on: May 13, 2014, 08:30:23 PM »
TAIKI,

I believe you have problem making the difference between a selection set and an ename.

A Selection Set is a special list of ename, you access it like so:

Code: [Select]
(repeat (setq i (sslength js))
   (setq ename (ssname js  (setq i (1- i)))
         fence (listpol ename)
    ss (ssget "_F" fence '((0 . "INSERT")))
   )
   (repeat (setq n (sslength ss))
       (setq pl (cons (cdr (assoc 10 (entget (ssname ss (setq n (1- n)))))) pl))
   )


Try to plug this in your routine.

ymg

TAIKI

  • Guest
Re: selection of blocks
« Reply #15 on: May 14, 2014, 04:22:10 PM »
Hello

Just for information of my progression for the moment insuffisant numbers of argument on setq ename.


Code - Auto/Visual Lisp: [Select]
  1. (defun c:test (/ ename fence ss i pl)
  2.   (or
  3.     (setq js (ssget "_I"))
  4.     (setq js (ssget "_P"))
  5.   )
  6.   (cond
  7.     (js
  8.      (sssetfirst nil js)
  9.      (initget "Existant Nouveau _Existent New")
  10.      (if
  11.        (eq (getkword
  12.              "\nTraiter jeu de sélection [Existant/Nouveau] <Existant>: "
  13.            )
  14.            "New"
  15.        )
  16.         (progn (sssetfirst nil nil)
  17.                (setq js (ssadd)
  18.                      js (ssget)
  19.                )
  20.         )
  21.      )
  22.     )
  23.     (T (setq js (ssget)))
  24.     (cond
  25.      (js
  26.        (repeat (setq i (sslength js))
  27.          (setq ename (ssname js (setq i (1- i)))
  28.                fence (listpol ename)
  29.                ss (ssget "_F" fence '((0 . "INSERT")))
  30.                (repeat (setq i (sslength ss))
  31.                  (setq pl
  32.                         (cons
  33.                           (cdr (assoc 10
  34.                                       (entget (ssname ss (setq i (1- i))))
  35.                                )
  36.                           )
  37.                           pl
  38.                         )
  39.                  )
  40.                )
  41.          )
  42.        )
  43.  
  44. ;;; listpol   by Gille Chanteau                                               ;
  45. ;;; Returns the vertices list of any type of polyline (WCS coordinates)       ;
  46. ;;;                                                                           ;
  47. ;;; Argument                                                                  ;
  48. ;;; en, a polyline (ename or vla-object)                                      ;
  49.  
  50.        (defun listpol (en / i p l)
  51.          (setq i (if (vlax-curve-IsClosed en)
  52.                    (vlax-curve-getEndParam en)
  53.                    (+ (vlax-curve-getEndParam en) 1)
  54.                  )
  55.          )
  56.          (while
  57.            (setq p (vlax-curve-getPointAtParam en (setq i (1- i))))
  58.             (setq l (cons (trans p 0 1) l))
  59.          )
  60.        )
  61.      )
  62.     )
  63.   )
  64. )
« Last Edit: May 16, 2014, 03:00:31 PM by TAIKI »

ymg

  • Guest
Re: selection of blocks
« Reply #16 on: May 14, 2014, 05:01:01 PM »
TAIKI,

The two repeat loops are nested.
You cannot use the same variable as index.

Code: [Select]
(repeat (setq i (sslength js))
    (setq ename (ssname js (setq i (1- i)))
          fence (listpol ename)
          ss (ssget "_F" fence '((0 . "INSERT")))
    )  
    (repeat (setq j (sslength ss))
       (setq pl (cons (cdr (assoc 10 (entget (ssname ss (setq j (1- j)))))) pl))
    )


Do use code tags around your code when posting in the Forum.

To do so Highlight the code you pasted in the reply window
and press on the # button.

ymg

TAIKI

  • Guest
Re: selection of blocks
« Reply #17 on: May 15, 2014, 04:46:57 PM »
hello

OK i have correct some parenthesis probleme the lisp charge succesfuly but now i have this error:

no function definition: LISTPOL

Code - Auto/Visual Lisp: [Select]
  1. (defun c:test (/ ename fence listpol ss j pl)
  2.   (or
  3.     (setq js (ssget "_I"))
  4.     (setq js (ssget "_P"))
  5.   )
  6.   (cond
  7.     (js
  8.       (sssetfirst nil js)
  9.       (initget "Existant Nouveau _Existent New")
  10.       (if (eq (getkword "\nTraiter jeu de sélection [Existant/Nouveau] <Existant>: ") "New")
  11.         (progn
  12.           (sssetfirst nil nil)
  13.           (setq
  14.             js (ssadd)
  15.             js (ssget)
  16.           )
  17.         )
  18.       )
  19.     )
  20.     (T
  21.       (setq js (ssget))
  22.     )
  23.   )
  24.   (cond
  25.     (js
  26.       (repeat (setq i (sslength js))
  27.         (setq
  28.           ename (ssname js (setq i (1- i)))
  29.           fence (listpol ename)
  30.           ss (ssget "_F" fence '((0 . "INSERT")))
  31.         )
  32.       )
  33.       (repeat (setq j (sslength ss))
  34.         (setq pl
  35.           (cons
  36.             (cdr (assoc 10 (entget (ssname ss (setq j (1- j))))))
  37.             pl
  38.           )
  39.         )
  40.       )
  41.     )
  42.   )
  43. ;;; listpol by Gille Chanteau ;
  44. ;;; Returns the vertices list of any type of polyline (WCS coordinates) ;
  45. ;;; ;
  46. ;;; Argument ;
  47. ;;; en, a polyline (ename or vla-object) ;
  48.   (defun listpol (en / i p l)
  49.     (setq i
  50.       (if (vlax-curve-IsClosed en)
  51.         (vlax-curve-getEndParam en)
  52.         (+ (vlax-curve-getEndParam en) 1)
  53.       )
  54.     )
  55.     (while
  56.       (setq p (vlax-curve-getPointAtParam en (setq i (1- i))))
  57.       (setq l (cons (trans p 0 1) l))
  58.     )
  59.   )
  60. )

snownut2

  • Swamp Rat
  • Posts: 971
  • Bricscad 22 Ultimate
Re: selection of blocks
« Reply #18 on: May 15, 2014, 04:52:26 PM »
It appears that the function "listpol" is within the "c:test" function.

Place it prior to the rest of the code in "c:test" function, then it should work.

ronjonp

  • Needs a day job
  • Posts: 7529
Re: selection of blocks
« Reply #19 on: May 15, 2014, 04:53:37 PM »
It's because you have it localized (defun c:test (/ ename fence listpol ss j pl) and it is defined after your routine runs. Put it at the top of your code.


*too slow

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

TAIKI

  • Guest
Re: selection of blocks
« Reply #20 on: May 15, 2014, 05:09:50 PM »
it's not resolved listpl on the top or not i have the same problem.

thank you.

ymg

  • Guest
Re: selection of blocks
« Reply #21 on: May 15, 2014, 05:41:01 PM »
TAIKI,

I told you the repeat Loop are nested.
and they must be nested.

In your last code this is not what you are doing.

JS est une selection de polyligne.

Pour chaque polyligne de la selection JS tu crees
une selection de blocs dans SS, c'est la premiere boucle repeat

Pour chacun des blocs dans SS tu obtiens la coordonnee
du point d'insertion que tu accumules dans la liste PL,
c'est la seconde boucle repeat

Tu traites le prochain polyligne.

Tous les polylignes sont traites PL contiens tous
les points d'insertion.


Autre point, je crois que ton "cond" ne vas pas.

Pour debuter oublie la preselection ne fais qu'une
selection de polyligne avec (SSGET)

Une fois que tu maitrises, tu pourras rajouter la preselection.

Je vois que tu as trouve comment faire pour inserer le code dans le forum.
C'est bien, maintenant tu devrais modifier tes anciens "post" pour les rajouter.

Code: [Select]
(defun c:test (/ ename fence listpol ss  i j pl)
   ;;; listpol by Gille Chanteau                                              ;
   ;;; Returns the vertices list of any type of polyline (WCS coordinates)    ;
   ;;;                                                                        ;
   ;;; Argument                                                               ;
   ;;; en, a polyline (ename or vla-object)                                   ;
   (defun listpol (en / i p l)
       (setq i (if (vlax-curve-IsClosed en)
                  (vlax-curve-getEndParam en)
                  (+ (vlax-curve-getEndParam en) 1)
               )
       )
       (while (setq p (vlax-curve-getPointAtParam en (setq i (1- i))))
          (setq l (cons (trans p 0 1) l))
       )
    )

  ;---------Corps du Programme------------------------------------------------;
    (prompt "\nSelectionnez les Polylignes a traiter: ")
    (if (setq js (ssget '((0 . "*POLYLINE"))))
       (repeat (setq i (sslength js))
           (setq  ename (ssname js (setq i (1- i)))
                  fence (listpol ename)
                     ss (ssget "_F" fence '((0 . "INSERT")))
           )     
           (repeat (setq j (sslength ss))
              (setq pl (cons (cdr (assoc 10 (entget (ssname ss (setq j (1- j)))))) pl))
           ); Fin Seconde Boucle repeat
       ); Fin Premiere Boucle repeat
   ) 
)

ymg
« Last Edit: May 15, 2014, 06:08:19 PM by ymg »

TAIKI

  • Guest
Re: selection of blocks
« Reply #22 on: May 17, 2014, 07:18:07 PM »
hello
Thanks a lot ymg, i think i have do some improvement but i have an error bad argument numberp: nil.

Code - Auto/Visual Lisp: [Select]
  1. (defun c:test (/ listpol ename fence ss j k pl all)
  2. ;;; listpol by Gille Chanteau ;
  3. ;;; Returns the vertices list of any type of polyline (WCS coordinates) ;
  4. ;;; ;
  5. ;;; Argument ;
  6. ;;; en, a polyline (ename or vla-object) ;
  7.   (defun listpol (en / i p l)
  8.     (setq i
  9.       (if (vlax-curve-IsClosed en)
  10.         (vlax-curve-getEndParam en)
  11.         (+ (vlax-curve-getEndParam en) 1)
  12.       )
  13.     )
  14.     (while
  15.       (setq p (vlax-curve-getPointAtParam en (setq i (1- i))))
  16.       (setq l (cons (trans p 0 1) l))
  17.     )
  18.   )
  19.   ;---------Corps du Programme------------------------------------------------;
  20.  (princ "\nSélectionnez les polylignes: ")
  21.  (or
  22.     (setq js (ssget "_I"))
  23.     (setq js (ssget "_P"))
  24.   );or
  25.   (cond
  26.     (js
  27.       (sssetfirst nil js)
  28.       (initget "Existant Nouveau _Existent New")
  29.       (if (eq (getkword "\nTraiter jeu de sélection [Existant/Nouveau] <Existant>: ") "New")
  30.         (progn
  31.           (sssetfirst nil nil)
  32.           (setq
  33.             js (ssadd)
  34.             js (ssget)
  35.           )
  36.         )
  37.       );js
  38.     );cond
  39.     (T
  40.       (setq js (ssget))
  41.     );t
  42.   );or
  43. ;---------------------fin de preselection------------------------;
  44.  (setq all (ssadd))
  45.   (cond      ;1
  46.     (js
  47.       (repeat (setq i (sslength js))   ;1
  48.         (setq
  49.           ename (ssname js (setq i (1- i)))
  50.           fence (listpol ename)
  51.           ss (ssget "_F" fence '((0 . "INSERT")))
  52.         );setq
  53. ;----------------------------------------------------------------;
  54.         (cond     ;2
  55.           (ss
  56.         (repeat (setq i (sslength ss))    ;2
  57.           (setq pl (cons (cdr (assoc 10 (entget (ssname ss (setq j (1- j))))))pl))
  58. ;----------------------------------------------------------------;
  59.          (cond   ;3
  60.            (pl
  61.              (repeat (setq k (sslength pl))   ;3
  62.                (ssadd (ssname pl (setq k (1- k))) all)
  63.               );repeat3
  64.             );pl
  65. ;-----------------------------------------------------------------;
  66.                );cond3
  67.               );repeat2
  68.              );ss
  69.             );cond2
  70.         );repeat1
  71.    (sssetfirst nil all)
  72.           );js
  73.     );cond1
  74.  (prin1)
  75. );defun

i've write some note but i don't know if it was a probleme

ymg

  • Guest
Re: selection of blocks
« Reply #23 on: May 17, 2014, 08:16:49 PM »
TAIKI,

Did you try last code I posted ?

Problem is you want to play with pre-selection
forget it for the moment.

You don't use the cruise control while learning to drive.

Another reason not to use pre-selection you would need
to check every item to filter out anything that is not a
polyline.

Another question why do you use cond when an if will do ?

ymg
« Last Edit: May 17, 2014, 08:54:21 PM by ymg »

TAIKI

  • Guest
Re: selection of blocks
« Reply #24 on: May 22, 2014, 03:46:58 PM »
ymg

sorry for the time i'm worked on night.

ok i forget pre-selection but if i've undertand correcly  pl return list of insertion point of all bloc on ss but for select just bloc who have insertion point on polylin, i'll compare whith js.

My question is what function do that

Thanks

ymg

  • Guest
Re: selection of blocks
« Reply #25 on: May 22, 2014, 05:07:52 PM »
TAIKI,

pl is the list of Insertion Points for every blocks that is
we accumulate in pl every blocks picked up by each
polyline that are in js.

So the code segment that does the accumulating
is the second repeat loop.

Code: [Select]
(repeat (setq j (sslength ss))
     (setq pl (cons (cdr (assoc 10 (entget (ssname ss (setq j (1- j)))))) pl))
)

Now the above repeat loop is being repeated for each polyline in js
That why we say that the second loop is nested inside the first one.



ymg
« Last Edit: May 22, 2014, 05:13:09 PM by ymg »

TAIKI

  • Guest
Re: selection of blocks
« Reply #26 on: May 26, 2014, 04:20:43 PM »
hello

Ymg, I want to thanks you for your time, but i think i'm not ready for this lisp. For the moment I learn with easiest code and when I'll be better I'll do this code.

So thank you and see you soon for my future lisp.

Taiki