TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: Mark on May 04, 2004, 01:44:38 PM

Title: extended data routine :: beta testers wanted
Post by: Mark on May 04, 2004, 01:44:38 PM
please do NOT run this on a production dwg and thanks for any feedback.
Code: [Select]

;;; read and write extended entity data
;;; adds a user note to an entity and date stamp
;;; version 0.1 Tue May 04, 2004
(defun idate () (atoi (rtos (getvar 'cdate) 2 0)))
 
(defun make-lst (xname note)
  (list
    (list -3
          (list xname (cons 1000 note) (cons 1040 (idate))))
    )
  )

(defun chk-for (xapp ent)
  (assoc 1000 (cdr (cadr (assoc -3 (entget ent (list xapp))))))
  )

(defun set-xdata (/ xapp ent kwd note new_ent)
  (setq xapp "user_xnote")


  (if (setq ent (car (entsel "\nSelect entity: ")))
    (if (chk-for xapp ent)
      (progn
        (initget 1 "Yes No")
        (setq
          kwd (getkword
                "\nEntity has note attached, overwrite? (Y/N): "
                )
          )
        (if (= kwd "Yes")
          (setq note (getstring T "\nEnter note: "))
          )
        )
      (setq note (getstring T "\nEnter note: "))
      )
    )

  (if (and note (/= note ""))
    (progn
      (setq new_ent
            (append (entget ent) (make-lst xapp note))
            )
      (entmod new_ent)
      )
    )
  (princ)
  )

(defun read-xdata (/ xapp ent xd)
  (setq xapp "user_xnote")
  (regapp xapp)

  (if (setq ent (car (entsel "\nSelect entity: ")))
    (setq xd (assoc -3 (entget ent (list xapp))))
    )

  (if xd
    (princ
      (strcat
        "\nNote: "
        (cdr (assoc 1000 (cdr (cadr xd))))
        "\nDate : "
        (rtos (cdr (assoc 1040 (cdr (cadr xd)))) 2 0)
        )
      )
    )
  (princ)
  )

(defun c:xNotes (/ kwd)
  (regapp "user_xnote")
  (initget "Add Read")
  (setq kwd
        (getkword "\nDo you want to Add or Read notes? (A/R) <Add>: ")
        )
  (cond ((or (= kwd nil)(= kwd "Add"))
         (set-xdata))
        ((= kwd "Read")
         (read-xdata))
        )
  )
Title: extended data routine :: beta testers wanted
Post by: Kerry on May 04, 2004, 05:36:44 PM
Dont have time to crash test it Mark :) but I'm sure you have done that already.

suggestion :
A Text file of default notes read into a list dialog works for me in a similar situation .. ensures consistancy, and saves spelling mistakes.
Title: extended data routine :: beta testers wanted
Post by: Mark on May 04, 2004, 05:46:11 PM
Quote from: Kerry Brown
suggestion :
A Text file of default notes read into a list dialog works for me in a similar situation .. ensures consistancy, and saves spelling mistakes.

excellent idea, thanks Kerry
Title: extended data routine :: beta testers wanted
Post by: Kerry on May 04, 2004, 05:51:51 PM
yep, its critical for me 'cause I have a habit of not ensuring my brain and fingers are fully co-ordinated.
.. particularly important as the text value is not available for visible confirmation.
Title: extended data routine :: beta testers wanted
Post by: SMadsen on May 05, 2004, 04:41:34 AM
Works like a charm, Mark. Don't have any comments, except maybe that it's a bit unclear what happens when the user chooses to overwrite the xdata and makes an empty response.
Title: extended data routine :: beta testers wanted
Post by: Mark on May 05, 2004, 07:01:26 AM
Thanks Stig.
new version:
Code: [Select]

;;; read and write extended entity data
;;; adds a user note to an entity and date stamp
;;; version 0.2 Wed May 05, 2004
(defun idate () (atoi (rtos (getvar 'cdate) 2 0)))
 
(defun make-lst (xname note)
  (list
    (list -3
          (list xname (cons 1000 note) (cons 1040 (idate))))
    )
  )

(defun chk-for (xapp ent)
  (assoc 1000 (cdr (cadr (assoc -3 (entget ent (list xapp))))))
  )

(defun set-xdata (/ xapp ent kwd note new_ent)
  (setq xapp "user_xnote")


  (if (setq ent (car (entsel "\nSelect entity: ")))
    (if (chk-for xapp ent)
      (progn
        (initget 1 "Yes No")
        (setq
          kwd (getkword
                "\nEntity has note attached, overwrite? (Y/N): "
                )
          )
        (if (= kwd "Yes")
          (setq note (getstring T "\nEnter note [<enter> to clear note]: "))
          )
        )
      (setq note (getstring T "\nEnter note: "))
      )
    )
  (if note
    (cond ((/= note "")
           (setq new_ent
                 (append (entget ent) (make-lst xapp note))
                 )
           (entmod new_ent))
          ((= note "")
           (setq new_ent
                 (append (entget ent)
                         (make-lst xapp (strcat "<removed by "(getvar 'loginname)">"))
                         )
                 )
           (entmod new_ent))
          )
    )
  (princ)
  )

(defun read-xdata (/ xapp ent xd)
  (setq xapp "user_xnote")
  (regapp xapp)

  (if (setq ent (car (entsel "\nSelect entity: ")))
    (setq xd (assoc -3 (entget ent (list xapp))))
    )

  (if xd
    (princ
      (strcat
        "\nNote: "
        (cdr (assoc 1000 (cdr (cadr xd))))
        "\nDate : "
        (rtos (cdr (assoc 1040 (cdr (cadr xd)))) 2 0)
        )
      )
    )
  (princ)
  )

(defun c:xNotes (/ kwd)
  (regapp "user_xnote")
  (initget "Add Read")
  (setq kwd
        (getkword "\nDo you want to Add or Read notes? (A/R) <Add>: ")
        )
  (cond ((or (= kwd nil)(= kwd "Add"))
         (set-xdata))
        ((= kwd "Read")
         (read-xdata))
        )
  )
Title: extended data routine :: beta testers wanted
Post by: Water Bear on May 05, 2004, 10:07:49 AM
Here's one just for comparison:
Code: [Select]
(defun C:STICKEMS ()

  (prompt "\nSTICKEMS - Attach notes to drawing entities")

  (initget 1 "R W")
  (setq CHOICE (getkword
"\nDo you want to Read or Write a note <R or W> ? "
)
)

  (while (null (setq ENTITY (car (entsel)))))

  (if (equal CHOICE "W")
    (progn

      (textpage)

      (setq NAME (getstring "\nWhat is your first name ? "))
      (setq NAME (strcat "STICKEMS"
(substr (rtos (getvar "CDATE") 2 8) 1 8)
(substr (rtos (getvar "CDATE") 2 8) 10 8)
NAME
)
   )
      (regapp NAME)

      (setq ENTLIST (entget ENTITY (list "*")))

      (prompt
"\nEnter your note line by line. When you are through"
)
      (prompt "\ntype the word EXIT on a line by itself.\n")

      (setq NEW_XDATA (list NAME (cons 1002 "{")))
      (while (/= "EXIT" (strcase (setq STRING (getstring T ">"))))
(setq NEW_XDATA (append NEW_XDATA (list (cons 1000 STRING))))
)
      (setq NEW_XDATA (append NEW_XDATA (list (cons 1002 "}"))))
      (setq NEW_XDATA (list -3 NEW_XDATA))

      (if (assoc -3 ENTLIST)
(setq NEW_ENTLIST (subst NEW_XDATA (assoc -3 ENTLIST) ENTLIST))
(setq NEW_ENTLIST (append ENTLIST (list NEW_XDATA)))
)
      (entmod NEW_ENTLIST)
      )
    )

  (if (equal CHOICE "R")
    (progn

      (setq ENTLIST (entget ENTITY (list "STICKEMS*")))
      (setq XDATA (cdr (assoc -3 ENTLIST)))

      (if (null XDATA)

(prompt "\nEntity does not have any STICKEMS.")

(progn
 (foreach ITEM XDATA

   (textpage)

   (setq DATE (substr (car ITEM) 9 8))
   (setq TIME (substr (car ITEM) 17 8))
   (setq NAME (substr (car ITEM) 25))

   (prompt (strcat "Posted by " NAME " on " DATE " at " TIME))

   (setq NOTES (cdr ITEM))
   (foreach LINE NOTES
     (if (= (car LINE) 1000)
(prompt (strcat "\n" (cdr LINE)))
)
     )

   (getstring "\n\nHit any key to continue ...")
   )
 )
)
      )
    )

  (prompt "\nProgram complete.")
  (princ)
  )
Title: extended data routine :: beta testers wanted
Post by: ronjonp on May 05, 2004, 01:22:14 PM
Work fine over here Mark.  :D
Title: extended data routine :: beta testers wanted
Post by: Slim© on May 05, 2004, 03:07:32 PM
Works really nice. I like it! :D
Title: extended data routine :: beta testers wanted
Post by: Mark on May 05, 2004, 03:50:29 PM
wait till you see the version !!
Title: extended data routine :: beta testers wanted
Post by: Mark on May 06, 2004, 07:43:04 AM
New version, all GUI. please see included help file.

http://www.theswamp.org/swamp.files/Public/xNotes-0.3.zip
Title: extended data routine :: beta testers wanted
Post by: hendie on May 06, 2004, 09:33:05 AM
Mark, I noticed that if you select one of the predefined notes, it does not update in the textbox. However, it does update the note as you can verify when you activate it again.
Title: extended data routine :: beta testers wanted
Post by: hendie on May 06, 2004, 09:42:46 AM
*idea* is there anyway you could incorporate a button/feature which would highlight all entites that already had xdata attached ?
Title: extended data routine :: beta testers wanted
Post by: Slim© on May 06, 2004, 10:27:01 AM
It hangs up my session of AutoCAD when I hit CANCEL. :?
2k2 LDD3
Title: extended data routine :: beta testers wanted
Post by: Mark on May 06, 2004, 12:33:43 PM
Quote from: Slim101
It hangs up my session of AutoCAD when I hit CANCEL. :?
2k2 LDD3


Does for me too! although it doesn't in Map. still digging ............
Title: extended data routine :: beta testers wanted
Post by: Mark on May 06, 2004, 02:57:16 PM
New version (0.4) fixed bug, see previous post.

Program
http://www.theswamp.org/swamp.files/Public/xNotes-0.4.zip

source files
http://www.theswamp.org/swamp.files/Public/xNotes-src.zip
Title: extended data routine :: beta testers wanted
Post by: Mark on May 07, 2004, 08:12:23 AM
New version (0.5)
reads from predefined notes, see included help file.

Program:
http://www.theswamp.org/swamp.files/Public/xNotes-0.5.zip

source files:
http://www.theswamp.org/swamp.files/Public/xNotes-src.zip
Title: extended data routine :: beta testers wanted
Post by: Water Bear on May 10, 2004, 06:47:59 AM
Mark..I see that you've been doing some detailed work on xdata. Is it possible to add xdata to a group, so that it shows up no matter which component is selected?
Title: extended data routine :: beta testers wanted
Post by: Mark on May 10, 2004, 07:46:33 AM
It can be done, but not with what I've done so far.
Title: extended data routine :: beta testers wanted
Post by: SMadsen on May 10, 2004, 08:34:18 AM
Man, you make it hard to test your stuff with that one file per function idea! Is it that VIM again?  :twisted:

Mark, I have a few comments. Got a few deadly crashes with your program but I didn't have time to see where they came from. Once I added a FINDFILE in READ-USER-XNOTES it didn't happen again.
Also, in the dialog function, wouldn't you rather have the chosen list item appear in the edit box? Of course, it should have a reset option if a note exists already.
Below are some small suggestions to the dialog code, such as offering edit of a chosen note and moving the OK processing outside of the dialog (it came up with empty $value)

Code: [Select]
(defun read-user-xnotes (/ xenv fo lst)
  (if (setq xenv (getenv "user-xnotes"))
    (if (not (findfile xenv))
      (progn
        (setenv "user-xnotes" "")
        (alert
          "user notes files missing \n will now reset environment"
        )
        (setq xenv (set-user-xnotes-env))
      )
    )
    (setq xenv (set-user-xnotes-env))
  )

  (if xenv
    (progn
      ;; {* changed
      ;;    only go ahead and open if xenv is actually found
      (cond ((setq xenv (findfile xenv))
             (setq fo (open xenv "r"))
             (while (setq each-line (read-line fo))
               (setq lst (cons each-line lst))
             )
             (close fo)
            )
      )
      ;; changed *}
    )
  )
  ;; btw, if statement is redundant :)
  (if lst (reverse lst))
)

Code: [Select]
(defun xnotes-dialog
       (msg note_type / dcl_id note action addval getNote)
  (defun getNote (val lst)
    (set_tile "addnote" (nth (atoi $value) lst))
  )

  (setq dcl_id (load_dialog "xnotes.dcl"))
  (if (not (new_dialog "xdialog" dcl_id))
    (exit)
  )

  (start_list "xnotelst")
  (setq tmplst (read-user-xnotes))
  (mapcar 'add_list tmplst)
  (end_list)

  (set_tile "note_t" note_type)

  (set_tile "addnote" msg)
  (mode_tile "addnote" 3)

  ;;{* added
  (action_tile "xnotelst" "(setq addval (getNote $value tmplst))")
  (action_tile "addnote" "(setq addval $value)")
  ;; a reset tile would be good (just gray it out if note doesn't exist)
  ;; added *}

  ;; {* removed
;;;  (action_tile
;;;    "accept"
;;;    "(progn
;;;      (setq val1 (get_tile \"xnotelst\"))
;;;      (setq val2 (get_tile \"addnote\"))
;;;      (done_dialog)
;;;    )"
;;;  )
;;;  (action_tile
;;;    "cancel"
;;;    "(progn (done_dialog) (exit))"
;;;  )
  ;; removed *}
  ;; {* changed
  (setq action (start_dialog))
  ;; changed *}
  ;; {* added
  (cond ((= action 1)
         (setq note addval)
        )
        ((setq note ""))
  )
  ;; added *}
  (unload_dialog dcl_id)
  ;; {* removed
;;;  (if (or val1 val2)
;;;    (if (= val1 "")
;;;      (setq note val2)
;;;                                        ; else
;;;      (setq note (nth (atoi val1) tmplst))
;;;    )
;;;  )
  ;; removed *}
  note
)