Hi Dave,
I hope the following program achieves the result you require.
It currently realigns the MText content and Block content of the MLeader to the current UCS.
The UCS (if set) needs to be in the same plane as the selected MLeaders.
Undesired results may occur on MLeaders that aren't in the same UCS plane.
I've only been able to test it on the examples you provide.
The realigned block content is upside down on some of the example MLeaders but I think it is unavoidable due to the setout geometry of the leader and the block content.
I would like to investigate this further and hopefully provide an update in the future to ensure MLeader content is realigned in a readable orientation.
I would also like to thank Lee Mac for providing the vector manipulation functions.
FYI: the 16 entries for DXF code 47 form a 4x4 matrix.
Regards,
Trevor
;; MLeadersAlign.lsp by Trevor Bird
;;
;; 2016-08-12
;;------------------------------------------------------------------------------
(defun c:mleadersalign
(
/
BlockContentPosition_15
BlockContentRotation_46
BlockMatrix1
BlockMatrix2
BlockPlaneX:uv
BlockPlaneY:uv
BlockPlaneZ:uv
ContentBasePosition_10
ContextData_10
ContextData_110
ContextData_111
ContextData_112
ContextData_12
ContextData_13
ContextData_14
ContextData_15
ContextData_16
ContextData_171
ContextData_175
ContextData_297
ContextData_42
ContextData_46
ContextData_DXF
ContextData_DXF_new
DogLeg:uv
Leader_10
Leader_11
Leader_40
Leader_DXF_new
list_47_new
MULTILEADER_172
MULTILEADER_DXF
MULTILEADER_DXF_new
MULTILEADER_ename
ScalarDistance
ScalarSign
ssC
ss_MULTILEADER
sv_ucsxdir_wcs
sv_ucsydir_wcs
TextDirection_13
TextLocation_12
ucsxdir_angle
ucsydir_angle
ucszdir_wcs
VectorsAngle
VectorsVDP
VectorX
VectorY
Vector:v
)
(setq ss_MULTILEADER (ssget (list '(0 . "MULTILEADER") (cons 410 (getvar 'CTAB)))))
(cond
( (not ss_MULTILEADER)
(princ "\nNo MLeaders selected.")
);(not ss_MULTILEADER)
(ss_MULTILEADER
(setq sv_ucsxdir_wcs (getvar 'UCSXDIR)
sv_ucsydir_wcs (getvar 'UCSYDIR)
ucsxdir_angle (atan (cadr sv_ucsxdir_wcs) (car sv_ucsxdir_wcs))
ucsydir_angle (atan (cadr sv_ucsydir_wcs) (car sv_ucsydir_wcs))
);setq
(setq ssC -1)
(repeat (sslength ss_MULTILEADER)
(setq MULTILEADER_ename (ssname ss_MULTILEADER (setq ssC (1+ ssC)))
MULTILEADER_DXF (entget MULTILEADER_ename)
MULTILEADER_DXF_new (reverse (cdr (member '(300 . "CONTEXT_DATA{") (reverse MULTILEADER_DXF))))
MULTILEADER_DXF_new (append MULTILEADER_DXF_new (cdr (member '(301 . "}") MULTILEADER_DXF)))
MULTILEADER_172 (cdr (assoc 172 MULTILEADER_DXF_new)) ; Content Type
ContextData_DXF (cdr (member '(300 . "CONTEXT_DATA{") MULTILEADER_DXF))
ContextData_DXF (reverse (cdr (member '(301 . "}") (reverse ContextData_DXF))))
ContextData_10 (cdr (assoc 10 ContextData_DXF)) ; Content Base Position
ContextData_110 (cdr (assoc 110 ContextData_DXF)) ; MLeader Plane Origin Point
ContextData_111 (cdr (assoc 111 ContextData_DXF)) ; MLeader Plane X-Axis Direction
ContextData_112 (cdr (assoc 112 ContextData_DXF)) ; MLeader Plane Y-Axis Direction
ContextData_297 (cdr (assoc 297 ContextData_DXF)) ; MLeader Plane Normal Reversed
);setq
;; <!-- Leader
(setq Leader_DXF_new (cdr (member '(302 . "LEADER{") ContextData_DXF))
Leader_DXF_new (reverse (cdr (member '(303 . "}") (reverse Leader_DXF_new))))
ContextData_DXF_new (reverse (cdr (member '(302 . "LEADER{") (reverse ContextData_DXF))))
ContextData_DXF_new (append ContextData_DXF_new (cdr (member '(303 . "}") ContextData_DXF)))
Leader_10 (cdr (assoc 10 Leader_DXF_new)) ; Last Leader Line Point
Leader_11 (cdr (assoc 11 Leader_DXF_new)) ; Dogleg Vector
Leader_40 (cdr (assoc 40 Leader_DXF_new)) ; Dogleg Length
DogLeg:uv Leader_11
);setq
;; Leader -->
;; <!-- Content Type
(cond
( (= MULTILEADER_172 acMTextContent)
(setq ContextData_12 (cdr (assoc 12 ContextData_DXF)) ; Text Location
ContextData_13 (cdr (assoc 13 ContextData_DXF)) ; Text Direction
ContextData_42 (cdr (assoc 42 ContextData_DXF)) ; Text Rotation
ContextData_171 (cdr (assoc 171 ContextData_DXF)) ; Text Attachment
ContextData_175 (cdr (assoc 175 ContextData_DXF)) ; Text Alignment Type
);setq
;; Determine new Dog Leg direction:
(if (minusp (leemac:vxv Leader_11 sv_ucsxdir_wcs))
(setq ScalarSign -1.0)
(setq ScalarSign 1.0)
);if
(setq DogLeg:uv (leemac:vxs sv_ucsxdir_wcs ScalarSign)
Leader_DXF_new (subst (cons 11 DogLeg:uv) (assoc 11 Leader_DXF_new) Leader_DXF_new)
);setq
;; Reset Text Location:
(setq Vector:v (mapcar '- ContextData_12 Leader_10)
VectorsVDP (leemac:vxv (leemac:unit Vector:v) Leader_11)
VectorsAngle (* ScalarSign (leemac:acos VectorsVDP))
ScalarDistance (distance Leader_10 ContextData_12)
VectorX (- (* (car DogLeg:uv) (cos VectorsAngle)) (* (cadr DogLeg:uv) (sin VectorsAngle)))
VectorY (+ (* (cadr DogLeg:uv) (cos VectorsAngle)) (* (car DogLeg:uv) (sin VectorsAngle)))
TextLocation_12 (mapcar '+ Leader_10 (leemac:vxs (list VectorX VectorY 0.0) ScalarDistance))
TextDirection_13 sv_ucsxdir_wcs
ContextData_DXF_new (subst (cons 12 TextLocation_12) (assoc 12 ContextData_DXF_new) ContextData_DXF_new)
ContextData_DXF_new (subst (cons 13 TextDirection_13) (assoc 13 ContextData_DXF_new) ContextData_DXF_new)
);setq
;; Reset Content Base Postion:
(setq Vector:v (mapcar '- ContextData_10 Leader_10)
VectorsVDP (leemac:vxv (leemac:unit Vector:v) Leader_11)
VectorsAngle (* ScalarSign (leemac:acos VectorsVDP))
ScalarDistance (distance Leader_10 ContextData_10)
VectorX (- (* (car DogLeg:uv) (cos VectorsAngle)) (* (cadr DogLeg:uv) (sin VectorsAngle)))
VectorY (+ (* (cadr DogLeg:uv) (cos VectorsAngle)) (* (car DogLeg:uv) (sin VectorsAngle)))
ContentBasePosition_10 (mapcar '+ Leader_10 (leemac:vxs (list VectorX VectorY 0.0) ScalarDistance))
ContextData_DXF_new (subst (cons 10 ContentBasePosition_10) (assoc 10 ContextData_DXF_new) ContextData_DXF_new)
);setq
);(= MULTILEADER_172 acMTextContent)
( (= MULTILEADER_172 acBlockContent)
(setq ContextData_14 (cdr (assoc 14 ContextData_DXF)) ; Block Content Normal Direction
ContextData_15 (cdr (assoc 15 ContextData_DXF)) ; Block Content Position
ContextData_16 (cdr (assoc 16 ContextData_DXF)) ; Block Content Scale
ContextData_46 (cdr (assoc 46 ContextData_DXF)) ; Block Content Rotation
);setq
;; Block Content Axes in UCS:
(setq ucszdir_wcs (leemac:v^v sv_ucsxdir_wcs sv_ucsydir_wcs)
BlockPlaneX:uv (mapcar '* ContextData_16 sv_ucsxdir_wcs)
BlockPlaneY:uv (mapcar '* ContextData_16 sv_ucsydir_wcs)
BlockPlaneZ:uv (mapcar '* ContextData_16 ucszdir_wcs)
);setq
;; Determine new Dog Leg direction:
(setq Vector:v (mapcar '- ContextData_15 Leader_10))
(if (minusp (leemac:vxv (leemac:unit Vector:v) sv_ucsxdir_wcs))
(setq ScalarSign -1.0)
(setq ScalarSign 1.0)
);if
(setq DogLeg:uv (leemac:vxs sv_ucsxdir_wcs ScalarSign)
Leader_DXF_new (subst (cons 11 DogLeg:uv) (assoc 11 Leader_DXF_new) Leader_DXF_new)
);setq
;; Determine new Block Content Rotation:
(cond
( (and (> ContextData_46 ucsydir_angle)
(< ContextData_46 (+ ucsydir_angle pi))
);and
(setq BlockContentRotation_46 ucsxdir_angle
ScalarDistance Leader_40
);setq
)
(ContextData_46
(setq BlockContentRotation_46 ucsxdir_angle
ScalarDistance (distance Leader_10 ContextData_15)
);setq
);ContextData_46
);cond
(setq ContextData_DXF_new (subst (cons 46 BlockContentRotation_46) (assoc 46 ContextData_DXF_new) ContextData_DXF_new))
;; Determine new Block Content Position:
(setq BlockContentPosition_15 (mapcar '+ Leader_10 (leemac:vxs DogLeg:uv ScalarDistance))
ContextData_DXF_new (subst (cons 15 BlockContentPosition_15) (assoc 15 ContextData_DXF_new) ContextData_DXF_new)
);setq
;; Reconstruct Block Content Matrix:
(setq BlockMatrix1
(list
(append BlockPlaneX:uv '(0.0))
(append BlockPlaneY:uv '(0.0))
(append BlockPlaneZ:uv '(0.0))
(append BlockContentPosition_15 '(1.0))
);list
BlockMatrix2 (apply 'mapcar (cons 'list BlockMatrix1))
list_47_new (apply 'append BlockMatrix2)
list_47_new (mapcar '(lambda (x) (cons 47 x)) list_47_new)
ContextData_DXF_new (vl-remove-if '(lambda (_dp) (= (car _dp) 47)) ContextData_DXF_new)
ContextData_DXF_new (append ContextData_DXF_new list_47_new)
);setq
);(= MULTILEADER_172 acBlockContent)
);cond
;; Content Type -->
;; Reconstruct MULTILEADER_DXF_new:
(setq Leader_DXF_new (cons '(302 . "LEADER{") Leader_DXF_new)
Leader_DXF_new (reverse (cons '(303 . "}") (reverse Leader_DXF_new)))
ContextData_DXF_new (append ContextData_DXF_new Leader_DXF_new)
ContextData_DXF_new (cons '(300 . "CONTEXT_DATA{") ContextData_DXF_new)
ContextData_DXF_new (reverse (cons '(301 . "}") (reverse ContextData_DXF_new)))
);setq
(setq MULTILEADER_DXF_new
(append MULTILEADER_DXF_new
ContextData_DXF_new
);append
);setq
(entmod MULTILEADER_DXF_new)
(entupd MULTILEADER_ename)
);repeat (sslength ss_MULTILEADER)
);ss_MULTILEADER
);cond
(princ)
);c:mleadersalign
;;------------------------------------------------------------------------------
;; Vector Norm - Lee Mac
;; Args: v - vector in R^n
(defun leemac:norm ( v )
(sqrt (apply '+ (mapcar '* v v)))
)
;; Vector x Scalar - Lee Mac
;; Args: v - vector in R^n, s - real scalar
(defun leemac:vxs ( v s )
(mapcar '(lambda ( n ) (* n s)) v)
)
;; Unit Vector - Lee Mac
;; Args: v - vector in R^n
(defun leemac:unit ( v )
( (lambda ( n ) (if (equal 0.0 n 1e-14) nil (leemac:vxs v (/ 1.0 n)))) (leemac:norm v))
)
;; Vector Cross Product - Lee Mac
;; Args: u,v - vectors in R^3
(defun leemac:v^v ( u v )
(list
(- (* (cadr u) (caddr v)) (* (cadr v) (caddr u)))
(- (* (car v) (caddr u)) (* (car u) (caddr v)))
(- (* (car u) (cadr v)) (* (car v) (cadr u)))
)
)
;; Vector Dot Product - Lee Mac
;; Args: u,v - vectors in R^n
(defun leemac:vxv ( u v )
(apply '+ (mapcar '* u v))
)
;; ArcCosine - Lee Mac
;; Args: -1 <= x <= 1
(defun leemac:acos ( x )
(if (<= -1.0 x 1.0)
(atan (sqrt (- 1.0 (* x x))) x)
)
)
;;------------------------------------------------------------------------------
(princ "\nMLeadersAlign loaded. Start command with MLEADERSALIGN.")
(princ)