Solve: add subtract two number and replace

Solve: add subtract two number and replace

taitrongsong
Enthusiast Enthusiast
1,914 Views
17 Replies
Message 1 of 18

Solve: add subtract two number and replace

taitrongsong
Enthusiast
Enthusiast

I have lisp as below to subtract two number and replace another number, please help me check error, many thanks:

(defun c:tru()
(vl-load-com)
((setq sobitru (car (entsel "\nChon so bi tru:"))
sotru (car (entsel "\nChon so tru:\n"))
kqua (- (atof (cdr (assoc 1 (entget sobitru))))
(atof (cdr (assoc 1 (entget sotru))))))
(princ kqua))
)
(if (not ssle) (setq ssle 0))
(setq obj (vlax-ename->vla-object (car (entsel "\nChon text de ghi ket qua:")))
ssle1 (getint (strcat "\nSo so le <" (itoa ssle) ">: ")))
(if ssle1 (setq ssle ssle1))
(vla-put-TextString obj (rtos kqua 2 ssle))
(princ)
)

0 Likes
Accepted solutions (4)
1,915 Views
17 Replies
Replies (17)
Message 2 of 18

Kent1Cooper
Consultant
Consultant

@taitrongsong wrote:

I have lisp as below to subtract two number and replace another number, please help me check error, many thanks:

(defun c:tru()
  (vl-load-com)
  ((setq

    sobitru (car (entsel "\nChon so bi tru:"))
    sotru (car (entsel "\nChon so tru:\n"))
    kqua

      (-

        (atof (cdr (assoc 1 (entget sobitru))))
        (atof (cdr (assoc 1 (entget sotru))))

      ); -

  ); setq
  (princ kqua))
)
  (if (not ssle) (setq ssle 0))
  (setq

    obj (vlax-ename->vla-object (car (entsel "\nChon text de ghi ket qua:")))
    ssle1 (getint (strcat "\nSo so le <" (itoa ssle) ">: "))

  ); setq
  (if ssle1 (setq ssle ssle1))
  (vla-put-TextString obj (rtos kqua 2 ssle))
  (princ)
)


If I understand what's happening, the parentheses that I have turned red are all extraneous.  Remove those, and see whether it works.

Kent Cooper, AIA
0 Likes
Message 3 of 18

taitrongsong
Enthusiast
Enthusiast

many tks Kent Cooper, but it's still not working,,,

0 Likes
Message 4 of 18

pbejse
Mentor
Mentor

How is it suppose to work @taitrongsong ?

Can you show us please.

 

0 Likes
Message 5 of 18

paullimapa
Mentor
Mentor
Accepted solution

what @Kent1Cooper corrected fixed the problem. Works for me. I'm able to successfully select two Text objects that contain numbers and then select a third Text object to place the result. Why is it not working for you?

 

; tru function
(defun c:tru 
  (/ kqua obj sobitru sotru ssle1) ; localize symbols except for ssle
 (vl-load-com)
 (setq sobitru (car (entsel "\nSelect 1st Text with Number:")) ; "\nChon so bi tru:"
        sotru (car (entsel "\nSelect 2nd Text with Number:")) ; "\nChon so tru:"
         kqua (- (atof (cdr (assoc 1 (entget sobitru)))) (atof (cdr (assoc 1 (entget sotru)))))
 )
 (princ kqua)
 (if (not ssle) (setq ssle 0))
 (setq obj (vlax-ename->vla-object (car (entsel "\nSelect 3rd Text to Place Result:"))) ; "\nChon text de ghi ket qua:"
     ssle1 (getint (strcat "\nEnter Decimal Precision <" (itoa ssle) ">: ")) ; "\nSo so le <"
 )
 (if ssle1 (setq ssle ssle1))
 (vla-put-TextString obj (rtos kqua 2 ssle))
(princ)
) ; defun

 

 

 

 

 


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
Message 6 of 18

devitg
Advisor
Advisor
Accepted solution

@paullimapa Hi . it was my same approach.

 

 to rest and put diff in  other BEFORE.pngto rest and put diff in  other after.png

 

 

Message 7 of 18

paullimapa
Mentor
Mentor

Very nice graphics to show that the lisp routine now works...


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes
Message 8 of 18

taitrongsong
Enthusiast
Enthusiast
many tks Mr. paulli_apa, Mr. devitg, it was done,,,,
0 Likes
Message 9 of 18

paullimapa
Mentor
Mentor

glad to have helped...cheers!!!


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes
Message 10 of 18

calderg1000
Mentor
Mentor
Accepted solution

Regards @taitrongsong 

Here my contribution for addition and subtraction operations. Excuse my mistakes, try to translate into your language.

(defun c:c_t ( / sobitru sotru kqua)
  (vl-load-com)
  (initget "C T")
  (setq opc (getkword "\nCh?n ho?t d?ng: Phép c?ng/Phép tr? [C/T]<C>: "))
  (if (= opc "C")
    (setq f ss)
    (setq f dd)
  )
  (setq sobitru (car (entsel "\nChon so bi tru:"))
        sotru   (car (entsel "\nChon so tru:\n"))
        kqua    (f (atof (cdr (assoc 1 (entget sobitru))))
                   (atof (cdr (assoc 1 (entget sotru))))
                )
  )
  (princ kqua)
  (if (not ssle)
    (setq ssle 0)
    (setq obj   (vlax-ename->vla-object (car (entsel "\nChon text de ghi ket qua:")))
          ssle1 (getint (strcat "\nSo so le <" (itoa ssle) ">: "))
    )
  )
  (if ssle1
    (setq ssle ssle1)
  )
  (vla-put-TextString obj (rtos kqua 2 ssle))
  (princ)
)
(defun ss (x y)
  (+ x y)
)
(defun dd (x y)
  (- x y)
)

 


Carlos Calderon G
EESignature
>Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

0 Likes
Message 11 of 18

taitrongsong
Enthusiast
Enthusiast
Hi Mr paulli_apa, how to change ssle to another color ? tks,,
0 Likes
Message 12 of 18

paullimapa
Mentor
Mentor
Accepted solution

If you're referring to changing the color of the last Text object selected then simply add this line of code before the closing defun parenthesis:

(command "_.CHPROP" obj "" "_Color" "5" "" "")

 This will change ssle to color 5.

Check this as example: https://www.cadtutor.net/forum/topic/62977-how-to-apply-chprop-command-on-selected-items/


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes
Message 13 of 18

calderg1000
Mentor
Mentor

Dear @taitrongsong 

Here is the code with a request to change the color of the result, with a dialog box.

 

(defun c:c_t ( / sobitru sotru kqua)
  (vl-load-com)
  (initget "C T")
  (setq opc (getkword "\nCh?n ho?t d?ng: Phép c?ng/Phép tr? [C/T]<C>: "))
  (if (= opc "C")
    (setq f ss)
    (setq f dd)
  )
  (setq sobitru (car (entsel "\nChon so bi tru:"))
        sotru   (car (entsel "\nChon so tru:\n"))
        kqua    (f (atof (cdr (assoc 1 (entget sobitru))))
                   (atof (cdr (assoc 1 (entget sotru))))
                )
  )
  (princ kqua)
  (if (not ssle)
    (setq ssle 0)
    (setq obj   (vlax-ename->vla-object (car (entsel "\nChon text de ghi ket qua:")))
          ssle1 (getint (strcat "\nSo so le <" (itoa ssle) ">: "))
    )
  )
  (if ssle1
    (setq ssle ssle1)
  )
  (vla-put-TextString obj (rtos kqua 2 ssle))
  (setq cc(acad_colordlg 256))
  (vla-put-color obj cc)
  (princ)
)
(defun ss (x y)
  (+ x y)
)
(defun dd (x y)
  (- x y)
)

 

 


Carlos Calderon G
EESignature
>Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

Message 14 of 18

symoin
Enthusiast
Enthusiast

(defun c:SUB2
(/ kqua obj sobitru sotru ssle1) ; localize symbols except for ssle
(vl-load-com)
(setq sobitru (car (entsel "\nSelect 1st Text with Number:")) ; "\nChon so bi tru:"
sotru (car (entsel "\nSelect 2nd Text with Number:")) ; "\nChon so tru:"
kqua (- (atof (cdr (assoc 1 (entget sobitru)))) (atof (cdr (assoc 1 (entget sotru)))))
)
(princ kqua)
(if (not ssle) (setq ssle 0))
(setq obj (vlax-ename->vla-object (car (entsel "\nSelect 3rd Text to Place Result:"))) ; "\nChon text de ghi ket qua:"
;ssle1 (getint (strcat "\nEnter Decimal Precision <" (itoa ssle) ">: ")) ; "\nSo so le <"
)
(if ssle1 (setq ssle ssle1))
(vla-put-TextString obj (rtos kqua 2 ssle))
(command "_.CHPROP" obj "" "_Color" "5" "" ""))
(princ); defun

 

Hi, can we fix the Decimals to 3 and Changing the color is not working. Could you please help on this?

0 Likes
Message 15 of 18

paullimapa
Mentor
Mentor

Just change the last section from

(vla-put-TextString obj (rtos kqua 2 ssle))
(command "_.CHPROP" obj "" "_Color" "5" "" ""))

to this 

(vla-put-TextString obj (rtos kqua 2 3)) ; sets decimal place to 3
(setq obj (vlax-vla-object->ename obj)) ; converts obj to name
(command "_.CHPROP" obj "" "_Color" "5" "" ""))

Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes
Message 16 of 18

Sea-Haven
Mentor
Mentor

Or leave as a vl object.

 

(vlax-put obj 'color 5)

 

0 Likes
Message 17 of 18

symoin
Enthusiast
Enthusiast

Thanks for your help,
I have a situation Where I need to find the difference between 2 layers (Before and after) and write the result in another layer (Difference). The drawing has points with elevations and the text as elevation labels, I need to write the difference in the Z value Elevations near the location. I there any way this can be achieved in one go rather than each pair of text (selecting 3 points). There are more than 2500 pair of such points.

 

0 Likes
Message 18 of 18

paullimapa
Mentor
Mentor

Best you post this request as a new topic so others can jump in to resolve 


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos