- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
I posted something similar a while back, but I could never get it to work the way I wanted, so I had Gemini write me a lisp routine to calculate glass size from a window. I pick two points to define a rectangle, add a user defined dimension and an angle of rotation, and the routine inserts the value as text in the center of the rectangle. So far, the routine works great, and I get nearly the result I want. However I wish it could diagonally stack the fractions similar to our office standards. I understand that the routine just uses TEXT, no MTEXT, so no stacked fractions, but as I understand it, it is much more difficult to use MTEXT with stacked fractions in a routine. Is it possible for one of you awesome users to look at the code and suggest a way to make this work with stacked fractions? Thanks in advance for all your help.
;; GS - Creates single-line Text with calculated width and height.
;;
;; This routine prompts the user for two points to define a rectangle
;; and then an additional distance. It now also prompts for a rotation
;; angle. It creates a Text entity at the center of the rectangle,
;; displaying the calculated dimensions with the specified rotation.
;;
(defun C:GS ( / pt1 pt2 width height ext_dist final_width final_height
display_width_string display_height_string mid_pt rotation_angle text_rot_angle
old_aunits)
(vl-load-com) ; Loads Visual Lisp extensions
;; Save the current angular units to restore later
(setq old_aunits (getvar "aunits"))
;; Temporarily set angular units to degrees (0) for user input
(setvar "aunits" 0)
;; 1. Get the two points to define the virtual rectangle
(setq pt1 (getpoint "\nSelect first corner of rectangle: "))
(setq pt2 (getcorner pt1 "\nSelect opposite corner: "))
;; 2. Calculate the width and height of the rectangle
(setq width (abs (- (car pt1) (car pt2))))
(setq height (abs (- (cadr pt1) (cadr pt2))))
;; 3. Get the user-defined distance to add
(setq ext_dist (getdist "\nSpecify distance to add to width and height: "))
;; 4. Add the distance to the width and height
(setq final_width (+ width ext_dist))
(setq final_height (+ height ext_dist))
;; 5. Get the rotation angle from the user using GETREAL
(setq rotation_angle (getreal "\nSpecify text rotation angle in degrees: "))
;; 6. Convert the final dimensions to a string with fractions using a custom function
(setq display_width_string (GS:rtos-frac final_width))
(setq display_height_string (GS:rtos-frac final_height))
;; 7. Construct the full Text content string, now with inch marks
(setq text_string (strcat display_width_string "\"" " x " display_height_string "\""))
;; 8. Calculate the midpoint of the virtual rectangle for Text placement
(setq mid_pt (list (+ (car pt1) (/ (- (car pt2) (car pt1)) 2.0))
(+ (cadr pt1) (/ (- (cadr pt2) (cadr pt1)) 2.0))
0.0
)
)
;; 9. Create the Text entity using the command function
;; Pass the rotation angle directly. It will be interpreted correctly in degrees.
(command "_.text" "_J" "_MC" mid_pt 2.25 rotation_angle text_string)
;; Restore the original angular units
(setvar "aunits" old_aunits)
(princ) ; Clean exit
)
;; A helper function to convert a real number to a string with reduced fractions.
;; This function does not use any system variables and is fully self-contained.
(defun GS:rtos-frac (num / int_part frac_part numerator denominator common_divisor)
(setq int_part (fix num))
(setq frac_part (- num int_part))
(if (>= frac_part 0.001) ; Check if there is a fractional part
(progn
(setq denominator 16) ; Use a common denominator, e.g., 16ths
(setq numerator (GS:round-custom (* frac_part denominator)))
(setq common_divisor (GS:gcd numerator denominator))
(setq numerator (/ numerator common_divisor))
(setq denominator (/ denominator common_divisor))
(strcat
(if (/= int_part 0) (itoa int_part) "") ; Add integer part if it's not 0
" "
(itoa numerator) "/" (itoa denominator)
)
)
(itoa int_part) ; If no fractional part, just return the integer
)
)
;; A helper function to calculate the Greatest Common Divisor (GCD) of two integers
(defun GS:gcd (a b)
(if (= b 0)
a
(GS:gcd b (rem a b))
)
)
;; Custom rounding function for older Lisp versions
(defun GS:round-custom (n)
(+ (fix n) (if (>= (rem n 1) 0.5) 1 0))
)
Solved! Go to Solution.