Author Topic: Get Text string from Attributes with user pick  (Read 2763 times)

0 Members and 1 Guest are viewing this topic.

Fabricio28

  • Swamp Rat
  • Posts: 670
Get Text string from Attributes with user pick
« on: May 21, 2014, 10:11:27 AM »
Hi guys,
I wanna make a little modification in that code.
Instead of select text, I'd like to select a attribute.

 Can anybody help me out please?

Code: [Select]
(defun c:t3 (/ txt1 txt2 txt3 pkt1)
  (setq txt1 (entsel "Select first text : "))
  (setq txt2 (entsel "Select second text : "))
  (setq txt3
         (strcat
           
           (rtos (- (atof (cdr (assoc 1 (entget (car txt1)))))
                    (atof (cdr (assoc 1 (entget (car txt2)))))
                 ) 2 2 )  ) )
  (if  (and (setq pkt1 (getpoint "Where to place difference ? : "))
     (if (> (cdr (assoc 40 (tblsearch "STYLE" (getvar "TEXTSTYLE")))) 0)
        (command "_.TEXT" pkt1 "" txt3)
        (command "_.TEXT" pkt1 "" "" txt3)
  ))

  (princ)
)
)

Thank in advance
« Last Edit: May 21, 2014, 02:23:07 PM by CAB »

roy_043

  • Water Moccasin
  • Posts: 1895
  • BricsCAD 18
Re: Request for CAB's code
« Reply #1 on: May 21, 2014, 10:22:42 AM »
Try nentsel instead of entsel.

Fabricio28

  • Swamp Rat
  • Posts: 670
Re: Request for CAB's code
« Reply #2 on: May 21, 2014, 10:29:19 AM »
wow  :ugly: me lol
Just Simple!!

Thank you very much roy!!!

May ask you one more thing?
I'd like to add sum symbol (+) for positive.

Because I'm working with (+) to fill and (-) to cut

Thank you

roy_043

  • Water Moccasin
  • Posts: 1895
  • BricsCAD 18
Re: Request for CAB's code
« Reply #3 on: May 21, 2014, 10:37:39 AM »
Code: [Select]
(setq result (- ... ...))
(strcat (if (>= result 0) "+" "") (rtos result 2 2))

Fabricio28

  • Swamp Rat
  • Posts: 670
Re: Request for CAB's code
« Reply #4 on: May 21, 2014, 12:55:50 PM »
Finally
Code: [Select]
(defun c:t3 (/ result txt1 txt2 txt3 pkt1)
  (setq txt1 (nentsel "Select first text : "))
  (setq txt2 (nentsel "Select second text : "))
  (setq result (- (atof (cdr (assoc 1 (entget (car txt1)))))
    (atof (cdr (assoc 1 (entget (car txt2)))))
        )
  )
  (setq txt3 (strcat (if (> result 0) "+" "") (rtos result 2 2)))
  (if
    (and (setq pkt1 (getpoint "Where to place difference ? : "))
  (if (> (cdr (assoc 40 (tblsearch "STYLE" (getvar "TEXTSTYLE")))) 0)
    (command "_.TEXT" pkt1 "" txt3)
    (command "_.TEXT" pkt1 "" "" txt3)
  )
    )
     (princ)
  )
)

Thank you very much roy  :-D

Fabricio28

  • Swamp Rat
  • Posts: 670
Re: Get Text string from Attributes with user pick
« Reply #5 on: May 22, 2014, 08:06:15 AM »
2nd option...
Why that code isn't working?

Code: [Select]
(defun c:t3 ()
  (setq txt1 (car(entsel"\nSelecione o primeiro texto :")))
  (setq txt1 (atof(cdr(assoc 1(entget txt1)))))
  (setq txt2 (car(entsel"\nSelecione o segundo texto :")))
  (setq txt2 (atof(cdr(assoc 1(entget txt2)))))
  (setq txt3 (- txt1 txt2))
  (if (>= txt3 0)
(setq txt4 (strcat "+"(rtos txt3)))
(setq txt4 (rtos txt3))
     )
  (setq p1 (getpoint "\nInforme o local do texto da diferença :"))
(command "text" p1 "" "" txt4 "")
  )

roy_043

  • Water Moccasin
  • Posts: 1895
  • BricsCAD 18
Re: Get Text string from Attributes with user pick
« Reply #6 on: May 22, 2014, 03:55:51 PM »
Why have you added the extra enter here?:
Code: [Select]
(command "text" p1 "" "" txt4 "")It should be:
Code: [Select]
(command "text" p1 "" "" txt4)

Fabricio28

  • Swamp Rat
  • Posts: 670
Re: Get Text string from Attributes with user pick
« Reply #7 on: May 23, 2014, 05:38:29 PM »
Hi roy,
I've noticed that mistake and I fixed it.

But the problem is when I pick the atributte
Alert:
Code: [Select]
ERRO : bad argument type: stringp nil
Thank in advance

hanhphuc

  • Newt
  • Posts: 64
Re: Get Text string from Attributes with user pick
« Reply #8 on: May 24, 2014, 01:32:45 AM »
hi i noticed the the 2nd code not nentsel??
thx

since i'm still new here, so also wanna practice & learn
i hope this can be applied for TEXT, MTEXT & ATTRIB
;24/05/14  hp#006
Code: [Select]
(defun t-t (et1 et2 / tx1 tx2 ans) ; argument: text entity
  (if (and et1 et2)
  (if (and
(member 1(setq tx1(assoc 1 (entget (car et1)))))
      (member 1(setq tx2(assoc 1 (entget (car et2))))))
(progn
(setq ans(- (atof (cdr tx1))
    (atof (cdr tx2))))
(strcat (if (> ans 0) "+" "")
(rtos ans 2 2))
)
   );if
    )
    )

(defun c:test (/ str pt)
(if
(setq str (t-t (nentsel "\nSelecione o primeiro texto : ")
  (nentsel "\nSelecione o segundo texto : ")))
  (setq pt (getpoint "\nInforme o local do texto da diferença : "))
); if
(if pt
    (if (> (cdr (assoc 40 (tblsearch "STYLE" (getvar "TEXTSTYLE")))) 0)
    (command "_.TEXT" pt "" str)
    (command "_.TEXT" pt "" "" str)
  );if txh
  );if pt
  (princ)
  )

;p/s: however i prefer entmake for text coding.
« Last Edit: June 06, 2014, 10:38:00 PM by hanhphuc »
( apply 'equal "hp" "happy" "hạnh phúc" "ハッピー" "幸福" "행복" ) ; error: too many arguments

roy_043

  • Water Moccasin
  • Posts: 1895
  • BricsCAD 18
Re: Get Text string from Attributes with user pick
« Reply #9 on: May 24, 2014, 07:20:15 AM »
@ hanhphuc:
You are right to check for correct user input but you should have another look at the way you use (if). It can be simplified. And I would not create a sub-function like your (t-t) function.

Code: [Select]
(defun c:t3 ( / oldOsmode pt result txt1 txt2)
  (if
    (and
      (setq txt1 (car (nentsel "\nSelecione o primeiro texto: ")))
      (setq txt1 (cdr (assoc 1 (entget txt1))))
      (setq txt2 (car (nentsel "\nSelecione o segundo texto: ")))
      (setq txt2 (cdr (assoc 1 (entget txt2))))
      (setq pt (getpoint "\nInforme o local do texto da diferença: "))
    )
    (progn
      (setq result (rtos (- (atof txt1) (atof txt2)) 2 2))
      (if (> (read result) 0) (setq result (strcat "+" result)))
      (setq oldOsmode (getvar 'osmode))
      (setvar 'osmode 0)
      (if (> (cdr (assoc 40 (tblsearch "STYLE" (getvar "TEXTSTYLE")))) 0)
        (command "_.text" pt "" result)
        (command "_.text" pt "" "" result)
      )
      (setvar 'osmode oldOsmode)
    )
  )
  (princ)
)

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Get Text string from Attributes with user pick
« Reply #10 on: May 24, 2014, 07:32:34 AM »
Roy I would go a step further & add a String test for each object.
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

roy_043

  • Water Moccasin
  • Posts: 1895
  • BricsCAD 18
Re: Get Text string from Attributes with user pick
« Reply #11 on: May 24, 2014, 08:33:47 AM »
Roy I would go a step further & add a String test for each object.
According to the DXF docs gc 1 is the 'Primary text value for an entity'. If that is true checking for gc 1 would already accomplish that. Another thing would be checking for valid numerical strings.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Get Text string from Attributes with user pick
« Reply #12 on: May 24, 2014, 03:15:57 PM »
Oops, sorry Roy.
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

roy_043

  • Water Moccasin
  • Posts: 1895
  • BricsCAD 18
Re: Get Text string from Attributes with user pick
« Reply #13 on: May 25, 2014, 04:11:18 AM »

hanhphuc

  • Newt
  • Posts: 64
Re: Get Text string from Attributes with user pick
« Reply #14 on: May 25, 2014, 04:53:31 AM »
@roy_043 : Thank you for pointing irrelevant in (if & t-t) coding. i appreciate this improved my coding practice though just a single advice :-)
( apply 'equal "hp" "happy" "hạnh phúc" "ハッピー" "幸福" "행복" ) ; error: too many arguments