Good day everyone,
This is a code that lets me add center line between two points together with the text, the text input is a measurement between pt1 and pt2 with a prefix "M", this is almost perfect to what I need. What I need to improve is to place the text aligned with the center line (as seen in Fig3) however the text is place perpendicularly to the center line (as seen in Fig2), if the drawing is horizontal the text is place somewhere else. Fig1 is the original condition of the drawing generated from 3D. Is there a way I can change the code to make the text always aligned to the created center line similar to Fig3 and I also want to use the Text Style for the size of the text. Thank you in advance everyone.
(defun c:CenterLineWithAlignedText ()
(setq pt1 (getpoint "\\nSelect point 'a': "))
(setq pt2 (getpoint pt1 "\\nSelect point 'b': "))
;; Create or set the "CEN" layer
(setq cen_layer "CEN")
(if (not (tblsearch "layer" cen_layer))
(entmake
'((0 . "LAYER")
(100 . "AcDbSymbolTableRecord")
(100 . "AcDbLayerTableRecord")
(2 . cen_layer)
(70 . 0)
(6 . "Continuous") ; Set desired line type
(62 . 256) ; Set desired color
)
)
)
(command "_.-layer" "_set" cen_layer)
;; Calculate midpoint between pt1 and pt2
(setq midpt (polar pt1 (angle pt1 pt2) (/ (distance pt1 pt2) 2.0)))
;; Draw center line on the "CEN" layer
(command "LINE" midpt pt2 "") ; Draw line from midpoint to point "b"
;; Prompt user to click after selecting points "a" and "b"
(prompt "\\nClick to add the endpoint of the line.")
;; Wait for user input (clicking the endpoint)
(setq pt3 (getpoint "\\nSelect the endpoint: "))
;; Calculate measurement value
(setq measurement (distance pt1 pt2))
(setq text_value (strcat "M" (rtos measurement 2 0))) ; Add "M" prefix to measurement (without trailing zero)
;; Calculate rotation angle for text (parallel to centerline)
(setq text_angle (angle pt1 pt2)) ; Use the angle of the centerline
;; Calculate text position (parallel to center line)
(setq text_height 2.0) ; Adjust text height as needed
(setq text_parallel_angle (+ text_angle (* pi 0.5))) ; Calculate parallel angle
(setq text_position (polar midpt text_parallel_angle (/ (distance midpt pt3) 2.0))) ; Position text at the middle of the centerline
;; Add text at the new endpoint (aligned with center line)
(command "TEXT" text_position text_height text_angle text_value "" "ALIGNED") ; Specify ALIGNED justification
;; Draw line from midpoint to the new endpoint
(command "LINE" midpt pt3 "")
(princ)
)