Author Topic: Group objects based on angle and rotation  (Read 2438 times)

0 Members and 1 Guest are viewing this topic.

Coder

  • Swamp Rat
  • Posts: 827
Group objects based on angle and rotation
« on: May 17, 2018, 05:57:33 PM »
Hello guys.

I have lots of blocks with the same name ( not dynamic neither attributed ) but inserted with different rotation and angle and I would like to be able to create a list of lists of objects based on identical angle and rotation of each group of blocks.

How can I write a function to group the blocks?
Scale factor of X,Y,Z is: 1,1,1

Thank you.

BIGAL

  • Swamp Rat
  • Posts: 1398
  • 40 + years of using Autocad
Re: Group objects based on angle and rotation
« Reply #1 on: May 18, 2018, 03:58:52 AM »
You can do a sort on a list so you would have the angle and a entity name eg ((32 as56vf)(56 wendfgt)(32 ahjtd45))

a sort would give ((32 as56vf)(32 ahjtd45)(56 wendfgt)) then look at 1st value and when it changes its a different block so make the group.

Code: [Select]
; sorts on 1st two items
(setq lst (vl-sort lst '(lambda (x y)
(cond
((= (cadr x)(cadr y))
(< (car x)(car y)))
((< (cadr x)(cadr y)))
))))
A man who never made a mistake never made anything

Coder

  • Swamp Rat
  • Posts: 827
Re: Group objects based on angle and rotation
« Reply #2 on: May 18, 2018, 04:09:25 AM »
Hello BIGAL.

Thank you for your kind reply.

I think I can sort blocks based on rotation but I am having a hard time to group them again based on the same angle they inserted in line.

Coder

  • Swamp Rat
  • Posts: 827
Re: Group objects based on angle and rotation
« Reply #3 on: May 18, 2018, 06:10:38 AM »
Maybe this is a challenging program!  :yes:

dgorsman

  • Water Moccasin
  • Posts: 2437
Re: Group objects based on angle and rotation
« Reply #4 on: May 18, 2018, 10:06:56 AM »
The solution set is easier when the problem is well defined.  Are you after a "group" or a "list", and do you mean an actual "group" or "list" or something else?   :|

Start solving the problem manually, and record the questions and answers as you go.  Then you can approximate that logic with code.
If you are going to fly by the seat of your pants, expect friction burns.

try {GreatPower;}
   catch (notResponsible)
      {NextTime(PlanAhead);}
   finally
      {MasterBasics;}

Coder

  • Swamp Rat
  • Posts: 827
Re: Group objects based on angle and rotation
« Reply #5 on: May 18, 2018, 12:59:13 PM »
Hello dgorsman,

Sorry for the confusion.

Group blocks means lists in a list to me like: ((entity entity entity) (entity entity entity entity) (  ..... so on))

Thank you.

roy_043

  • Water Moccasin
  • Posts: 1895
  • BricsCAD 18
Re: Group objects based on angle and rotation
« Reply #6 on: May 19, 2018, 04:16:25 AM »
Code - Auto/Visual Lisp: [Select]
  1. ; (setq lst (SortInsert (ssget)))
  2. (defun SortInsert (ss / ang fnd i obj ret)
  3.   (if ss
  4.     (repeat (setq i (sslength ss))
  5.       (setq obj (vlax-ename->vla-object (ssname ss (setq i (1- i)))))
  6.       (if (= "AcDbBlockReference" (vla-get-objectname obj))
  7.         (progn
  8.           (setq ang (vla-get-rotation obj))
  9.           (if (setq fnd (assoc ang ret))
  10.             (setq ret (subst (vl-list* ang (vlax-vla-object->ename obj) (cdr fnd)) fnd ret))
  11.             (setq ret (cons (list ang (vlax-vla-object->ename obj)) ret))
  12.           )
  13.         )
  14.       )
  15.     )
  16.   )
  17. )

Coder

  • Swamp Rat
  • Posts: 827
Re: Group objects based on angle and rotation
« Reply #7 on: May 19, 2018, 07:20:41 AM »
Thank you roy for your kind reply and your time.

Your code is working very good but can you please modify the code to group also blocks as per common angle based on the insertion point?

example: ((ang ent rot) .... etc)

Many thanks.

roy_043

  • Water Moccasin
  • Posts: 1895
  • BricsCAD 18
Re: Group objects based on angle and rotation
« Reply #8 on: May 19, 2018, 08:29:53 AM »
I do not understand. The list already contains the rotation angles (in radians).
((angle1 ename ename ...) (angle2 ename ename ...) ...)

Coder

  • Swamp Rat
  • Posts: 827
Re: Group objects based on angle and rotation
« Reply #9 on: May 19, 2018, 11:59:54 AM »
I do not understand. The list already contains the rotation angles (in radians).
((angle1 ename ename ...) (angle2 ename ename ...) ...)

Thank you for your kind reply.

This is my goal of the codes as drawn in the image down and sorry for the confusion.

roy_043

  • Water Moccasin
  • Posts: 1895
  • BricsCAD 18
Re: Group objects based on angle and rotation
« Reply #10 on: May 20, 2018, 06:47:36 AM »
Code - Auto/Visual Lisp: [Select]
  1. ; (setq lst (GroupInsert_RotationAlignment (ssget)))
  2. ; The function groups inserts based on their rotation angle and alignment.
  3. ;   Two inserts are considered aligned if the angle of the line
  4. ;   connecting their insertion points matches their rotation angle.
  5. ; Return value: List with this format:
  6. ; ((angle insPt enm enm ... enm) (angle insPt enm enm ... enm) ...)
  7. (defun GroupInsert_RotationAlignment (ss / ang fnd i obj pt ret)
  8.   (if ss
  9.     (repeat (setq i (sslength ss))
  10.       (setq obj (vlax-ename->vla-object (ssname ss (setq i (1- i)))))
  11.       (if (= "AcDbBlockReference" (vla-get-objectname obj))
  12.         (progn
  13.           (setq ang (vla-get-rotation obj))
  14.           (setq pt (vlax-get obj 'insertionpoint))
  15.           (if
  16.             (setq fnd
  17.               (vl-some
  18.                 '(lambda (sub)
  19.                   (if
  20.                     (and
  21.                       (equal ang (car sub) 1e-8)
  22.                       (or
  23.                         (equal pt (cadr sub) 1e-8)
  24.                         (equal ang (angle pt (cadr sub)) 1e-8)
  25.                         (equal ang (angle (cadr sub) pt) 1e-8)
  26.                       )
  27.                     )
  28.                     sub
  29.                   )
  30.                 )
  31.                 ret
  32.               )
  33.             )
  34.             (setq ret (subst (vl-list* (car fnd) (cadr fnd) (vlax-vla-object->ename obj) (cddr fnd)) fnd ret))
  35.             (setq ret (cons (list ang pt (vlax-vla-object->ename obj)) ret))
  36.           )
  37.         )
  38.       )
  39.     )
  40.   )
  41. )
« Last Edit: May 20, 2018, 02:40:34 PM by roy_043 »

Coder

  • Swamp Rat
  • Posts: 827
Re: Group objects based on angle and rotation
« Reply #11 on: May 20, 2018, 09:59:49 AM »
Hello roy.

This is awesome and great work , thank you so much.

Have a great weekend.

roy_043

  • Water Moccasin
  • Posts: 1895
  • BricsCAD 18
Re: Group objects based on angle and rotation
« Reply #12 on: May 20, 2018, 02:45:07 PM »
Good to hear that.
I have made a small change to the code in my previous post. On line 34 I have changed ang to (car fnd). This is more consistent.

Coder

  • Swamp Rat
  • Posts: 827
Re: Group objects based on angle and rotation
« Reply #13 on: May 21, 2018, 01:17:29 AM »
Thank you once again.