TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: Fabricio28 on May 21, 2014, 10:11:27 AM

Title: Get Text string from Attributes with user pick
Post by: Fabricio28 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
Title: Re: Request for CAB's code
Post by: roy_043 on May 21, 2014, 10:22:42 AM
Try nentsel instead of entsel.
Title: Re: Request for CAB's code
Post by: Fabricio28 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
Title: Re: Request for CAB's code
Post by: roy_043 on May 21, 2014, 10:37:39 AM
Code: [Select]
(setq result (- ... ...))
(strcat (if (>= result 0) "+" "") (rtos result 2 2))
Title: Re: Request for CAB's code
Post by: Fabricio28 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
Title: Re: Get Text string from Attributes with user pick
Post by: Fabricio28 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 "")
  )
Title: Re: Get Text string from Attributes with user pick
Post by: roy_043 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)
Title: Re: Get Text string from Attributes with user pick
Post by: Fabricio28 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
Title: Re: Get Text string from Attributes with user pick
Post by: hanhphuc 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.
Title: Re: Get Text string from Attributes with user pick
Post by: roy_043 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)
)
Title: Re: Get Text string from Attributes with user pick
Post by: CAB on May 24, 2014, 07:32:34 AM
Roy I would go a step further & add a String test for each object.
Title: Re: Get Text string from Attributes with user pick
Post by: roy_043 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.
Title: Re: Get Text string from Attributes with user pick
Post by: CAB on May 24, 2014, 03:15:57 PM
Oops, sorry Roy.
Title: Re: Get Text string from Attributes with user pick
Post by: roy_043 on May 25, 2014, 04:11:18 AM
Oops, sorry Roy.
No problem.
Title: Re: Get Text string from Attributes with user pick
Post by: hanhphuc 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 :-)