• Industries
  • Products
  • Buy
  • Services & Support
  • Communities
  • Discussion Groups

    Visual LISP, AutoLISP and General Customization

    Reply
    Distinguished Contributor
    Posts: 145
    Registered: ‎11-05-2008

    [ToString function] variables conversion function to string

    99 Views, 5 Replies
    02-17-2013 06:49 PM

    Hi,

     

    I want to write content of variables to a text file but I should have conversion function for variables.

    for example

    (setq a 123); a is an integer type, contetnt of a is 123

    so I should use (itoa a) to get "123" as a string and then I can write it to text file.

    but what about another type of variables?

     

    (defun ToString ( a / result)
    (setq result "")
    (cond
    	((= (type a) 'ENAME) 
    		(progn (setq result "Entity names")))
    	((= (type a) 'EXRXSUBR) 
    		(progn (setq result "External ObjectARX applications")))
    	((= (type a) 'FILE) 
    		(progn (setq result "File descriptors")))
    	((= (type a) 'INT) 
    		(progn (setq result (itoa a))));;"Integers"
    	((= (type a) 'LIST) 
    		(progn (setq result "Lists")))
    	((= (type a) 'PAGETB) 
    		(progn (setq result "Function paging table")))
    	((= (type a) 'PICKSET) 
    		(progn (setq result "Selection sets")))
    	((= (type a) 'REAL) 
    		(progn (setq result (rtos a 2 3))));;"Floating-point numbers"
    	((= (type a) 'SAFEARRAY) 
    		(progn (setq result "Safearray")))
    	((= (type a) 'STR) 
    		(progn (setq result a)));;"Strings"
    	((= (type a) 'SUBR) 
    		(progn (setq result "Internal AutoLISP functions")))
    	((= (type a) 'SYM) 
    		(progn (setq result "Symbols")))
    	((= (type a) 'VARIANT) 
    		(progn (setq result "Variant")))
    	((= (type a) 'USUBR) 
    		(progn (setq result "User-defined functions loaded from LISP source files")))
    	((= (type a) 'VLA-object) 
    		(progn (setq result "ActiveX objects")))
    )
    Result
    )

     

    Please use plain text.
    *Expert Elite*
    Posts: 2,057
    Registered: ‎11-24-2009

    Re: [ToString function] variables conversion function to string

    02-17-2013 07:15 PM in reply to: aqdam1978

    aqdam1978 wrote:

    Hi,

     

    I want to write content of variables to a text file but I should have conversion function for variables.

    for example

    (setq a 123); a is an integer type, contetnt of a is 123

    so I should use (itoa a) to get "123" as a string and then I can write it to text file.

    but what about another type of variables?

     

     


    Would you want the "value" of the variable to write to string?

    for example  (setq a '(1.2 2.5 0.0))

    (type a) = LIST

    write to string --- > "(1.2 2.5 0.0)" and not "lists" as you have shown on your code?

     

     

     

    Please use plain text.
    Distinguished Contributor
    Posts: 145
    Registered: ‎11-05-2008

    Re: [ToString function] variables conversion function to string

    02-17-2013 07:17 PM in reply to: pbejse

    yes, exactly, because I can't write any variables in a text file except string variables!

    Please use plain text.
    *Expert Elite*
    Posts: 2,057
    Registered: ‎11-24-2009

    Re: [ToString function] variables conversion function to string

    02-17-2013 07:32 PM in reply to: aqdam1978

    aqdam1978 wrote:

    yes, exactly, because I can't write any variables in a text file except string variables!


     

    Its easy. waht you need is an association list .

     

    NOT sure why you need to write to string the "other types" but you may need to add an function to evaluate the variable value and convert it to a string like the one on your sample

    (INT (itoa a)) 

     

    (eval..  will evaluate the second element 

     

     

    (defun ToString  (a / result)
      (eval (cadr (assoc (type a)
                         '((ENAME "Entity names")
                           (EXRXSUBR "External ObjectARX applications")
                           (FILE "File descriptors")
                           (INT (itoa a))
                           (LIST (vl-princ-to-string a))
                           (PAGETB "Function paging table")
                           (PICKSET "Selection sets")
                           (REAL (rtos a 2 3))
                           (SAFEARRAY "Safearray")
                           (STR a)
                           (SUBR "Internal AutoLISP functions")
                           (SYM "Symbols")
                           (VARIANT "Variant")
                           (USUBR "User-defined functions loaded from LISP source files")
                           (VLA-object "ActiveX objects"))))
            )
      )

    As for the other types, i' leave that to you to figure out the functions

     

    HTH

     

    EDIT: That was you question, silly me... I woiuld love to play but you need to give an example for each type then i can point you in the right direction. 

     

     

    Please use plain text.
    Distinguished Contributor
    Posts: 145
    Registered: ‎11-05-2008

    Re: [ToString function] variables conversion function to string

    02-17-2013 08:17 PM in reply to: pbejse

    Hi Pbejse,

     

    you are hero!

     

    I just want to have general function to convert all types of variable to string.

    I call it ToString.

    for exaple see the "ToString" function usage in this code:

     

    (defun c:GetSystemInfo(/ item meth1 meth2 process wmi fn des)
    	(defun ToString  (a)
    	  (eval (cadr (assoc (type a)
    						 '((ENAME "Entity names")
    						   (EXRXSUBR "External ObjectARX applications")
    						   (FILE "File descriptors")
    						   (INT (itoa a))
    						   (LIST (vl-princ-to-string a))
    						   (PAGETB "Function paging table")
    						   (PICKSET "Selection sets")
    						   (REAL (rtos a 2 3))
    						   (SAFEARRAY "Safearray")
    						   (STR a)
    						   (SUBR "Internal AutoLISP functions")
    						   (SYM "Symbols")
    						   (VARIANT "Variant")
    						   (USUBR "User-defined functions loaded from LISP source files")
    						   (VLA-object "ActiveX objects"))))
    			)
    	)	
    	(defun lister(val / it)
    		(vlax-for it val
    		(if (vlax-get it 'value)
    		(write-line (strcat (vlax-get it 'name) " : " (ToString (vlax-get it 'value))) des)
    		);;if
    		);;vlax
    	)
    
    (vl-load-com)
    (setq fn "D:\\SystemInfo.txt")
    (setq des (open fn "w" ))
    (setq WMI (vlax-create-object "WbemScripting.SWbemLocator"))
    (setq meth1 (vlax-invoke WMI 'ConnectServer nil nil nil nil nil nil nil nil))
    (write-line "------------------------------------------------------------" des)
    (foreach process (list "BIOS" "Processor" "VideoController" "SoundDevice" "CDROMDrive" "OperatingSystem" "ComputerSystem" "PhysicalMemory" "DiskDrive")
    	(setq meth2 (vlax-invoke meth1 'ExecQuery (strcat "Select * from Win32_" Process)))
    	(write-line (strcat "<<< features of " process " >>>:\n------------------------------------------------------------") des)
    	(vlax-for item meth2
    		(lister (vlax-get item 'Properties_))
    		(lister (vlax-get item 'Qualifiers_))
    		(write-line "------------------------------------------------------------" des)
    	)
    );;foreach
    (close des)
    (mapcar 'vlax-release-object (list meth1 meth2 wmi))
    (startapp "Notepad" fn)
    (princ)
    )

     Thank you for all of your helps and supports.

     

    Abbas

    Please use plain text.
    *Expert Elite*
    Posts: 2,057
    Registered: ‎11-24-2009

    Re: [ToString function] variables conversion function to string

    02-17-2013 08:56 PM in reply to: aqdam1978

    it that case you dont need a fancy sub to convert all the values to string

    vl-princ-to-string will suffice

     

     

    (defun lister (val file / it)
    		(vlax-for it val
    		(if (vlax-get it 'value)
    		(write-line (strcat (vlax-get it 'name) " : " (vl-princ-to-string (vlax-get it 'value))) file)
    		);;if
    		);;vlax
    	)

     

    (vlax-for item meth2
    		(lister (vlax-get item 'Properties_) des)
    		(lister (vlax-get item 'Qualifiers_) des)
    		(write-line "------------------------------------------------------------" des)
    	)

     

    HTH

     

    Please use plain text.