马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
本帖最后由 Highflybird 于 2013-5-17 11:47 编辑
=========================================
2013.05.17更新
如果对帖子中的代码有疑问或者可能的抄录笔误,请下载下面附件:
=========================================
首先申明,这篇帖子不建议初学者浏览。一则我不能一一回答不明白者的提问,二则避免初学者钻牛角尖。因此有很多地方并不给出完整的代码。
脚本有很多用处,在CAD中如果能灵活运用,也可以为图纸之外的事情发挥较大作用。譬如近来有很多帖子问及系统中有多少个磁盘,设备系列号,屏幕分辨率,等等。如果知道了这方面的知识,完全是可以几行代码就解决问题的。
关于脚本的更多请搜索。
AutoCAD作为一种具有高度开放结构的CAD平台软件,它提供了强大的二次开发环境。从AutoCAD R14版开始,AutoCAD引入了ActiveX Automation技术术。由于ActiveX技术是一种完全面向对象的技术,所以许多面向对象化编程的语言和应用程序,可以通过ActiveX与AutoCAD进行通信,并操纵AutoCAD的许多功能。
AutoCAD ActiveX技术提供了一种机制,该机制可使编程者通过编程手段从AutoCAD的内部或外部来操纵AutoCAD。ActiveX是由一系列的对象,按一定的层次组成的一种对象结构,每一个对象代表了AutoCAD中一个明确的功能,如绘制图形对象、定义块和属性等等。ActiveX所具备的绝大多数AutoCAD功能,均以方法和属性的方式被封装在ActiveX对象中,只要使用某种方式,使ActiveX对象得以“暴露”,那么就可以使用各种面向对象编程的语言对其中的方法、属性进行引用,从而达到对AutoCAD实现编程的目的。这两者我不再详细描述。
CAD中执行ActiveX函数的方法有两种,一种用vlax-import-type-library函数引入,另外一种用vlax-invoke,和vlax-get(或者vlax-invoke-methode,和vlax-get-property);后一种比前一种更广,但前一种 它的优势在于 可以写更少的代码,更智能化,能利用Vlisp编辑器的自动完成功能获得更多的用法。但对于有的不能用vlax-import-type-library。本帖大多数用的是前者。
首先用了一个函数,为后面的程序做准备。
;;;获得系统工作路径
[pcode=lisp,true](defun GetSpecialPath (n / fso path)
(setq fso (vlax-create-object "Scripting.FileSystemObject"))
(setq path (vlax-get (vlax-invoke fso 'GetSpecialFolder n) 'path))
(vlax-release-object fso)
path
)[/pcode]
脚本技术中有几个很重要的东西: WScipt.Shell对象,FileSystemObject对象,Shell.Application对象,WMI,以及
ScriptControl对象。这几个对象我不再介绍了。
脚本宿主对象
[pcode=lisp,true]setq path (strcat (GetSpecialPath 1) "/wshom.ocx")) ;;
(if (not wc-Alias)
(vlax-import-type-library
:tlb-filename path
:methods-prefix "wm-"
:properties-prefix "wp-"
:constants-prefix "wc-"
)
)
(setq wsh (vlax-create-object "WScript.shell"))[/pcode]
脚本本身
[pcode=lisp,true](setq path (strcat (GetSpecialPath 1) "/msscript.ocx"))
(if (not sc-Connected)
(vlax-import-type-library
:tlb-filename path
:methods-prefix "sm-"
:properties-prefix "sp-"
:constants-prefix "sc-"
)
)
(setq scr (vlax-create-object "ScriptControl"))
[/pcode]
文件系统对象
[pcode=lisp,true](setq path (strcat (getSpecialPath 1) "/scrrun.dll"))
(if (not fc-Alias)
(vlax-import-type-library
:tlb-filename path
:methods-prefix "fm-"
:properties-prefix "fp-"
:constants-prefix "fc-"
)
)
(setq fso (vlax-create-object "Scripting.FileSystemObject"))
[/pcode]
Shell对象
[pcode=lisp,true](setq path (strcat (getSpecialPath 1) "/shell32.dll"))
(if (not ac-ssfwindows)
(vlax-import-type-library
:tlb-filename path
:methods-prefix "am-"
:properties-prefix "ap-"
:constants-prefix "ac-"
)
)
(setq sha (vlax-create-object "shell.application"))
[/pcode]
下面我一一介绍它们的用法。
1.WSH对象,ScriptControl对象和WMI
;;简单的欢迎
[pcode=lisp,true] (wm-Popup wsh "Hello,World!")[/pcode]
;;输入框
- (vlax-invoke scr 'ExecuteStatement "str=InputBox(\"输入您的名字:\", \"输入框\")")
- (sm-ExecuteStatement scr "str=InputBox(\"输入您的名字:\", \"输入框\")")
;;求值
- (vlax-invoke scr 'eval "str")
- (sm-eval scr "str")
;;利用wscript发送键
- (wm-sendkeys wsh "C{ENTER}0,0{ENTER}100{ENTER}") ;在CAD命令状态下画一个圆
- (WM-SENDKEYS wsh "赌") ;很神奇的,居然是打开我的电脑
- (WM-SENDKEYS wsh "品") ;打开计算器
- (WM-SENDKEYS wsh "血") ;打开搜索
- (WM-SENDKEYS wsh "恋") ;打开媒体播放器
- (WM-SENDKEYS wsh "爽") ;打开主页
;;创建一个URL的快捷方式
- (setq Spec (wp-get-SpecialFolders wsh))
- (setq deskTopPath (wm-item spec "DeskTop"))
- (setq url (wm-CreateShortcut wsh (strcat deskTopPath "/MyTest.URL")))
- (wp-put-TargetPath url "http://bbs.xdcad.com")
- (wm-save url)
;;创建一个快捷方式并指定快捷键
- (setq link (wm-CreateShortcut wsh (strcat DeskTopPath "/测试快捷方式.lnk")))
- (wp-put-TargetPath link "http://bbs.xdcad.com")
- (wp-put-WindowStyle link 1)
- (wp-put-Hotkey link "Ctrl+Alt+e")
- (wp-put-IconLocation link "shell32.dll,14")
- (wp-put-Description link "测试快捷方式的描述")
- (wp-put-WorkingDirectory link "c:/")
- (wm-save link)
;;运行命令
- (wm-run wsh "cmd.exe /C dir c:\\temp\\*.* /a /s >>c:\\1.txt")
;;获得系统环境变量
- (Setq env (wp-get-Environment wsh "System"))
;;系统目录
- (alert (wp-get-item env "WINDIR"))
- (alert (wm-ExpandEnvironmentStrings wsh "%windir%"))
- (alert (wp-get-Item env "TMP"))
- (alert (wp-get-Item env "TEMP"))
;;增加和移除环境变量
- (alert "Add a test var to the system!")
- (wp-put-item env "TestVar" "Windows Script Host")
- (alert "Remove the test var from the system!")
- (wm-remove env "TestVar")
;;列出某个环境变量的全部
- (setq i 0)
- (repeat (wm-count env)
- (princ (wp-get-item env i)) ;但是不会显现出来在vbs中运行正常
- (setq i (1+ i))
- )
;;以下相同
- (setq str
- "Set WshShell = CreateObject(\"WScript.Shell\")
- Msgbox \"Environment.item: \"& WshShell.Environment.item(\"WINDIR\")
- Msgbox \"ExpandEnvironmentStrings: \"& WshShell.ExpandEnvironmentStrings(\"%windir%\")
- set oEnv=WshShell.Environment(\"System\")
- Msgbox \"Adding ( TestVar=Windows Script Host ) to the System type environment\"
- oEnv(\"TestVar\") = \"Windows Script Host\"
- Msgbox \"removing ( TestVar=Windows Script Host ) from the System type environment\"
- oEnv.Remove \"TestVar\"
- for each sitem in oEnv
- strval=strval & sItem & vbcrlf
- next
- Msgbox \"System Environment:\" & vbcrlf & vbcrlf & strval
- strval=\"\"'
- set oEnv=WshShell.Environment(\"Process\")
- for each sitem in oEnv
- strval=strval & sItem & vbcrlf
- next
- Msgbox \"Process Environment:\" & vbcrlf & vbcrlf & strval
- strval=\"\"
- set oEnv=WshShell.Environment(\"User\")
- for each sitem in oEnv
- strval=strval & sItem & vbcrlf
- next
- Msgbox \"User Environment:\" & vbcrlf & vbcrlf & strval
- strval=\"\"
- set oEnv=WshShell.Environment(\"Volatile\")
- for each sitem in oEnv
- strval=strval & sItem & vbcrlf
- next
- Msgbox \"Volatile Environment:\" & vbcrlf & vbcrlf & strval
- strval=\"\"
- set oEnv = nothing
- set WshShell = nothing
- "
- )
- (vlax-invoke Scr 'ExecuteStatement str)
;;读写注册表 regread ,regwrite,regdelete
- (vlax-invoke wsh 'RegRead "HKCU\\Software\\AutoDesk\\AutoCAD\\R16.2\\curver") ;确保你装的是autocad 2006否则出错
;;系统信息篇
;;(如获取机器的物理地址)
- (setq str "Set mc=GetObject(\"Winmgmts:\")")
- (SM-EXECUTESTATEMENT scr str)
- (setq objWMI (vla-eval scr "mc"))
- (setq objNet (vlax-invoke objWMI 'InstancesOF "Win32_NetworkAdapterConfiguration"))
- (princ "\n物理地址是:")
- (vlax-for obj objNet
- (if(/= (vlax-get obj 'IPEnabled) 0)
- (princ (vlax-get obj 'MacAddress))
- )
- )
;;也可以按照如下方式获得详细信息
- (foreach p (list
- "Win32_ComputerSystem"
- "Win32_Service"
- "Win32_LogicalMemoryConfiguration"
- "Win32_Process"
- "Win32_Processor"
- "Win32_OperatingSystem"
- "Win32_WMISetting"
- "__NAMESPACE"
- "win32_baseboard"
- "win32_videocontroller"
- "win32_DiskDrive"
- "win32_physicalMemory"
- "Win32_Environment"
- "Win32_ProcessStartTrace"
- "Win32_PnpDevice"
- "Win32_SoundDevice"
- "Win32_ProductCheck"
- "Win32_NetworkAdapter"
- "Win32_CDROMDrive"
- "Win32_DesktopMonitor"
- "Win32_NetworkAdapterConfiguration"
- ;;"Win32_NTLogEvent" ;太多了
- )
- (setq objSYS (vlax-invoke objWMI 'InstancesOf p))
- (vlax-for n objSYS
- (alert (vlax-invoke n 'GetObjectText_))
- )
- )
;;以下相同只不过只是收集简单的信息
- (setq WMI (vla-eval scr "mc"))
;;收集计算机用户信息
- (setq col (vlax-invoke WMI 'ExecQuery "Select * from Win32_ComputerSystem" "WQL" 48))
- (vlax-for n Col
- (princ "\n用户名是:")
- (princ (vlax-get n 'name))
- )
;;获取进程
- (setq col (vlax-invoke WMI 'ExecQuery "Select * from Win32_Process" "WQL" 48))
- (vlax-for n Col
- (princ (vlax-get n 'name))
- )
;;获取CPU信息
- (setq col (vlax-invoke WMI 'ExecQuery "Select * from Win32_Processor" "WQL" 48))
- (vlax-for n Col
- (princ (vlax-get n 'name))
- )
;;获取内存总容量
- (setq col (vlax-invoke WMI 'ExecQuery "Select * from Win32_ComputerSystem" "WQL" 48))
- (vlax-for n Col
- (princ (/ (read (vlax-get n 'TotalPhysicalMemory)) 1048576))
- (princ "M")
- )
;;获取内存外频和数量信息
- (setq col (vlax-invoke WMI 'ExecQuery "Select * from Win32_PhysicalMemory" "WQL" 48))
- (vlax-for n Col
- (princ "\n")
- (princ (vlax-get n 'Description))
- (princ "\n")
- (princ (vlax-get n 'DeviceLocator))
- (princ "\n")
- (princ (vlax-get n 'speed))
- )
;;获取显卡信息
- (setq col (vlax-invoke WMI 'ExecQuery "Select * from Win32_VideoController" "WQL" 48))
- (vlax-for n Col
- (princ "\n")
- (princ (vlax-get n 'Caption))
- (princ "\n")
- (princ (vlax-get n 'VideoModeDescription))
- )
;;获取硬盘基本信息
- (setq col (vlax-invoke WMI 'ExecQuery "Select * from Win32_DiskDrive" "WQL" 48))
- (vlax-for n Col
- (princ "\n硬盘的设备编号是:")
- (princ (vlax-get n 'Caption))
- (princ "\n这个硬盘的容量是:")
- (princ (/ (read (vlax-get n 'size)) 1073741824))
- (princ "G")
- )
;;获取声卡信息
- (setq col (vlax-invoke WMI 'ExecQuery "Select * from Win32_SoundDevice" "WQL" 48))
- (vlax-for n Col
- (princ "\n声卡的信息是:")
- (princ (vlax-get n 'ProductName))
- )
;;获取网卡信息
- (setq col (vlax-invoke WMI 'ExecQuery "Select * from Win32_NetworkAdapter" "WQL" 48))
- (vlax-for n Col
- (princ "\n网卡的设备描述是:")
- (princ (vlax-get n 'Description))
- (princ "\n网卡的信MAC地址是:")
- (princ (vlax-get n 'MACAddress))
- )
;;获取软驱信息
- (setq col (vlax-invoke WMI 'ExecQuery "Select * from Win32_FloppyDrive" "WQL" 48))
- (vlax-for n Col
- (princ "\n软驱的信息是:")
- (princ (vlax-get n 'Caption))
- )
;;获取CD/DVD ROM信息
- (setq col (vlax-invoke WMI 'ExecQuery "Select * from Win32_CDROMDrive" "WQL" 48))
- (vlax-for n Col
- (princ "\n光驱的信息是:")
- (princ (vlax-get n 'Name))
- (princ "\n光驱的信息是:")
- (princ (vlax-get n 'Description))
- )
;;获取屏幕分辨率
- (setq CoL (vlax-invoke WMI 'ExecQuery "Select * from Win32_DesktopMonitor" "WQL" 48))
- (vlax-for n Col
- (princ "\n屏幕横向分辨率为:")
- (princ (vlax-get n 'ScreenWidth))
- (princ "\n屏幕竖向分辨率为:")
- (princ (vlax-get n 'ScreenHeight))
- )
小提醒:如果用vlax-Create-object方法创建了一个对象,别忘记用vlax-release-object释放它。 |