Rotate A Selected Side Of A Polyline To Zero Degrees In Autolisp

Rotate A Selected Side Of A Polyline To Zero Degrees In Autolisp

Anonymous
Not applicable
2,316 Views
7 Replies
Message 1 of 8

Rotate A Selected Side Of A Polyline To Zero Degrees In Autolisp

Anonymous
Not applicable

I am looking for autolisp command that can rotate to selected side of a closed polyline to zero. The shape would also have one side being a curve and can be selected to rotate along the start and end of the curve as the reference.

Accepted solutions (1)
2,317 Views
7 Replies
Replies (7)
Message 2 of 8

Ranjit_Singh
Advisor
Advisor

@Anonymous wrote:

I am looking for autolisp command that can rotate to selected side of a closed polyline to zero. The shape would also have one side being a curve and can be selected to rotate along the start and end of the curve as the reference.


Not sure exactly what you mean. Maybe post a screenshot to explain better.

0 Likes
Message 3 of 8

john.uhden
Mentor
Mentor

About what point should it be rotated?

Does it matter in which direction the polyline is drawn, or should the westerlymost vertex of the segment remain westerlymost? (n the sense that 0.0 degrees is due east.)

 

I think I understand what you mean by "The shape would also have one side being a curve and can be selected to rotate along the start and end of the curve as the reference."  To me it means that the bulge should be ignored and to use just the direction between the adjacent vertices.

John F. Uhden

0 Likes
Message 4 of 8

phanaem
Collaborator
Collaborator
Accepted solution

@Anonymous wrote:

I am looking for autolisp command that can rotate to selected side of a closed polyline to zero. The shape would also have one side being a curve and can be selected to rotate along the start and end of the curve as the reference.



If I understand correctly, try this lisp.

It is important where you make the selection. The nearest vertex is the base point of the rotation. The other vertex of the clicked segment is the reference point.

 

(defun c:test ( / e p a p1 p2)
  (if
    (setq e (entsel "\nSelect polyline: "))
    (progn
      (setq p (cadr e)
            e (car  e)
            a (vlax-curve-getparamatpoint e (vlax-curve-getclosestpointto e p))
            p1 (vlax-curve-getpointatparam e (if (< (- a (fix a)) 0.5) (fix a) (1+ (fix a))))
            p2 (vlax-curve-getpointatparam e (if (< (- a (fix a)) 0.5) (1+ (fix a)) (fix a)))
            )
      (command "_rotate" e "" "_non" p1 "_r" "_non" p1 "_non" p2 0.0)
    )
  )
  (princ)
)

EDIT: Feel free to add error trap for object type selection and/or locked layers. Also, it works in WCS only.

Message 5 of 8

john.uhden
Mentor
Mentor

Nicely done.  I was wondering if the first responder was going to account for treating the closing segment correctly.

I just can't help honoring Stephan Koster a little more...

Rather than Iif ,,, (progn

just (and ... would work quite nicely.

John F. Uhden

0 Likes
Message 6 of 8

danglar
Advocate
Advocate

Brilliant solution!

Is it possible to add your approach to this lisp?

now it works only for Blocks ,MTEXTs,*LEADERs and TEXTs.

With your addition it can works for polylines too

 

;;; ------------------------------------------------------------------------

(defun c:zzr () (c:zZeroRotation)) ; Rotate Multileaders, Text, Mtext, Blocks to 0 relative to current UCS
(defun c:zZeroRotation (/ *error* AT:UCSAngle ang ss name ldr pts23 base refang)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;; SUBROUTINES ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


  (defun *error* (msg)
    (and *AcadDoc* (vla-endundomark *AcadDoc*))
    (if (and msg (not (wcmatch (strcase msg) "*BREAK*,*CANCEL*,*QUIT*,")))
      (princ (strcat "\nError: " msg))
    )
  )


  (defun AT:UCSAngle (/)
    ;; Return current UCS angle
    ;; Alan J. Thompson, 04.06.10
    ((lambda (x) (atan (cadr x) (car x))) (trans (getvar 'UCSXDIR) 0 (trans '(0. 0. 1.) 1 0 T) T))
  )


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;; MAIN ROUTINE ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

  (vl-load-com)

  (vla-startundomark
    (cond (*AcadDoc*)
          ((setq *AcadDoc* (vla-get-activedocument (vlax-get-acad-object))))
    )
  )

  (if (ssget "_:A" '((0 . "INSERT,MTEXT,*LEADER,TEXT"))); changed to *LEADER - both regular and Multi
    (progn
      (setq ang (AT:UCSAngle))
      (vlax-for x (setq ss (vla-get-activeselectionset *AcadDoc*))
        (cond
          ((vl-position (setq name (vla-get-objectname x)) '("AcDbBlockReference" "AcDbText"))
            (vla-put-rotation x ang)
          )
          ((eq name "AcDbMText") (vla-put-rotation x 0.))
          ((and (eq name "AcDbMLeader") (eq (vla-get-contenttype x) 2))
            (vla-put-textrotation x 0.)
          )
          ((eq name "AcDbLeader"); added another condition
            (setq
              ldr (vlax-vla-object->ename x); Leader entity
              pts23 ; defining points 2 & 3
                (cdr ; remove first one [arrow point]
                  (mapcar 'cdr ; remove 10's [leave coordinates only]
                    (vl-remove-if
                      '(lambda (x) (/= (car x) 10))
                      (entget ldr)
                    ); ...remove...
                  ); mapcar
                ); cdr & pts23
            ); setq
            (command "_.rotate" ldr ""
              (setq base (trans (car pts23) 0 1)); (trans)lated from WCS to current UCS
              "_reference" (angtos (setq refang (angle base (trans (cadr pts23) 0 1))) 2 8)
              (angtos (* (fix (+ (/ refang pi) 0.5)) pi) 2 8); nearer horizontal direction
            ); command
          ); Leader condition
        ); cond
      ); vlax-for
      (vla-delete ss)

    )
  )
  (*error* nil)
  (princ)
)

;;; ------------------------------------------------------------------------
0 Likes
Message 7 of 8

danglar
Advocate
Advocate

.. taking respect to creator:

ZeroRotation.lsp v1.2
;;;
;;;	Copyright© 03.09.09
;;;	Alan J. Thompson (alanjt)
;;;
;;;	Contact: alanjt @ TheSwamp.org, CADTutor.net
;;;
;;;	Permission to use, copy, modify, and distribute this software
;;;	for any purpose and without fee is hereby granted, provided
;;;	that the above copyright notice appears in all copies and
;;;	that both that copyright notice and the limited warranty and
;;;	restricted rights notice below appear in all supporting
;;;	documentation.
;;;
;;;	The following program(s) are provided "as is" and with all faults.
;;;	Alan J. Thompson DOES NOT warrant that the operation of the program(s)
;;;	will be uninterrupted and/or error free.
;;;
;;;	Set objects (Multileaders, Text, Mtext, Blocks) with a
;;;	rotation of 0 (relative to current UCS).
;;;
;;;	Revision History:
;;;
;;;	v1.1 (10.23.09) 1. Minor rewrite for speed optimization.
;;;     v1.2 (05.31.11) 1. Complete rewrite.
;;;
0 Likes
Message 8 of 8

xuanquang1994py
Observer
Observer
hi mr danglar!
Can we rotate the leader by 2 points or polyline with that code?
0 Likes