Author Topic: add value to attribute lisp help please  (Read 4159 times)

0 Members and 1 Guest are viewing this topic.

Pad

  • Bull Frog
  • Posts: 342
add value to attribute lisp help please
« on: October 11, 2010, 09:20:04 AM »
Hi

I'm in a little bit of a pickle, I thought I had a lisp to do this but its seems I don't.

I have a block called 'pt2' which contains a number of attributes.
I need to add a value to the attribute with the tag name 'level' only.

Does anybody have a lisp with this capability that they could share please?

Many thanks
Pads

I have this lisp (I do not know the author) but it doesnt work on attributes

Code: [Select]
(defun MakeX (entname)
  (vlax-ename->vla-object entname)
)

(defun GetX (object prop)
  (vlax-get object prop)
)

(defun PutX (object prop val)
  (vlax-put object prop val)
)

(defun c:AddText (/ txt1 txt2 cnt1 ent1 pec1)

(command "_.undo" "_end")
(command "_.undo" "_group")
(vl-load-com)
(setq txt1 (getreal "\nEnter value to increase by (if decreasing, add a minus sign before). "))
(setq cnt1 0)
(if txt1
  (progn
    (if (not *pec1)
      (setq *pec1 3)
    )
    (setq pec1 (getreal (strcat "\nHow many decimal places [" (itoa *pec1) "]? ")))
    (if pec1
      (setq *pec1 (fix pec1))
    )
    (setq ss (ssget '((0 . "*TEXT"))))
    (while (/= cnt1 (sslength ss))
      (setq ent1 (MakeX (ssname ss cnt1)))
      (setq txt2 (GetX ent1 'TextString))
      (MakeSureNum txt2 ent1)
      (setq cnt1 (1+ cnt1))
    )
  )
)
(prompt "\n  May get rounding-off if new decimal is less then existing decimal places!!")
(command "_.undo" "_end")
(princ)
)

;==============

(defun MakeSureNum (txt3 ent1 / cnt1 cnt2 txt4 txt5 txt6 txt7)

(setq cnt1 0
  cnt2 1
)
(if (and (>= (ascii (substr txt3 1 1)) 65) (/= (ascii (substr txt3 1 1)) 32))
  (while (>= (ascii (substr txt3 1 1)) 65)
    (if txt4
      (setq txt4 (strcat txt4 (substr txt3 1 1)))
      (setq txt4 (substr txt3 1 1))
    )
    (setq txt3 (substr txt3 2 (strlen txt3)))
  )
)
(while (= (substr txt3 1 1) " ")
  (setq txt4 (strcat txt4 " "))
  (setq txt3 (substr txt3 2 (strlen txt3)))
)
(if (< (ascii (substr txt3 1 1)) 65)
  (while (and (<= (ascii (substr txt3 1 1)) 65) (/= (ascii (substr txt3 1 1)) 0) (/= (ascii (substr txt3 1 1)) 32))
    (if txt5
      (setq txt5 (strcat txt5 (substr txt3 1 1)))
      (setq txt5 (substr txt3 1 1))
    )
    (setq txt3 (substr txt3 2 (strlen txt3)))
  )
)
(setq txt5 (atof txt5))
(setq txt6 (+ txt5 txt1))
(setq txt7
  (if txt4
    (strcat txt4 (rtos txt6 2 *pec1) txt3)
    (strcat (rtos txt6 2 *pec1) txt3)
  )
)
(PutX ent1 'Textstring txt7)

)

and this one
« Last Edit: October 11, 2010, 09:31:09 AM by Pad »

Pad

  • Bull Frog
  • Posts: 342
Re: add value to attribute lisp help please
« Reply #1 on: October 11, 2010, 10:12:50 AM »
ok, I have found this lisp below,  I would like to modify it so that I can specify the decimal places.
Any idea what to change?
I assume it's this bit, but I dont understand enough of it to be able to modify it.

Code: [Select]
; @GetPrecision (local defun)
;
; determine a number's precision
;
(defun @GetPrecision ( Number / i )
(if (eq Number (fix Number))
0
(cond
( (progn (setq i 16) (numberp Number))
(while
(eq Number
(read (rtos Number 2 (setq i (1- i))))
)
)
(1+ i)


here is the full lisp

Code: [Select]
;////////////////////////////////////////////////////////////////////////////
;
; IncAtts.lsp * Version 1.0 * 2002/10/03
;
; Increment (or Decrement) attribute values.
;
; Copyright © 2002 Michael Puckett . All Rights Reserved
;
;////////////////////////////////////////////////////////////////////////////
;
; You are granted a temporary license to use the utility(s) defined
; within this source code without fee subject to the following terms:
;
; Michael Puckett, hereafter referred to as "THE AUTHOR", has
; provided this utility(s) as is, complete with all faults,
; imperfections and errors. THE AUTHOR specifically and explicitly
; disclaims any implied warranty of merchantability or fitness for
; any particular use. THE AUTHOR does not warrant that the operation
; of the utility(s) will be uninterrupted or error free. the user of
; this utility(s) assumes all responsibility for any problems that may
; result, either directly or indirectly, from the use of this utility(s).
;
; THE AUTHOR reserves the right to revoke the temporary license at
; any time for any reason.
;
; If you do not fully and completely agree with all these terms and
; conditions you must not use this utility(s) and must remove the
; source code from your computer and all storage media immediately.
;
;////////////////////////////////////////////////////////////////////////////

(defun c:IncAtts

( /
;
; local defuns
;
@Begin @End @Trim @Children @StrToNum
@GetUnits @GetPrecision @IncrAtts
;
; local vars
;
i ss TagSpec ByValue
)
;
; @Begin (local defun)
;
; initialize vars, enable undo
; sequence if possible
;
(defun @Begin ()
(setvar "cmdecho" 0)
(while (eq 8 (logand 8 (getvar "undoctl")))
(command ".undo" "_end")
)
(if (zerop (logand 2 (getvar "undoctl")))
(if (eq 1 (logand 1 (getvar "undoctl")))
(command ".undo" "_begin")
)
)
)
;
; @End (local defun)
;
; deinitialize vars, terminate undo
; sequence if possible
;
(defun @End ()
(if (eq 8 (logand 8 (getvar "undoctl")))
(command ".undo" "_end")
)
(setvar "cmdecho" 1)
(princ)
)
;
; @Trim (local defun)
;
; Remove leading / Trailing white space.
;
(defun @Trim ( Text / i)
(cond
((zerop (setq i (strlen Text))) Text)
((< (ascii Text) 33) (@Trim (substr Text 2)))
((< (ascii (substr Text i)) 33) (@Trim (substr Text 1 (1- i))))
(t Text)
)
)
;
; @Children (local defun)
;
; Return list of entities that are a child
; of the parent entity supplied, eg: a block
; with attributes or a polyline with vertices
;
(defun @Children ( Ename / Data Result )
(if (assoc 66 (entget Ename))
(reverse
(while
(/= "SEQEND"
(cdr
(assoc 0
(setq Data
(entget
(setq Ename
(entnext Ename)
)
)
)
)
)
)
(setq Result
(cons (cdr (assoc -1 Data))
Result
)
)
)
)
)
)
;
; @StrToNum (local defun)
;
; Convert a string to value if possible,
; returning the best converted value.
;
(defun @StrToNum ( Str / Num )
(if
(setq Num
(cond
( (eq 'str (type (setq Str (@Trim Str))))
(cond
((distof Str 2))
((distof Str 3))
((distof Str 5))
((distof Str 1))
)
)
)
)
(if (wcmatch Str "*`.*")
Num
(fix Num)
)
)
)
;
; @GetUnits (local defun)
;
; determine string's engineering units if applicable
;
(defun @GetUnits ( Str )
(if (eq 'str (type (setq Str (@Trim Str))))
(cond
((distof Str 2) 2)
((distof Str 3) 3)
((distof Str 5) 5)
((distof Str 1) 1)
)
)
)
;
; @GetPrecision (local defun)
;
; determine a number's precision
;
(defun @GetPrecision ( Number / i )
(if (eq Number (fix Number))
0
(cond
( (progn (setq i 16) (numberp Number))
(while
(eq Number
(read (rtos Number 2 (setq i (1- i))))
)
)
(1+ i)
)
)
)
)
;
; @IncrAtts (local defun)
;
; does the actual grunt work for a given
; insert's child entities (attributes)
;
(defun @IncrAttribs
( Parent TagSpec ByValue /
Modifed Data Str Value Pair
)
(foreach Child (@Children Parent)
(cond
( (and
; tag name is acceptable ...
(wcmatch
(strcase
(cdr (assoc 2 (setq Data (entget Child))))
)
TagSpec
)
; data appears numerical
(setq Value
(@StrToNum
(setq Str
(cdr
(setq Pair
(assoc 1 Data)
)
)
)
)
)
)
; modify it
(entmod
(subst
; put data back in format as original
(cons 1
(rtos
(+ Value ByValue)
(@GetUnits Str)
(@GetPrecision Value)
)
)
Pair
Data
)
)
; flag that a child
; entity was modified
(setq Modified T)
)
)
)
; refresh the parent
; entity (the insert)
(if Modified (entupd Parent))
)
;
; "main"
;
(cond
( (setq
i -1
ss (ssget '((0 . "insert")(66 . 1)))
)
(@Begin)
(setq TagSpec
(if
(eq ""
(setq TagSpec
(getstring
"\nEnter attribute tag spec <*>: "
)
)
)
"*"
(strcase TagSpec)
)
)
(initget 1)
(setq ByValue (getreal "\nEnter Incr/Decr (+/-) value: "))
(repeat (sslength ss)
(@IncrAttribs (ssname ss (setq i (1+ i))) TagSpec ByValue)
)
(@End)
)
)
(princ)
)

; < end c:IncAtts >


Thanks

Pad

  • Bull Frog
  • Posts: 342
Re: add value to attribute lisp help please
« Reply #2 on: October 11, 2010, 10:50:19 AM »
rightio
that incatts.lsp was giving me all sorts of weird results.
looks like a really useful lisp if only the precison could be specified (decimal places).

adding 69.355 to 50 for instance would give me 119
whilst adding 69.355 to 50.30 would give me 119.6550000000000

have since found this lisp which does the job

Code: [Select]
(defun AddNumAtt (add tag / e ei et txt)
(setq e (entnext)
tag (strcase tag))
(while (setq e (entnext e))
(setq ei (entget e)
et (cdr (assoc 0 ei)))
(and (= "ATTRIB" et)
(wcmatch (cdr (assoc 2 ei)) tag)
(setq txt (cdr (assoc 1 ei)))
(numberp (setq num (read txt)))
(entmod
(subst (cons 1 (rtos (+ add num) 2 2)) (assoc 1 ei) ei))  ; ) 2 2)  change second 2 for precision.
(entupd e)))
(princ))

;(AddNumAtt 10 "*") ;  (AddNumatt value to +/-  tagname)