Quantcast
Channel: Autodesk India Community aggregator
Viewing all articles
Browse latest Browse all 1680

AutoCAD Customization: Visual LISP, AutoLISP and General Customization: Association List to XML? Please help.

$
0
0

Hi all,

 

I have been tinkering away at this ever since it was suggested to utilize XML for data transport and storage and then W3C went and stated "If developers DO have sense, future applications will exchange their data in XML." So, it became a priority to me and perhaps you.

 

Many of us, including me, prefer to just stick with (A/V)LISP for as long as AutoCAD will accept it. I did many loong searches for hints of LISP to XML functions finding only the xml-api.lsp and it's great for what it does.

 

Because I still need a good simple way to generate the XML file to use the xml-api I have kicked around a way to dump an association list into xml format. Namely a way to construct valid XML from a structured association list. Atoms become tags and dotted pairs become attributes + values. Nesting is intrinsic of both although terminology is different they are essentially the same.

 

The code below is my current progress with help from Lee Mack & hmsilva (thank you both for those bits & pieces). I'm not an expert, obviously ;) but I know what I have this far is cool. My need for help here could be benefitial to anyone traveling this road or any developer whom has sense :)

 

Running the two test functions will reveal the situation. A top level list as with "testit1" works well (read notes), but as soon as I nest deeper, errors arise. I have an idea of what is needed but the code already has issues due to having been formed on top of a recursive function; and, I'm struggling to solve.

 

Can you help?

 

(DEFUN ASSOC->XML (l)
  (VL-LOAD-COM)
  
  (DEFUN XMLify (data / x y z) ;| hmsilva - http://forums.autodesk.com/t5/Visual-LISP-AutoLISP-and-General/Recursively-check-for-characters-and-substitute/m-p/4845525#M320052 |;
    (SETQ x '("<" ">" "& " "'" "\"")
	  y '("&lt;" "&gt;" "&amp; " "&apos;" "&quot;")
	  z (LENGTH x)
    )
    (WHILE (NOT (MINUSP (SETQ z (1- z))))
      (WHILE (VL-STRING-SEARCH (NTH z x) data)
        (SETQ data (VL-STRING-SUBST (NTH z y) (NTH z x) data))
      )
    )
    data
  )

  (DEFUN rtn-str (val) (IF (EVAL val) (IF (= (TYPE val) 'int) (ITOA val) val)))
  
  (FOREACH i l
    (COND
      ; <- Somehow collect parents nodes for closing later 
      ((ATOM i)
       (IF (EQ i (STRCASE i))
	 (PROGN
	   (IF (AND (/= ax|p i) (NOT (NULL ax|p)))
	     (PROGN (WRITE-LINE (STRCAT " /></" ax|p "><" i) output)
		    (SETQ ax|p i ax|t nil)
	     )
	     (PROGN (SETQ ax|p i) (WRITE-LINE (STRCAT "<" i) output))
	   )
	 )
	 (IF (NULL ax|t)
	   (PROGN (WRITE-LINE (STRCAT "><" (STRCASE ax|p T) " ITEM=\"" i "\"") output) (SETQ ax|t T))
	   (PROGN (WRITE-LINE (STRCAT " /><" (STRCASE ax|p T) " ITEM=\"" i "\"") output) (SETQ ax|t nil))
	 )
       )
      )
      ((= (CDR i) nil) (ASSOC->XML (CDR i)))
      ((NOT (OR (ATOM i) (LISTP (CDR i))))
       (WRITE-LINE
	 (STRCAT (IF (VL-SYMBOLP (CAR i)) (VL-SYMBOL-NAME (CAR i)) (CAR i))
		 "=\"" (XMLify (rtn-str (CDR i))) "\""
	 ) output
       )
       (SETQ ax|t T)
      )
      ((LISTP i) (ASSOC->XML i))
    )
  )
  ; <- Somehow close parent nodes before exiting 
  (PRINC)
)

;;| Tests 1 & 2 
(DEFUN c:testit1 ()
  (SETQ	ax|p nil ax|pp nil ax|t nil)
  (ASSOC->XML
    (CDR
      '(SOFTSCAPE ("PALM" ("Pygmy Palm" (BOTANICAL . "Phoenix robelinii") (SIZE . " 15 GAL") (COLOR) (QTY . 2)))
		  ("SHRUB" ("Arizona Yellow Bells" (BOTANICAL . "Tecoma stans var. agustata") (SIZE . " 5 GAL") (COLOR) (QTY . 2))
			   ("Brave River Sage" (BOTANICAL . "Leucophyllum langmaniae") (SIZE . " 5 GAL") (COLOR) (QTY . 6))
			   ("Chuparosa" (BOTANICAL . "Justicia californica") (SIZE . " 5 GAL") (COLOR) (QTY . 4))
			   ("Pink Fairy Duster" (BOTANICAL . "Calliandra eriophylla") (SIZE . " 5 GAL") (COLOR . "PINK") (QTY . 5))
		  )
		  ("GROUNDCOVER" ("New Gold Lantana" (BOTANICAL . "Lantana hybrid 'New Gold'") (SIZE . " 5 GAL") (COLOR) (QTY . 5)))
		  ("ACCENT" ("Ocotillo" (BOTANICAL . "Fouquieria splendens") (SIZE . " bare root") (COLOR) (QTY . 1))
			    ("Toothless Desert Spoon" (BOTANICAL . "Dasylirion quadrangulatum") (SIZE . " 5 GAL") (COLOR) (QTY . 2))
		  )
		  ("CACTUS" ("Argentine Giant" (BOTANICAL . "Trichocereus candicans") (SIZE . " 5 GAL") (COLOR) (QTY . 1))
			    ("Mexican Fencepost" (BOTANICAL . "Pachycereus marginatus") (SIZE . " 5 GAL") (COLOR) (QTY . 2))
			    ("Purple Prickley Pear" (BOTANICAL . "Opuntia violacea 'Santa Rita'") (SIZE . " 5 GAL") (COLOR) (QTY . 2))
		  )
		  ("LIGHTS" ("Path" (BOTANICAL) (SIZE) (COLOR . "RUST") (QTY . 5))
			    ("Spot" (BOTANICAL) (SIZE) (COLOR . "RUST") (QTY . 5))
			    ("Transformer" (BOTANICAL) (SIZE) (COLOR) (MODEL . "300") (QTY . 1))
		  )
       )
    )
  )
  (PRINC (STRCAT " /></" ax|p ">")) ; Close parent tag 
  (if ax|pp (princ (strcat "</" ax|pp ">"))) ; Close top-level parent tag 
  (SETQ	ax|p nil ax|pp nil ax|t nil) ; empty globs 
  (PRINC)
)

(DEFUN c:testit2 ()
  (SETQ	ax|p nil ax|pp nil ax|t nil)
  (ASSOC->XML 
    '("DrawingData" (DATEDRAWN . "1/6/14") (DRAFTNAME . "Me")
	(REVISIONS ("REVISION1" (WHO . "You") (WHEN . "2/16/2014") (WHAT . "Material colors, CO# 52498"))
		   ("REVISION2" (WHO . "Another") (WHEN . "2/24/2014") (WHAT . "SQFT Adjusted"))
		   ("REVISION3" (WHO . "You") (WHEN . "2/25/2014") (WHAT . "Something else"))
		   ("REVISION4" (WHO . "Other") (WHEN . "2/26/2014") (WHAT . "Add Coping, CO# 52633"))
		   ("REVISION5" (WHO . "Another") (WHEN . "2/28/2014") (WHAT . "Add Sand color"))
		   ("REVISION6" (WHO . "Me") (WHEN . "3/2/2014") (WHAT . "Revised Per Layout"))
		   ("REVISION7" (WHO . "Other") (WHEN . "3/4/2014") (WHAT . "Change Per Super, CO# 52891"))
	)
        (SOFTSCAPE ("PALM" ("Pygmy Palm" (BOTANICAL . "Phoenix robelinii") (SIZE . " 15 GAL") (COLOR) (QTY . 2)))
		   ("SHRUB" ("Arizona Yellow Bells" (BOTANICAL . "Tecoma stans var. agustata") (SIZE . " 5 GAL") (COLOR) (QTY . 2))
			    ("Brave River Sage" (BOTANICAL . "Leucophyllum langmaniae") (SIZE . " 5 GAL") (COLOR) (QTY . 6))
			    ("Chuparosa" (BOTANICAL . "Justicia californica") (SIZE . " 5 GAL") (COLOR) (QTY . 4))
			    ("Pink Fairy Duster" (BOTANICAL . "Calliandra eriophylla") (SIZE . " 5 GAL") (COLOR . "PINK") (QTY . 5))
		   )
		   ("GROUNDCOVER" ("New Gold Lantana" (BOTANICAL . "Lantana hybrid 'New Gold'") (SIZE . " 5 GAL") (COLOR) (QTY . 5)))
		   ("ACCENT" ("Ocotillo" (BOTANICAL . "Fouquieria splendens") (SIZE . " bare root") (COLOR) (QTY . 1))
			     ("Toothless Desert Spoon" (BOTANICAL . "Dasylirion quadrangulatum") (SIZE . " 5 GAL") (COLOR) (QTY . 2))
		   )
		   ("CACTUS" ("Argentine Giant" (BOTANICAL . "Trichocereus candicans") (SIZE . " 5 GAL") (COLOR) (QTY . 1))
			     ("Mexican Fencepost" (BOTANICAL . "Pachycereus marginatus") (SIZE . " 5 GAL") (COLOR) (QTY . 2))
			     ("Purple Prickley Pear" (BOTANICAL . "Opuntia violacea 'Santa Rita'") (SIZE . " 5 GAL") (COLOR) (QTY . 2))
		   )
		   ("LIGHTS" ("Path" (BOTANICAL) (SIZE) (COLOR . "RUST") (QTY . 5))
			     ("Spot" (BOTANICAL) (SIZE) (COLOR . "RUST") (QTY . 5))
			     ("Transformer" (BOTANICAL) (SIZE) (COLOR) (MODEL . "300") (QTY . 1))
		   )
        )
     )
  )
  (PRINC (STRCAT " /></" ax|p ">")) ; Close parent tag 
  (if ax|pp (princ (strcat "</" ax|pp ">"))) ; Close root tag 
  (SETQ	ax|p nil ax|pp nil ax|t nil) ; empty globs 
  (PRINC)
)

 

Any help and/or advice is greatly appreciated. (BTW- this version does not write to a file, that is the easy part. If you choose to test it use textscr to view the results)

 

Thank you.


Viewing all articles
Browse latest Browse all 1680