Inserting calculation result as text

Inserting calculation result as text

allwinaugustine
Participant Participant
390 Views
3 Replies
Message 1 of 4

Inserting calculation result as text

allwinaugustine
Participant
Participant

I was trying to create a lisp program to get the results using numbers from the text.

formulae is y=x-kd where,

x & d are extracted from the two different texts and value of k is as user input. I was able to get the values correct on command line. But unable to insert it as a text entity. I am getting an error called,

 

error: bad DXF group: nil

 

code is given below. Can someone please help me with it ?

------------------------------------------------------------------------------------------------------------------

(defun c:calculateY (/ xText dText k x d y xEnt dEnt entdata insertionPoint textHeight textStyle)
;; Prompt the user to select the text entity containing the value of x
(setq xEnt (car (entsel "\nSelect text entity containing the value of x: ")))

;; If a valid entity is selected for x
(if (and xEnt (eq (cdr (assoc 0 (entget xEnt))) "TEXT"))
(progn
(setq xText (cdr (assoc 1 (entget xEnt))))
(setq x (atof xText))
)
(progn
(princ "\nInvalid selection for x. Please select a text entity.")
(exit)
)
)

;; Prompt the user to select the text entity containing the value of d
(setq dEnt (car (entsel "\nSelect text entity containing the value of d: ")))

;; If a valid entity is selected for d
(if (and dEnt (eq (cdr (assoc 0 (entget dEnt))) "TEXT"))
(progn
(setq dText (cdr (assoc 1 (entget dEnt))))
(setq d (atof dText))
)
(progn
(princ "\nInvalid selection for d. Please select a text entity.")
(exit)
)
)

;; Prompt the user for the value of k
(setq k (getreal "\nEnter the value of k: "))

;; Calculate the value of y if x, d, and k are valid
(if (and x d k)
(progn
(setq y (- x (* k d)))
(princ (strcat "\nThe value of y is: " (rtos y 2 2)))

;; Get text height and style from x text entity
(setq textHeight (cdr (assoc 40 (entget xEnt))))
(setq textStyle (cdr (assoc 7 (entget xEnt))))

;; Ensure textHeight and textStyle are valid
(if (and textHeight textStyle)
(progn
;; Prompt for the insertion point of the new text
(setq insertionPoint (getpoint "\nSpecify insertion point for new text: "))

;; Ensure insertionPoint is valid
(if insertionPoint
(progn
(setq newText (rtos y 2 2))
(entmake (list (cons 0 "TEXT")
(cons 10 insertionPoint)
(cons 1 newText)
(cons 40 textHeight)
(cons 7 textStyle)
(cons 8 (cdr (assoc 8 (entget xEnt)))) ; Layer
(assoc 50 (entget xEnt)) ; Rotation angle
(assoc 41 (entget xEnt)) ; Width factor
(assoc 71 (entget xEnt)) ; Text generation flags
(assoc 72 (entget xEnt)) ; Horizontal text alignment
(assoc 73 (entget xEnt)) ; Vertical text alignment
(assoc 74 (entget xEnt)) ; Text line spacing style
(assoc 210 (entget xEnt)))) ; Normal vector

(princ (strcat "\nNew text created with value of y: " newText))
)
(princ "\nError: Invalid insertion point.")
)
)
(princ "\nError: Invalid text height or style.")
)
)
(princ "\nError: Unable to calculate y. Please ensure all inputs are valid.")
)

(princ)
)

 

 

0 Likes
Accepted solutions (1)
391 Views
3 Replies
Replies (3)
Message 2 of 4

komondormrex
Mentor
Mentor
Accepted solution

you need to comment out line

(assoc 74 (entget xEnt)) ; Text line spacing style

because 'text' entity has no 74 group

0 Likes
Message 3 of 4

allwinaugustine
Participant
Participant

Thank you so much Bro! You saved my day !

0 Likes
Message 4 of 4

komondormrex
Mentor
Mentor

your welcome)

0 Likes