Author Topic: insert value into attribute  (Read 8160 times)

0 Members and 1 Guest are viewing this topic.

snownut2

  • Swamp Rat
  • Posts: 971
  • Bricscad 22 Ultimate
Re: insert value into attribute
« Reply #30 on: November 24, 2012, 03:42:10 PM »
Thank you to all who helped on this project, CAB, Lee Mac & irned.

Below are the completed Functions, I am sure they are not as concise as Lee Mac's work (probably take 3-lines of code in total).

The below functions will perform the following;
       1.- Create a Sub-Dictionary within the NOD.
            2.- Create an Xrecord within the Sub-Dictionary.
                 3.- Retrieve Values within the Xrecord, based in the nth occurrence of the associated DXF Group Code.
                      4.- Update Values within the Xrecord, based in the nth occurrence of the associated DXF Group Code.
                            5.- Read the entire contents of the Xrecord.

This should give you enough Xrecord tools to be able to incorporate their use quite easily.

Function to Create the "SADATA_DICT" within the NOD
        Syntax for function call;               
               (GC_Dict  < dictname >)

Code: [Select]

(defun GC_Dict ( dictname / adict) 
  (if (not (setq adict (dictsearch (namedobjdict) dictname)))
    (progn
      (setq adict (entmakex '((0 . "DICTIONARY")(100 . "AcDbDictionary"))))
      (if adict (setq adict (dictadd (namedobjdict) dictname adict)))
    )
    (setq adict (cdr (assoc -1 adict)))
  )
);defun

(GC_Dict "SADATA_DICT") Example Calling Line



Function to Create an XRECORD in the "SADATA_DICT" within the NOD
        Syntax for function call;
         (GM_Xrecord < XRECORD NAME >)

Code: [Select]

(defun GM_Xrecord ( DATA_VARS / adict anXrec)
  (cond
    ((setq adict (GC_Dict "SADATA_DICT"))
     (cond     
       ((not (setq anXrec (dictsearch adict DATA_VARS)))
(setq anXrec (entmakex values))
        (if anXrec (setq anXrec (dictadd adict DATA_VARS anXrec)))
)
       (setq anXrec
(cdr (assoc -1 (dictsearch adict DATA_VARS)))
);setq
       );cond
     );setq
    );cond
  );defun


(GM_Xrecord "LDATA_VARS") Example Calling Line ;


Following are some examples for the <Values> variable that needs to be created prior to Calling the (GM_Xrecord) Function.

Code: [Select]

(setq x 11.23
      y 23.43
      z 43.23
      test 34.54
      test2 "This is line 5")

(setq Values  (list(cons 0 "XRECORD") ; used for storing integers in "XRECORDS" can also use 270-289
   (cons 100  "AcDbXrecord")
   (cons 70 2 )
   (cons 71 3 )
   (cons 72 4 )
   (cons 73 5 )
   (cons 74 6 )
   (cons 75 7 )
   (cons 76 8 )
   (cons 12 (list x y))
   (cons 11 (list x y z))
   )
      );setq

(setq Values  (list(cons 0 "XRECORD") ; used for storing reals in "XRECORDS" can also use 140-149
   (cons 100  "AcDbXrecord")
   (cons 40 2.1234 )
   (cons 41 3.4568 )
   (cons 42 4.3254 )
   (cons 43 5.3214 )
   (cons 44 6.3256 )
   (cons 45 test )
   (cons 46 8.6384 )
   (cons 47 9.2458 )
   (cons 48 10.7854 )
   )
      );setq

(setq Values  (list(cons 0 "XRECORD") ; used for storing strings in "XRECORDS" can also use 301-309
   (cons 100  "AcDbXrecord")
   (cons 300 "This is line 1" )
   (cons 300 "This is line 2" )
   (cons 300 "This is line 10" )
   (cons 300 "This is line 3" )
   (cons 300 "This is line 4" )
   (cons 300 test2 )
   (cons 300 "This is line 6" )
   (cons 300 "This is line 7" )
   (cons 300 "This is line 8" )
   (cons 300 "This is line 9" )
   )
      );setq


Function to retrieve values from an XRECORD using the DXF Group Code      
      Syntax for function call;                        
                (GetVar_Xrecord <XRECORD NAME> <DXF Group Code to Update> <Nth Group Code>)   

Many Thanks to Lee Mac for his LM:mAssoc Function.

Code: [Select]

(defun GetVar_Xrecord ( DATA_VARS DXF_VAL LnmBr / xRecFile vars )
  (defun LM:mAssoc ( DXF_VAL xRecFile / item )
    (if (setq item (assoc DXF_VAL xRecFile))
      (cons (cdr item) (LM:mAssoc DXF_VAL (cdr (member item xRecFile))))
      )
    );defun

  (setq vars (GM_Xrecord DATA_VARS))
  (cond (vars
         (setq xRecFile  (entget vars))
(cdr (cons DXF_VAL(nth LnmBr (LM:mAssoc DXF_VAL xRecFile))))
        )
        (T nil)
  )
);defun

(setq VARNAME (GetVar_Xrecord "LDATA_VARS" 300 2 )) Calling Line


Function to Update the values in an XRECORD using the nth DXF Group Code   
        Syntax for function call;                        
              (Update_Xrecord <XRECORD NAME> <DXF Group Code to Update> <New Value> <Nth Group Code>)

Code: [Select]

(defun Update_Xrecord (DATA_VARS DXF_VAL MOD_VAL LnmBr / xRecFile Values SAdict)
  (defun LM:mAssoc ( DXF_VAL xRecFile / item )
    (if (setq item (assoc DXF_VAL xRecFile))
      (cons (cdr item) (LM:mAssoc DXF_VAL (cdr (member item xRecFile))))
      )
    );defun

  (setq SAdict (dictsearch (namedobjdict) "SADATA_DICT")
xRecFile (dictsearch (cdr (assoc -1 SAdict)) DATA_VARS)

  (setq values (subst (cons DXF_VAL MOD_VAL) (cons DXF_VAL(nth LnmBr (LM:mAssoc DXF_VAL xRecFile))) xRecFile))
  (entdel (cdr (assoc -1 xRecFile)))
  (GM_Xrecord DATA_VARS)
  );defun

(Update_Xrecord "LDATA_VARS" 300 "Update test successful again 3 !" 4) Example Calling line


Function to See the Values in the specified XRECORD
       Syntax for function call;   
       (xRecord_Exist < Xrecord Name >)   

Code: [Select]

(defun xRecord_Exist ( DATA_VARS / SAdict xRecFile )
  (setq SAdict (dictsearch (namedobjdict) "SADATA_DICT")
xRecFile (dictsearch (cdr (assoc -1 SAdict)) DATA_VARS)
)
  (princ xRecFile)
  )

(xRecord_Exist "LDATA_VARS")  ExampleCalling line


Any comments or feedback is always welcome.

Enjoy,

Bruce