Author Topic: GetRam | GetFreeRam  (Read 4234 times)

0 Members and 1 Guest are viewing this topic.

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
GetRam | GetFreeRam
« on: April 22, 2010, 05:12:31 PM »
A colleague needed these today -- in case anyone else has need down the road --

Code: [Select]
(defun _GetRam ( / wmiLocater wmiService queryResult result )

[color=green]    ;;========================================================================
    ;;
    ;;  2010/04/22 | Quick & Dirty | Michael Puckett
    ;;
    ;;========================================================================[/color]

    (vl-catch-all-apply
       '(lambda ( )
            (vlax-for item
                (setq queryResult
                    (vlax-invoke
                        (setq
                            wmiLocater (vlax-create-object "WbemScripting.SWbemLocator")
                            wmiService (vlax-invoke wmiLocater 'ConnectServer "." "root\\cimv2")
                        )
                       'ExecQuery
                        (strcat
                            "Select * "
                            "from   Win32_ComputerSystem "
                        )
                    )
                )
                (setq result (vlax-get item 'TotalPhysicalMemory))
            )
        )
    )

    (if queryResult (vlax-release-object queryResult))
    (if wmiService (vlax-release-object wmiService))
    (if wmiLocater (vlax-release-object wmiLocater))

    (if result (/ (distof result) 1024 1024)) [color=green];; return MB[/color]

)

Code: [Select]
(defun _GetFreeRam ( / wmiLocater wmiService queryResult result )

[color=green]    ;;========================================================================
    ;;
    ;;  2010/04/22 | Quick & Dirty | Michael Puckett
    ;;
    ;;========================================================================[/color]

    (vl-catch-all-apply
       '(lambda ( )
            (vlax-for item
                (setq queryResult
                    (vlax-invoke
                        (setq
                            wmiLocater (vlax-create-object "WbemScripting.SWbemLocator")
                            wmiService (vlax-invoke wmiLocater 'ConnectServer "." "root\\cimv2")
                        )
                       'ExecQuery
                        (strcat
                            "Select * "
                            "from   Win32_OperatingSystem "
                        )
                    )
                )
                (setq result (vlax-get item 'FreePhysicalMemory))
            )
        )
    )

    (if queryResult (vlax-release-object queryResult))
    (if wmiService (vlax-release-object wmiService))
    (if wmiLocater (vlax-release-object wmiLocater))

    (if result (/ (distof result) 1024)) [color=green];; return MB[/color]

)

cheers
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

Lee Mac

  • Seagull
  • Posts: 12924
  • London, England
Re: GetRam | GetFreeRam
« Reply #1 on: April 22, 2010, 06:04:33 PM »
Thanks Michael - they work well.

If you have the time, I have a few questions about the code:

  • Why do you use "." for the Server and "root\\cimv2" for the namespace? Are these the defaults?
  • I don't quite understand the structure of SWbemObjectSet that is returned by the ExecQuery, but could you use "Select TotalPhysicalMemory" as opposed to "Select *" ?

Sorry to be a bother.

Lee

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: GetRam | GetFreeRam
« Reply #2 on: April 22, 2010, 06:09:12 PM »
I didn't put much thought  into it to be honest -- I just converted this vbscript code to lisp.
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: GetRam | GetFreeRam
« Reply #3 on: April 22, 2010, 06:12:45 PM »
I did lift this part:

Code: [Select]
                        (setq
                            wmiLocater (vlax-create-object "WbemScripting.SWbemLocator")
                            wmiService (vlax-invoke wmiLocater 'ConnectServer "." "root\\cimv2")
                        )

From some previous work I had done and if memory serves Daniel had helped me with it.
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

Lee Mac

  • Seagull
  • Posts: 12924
  • London, England
Re: GetRam | GetFreeRam
« Reply #4 on: April 22, 2010, 06:17:40 PM »
Gotcha - thanks mate  :-)

alanjt

  • Needs a day job
  • Posts: 5352
  • Standby for witty remark...
Re: GetRam | GetFreeRam
« Reply #5 on: April 22, 2010, 07:43:53 PM »
Cool stuff Michael. Always teaching us something new. :)
Civil 3D 2019 ~ Windohz 7 64bit
Dropbox

VovKa

  • Water Moccasin
  • Posts: 1632
  • Ukraine
Re: GetRam | GetFreeRam
« Reply #6 on: April 23, 2010, 03:03:23 AM »
i wonder why MS guys decided to count "TotalPhysicalMemory" in bytes and "FreePhysicalMemory" in kilobytes?

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: GetRam | GetFreeRam
« Reply #7 on: April 23, 2010, 09:29:25 AM »
i wonder why MS guys decided to count "TotalPhysicalMemory" in bytes and "FreePhysicalMemory" in kilobytes?

I wondered who would be first to catch that. :) When I first wrote them I was like "What  the hell? -- Left hand doesn't know what the right hand is doing".
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

Andrea

  • Water Moccasin
  • Posts: 2372
Re: GetRam | GetFreeRam
« Reply #8 on: April 23, 2010, 11:19:02 AM »
is something like...

Code: [Select]
(command "_shell" "mem > c:\\mem.txt")
(startapp "notepad" "c:\\mem.txt")

working ?
Keep smile...

HasanCAD

  • Swamp Rat
  • Posts: 1422
Re: GetRam | GetFreeRam
« Reply #9 on: April 25, 2010, 12:03:34 PM »
I tried to show the resule in an alert box bot not working

Code: [Select]
(DEFUN C:GetRamF ()

  (setq GetRam_T (_GetRam))
  (setq GetRam_F (_GetFreeRam))
  (alert GetRam_T"\n" GetRam_F)
  )

alanjt

  • Needs a day job
  • Posts: 5352
  • Standby for witty remark...
Re: GetRam | GetFreeRam
« Reply #10 on: April 25, 2010, 12:13:22 PM »
I tried to show the resule in an alert box bot not working

Code: [Select]
(DEFUN C:GetRamF ()

  (setq GetRam_T (_GetRam))
  (setq GetRam_F (_GetFreeRam))
  (alert GetRam_T"\n" GetRam_F)
  )

Look at strcat to join strings.

eg.
(strcat "H" "E" "L" "P") => "HELP"
Civil 3D 2019 ~ Windohz 7 64bit
Dropbox

HasanCAD

  • Swamp Rat
  • Posts: 1422
Re: GetRam | GetFreeRam
« Reply #11 on: April 25, 2010, 12:17:44 PM »
alanjt
Thanx for your reply
As expected your replies are very fast more than I imagine

alanjt

  • Needs a day job
  • Posts: 5352
  • Standby for witty remark...
Re: GetRam | GetFreeRam
« Reply #12 on: April 25, 2010, 12:23:22 PM »
Oops, didn't realize the return was a real. You'll have to convert to string first.
Look at RToS or vl-princ-to-string.
Civil 3D 2019 ~ Windohz 7 64bit
Dropbox

HasanCAD

  • Swamp Rat
  • Posts: 1422
Re: GetRam | GetFreeRam
« Reply #13 on: April 25, 2010, 12:32:48 PM »
the 2 previous routines and gives alert message

Code: [Select]
; o_|_||
;   Subroutine

;;  2010/04/22 | Quick & Dirty | Michael Puckett

(defun _GetRam ( / wmiLocater wmiService queryResult result )


    (vl-catch-all-apply
       '(lambda ( )
            (vlax-for item
                (setq queryResult
                    (vlax-invoke
                        (setq
                            wmiLocater (vlax-create-object "WbemScripting.SWbemLocator")
                            wmiService (vlax-invoke wmiLocater 'ConnectServer "." "root\\cimv2")
                        )
                       'ExecQuery
                        (strcat
                            "Select * "
                            "from   Win32_ComputerSystem "
                        )
                    )
                )
                (setq result (vlax-get item 'TotalPhysicalMemory))
            )
        )
    )

    (if queryResult (vlax-release-object queryResult))
    (if wmiService (vlax-release-object wmiService))
    (if wmiLocater (vlax-release-object wmiLocater))

    (if result (/ (distof result) 1024 1024)) ;; return MB

)


(defun _GetFreeRam ( / wmiLocater wmiService queryResult result )

  (vl-catch-all-apply
       '(lambda ( )
            (vlax-for item
                (setq queryResult
                    (vlax-invoke
                        (setq
                            wmiLocater (vlax-create-object "WbemScripting.SWbemLocator")
                            wmiService (vlax-invoke wmiLocater 'ConnectServer "." "root\\cimv2")
                        )
                       'ExecQuery
                        (strcat
                            "Select * "
                            "from   Win32_OperatingSystem "
                        )
                    )
                )
                (setq result (vlax-get item 'FreePhysicalMemory))
            )
        )
    )

    (if queryResult (vlax-release-object queryResult))
    (if wmiService (vlax-release-object wmiService))
    (if wmiLocater (vlax-release-object wmiLocater))

    (if result (/ (distof result) 1024)) ;; return MB

)
; o_|_||
;                          Subroutine


; o_|_||
;  mainroutine
(DEFUN C:GetRamF ()

  (setq GetRam_T (_GetRam))
  (setq GetRam_F (_GetFreeRam))
  (alert (strcat "total Ram = " (rtos GetRam_T) "\nFree ram = " (rtos GetRam_F)))
  )

; o_|_||
;                         mainroutine

Thanx for alanjt

HasanCAD

  • Swamp Rat
  • Posts: 1422
Re: GetRam | GetFreeRam
« Reply #14 on: April 25, 2010, 12:47:07 PM »
Oops, didn't realize the return was a real. You'll have to convert to string first.
Look at RToS or vl-princ-to-string.

I got it after try and error
I tried to use itoa then rtos

This pulling me to ask
Is there a way to know is the result string, intiger or real?

alanjt

  • Needs a day job
  • Posts: 5352
  • Standby for witty remark...
Re: GetRam | GetFreeRam
« Reply #15 on: April 25, 2010, 12:48:08 PM »
(type "test")
(type (car (entsel)))
(type '(1 2 3))
(type 1.25)
(type 1)
Civil 3D 2019 ~ Windohz 7 64bit
Dropbox

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: GetRam | GetFreeRam
« Reply #16 on: April 25, 2010, 12:49:04 PM »
Use the type function:

(type 42) => 'int
(type 42.0) => 'real
(type "42") => 'str

etc.

edit: gat damn I type too slow
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

HasanCAD

  • Swamp Rat
  • Posts: 1422
Re: GetRam | GetFreeRam
« Reply #17 on: April 25, 2010, 12:53:57 PM »
MP
You have to get more practises in typing  ;-)

alanjt

  • Needs a day job
  • Posts: 5352
  • Standby for witty remark...
Re: GetRam | GetFreeRam
« Reply #18 on: April 25, 2010, 01:08:37 PM »
edit: gat damn I type too slow
Post ninja strikes again!
Civil 3D 2019 ~ Windohz 7 64bit
Dropbox

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: GetRam | GetFreeRam
« Reply #19 on: April 25, 2010, 02:29:54 PM »
 :-D
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

JohnK

  • Administrator
  • Seagull
  • Posts: 10657
Re: GetRam | GetFreeRam
« Reply #20 on: April 25, 2010, 06:15:31 PM »
> post ninja

*rofl*
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

alanjt

  • Needs a day job
  • Posts: 5352
  • Standby for witty remark...
Re: GetRam | GetFreeRam
« Reply #21 on: April 25, 2010, 07:34:18 PM »
Civil 3D 2019 ~ Windohz 7 64bit
Dropbox