[求助]如何用VBA把CAD中画的图保存成块和写块
<p>如何用VBA把CAD中画的图保存成块和写块:</p><p>我想编一个窗体可以实现,把CAD中画好的图变成图块,保存起来,以后每次对可以直接用.很急那位高手帮帮我!先在此谢谢了.!!!!</p> 要先创建块,然后再block.中添加对象。 <p></p><p>---------------------------------------------</p><p>VERSION 5.00<br/>Begin VB.Form MakeNewBlockForm <br/> BorderStyle = 3 'Fixed Dialog<br/> Caption = "创建新图块"<br/> ClientHeight = 3585<br/> ClientLeft = 6060<br/> ClientTop = 2085<br/> ClientWidth = 4860<br/> Icon = "MakeNewBlockForm.frx":0000<br/> KeyPreview = -1 'True<br/> LinkTopic = "Form1"<br/> MaxButton = 0 'False<br/> MinButton = 0 'False<br/> ScaleHeight = 3585<br/> ScaleWidth = 4860<br/> ShowInTaskbar = 0 'False<br/> Begin VB.PictureBox Picture1 <br/> Appearance = 0 'Flat<br/> BorderStyle = 0 'None<br/> ForeColor = &H80000008&<br/> Height = 2175<br/> Left = 2400<br/> ScaleHeight = 2175<br/> ScaleWidth = 2385<br/> TabIndex = 15<br/> Top = 810<br/> Width = 2385<br/> Begin VB.Frame Frame1 <br/> Caption = "对象"<br/> Height = 2085<br/> Left = 60<br/> TabIndex = 16<br/> Top = 30<br/> Width = 2265<br/> Begin VB.CommandButton CmdSelectObjects <br/> Height = 435<br/> Left = 240<br/> Picture = "MakeNewBlockForm.frx":548A<br/> Style = 1 'Graphical<br/> TabIndex = 20<br/> Top = 300<br/> Width = 465<br/> End<br/> Begin VB.OptionButton OptNoChange <br/> Caption = "保留"<br/> Height = 315<br/> Left = 300<br/> TabIndex = 19<br/> Top = 960<br/> Width = 1605<br/> End<br/> Begin VB.OptionButton OptBlock <br/> Caption = "转化为块"<br/> Height = 315<br/> Left = 300<br/> TabIndex = 18<br/> Top = 1290<br/> Value = -1 'True<br/> Width = 1605<br/> End<br/> Begin VB.OptionButton OptDelect <br/> Caption = "删除"<br/> Height = 315<br/> Left = 300<br/> TabIndex = 17<br/> Top = 1650<br/> Width = 1605<br/> End<br/> Begin VB.Label SkinLabel3 <br/> Caption = "选择对象(&T):"<br/> Height = 225<br/> Left = 780<br/> TabIndex = 21<br/> Top = 405<br/> Width = 1155<br/> End<br/> End<br/> End<br/> Begin VB.CommandButton CmdOK <br/> Caption = "确定"<br/> Default = -1 'True<br/> Height = 495<br/> Left = 1230<br/> TabIndex = 14<br/> Top = 3000<br/> Width = 1065<br/> End<br/> Begin VB.CommandButton CmdCancle <br/> Caption = "取消"<br/> Height = 495<br/> Left = 2940<br/> TabIndex = 13<br/> Top = 3000<br/> Width = 1065<br/> End<br/> Begin VB.Frame Frame2 <br/> Caption = "基点"<br/> Height = 2085<br/> Left = 90<br/> TabIndex = 4<br/> Top = 840<br/> Width = 2265<br/> Begin VB.TextBox Text1 <br/> Enabled = 0 'False<br/> Height = 270<br/> Left = 450<br/> TabIndex = 11<br/> Text = "0"<br/> ToolTipText = "插入点的Y坐标"<br/> Top = 1620<br/> Width = 1485<br/> End<br/> Begin VB.TextBox Text2 <br/> Enabled = 0 'False<br/> Height = 270<br/> Left = 450<br/> TabIndex = 8<br/> Text = "0"<br/> ToolTipText = "插入点的X坐标"<br/> Top = 870<br/> Width = 1485<br/> End<br/> Begin VB.TextBox Text3 <br/> Enabled = 0 'False<br/> Height = 270<br/> Left = 450<br/> TabIndex = 7<br/> Text = "0"<br/> ToolTipText = "插入点的Y坐标"<br/> Top = 1260<br/> Width = 1485<br/> End<br/> Begin VB.CommandButton CmdPickPoint <br/> Height = 435<br/> Left = 240<br/> Picture = "MakeNewBlockForm.frx":5B8C<br/> Style = 1 'Graphical<br/> TabIndex = 5<br/> Top = 300<br/> Width = 465<br/> End<br/> Begin VB.Label SkinLabel4 <br/> Caption = "拾取点(&K):"<br/> Height = 225<br/> Left = 780<br/> TabIndex = 6<br/> Top = 405<br/> Width = 915<br/> End<br/> Begin VB.Label SkinLabel16 <br/> Caption = "X:"<br/> Height = 165<br/> Left = 240<br/> TabIndex = 9<br/> Top = 900<br/> Width = 225<br/> End<br/> Begin VB.Label SkinLabel17 <br/> Caption = "Y:"<br/> Height = 165<br/> Left = 240<br/> TabIndex = 10<br/> Top = 1305<br/> Width = 225<br/> End<br/> Begin VB.Label SkinLabel5 <br/> Caption = "Z:"<br/> Height = 165<br/> Left = 240<br/> TabIndex = 12<br/> Top = 1665<br/> Width = 225<br/> End<br/> End<br/> Begin VB.TextBox TxtBlockName <br/> Height = 315<br/> Left = 1110<br/> TabIndex = 1<br/> Top = 60<br/> Width = 3615<br/> End<br/> Begin VB.ComboBox ComFolderName <br/> Height = 300<br/> Left = 1110<br/> Style = 2 'Dropdown List<br/> TabIndex = 0<br/> Top = 450<br/> Width = 3615<br/> End<br/> Begin VB.Label SkinLabel1 <br/> Caption = "名称:"<br/> Height = 225<br/> Left = 90<br/> TabIndex = 2<br/> Top = 90<br/> Width = 915<br/> End<br/> Begin VB.Label SkinLabel2 <br/> Caption = "存放目录:"<br/> Height = 225<br/> Left = 90<br/> TabIndex = 3<br/> Top = 480<br/> Width = 915<br/> End<br/>End<br/>Attribute VB_Name = "MakeNewBlockForm"<br/>Attribute VB_GlobalNameSpace = False<br/>Attribute VB_Creatable = False<br/>Attribute VB_PredeclaredId = True<br/>Attribute VB_Exposed = False<br/>Option Explicit</p><p>Dim SSet As Object<br/>Dim ptBase As Variant<br/>Dim strPath As String</p><p>Private Sub CmdCancle_Click()<br/> Unload Me<br/>End Sub</p><p>Private Sub CmdOK_Click()<br/> If Trim(TxtBlockName.Text) = "" Then<br/> If MsgBox("请输入图块名称", vbCritical + vbOKOnly, AppName) = vbOK Then Exit Sub<br/> End If<br/> <br/> Me.Hide<br/> <br/> ' 提示用户输入块定义的名称<br/> 'Dim strName As String<br/> 'strName = ThisDrawing.Utility.GetString(True, vbCrLf & "输入块的名称:")<br/> <br/> ' 获得相对路径<br/> strPath = App.Path & "\BlockLib\" & ComFolderName & "\" & Trim(TxtBlockName) & ".dwg"<br/> 'strPath = App.Path & "\BlockLib\" & Trim(TxtBlockName) & ".dwg"<br/> <br/> ' 将所有的实体移动到原点附近,确保块定义的插入点无误<br/> 'Dim ptOrigin(0 To 2) As Double<br/> 'ptOrigin(0) = 0: ptOrigin(1) = 0: ptOrigin(2) = 0<br/> 'Dim Ent As OBJECT<br/> 'For Each Ent In SSet<br/> ' Ent.Move ptBase, ptOrigin<br/> 'Next<br/> <br/> ' 将块定义导出<br/> 'ThisDrawing.Wblock strPath, SSet ' 使用此方法创建的块没有浏览缩略图<br/> <br/> ThisDrawing.SetVariable "FILEDIA", 0<br/> <br/> ' 将块定义导出<br/> ThisDrawing.SendCommand "-WBLOCK" & vbLf & strPath & vbLf & vbLf & axPoint2lspPoint(ptBase) & vbLf & axSSet2lspEnts(SSet) & vbLf & vbLf<br/> <br/> Call CmdOKNextCode<br/>End Sub</p><p>Private Sub CmdOKNextCode()</p><p> ThisDrawing.SetVariable "FILEDIA", 1<br/> <br/> If OptNoChange.Value Then<br/> 'For Each Ent In SSet<br/> ' Ent.Move ptOrigin, ptBase<br/> 'Next<br/> End If<br/> <br/> If OptDelect.Value Then<br/> ' 删除图形中绘制的所有对象<br/> SSet.Delete<br/> End If<br/> <br/> If OptBlock.Value Then<br/> <br/> ' 删除图形中绘制的所有对象<br/> 'SSet.Erase<br/> SSet.Delete<br/> <br/> Dim ObjBlock As Object<br/> Set ObjBlock = ThisDrawing.ModelSpace.InsertBlock(ptBase, strPath, 1, 1, 1, 0)<br/> End If<br/> <br/> Set SSet = Nothing<br/> <br/> Unload Me<br/>End Sub</p><p>Private Sub Command1_Click()<br/> Unload Me<br/>End Sub</p><p>Private Sub CmdPickPoint_Click()<br/> Me.Hide<br/> <br/> ' 提示用户输入块定义的基点<br/> ptBase = ThisDrawing.Utility.GetPoint(, vbCrLf & "拾取基点:")<br/> <br/> Me.Show<br/>End Sub</p><p>Private Sub CmdSelectObjects_Click()<br/> Me.Hide</p><p> Set SSet = GetSelectionSetObject<br/> <br/> Me.Show<br/>End Sub</p><p>Private Sub Form_Load()<br/> On Error GoTo ErrHandle<br/> <br/> Dim FSO As Object<br/> Dim Fols As Object<br/> Dim Fol As Object<br/> <br/> Set FSO = CreateObject("Scripting.FileSystemObject")<br/> Set Fols = FSO.GetFolder(App.Path & "\BlockLib\")</p><p> For Each Fol In Fols.SubFolders<br/> ComFolderName.AddItem Fol.Name<br/> Next<br/> <br/> ComFolderName.ListIndex = 0<br/> <br/> If Not GetAutoCADApplication(Me) Then CloseSubFroms: Exit Sub<br/> <br/> Call MoveXWindowsCenter(Me)<br/> <br/> Set FSO = Nothing<br/> Set Fols = Nothing<br/> <br/> Exit Sub<br/>ErrHandle:<br/> MsgBox Err.Description, vbCritical + vbOKOnly, AppName<br/> Err.Clear<br/> Unload Me<br/>End Sub</p><p>Private Sub Form_KeyPress(KeyAscii As Integer)<br/> If KeyAscii = 27 Then '用户按了ESC键,退出<br/> Unload Me<br/> End If<br/>End Sub</p><p>Private Sub TextFocus(ctl As Control) '定义过程<br/> ctl.SelStart = 0<br/> ctl.SelLength = Len(ctl.Text)<br/>End Sub</p><p>Private Sub Text1_GotFocus()<br/> TextFocus Text1 '过程调用<br/>End Sub</p><p>Public Property Set Application(ByVal vNewApplication As Object)<br/> Set AcadApp = vNewApplication<br/>End Property</p><p>---------------------------------------------</p><p></p>
页:
[1]