How to reverse a polyline using an AutoCAD Lisp

How to reverse a polyline using an AutoCAD Lisp

n.lagos.pdr
Contributor Contributor
1,329 Views
6 Replies
Message 1 of 7

How to reverse a polyline using an AutoCAD Lisp

n.lagos.pdr
Contributor
Contributor

How to create a Lisp routine that applies the reverse command to a polyline drawn from right to left, and at the same time does not apply the reverse command if it was drawn from left to right?

0 Likes
Accepted solutions (1)
1,330 Views
6 Replies
Replies (6)
Message 2 of 7

Kent1Cooper
Consultant
Consultant

What if it was drawn from top to bottom?  [That is, will there always be an unmistakeable "right" and "left"?]  What if it goes around, or zigzags, or otherwise has parts going in opposite directions?

 

And why?  If it's for the uprightness of text elements, in recent-enough versions linetypes can be defined so it doesn't matter in which direction it was drawn.

Kent Cooper, AIA
0 Likes
Message 3 of 7

calderg1000
Mentor
Mentor

Regards @n.lagos.pdr 

By pressing the icon or typing REVERSE in the command bar, you can do what you need

calderg1000_0-1707390634737.png

 


Carlos Calderon G
EESignature
>Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

0 Likes
Message 4 of 7

n.lagos.pdr
Contributor
Contributor

Thank you very much for addressing my question. I want to create a routine in AutoLISP to automate a repetitive task in my work. This task involves measuring certain polylines to insert blocks aligned with them. The polylines are drawn by another person, so they do not take care to create them in the same orientation. The LISP routine I want to create should select a polyline and perform a reverse when the leftmost vertex is not vertex 1, and then apply the measure command.

0 Likes
Message 5 of 7

n.lagos.pdr
Contributor
Contributor

Thank you for your response. What I need is to create a Lisp routine to automate a repetitive task. I need to address the issue of reversing as part of a larger code.

0 Likes
Message 6 of 7

Kent1Cooper
Consultant
Consultant
Accepted solution

@n.lagos.pdr wrote:

....  The LISP routine I want to create should select a polyline and perform a reverse when the leftmost vertex is not vertex 1, and then apply the measure command.


When you say "the leftmost vertex is not vertex 1," do you mean only that vertex 1 is not at a lower X-coordinate value than the end vertex?  Could the leftmost vertex be neither the first nor last one?

Kent1Cooper_0-1707398414966.png

EDIT:  If it's only a question of Reversing when the start is to the right of the end, try this:

(defun C:SLE (/ esel pl); = Start at Left End
  (if
    (and
      (setq esel (entsel "\nPolyline or Line to Reverse if drawn right-to-left: "))
      (wcmatch (cdr (assoc 0 (entget (setq pl (car esel))))) "*POLYLINE,LINE") 
    ); and
    (if (> (car (vlax-curve-getStartPoint pl)) (car (vlax-curve-getEndPoint pl)))
      (command "_.reverse" pl ""); then
    ); if
    (prompt "\nNothing selected, or not a Polyline or Line.")
  )
  (prin1)
)

If it's vertical or closed [X coordinates of start and end are equal], it will not be reversed.

Since the same standard can be applied to a Line by the same approach, it allows for them.  It could also allow a Spline or even a Helix, but REVERSE won't work on other things [e.g. an Arc].  If those or other kinds of paths are wanted, it could be done [see my ReverseDirection.lsp routine, >here<].

Kent Cooper, AIA
Message 7 of 7

n.lagos.pdr
Contributor
Contributor
Great, the Lisp you provided completely meets my needs. Thank you very much
0 Likes