TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: MP on May 02, 2005, 08:15:39 PM

Title: Dictionaries and Xrecords ...
Post by: MP on May 02, 2005, 08:15:39 PM
Ths has probably been covered ad nauseum, but a colleague asked if I'd show him how to do this so I penned this quick ...

GetOrAddDict

Code: [Select]
(defun GetOrAddDict ( parentDict dictName )

    ;;  no error checking in the interests of speed,
    ;;  caller responsible for ensuring appropriate
    ;;  data is passed

    (cond
        (   (cdr
                (assoc -1
                    (dictsearch
                        parentDict
                        dictName
                    )
                )
            )    
        )
        (   (dictadd parentDict dictName
                (entmakex
                   '(   (0 . "DICTIONARY")
                        (100 . "AcDbDictionary")
                    )
                )
            )
        )    
    )
)

AddOrReplaceXrec

Code: [Select]
(defun AddOrReplaceXrec ( parentDict xrecName xrecData / xrec )

    ;;  no error checking in the interests of speed,
    ;;  caller responsible for ensuring appropriate
    ;;  data is passed

    (if
        (setq xrec
            (dictsearch
                parentDict
                xrecName
            )
        )
        (entdel
            (cdr
                (assoc -1 xrec)
            )
        )
    )
   
    (dictadd
        parentDict
        xrecName
        (entmakex
            (append    
               '(   (0 . "XRECORD")
                    (100 . "AcDbXrecord")
                )
                xrecData
            )
        )
    )
)

c:Test

Code: [Select]
(defun c:test ( / dict1 dict2 xrec1 xrec2 )

    ;;  dict1 is child of primary dictionary

    (setq dict1
        (GetOrAddDict
            (namedobjdict)
            "MyDictionary1"
        )
    )
   
    ;;  dict2 is a child of dict1
   
    (setq dict2
        (GetOrAddDict
            dict1
            "MyDictionary2"
        )
    )
   
    ;;  add xrec to dict1
   
    (setq xrec1
        (AddOrReplaceXrec
            dict1
            "MyXrecord1"
           '(   (1 . "KOOL")
                (10 0.0 0.0 0.0)
                (70 . 42)
            )
        )
    )
   
    ;;  add xrec to dict2
   
    (setq xrec2
        (AddOrReplaceXrec
            dict2
            "MyXrecord2"
           '(   (1 . "KOOL")
                (10 0.0 0.0 0.0)
                (70 . 42)
            )
        )
    )

    ;;  show what we made ...
   
    (mapcar
       '(lambda (ename)
            (princ (strcat (chr 40) "\n"))
            (foreach pair (entget (eval ename))
                (princ "    ")
                (princ pair)
                (princ "\n")
            )
            (princ (strcat (chr 41) "\n\n"))
        )
       '(
            dict1
            dict2
            xrec1
            xrec2
        )
    )    
   
    (princ)
   
)

Running c:Test might produce something like this ...

Code: [Select]
(
    (-1 . <Entity name: 4007ce50>)
    (0 . DICTIONARY)
    (5 . 72)
    (102 . {ACAD_REACTORS)
    (330 . <Entity name: 4007cc60>)
    (102 . })
    (330 . <Entity name: 4007cc60>)
    (100 . AcDbDictionary)
    (280 . 0)
    (281 . 1)
    (3 . MyDictionary2)
    (350 . <Entity name: 4007ce58>)
    (3 . MyXrecord1)
    (350 . <Entity name: 4007ce60>)
)

(
    (-1 . <Entity name: 4007ce58>)
    (0 . DICTIONARY)
    (5 . 73)
    (102 . {ACAD_REACTORS)
    (330 . <Entity name: 4007ce50>)
    (102 . })
    (330 . <Entity name: 4007ce50>)
    (100 . AcDbDictionary)
    (280 . 0)
    (281 . 1)
    (3 . MyXrecord2)
    (350 . <Entity name: 4007ce68>)
)

(
    (-1 . <Entity name: 4007ce60>)
    (0 . XRECORD)
    (5 . 74)
    (102 . {ACAD_REACTORS)
    (330 . <Entity name: 4007ce50>)
    (102 . })
    (330 . <Entity name: 4007ce50>)
    (100 . AcDbXrecord)
    (280 . 1)
    (1 . KOOL)
    (10 0.0 0.0 0.0)
    (70 . 42)
)

(
    (-1 . <Entity name: 4007ce68>)
    (0 . XRECORD)
    (5 . 75)
    (102 . {ACAD_REACTORS)
    (330 . <Entity name: 4007ce58>)
    (102 . })
    (330 . <Entity name: 4007ce58>)
    (100 . AcDbXrecord)
    (280 . 1)
    (1 . KOOL)
    (10 0.0 0.0 0.0)
    (70 . 42)
)

For what its worth thought I'd share it here.

Cheers.

:)
Title: Dictionaries and Xrecords ...
Post by: daron on May 03, 2005, 08:06:49 AM
It's worth a lot. Added to Favorites. Thanks.
Title: Dictionaries and Xrecords ...
Post by: MP on May 03, 2005, 08:09:23 AM
Thanks Daron. :)
Title: Dictionaries and Xrecords ...
Post by: Mark on May 03, 2005, 08:14:01 AM
Thanks a bunch MP, that's good stuff.

wish I had time to use it right now ...
Title: Dictionaries and Xrecords ...
Post by: MP on May 03, 2005, 08:17:28 AM
My pleasure Mr. T.

It will wait patiently for you.

:)
Title: Dictionaries and Xrecords ...
Post by: daron on May 03, 2005, 08:19:01 AM
Mr. T??? Now I have a different visual of Mark with an AFRO and gold chains and calling everyone a pitiful fool. hehehehe.
Title: Dictionaries and Xrecords ...
Post by: MP on May 03, 2005, 08:24:29 AM
Sounds like a photoshop challenge to me.

Where's craig?
Title: Dictionaries and Xrecords ...
Post by: daron on May 03, 2005, 08:29:14 AM
Got any pictures of Mr. T? I know where to find some of Mark.
Title: Dictionaries and Xrecords ...
Post by: Mark on May 03, 2005, 01:11:07 PM
Quote from: MP
Sounds like a photoshop challenge to me.


Oh please no ... not Mr T. aaahhhh ..... :)
Title: Dictionaries and Xrecords ...
Post by: CAB on May 03, 2005, 01:45:00 PM
Michael,
Very nice of you to share your knowledge.
I'd love to dive into this but it will have to wait as my free time is gone for awhile.
I reserve the right to as questions later. :)

Thanks

CAB
Title: Dictionaries and Xrecords ...
Post by: MP on May 03, 2005, 02:02:58 PM
No worries Alan, as long as I'm around it's my pleasure to share the few bits I claim to remotely understand.

:)
Title: Dictionaries and Xrecords ...
Post by: SMadsen on May 03, 2005, 05:02:09 PM
Quote from: Mark Thomas
Quote from: MP
Sounds like a photoshop challenge to me.


Oh please no ... not Mr T. aaahhhh ..... :)

No Mr. T? How'bout this (http://www.theswamp.org/lilly_pond/smadsen/mark-photo3.jpg?nossi=1) then?

Sorry Mark, couldn't help it!!
Title: Dictionaries and Xrecords ...
Post by: Mark on May 03, 2005, 05:07:14 PM
I scared to look .... :)
Title: Dictionaries and Xrecords ...
Post by: MP on May 03, 2005, 05:08:41 PM
I was after I looked. :shock:
Title: Dictionaries and Xrecords ...
Post by: CAB on May 03, 2005, 06:32:08 PM
:shock: EXACTLY