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 (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 (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(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 ...
(
(-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.