本帖最后由 作者 于 2007-1-12 17:27:19 编辑
以前,我们为了做图库,每一个图块均必须保存为一个图形文件,以便在编程时直接插入选定的图形,这样做出来的程序,图形文件的数量就会很多,因为有时你的图库内容很多。
现在利用ObjectDbx技术可以将这些图块放在一个图形中,做到真正的图库,以下为程序内容:
引用:ObjectDbx 1.0类型库(文件为:c:\program files\AutoCAD 2002\AXDB15.TLB)
插入模块1,输入以下代码:- Public Declare Function GetSaveFileName Lib "comdlg32.dll" Alias "GetSaveFileNameA" _
- (LPOPENFILENAME As OPENFILENAME) As Long
- Public Declare Function GetOpenFileName Lib "comdlg32.dll" Alias "GetOpenFileNameA" _
- (pOpenfilename As OPENFILENAME) As Long
- Public Const OFN_PATHMUSTEXIST = &H800
- Public Const OFN_FILEMUSTEXIST = &H1000
- Public Const OFN_HIDEREADONLY = &H4 '隐蔽只读复选框
- Public Type OPENFILENAME
- lStructSize As Long
- hwndOwner As Long '拥有对话框的窗口
- hInstance As Long
- lpstrFilter As String '装载文件过滤器的缓冲区
- lpstrCustomFilter As String
- nMaxCustFilter As Long
- nFilterIndex As Long
- lpstrFile As String
- nMaxFile As Long
- lpstrFileTitle As String
- nMaxFileTitle As Long
- lpstrInitialDir As String
- lpstrTitle As String '对话框的标题
- flags As Long
- nFileOffset As Integer
- nFileExtension As Integer
- lpstrDefExt As String
- lCustData As Long
- lpfnHook As Long
- lpTemplateName As String
- End Type
- Function GetFile(strTitle As String, strFilter As String, Optional strIniDir As String) As String
- On Error Resume Next
- Dim FileName As String
- Dim OFileBox As OPENFILENAME
- With OFileBox
- .lpstrTitle = strTitle '对话框标题
- .lpstrInitialDir = strIniDir '初始目录
- .lStructSize = Len(OFileBox)
- .hwndOwner = ThisDrawing.HWND
- .flags = OFN_PATHMUSTEXIST Or OFN_FILEMUSTEXIST Or OFN_HIDEREADONLY
- .lpstrFile = String$(255, 0)
- .nMaxFile = 255
- .lpstrFileTitle = String$(255, 0)
- .nMaxFileTitle = 255
- .lpstrFilter = strFilter '过滤器
- .nFilterIndex = 1
- End With
- lntFile = GetOpenFileName(OFileBox) '执行打开对话框
- If lntFile <> 0 Then
- FileName = Left(OFileBox.lpstrFile, InStr(OFileBox.lpstrFile, vbNullChar) - 1)
- GetFile = FileName
- Else
- GetFile = ""
- End If
- End Function
插入窗体userform1,自上而下插入以下控件:
标签(Label1)
文本框(TextBox1)
命令按钮(CommandButton1)
标签(Label2)
组合框(ComboBox1)
命令按钮(CommandButton2) 命令按钮(CommandButton3)
然后在窗体的代码窗中输入以下代码:- Option Explicit
- Dim objDbx As AxDbDocument
- Dim elem As Object
- Dim blkName As String
- Dim dwgName As String
- Dim blkObj(0) As Object
- Dim pnt As Variant
- Private Sub CommandButton1_Click()
- Me.TextBox1 = GetFile("打开图形", "图形文件(*.dwg)" & vbNullChar & "*.dwg")
- End Sub
- Private Sub CommandButton2_Click()
- blkName = Me.ComboBox1.SelText
- dwgName = Me.TextBox1.Value
- Me.Hide
-
- pnt = ThisDrawing.Utility.GetPoint(, "选择插入点:")
- Set blkObj(0) = objDbx.Blocks(blkName)
- objDbx.CopyObjects blkObj, ThisDrawing.ModelSpace
- ThisDrawing.ModelSpace.InsertBlock pnt, blkName, 1, 1, 1, 0
- Unload UserForm1
- Set elem = Nothing
- Set objDbx = Nothing
-
- End Sub
- Private Sub CommandButton3_Click()
- Unload UserForm1
- Set elem = Nothing
- Set objDbx = Nothing
- End Sub
- Private Sub TextBox1_Change()
- If Dir(Me.TextBox1.Value) <> "" Then
- objDbx.Open Me.TextBox1.Value
- For Each elem In objDbx.Blocks
- If Left(elem.Name, 1) <> "*" Then
- Me.ComboBox1.AddItem elem.Name
- End If
- Next
- End If
-
- End Sub
- Private Sub UserForm_Initialize()
- Set objDbx = GetInterfaceObject("ObjectDBX.AxDbDocument")
- Me.CommandButton1.Caption = "浏览"
- Me.CommandButton2.Caption = "插入"
- Me.CommandButton3.Caption = "取消"
- Me.Label1.Caption = "选择图形:"
- Me.Label2.Caption = "选择图块:"
- Me.Caption = "插入外部图形中的图块示例"
- End Sub
然后在ThisDrawing代码窗中输入以下代码:- Sub InsBlk()
- Load UserForm1
- UserForm1.Show
- End Sub
这样就可以试试你的程序了。
如果觉得麻烦,这个我已经打包成一个文件,大家拿去试试吧:
下载的文件已经改为最新无错版本,可用于2000-2004版本 |