Visual LISP, AutoLISP and General Customization

Visual LISP, AutoLISP and General Customization

Reply
Mentor
aqdam1978
Posts: 265
Registered: ‎11-05-2008
Message 1 of 6 (218 Views)

[ToString function] variables conversion function to string

218 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
)

 

*Expert Elite*
pbejse
Posts: 2,440
Registered: ‎11-24-2009
Message 2 of 6 (211 Views)

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?

 

 

 

Mentor
aqdam1978
Posts: 265
Registered: ‎11-05-2008
Message 3 of 6 (209 Views)

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!

*Expert Elite*
pbejse
Posts: 2,440
Registered: ‎11-24-2009
Message 4 of 6 (200 Views)

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. 

 

 

Mentor
aqdam1978
Posts: 265
Registered: ‎11-05-2008
Message 5 of 6 (181 Views)

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

*Expert Elite*
pbejse
Posts: 2,440
Registered: ‎11-24-2009
Message 6 of 6 (175 Views)

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

 

You are not logged in.

Log into access your profile, ask and answer questions, share ideas and more. Haven't signed up yet? Register

Announcements
Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.

Need installation help?

Start with some of our most frequented solutions to get help installing your software.

Ask the Community