- 积分
- 2003
- 明经币
- 个
- 注册时间
- 2003-4-30
- 在线时间
- 小时
- 威望
-
- 金钱
- 个
- 贡献
-
- 激情
-
|
楼主 |
发表于 2003-10-23 09:07:00
|
显示全部楼层
那我就贴出来,欢迎大家批评指教
是VB代码
form1代码:
Sub pdx()
On Error Resume Next
Dim acadapp As AcadApplication
Dim acaddoc As AcadDocument
Set acadapp = ConnectToAcad(acadapp)
Set acaddoc = acadapp.ActiveDocument
Dim polarPnt(0 To 5) As Variant
Dim angle As Double
Dim angle1 As Double
Dim angle2 As Double
Dim angle3 As Double
Dim distance As Double
Dim distance1 As Double
Dim templine As AcadLine
AppActivate acadapp.Caption
Dim moren As Double
moren = Val(GetKeyValue(HKEY_LOCAL_MACHINE, "software\Topirol工具箱\pdx\", "configvalue"))
If moren = 0 Then
moren = 40
End If
distance = getconfigvalue(acaddoc, moren, "设置起伏距离(" & moren & "):")
On Error GoTo errhandle1
polarPnt(0) = acaddoc.Utility.GetPoint(, "选择起始点")
polarPnt(5) = acaddoc.Utility.GetPoint(polarPnt(0), "选择结束点")
Set templine = acaddoc.ModelSpace.AddLine(polarPnt(0), polarPnt(5))
angle = templine.angle
angle1 = 1.2741
angle2 = 4.9742
angle3 = 1.2741
If distance >= templine.Length Then
distance = templine.Length / 5
End If
distance1 = (templine.Length - distance) / 2
polarPnt(1) = acaddoc.Utility.PolarPoint(polarPnt(0), angle, distance1)
polarPnt(2) = acaddoc.Utility.PolarPoint(polarPnt(1), angle + angle1, distance)
polarPnt(3) = acaddoc.Utility.PolarPoint(polarPnt(2), angle + angle2, 2 * distance)
polarPnt(4) = acaddoc.Utility.PolarPoint(polarPnt(3), angle + angle3, distance)
Dim lineObj As AcadPolyline
Dim points(0 To 17) As Double
Dim I As Integer
For I = 0 To 5 Step 1
points(3 * I) = polarPnt(I)(0)
points(3 * I + 1) = polarPnt(I)(1)
points(3 * I + 2) = polarPnt(I)(2)
Next
Set lineObj = acaddoc.ModelSpace.AddPolyline(points)
lineObj.Color = 3
lineObj.Update
templine.Delete
exit_Here:
errhandle1:
Dim varCancel As Variant
Select Case Err.Number
Case -2147352567
'按了取消键或其它透明命令
varCancel = acaddoc.GetVariable("LASTPROMPT")
If InStr(1, varCancel, "*Cancel*") <> 0 And InStr(1, varCancel, "*取消*") <> 0 Then
Err.Clear
Resume exit_Here
Else
Err.Clear
Resume
End If
Case -2147467259
'右键单击或回车或空格
GoTo errhandle
Case Else
Err.Clear
End Select
errhandle:
End Sub
Function ConnectToAcad(acadapp As AcadApplication) As AcadApplication
On Error Resume Next
Set acadapp = GetObject(, "AutoCAD.Application")
If Err Then
Err.Clear
Set acadapp = CreateObject("AutoCAD.Application")
If Err Then
MsgBox Err.Description
Exit Function
End If
End If
Set ConnectToAcad = acadapp
End Function
Private Sub Form_Load()
pdx
End
End Sub
Function getconfigvalue(acaddoc As AcadDocument, moren As Double, prompt As String) As Double
Dim configvalue As String
configvalue = acaddoc.Utility.GetString(False, prompt)
If configvalue <> "" And IsNumeric(configvalue) Then
getconfigvalue = CDbl(configvalue)
UpdateKey HKEY_LOCAL_MACHINE, "software\Topirol工具箱\pdx\", "configvalue", REG_SZ, configvalue, LenB(configvalue)
Else
getconfigvalue = moren
End If
End Function
模块代码:
Option Explicit
' 这个模块用于读和写注册表关键字。
' 不同于VB 的内部注册表访问方法,它可以
' 通过字符串的值来读和写任何注册表关键字。
'---------------------------------------------------------------
'-注册表 API 声明...
'RegCloseKey 用于关闭系统注册表中的一个项(或键)
'RegCreateKeyEx用于创建注册表项
'RegOpenKeyEx用于打开注册表项
'RegQueryValueEx 用于获取一个项的设置值
'RegSetValueEx 用于设置指定项的值
'---------------------------------------------------------------
Public Declare Function RegCloseKey Lib "advapi32" (ByVal hkey As Long) As Long
Public Declare Function RegCreateKeyEx Lib "advapi32" Alias "RegCreateKeyExA" (ByVal hkey As Long, ByVal lpSubKey As String, ByVal Reserved As Long, ByVal lpClass As String, ByVal dwOptions As Long, ByVal samDesired As Long, ByRef lpSecurityAttributes As SECURITY_ATTRIBUTES, ByRef phkResult As Long, ByRef lpdwDisposition As Long) As Long
Public Declare Function RegOpenKeyEx Lib "advapi32" Alias "RegOpenKeyExA" (ByVal hkey As Long, ByVal lpSubKey As String, ByVal ulOptions As Long, ByVal samDesired As Long, ByRef phkResult As Long) As Long
Public Declare Function RegQueryValueEx Lib "advapi32" Alias "RegQueryValueExA" (ByVal hkey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, ByRef lpType As Long, ByVal lpData As String, ByRef lpcbData As Long) As Long
Public Declare Function RegSetValueEx Lib "advapi32" Alias "RegSetValueExA" (ByVal hkey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, ByVal lpData As String, ByVal cbData As Long) As Long
'---------------------------------------------------------------
'- 注册表 Api 常数...
'---------------------------------------------------------------
' Reg Data Types...
Public Const REG_SZ = 1 ' Unicode空终结字符串
Public Const REG_EXPAND_SZ = 2 ' Unicode空终结字符串
Public Const REG_DWORD = 4 ' 32-bit 数字
Public Const REG_BINARY = 3
' 注册表创建类型值...
Public Const REG_OPTION_NON_VOLATILE = 0 ' 当系统重新启动时,关键字被保留
' 注册表关键字安全选项...
Public Const READ_CONTROL = &H20000
Public Const KEY_QUERY_VALUE = &H1
Public Const KEY_SET_VALUE = &H2
Public Const KEY_CREATE_SUB_KEY = &H4
Public Const KEY_ENUMERATE_SUB_KEYS = &H8
Public Const KEY_NOTIFY = &H10
Public Const KEY_CREATE_LINK = &H20
Public Const KEY_READ = KEY_QUERY_VALUE + KEY_ENUMERATE_SUB_KEYS + KEY_NOTIFY + READ_CONTROL
Public Const KEY_WRITE = KEY_SET_VALUE + KEY_CREATE_SUB_KEY + READ_CONTROL
Public Const KEY_EXECUTE = KEY_READ
Public Const KEY_ALL_ACCESS = KEY_QUERY_VALUE + KEY_SET_VALUE + _
KEY_CREATE_SUB_KEY + KEY_ENUMERATE_SUB_KEYS + _
KEY_NOTIFY + KEY_CREATE_LINK + READ_CONTROL
' 注册表关键字根类型...
Public Const HKEY_CLASSES_ROOT = &H80000000
Public Const HKEY_CURRENT_USER = &H80000001
Public Const HKEY_LOCAL_MACHINE = &H80000002
Public Const HKEY_USERS = &H80000003
Public Const HKEY_PERFORMANCE_DATA = &H80000004
' 返回值...
Public Const ERROR_NONE = 0
Public Const ERROR_BADKEY = 2
Public Const ERROR_ACCESS_DENIED = 8
Public Const ERROR_SUCCESS = 0
'---------------------------------------------------------------
'- 注册表安全属性类型...
'---------------------------------------------------------------
Private Type SECURITY_ATTRIBUTES
nLength As Long
lpSecurityDescriptor As Long
bInheritHandle As Boolean
End Type
'-------------------------------------------------------------------------------------------------
'本函数在注册表中创建新的项及键值
'sample usage - Debug.Print UpodateKey(HKEY_CLASSES_ROOT, "keyname", "newvalue")
'-------------------------------------------------------------------------------------------------
Public Function UpdateKey(KeyRoot As Long, KeyName As String, SubKeyName As String, SubReg As Long, SubKeyValue As String, IngNumber As Long) As Long
Dim rc As Long ' 返回代码
Dim hkey As Long ' 处理一个注册表关键字
Dim hDepth As Long '
Dim lpAttr As SECURITY_ATTRIBUTES ' 注册表安全类型
lpAttr.nLength = 50 ' 设置安全属性为缺省值...
lpAttr.lpSecurityDescriptor = 0 ' ...
lpAttr.bInheritHandle = True ' ...
'------------------------------------------------------------
'- 创建/打开注册表关键字...
'创建/打开//KeyRoot//KeyName
' 错误处理...
'------------------------------------------------------------
rc = RegCreateKeyEx(KeyRoot, KeyName, 0, "", 0, KEY_WRITE, lpAttr, hkey, hDepth)
If (rc <> ERROR_SUCCESS) Then GoTo CreateKeyError
'------------------------------------------------------------
'- 创建/修改关键字值...
' 要让RegSetValueEx() 工作需要输入一个空格...
' 创建/修改关键字值
'- 关闭注册表关键字...
'------------------------------------------------------------
Select Case SubReg
Case REG_SZ
rc = RegSetValueEx(hkey, SubKeyName, 0, SubReg, SubKeyValue, IngNumber)
If (rc <> ERROR_SUCCESS) Then GoTo CreateKeyError
End Select
rc = RegCloseKey(hkey) ' 退出
Exit Function ' 错误处理
CreateKeyError:
UpdateKey = False ' 设置错误返回代码
rc = RegCloseKey(hkey) ' 试图关闭关键字
End Function
'-------------------------------------------------------------------------------------------------
'本函数在注册表中读取键值
'sample usage - Debug.Print GetKeyValue(HKEY_CLASSES_ROOT, "COMCTL.ListviewCtrl.1\CLSID", "")
'-------------------------------------------------------------------------------------------------
Public Function GetKeyValue(KeyRoot As Long, KeyName As String, SubKeyRef As String) As String
Dim I As Long ' 循环计数器
Dim rc As Long ' 返回代码
Dim hkey As Long ' 处理打开的注册表关键字
Dim hDepth As Long '
Dim sKeyVal As String
Dim lKeyValType As Long ' 注册表关键字数据类型
Dim tmpVal As String ' 注册表关键字的临时存储器
Dim KeyValSize As Long ' 注册表关键字变量尺寸
' 在 KeyRoot {HKEY_LOCAL_MACHINE...} 下打开注册表关键字
'------------------------------------------------------------
rc = RegOpenKeyEx(KeyRoot, KeyName, 0, KEY_ALL_ACCESS, hkey) ' 打开注册表关键字
If (rc <> ERROR_SUCCESS) Then GoTo GetKeyError ' 处理错误...
tmpVal = String$(1024, 0) ' 分配变量空间
KeyValSize = 1024 ' 标记变量尺寸
'------------------------------------------------------------
' 检索注册表关键字的值...
'------------------------------------------------------------
rc = RegQueryValueEx(hkey, SubKeyRef, 0, _
lKeyValType, tmpVal, KeyValSize) ' 获得/创建关键字的值
If (rc <> ERROR_SUCCESS) Then GoTo GetKeyError ' 错误处理
tmpVal = Left$(tmpVal, InStr(tmpVal, Chr(0)) - 1)
'------------------------------------------------------------
' 决定关键字值的转换类型...
'------------------------------------------------------------
Select Case lKeyValType ' 搜索数据类型...
Case REG_SZ, REG_EXPAND_SZ ' 字符串注册表关键字数据类型
sKeyVal = tmpVal ' 复制字符串的值
Case REG_DWORD ' 四字节注册表关键字数据类型
For I = Len(tmpVal) To 1 Step -1 ' 转换每一位
sKeyVal = sKeyVal + Hex(Asc(Mid(tmpVal, I, 1))) ' 一个字符一个字符地生成值。
Next
sKeyVal = Format$("&h" + sKeyVal) ' 转换四字节为字符串
End Select
GetKeyValue = sKeyVal ' 返回值
rc = RegCloseKey(hkey) ' 关闭注册表关键字
Exit Function ' 退出
GetKeyError: ' 错误发生过后进行清除...
GetKeyValue = vbNullString ' 设置返回值为空字符串
rc = RegCloseKey(hkey) ' 关闭注册表关键字
End Function |
|