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