Author Topic: Auto Arrange  (Read 9982 times)

0 Members and 1 Guest are viewing this topic.

liuhaixin88

  • Guest
Re: Auto Arrange
« Reply #15 on: May 02, 2014, 03:54:40 AM »
This is similar to your 11.gif... It uses rotate&copy, but not mirror... Tested on simple LWPOLYLINES like on posted 11.gif...



Marko,I'd like to forward your code to “ bbs.mjtd.com” , I will keep the author's name, Do you agree?


ribarm

  • Gator
  • Posts: 3279
  • Marko Ribar, architect
Re: Auto Arrange
« Reply #16 on: May 02, 2014, 06:34:25 AM »
Look, similar to 1.png you posted... Also applicable only for LWPOLYLINE in WCS... You have to accept or hit ENTER if position isn't aligned as should...

Code - Auto/Visual Lisp: [Select]
  1. (defun c:rotarrange-180 ( / adoc msp LM:MinEncCircle LM:RemoveWithFuzz LM:GetInsideAngle LM:3PCircle mid LM:ConvexHull LM:Clockwise-p LM:LWPoly->List pl n ri c minp maxp ar arl amin alst->amin bbox plc bbvl bbe bbc vx vy v ch )
  2.  
  3.   (setq msp (vla-get-modelspace adoc))
  4.  
  5.   (defun LM:MinEncCircle ( lst / _sub )
  6.  
  7.       (defun _sub ( p1 p2 l1 / a1 a2 l2 p3 p4 )
  8.           (setq l2 (LM:RemoveWithFuzz (list p1 p2) l1 1e-8)
  9.                 p3 (car l2)
  10.                 a1 (LM:GetInsideAngle p1 p3 p2)
  11.           )
  12.           (foreach p4 (cdr l2)
  13.               (if (< (setq a2 (LM:GetInsideAngle p1 p4 p2)) a1)
  14.                   (setq p3 p4 a1 a2)
  15.               )
  16.           )
  17.           (cond
  18.               (   (<= (/ pi 2.0) a1)
  19.                   (list (mid p1 p2) (/ (distance p1 p2) 2.0))
  20.               )
  21.               (   (vl-some
  22.                       (function
  23.                           (lambda ( a b c )
  24.                               (if (< (/ pi 2.0) (LM:GetInsideAngle a b c)) (_sub a c l1))
  25.                           )
  26.                       )
  27.                       (list p1 p1 p2) (list p2 p3 p1) (list p3 p2 p3)
  28.                   )
  29.               )
  30.               (   (LM:3PCircle p1 p2 p3)   )
  31.           )
  32.       )
  33.  
  34.       ((lambda ( lst ) (_sub (car lst) (cadr lst) lst)) (LM:ConvexHull lst))
  35.   )
  36.  
  37.   ;; Remove With Fuzz  -  Lee Mac
  38.   ;; Removes items from a list which are equal to a supplied tolerance
  39.  
  40.   (defun LM:RemoveWithFuzz ( l1 l2 fz )
  41.       (vl-remove-if
  42.           (function
  43.               (lambda ( a )
  44.                   (vl-some
  45.                       (function (lambda ( b ) (equal a b fz)))
  46.                       l1
  47.                   )
  48.               )
  49.           )
  50.           l2
  51.       )
  52.   )
  53.  
  54.   ;; Get Inside Angle  -  Lee Mac
  55.   ;; Returns the smaller angle subtended by three points with vertex at p2
  56.  
  57.   (defun LM:GetInsideAngle ( p1 p2 p3 )
  58.       (   (lambda ( a ) (min a (- (+ pi pi) a)))
  59.           (rem (+ pi pi (- (angle p2 p1) (angle p2 p3))) (+ pi pi))
  60.       )
  61.   )
  62.  
  63.   ;; 3-Point Circle  -  Lee Mac
  64.   ;; Returns the Center and Radius of the Circle defined by
  65.   ;; the supplied three points.
  66.  
  67.   (defun LM:3PCircle ( p1 p2 p3 / cn m1 m2 )
  68.       (setq m1 (mid p1 p2)
  69.             m2 (mid p2 p3)
  70.       )
  71.       (list
  72.           (setq cn
  73.               (inters
  74.                   m1 (polar m1 (+ (angle p1 p2) (/ pi 2.)) 1.0)
  75.                   m2 (polar m2 (+ (angle p2 p3) (/ pi 2.)) 1.0)
  76.                   nil
  77.               )
  78.           )
  79.           (distance cn p1)
  80.       )
  81.   )
  82.  
  83.   ;; Midpoint - Lee Mac
  84.   ;; Returns the midpoint of two points
  85.  
  86.   (defun mid ( a b )
  87.       (mapcar (function (lambda ( a b ) (/ (+ a b) 2.0))) a b)
  88.   )
  89.  
  90.   ;; Convex Hull  -  Lee Mac
  91.   ;; Implements the Graham Scan Algorithm to determine the
  92.   ;; Convex Hull of a list of points.
  93.  
  94.   (defun LM:ConvexHull ( lst / hul p0 )
  95.       (cond
  96.           (   (< (length lst) 4)
  97.               lst
  98.           )
  99.           (   t
  100.               (setq p0 (car lst))
  101.               (foreach p1 (cdr lst)
  102.                   (if (or (< (cadr p1) (cadr p0))
  103.                           (and (= (cadr p1) (cadr p0)) (< (car p1) (car p0)))
  104.                       )
  105.                       (setq p0 p1)
  106.                   )
  107.               )
  108.               (setq lst
  109.                   (vl-sort lst
  110.                       (function
  111.                           (lambda ( a b / c d )
  112.                               (if (or (equal (setq c (angle p0 a)) (setq d (angle p0 b)) 1e-8) (and (or (equal c 0.0 1e-8) (equal c (* 2 pi) 1e-8)) (or (equal d 0.0 1e-8) (equal d (* 2 pi) 1e-8))))
  113.                                   (< (distance (list (car p0) (cadr p0)) a) (distance (list (car p0) (cadr p0)) b))
  114.                                   (< c d)
  115.                               )
  116.                           )
  117.                       )
  118.                   )
  119.               )
  120.               (setq hul (list (caddr lst) (cadr lst) (car lst)))
  121.               (foreach pt (cdddr lst)
  122.                   (setq hul (cons pt hul))
  123.                   (while (and (caddr hul) (LM:Clockwise-p (caddr hul) (cadr hul) pt))
  124.                       (setq hul (cons pt (cddr hul)))
  125.                   )
  126.               )
  127.               hul
  128.           )
  129.       )
  130.   )
  131.  
  132.   ;; Clockwise-p  -  Lee Mac
  133.   ;; Returns T if p1,p2,p3 are clockwise oriented or collinear
  134.                    
  135.   (defun LM:Clockwise-p ( p1 p2 p3 )
  136.       (<  (-  (* (- (car  p2) (car  p1)) (- (cadr p3) (cadr p1)))
  137.               (* (- (cadr p2) (cadr p1)) (- (car  p3) (car  p1)))
  138.           )
  139.           1e-8
  140.       )
  141.   )
  142.  
  143.   ;; LWPolyline to Point List  -  Lee Mac
  144.   ;; Returns a list of points describing the supplied LWPolyline
  145.  
  146.   (defun LM:LWPoly->List ( ent n / der di1 di2 inc lst par )
  147.       (setq par 0)
  148.       (repeat (cdr (assoc 90 (entget ent)))
  149.           (if (setq der (vlax-curve-getsecondderiv ent par))
  150.               (if (equal der '(0.0 0.0 0.0) 1e-8)
  151.                   (setq lst (cons (vlax-curve-getpointatparam ent par) lst))
  152.                   (if
  153.                       (setq di1 (vlax-curve-getdistatparam ent par)
  154.                             di2 (vlax-curve-getdistatparam ent (1+ par))
  155.                       )
  156.                       (progn
  157.                           (setq inc (/ (- di2 di1) n))
  158.                           (while (< di1 di2)
  159.                               (setq lst (cons (vlax-curve-getpointatdist ent di1) lst)
  160.                                     di1 (+ di1 inc)
  161.                               )
  162.                           )
  163.                           (if (equal (vlax-curve-getpointatdist ent di1) (vlax-curve-getendpoint ent) 1e-8)
  164.                               (setq lst (cons (vlax-curve-getendpoint ent) lst))
  165.                           )
  166.                       )
  167.                   )
  168.               )
  169.           )
  170.           (setq par (1+ par))
  171.       )
  172.       lst
  173.   )
  174.  
  175.   (while (or (not (setq pl (car (entsel "\nPick LWPOLYLINE shape")))) (not (eq (cdr (assoc 0 (entget pl))) "LWPOLYLINE"))))
  176.   (initget 7)
  177.   (setq n (getint "\nSpecify rotational derivation of full rotation - 360 degree / ? : "))
  178.   (setq ri (/ 360.0 n))
  179.   (setq ri (* (/ ri 180.0) pi))
  180.   (setq c (car (LM:MinEncCircle (LM:LWPoly->List pl 25))))
  181.   (repeat n
  182.     (vla-rotate (vlax-ename->vla-object pl) (vlax-3d-point c) ri)
  183.     (vla-getboundingbox (vlax-ename->vla-object pl) 'minp 'maxp)
  184.     (setq minp (vlax-safearray->list minp))
  185.     (setq maxp (vlax-safearray->list maxp))
  186.     (setq ar (* (abs (- (car maxp) (car minp))) (abs (- (cadr maxp) (cadr minp)))))
  187.     (setq arl (cons ar arl))
  188. ;    (vla-regen adoc 1)
  189.   )
  190.   (setq amin (apply 'min arl))
  191.   (setq arl (reverse arl))
  192.   (setq alst->amin (reverse (member amin (reverse arl))))
  193.   (foreach a alst->amin
  194.     (vla-rotate (vlax-ename->vla-object pl) (vlax-3d-point c) ri)
  195. ;    (vla-regen adoc 1)
  196.   )
  197.   (vla-getboundingbox (vlax-ename->vla-object pl) 'minp 'maxp)
  198.   (setq minp (vlax-safearray->list minp))
  199.   (setq maxp (vlax-safearray->list maxp))
  200.   (setq bbox (entmakex (list '(0 . "LWPOLYLINE") '(100 . "AcDbEntity") '(100 . "AcDbPolyline") '(90 . 4) '(70 . 1) (cons 38 (caddr minp)) (cons 10 (list (car minp) (cadr minp))) (cons 10 (list (car maxp) (cadr minp))) (cons 10 (list (car maxp) (cadr maxp))) (cons 10 (list (car minp) (cadr maxp))) (list 210 0.0 0.0 1.0))))
  201.   (setq plc (vla-copy (vlax-ename->vla-object pl)))
  202.   (setq bbvl (mapcar 'cdr (vl-remove-if-not '(lambda ( x ) (= (car x) 10)) (entget bbox))))
  203.   (setq bbe (cdr (assoc 38 (entget bbox))))
  204.   (setq bbvl (mapcar '(lambda ( x ) (list (car x) (cadr x) bbe)) bbvl))
  205.   (setq bbvl (mapcar '(lambda ( x ) (trans x bbox 0)) bbvl))
  206.   (setq bbc (car (LM:MinEncCircle bbvl)))
  207.   (vla-rotate plc (vlax-3d-point bbc) pi)
  208.   (setq vx (mapcar '- (list (car maxp) (cadr maxp)) (list (car minp) (cadr maxp)) ))
  209.   (setq vy (mapcar '- (list (car maxp) (cadr maxp)) (list (car maxp) (cadr minp)) ))
  210.   (if (< (sqrt (apply '+ (mapcar '* vx vx))) (sqrt (apply '+ (mapcar '* vy vy))))
  211.     (setq v vx) (setq v vy)
  212.   )
  213.   (vla-move plc (vlax-3d-point '(0.0 0.0 0.0)) (vlax-3d-point v))
  214.   (entdel bbox)
  215.   (initget "Yes No")
  216.   (setq ch (getkword "\nIs position of copied and rotated shape OK [Yes/No] <No> : "))
  217.   (if (not (eq ch "Yes"))
  218.     (vla-move plc (vlax-3d-point v) (vlax-3d-point (mapcar '* v '(-1.0 -1.0 -1.0))))
  219.   )
  220.   (princ)
  221. )
  222.  

M.R.
« Last Edit: June 19, 2019, 12:58:11 AM by ribarm »
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

liuhaixin88

  • Guest
Re: Auto Arrange
« Reply #17 on: May 02, 2014, 06:39:50 AM »
Look, similar to 1.png you posted... Also applicable only for LWPOLYLINE in WCS and with straight segments (edges)... You have to accept or hit ENTER if position isn't aligned as should...



Thank you very much!  Marko
I'd like to forward your code to “ bbs.mjtd.com” , I will keep the author's name, Do you agree?

ribarm

  • Gator
  • Posts: 3279
  • Marko Ribar, architect
Re: Auto Arrange
« Reply #18 on: May 02, 2014, 06:57:30 AM »
Look, similar to 1.png you posted... Also applicable only for LWPOLYLINE in WCS and with straight segments (edges)... You have to accept or hit ENTER if position isn't aligned as should...



Thank you very much!  Marko
I'd like to forward your code to “ bbs.mjtd.com” , I will keep the author's name, Do you agree?

As you wish, just be sure I won't be fooled and laugh at...
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

liuhaixin88

  • Guest
Re: Auto Arrange
« Reply #19 on: May 02, 2014, 07:00:45 AM »
Look, similar to 1.png you posted... Also applicable only for LWPOLYLINE in WCS and with straight segments (edges)... You have to accept or hit ENTER if position isn't aligned as should...



Thank you very much!  Marko
I'd like to forward your code to “ bbs.mjtd.com” , I will keep the author's name, Do you agree?

As you wish, just be sure I won't be fooled and laugh at...

Thanks, I'm sure not.

ribarm

  • Gator
  • Posts: 3279
  • Marko Ribar, architect
Re: Auto Arrange
« Reply #20 on: May 02, 2014, 10:57:20 AM »
Look, similar to 1.png you posted... Also applicable only for LWPOLYLINE in WCS and with straight segments (edges)... You have to accept or hit ENTER if position isn't aligned as should...



Thank you very much!  Marko
I'd like to forward your code to “ bbs.mjtd.com” , I will keep the author's name, Do you agree?

As you wish, just be sure I won't be fooled and laugh at...

Thanks, I'm sure not.

太好了,我很高興...
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

liuhaixin88

  • Guest
Re: Auto Arrange
« Reply #21 on: May 02, 2014, 08:11:02 PM »
Look, similar to 1.png you posted... Also applicable only for LWPOLYLINE in WCS and with straight segments (edges)... You have to accept or hit ENTER if position isn't aligned as should...



Thank you very much!  Marko
I'd like to forward your code to “ bbs.mjtd.com” , I will keep the author's name, Do you agree?

As you wish, just be sure I won't be fooled and laugh at...

Thanks, I'm sure not.

太好了,我很高興...

Haha, Mr. marko,your Chinese is excellent.