Lisp to move Rectangle center to Line midpoint

Lisp to move Rectangle center to Line midpoint

hythamthelove
Advocate Advocate
776 Views
10 Replies
Message 1 of 11

Lisp to move Rectangle center to Line midpoint

hythamthelove
Advocate
Advocate

Hello everyone,

I have a rectangle and a line, I want to move the rectangle from its geometric center to the line midpoint. I want to select both at once the rectangle and the line then the rectangle is moved automatically to the line midpoint.

 

Does anyone have a lisp for that ?

0 Likes
Accepted solutions (1)
777 Views
10 Replies
Replies (10)
Message 2 of 11

Kent1Cooper
Consultant
Consultant

I don't have such a thing, but it sounds simple enough.  What is the rectangle?  [Four Lines?  Closed Polyline?  Block?  If the latter, is its insertion point in the middle?  Etc.]  And by "line" do you actually mean a Line entity specifically?  Or could it be a Polyline, or a segment of a longer Polyline, or...?  If the rectangle is a Polyline, should the command determine whether it's actually rectangular, not just any Polyline, before Moving it?  Is Moving it all you want to do, or should it also align it directionally somehow with the Line?  A small drawing with before and after on a representative variety of scenarios would answer a lot of questions.

Kent Cooper, AIA
0 Likes
Message 3 of 11

hythamthelove
Advocate
Advocate

sorry for being unclear, the rectangles are closed polylines and the lines are really lines. The prblem is it is a very repeated thing, but i don't guess that a lisp can do the movement right for all lines and rectangles at once, so at least if i can select a line and a rectangle at once and the rectangle is moves it would be easier.

 

hythamthelove_0-1718891398621.png

 

0 Likes
Message 4 of 11

Kent1Cooper
Consultant
Consultant
Accepted solution

This worked for me in your sample drawing and in very limited other trial.

 

(defun C:R2LM ; = Rectangle to Line Midpoint
  (/ ss type0 type1 rec lin)
  (prompt "\nTo move a Rectangle's center to a Line's Midpoint,")
  (if
    (and
      (setq ss
        (ssget
          '(
            (-4 . "<OR")
              (0 . "LINE")
              (-4 . "<AND")
                (0 . "LWPOLYLINE") (-4 . "&") (70 . 1) (90 . 4); 4-vertex closed
              (-4 . "AND>")
            (-4 . "OR>")
          ); list
        ); ssget
      ); setq
      (= (sslength ss) 2); 2 and only 2 objects
      (setq
        type0 (cdr (assoc 0 (entget (ssname ss 0))))
        type1 (cdr (assoc 0 (entget (ssname ss 1))))
      ); setq
      (/= type0 type1); not the same kind [i.e. one of each]
    ); and
    (progn ; then
      (setq
        rec (ssname ss (if (= type0 "LINE") 1 0)) 
        lin (ssname ss (if (= type0 "LINE") 0 1))
      ); setq
      (command "_.move" rec ""
        "_m2p"
          (vlax-curve-getStartPoint rec)
          (vlax-curve-getPointAtParam rec 2)
        "_m2p"
          (vlax-curve-getStartPoint lin)
          (vlax-curve-getEndPoint lin)
      ); command
    ); progn
    (prompt "\nMust select only one Rectangle and one Line."); else
  ); if
  (prin1)
)

 

It limits selection to only one 4-vertex closed Lightweight Polyline and one Line.  But it does not check anything else about them, particularly whether the Polyline is rectangular shaped [it could be skewed in any way, and even include arc segments, etc.]  It doesn't accept "heavy" 2D or 3D Polylines, but could be made to.  Because it's simpler, it uses the point midway between opposite corners of the rectangle as the Move base point, which is the same as the geometric center or centroid for a rectangle, but would not always be if you want it to work with non-rectangular Polylines.  And it doesn't care whether there's any relationship, either directionally or positionally, between the rectangle and the Line -- they could be miles apart and running in any oddball directions and in different coordinate systems, etc.

 

It could be made to limit selection further and/or to check after selection for various things if needed, such as the Layer(s) of the objects, the length of the Line relative to the size of the rectangle, and surely other things you might care about.  And it could get the usual enhancements -- *error* handling, Undo begin/end wrapping, etc.

Kent Cooper, AIA
Message 5 of 11

paullimapa
Mentor
Mentor

@Kent1Cooper If the blue LINES are always the same length as the short side of the closed rectangular PLINES, how's about the idea of automatically ERASing all the blue LINES, automatically selecting all closed PLINES with 4 vertices and then draw new LINEs from the mid of the longest side to the opposite and place them on the same layer as the original blue LINEs?


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes
Message 6 of 11

Kent1Cooper
Consultant
Consultant

[That would, in effect, Move the Line to the rectangle, which is the opposite of what they're after.]

Kent Cooper, AIA
0 Likes
Message 7 of 11

paullimapa
Mentor
Mentor

that should match the desired OP result assuming there is a blue LINE for each rectangle

paullimapa_0-1718901914088.png

 


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes
Message 8 of 11

paullimapa
Mentor
Mentor

ok, nevermind...I understand what you're saying now..the rectangles have to move and not the lines


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes
Message 9 of 11

Kent1Cooper
Consultant
Consultant

I think [@hythamthelove correct me if I'm wrong] the sample drawing has "before" on top and "after" below.  If that's right, what you describe would, I think, have this result [dashed green are the original Line positions]:

Kent1Cooper_0-1718902362646.png

Kent Cooper, AIA
Message 10 of 11

paullimapa
Mentor
Mentor

My wishful result would be so much easier to solve 


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes
Message 11 of 11

hythamthelove
Advocate
Advocate

Thank you, this works so good

0 Likes