回望南山
记忆痕迹可以鲜明, 回望往事如数家珍——
posts - 177,  comments - 54,  trackbacks - 0

This article first appeared in the September 2002 issue of "CAD Encoding."

In this article, we are going to have a look at extracting information from AutoCAD and displaying the results in an HTML file.
Just to be clever, and to keep everybody happy, we'll be writing the coding using VBA, AutoLISP, Visual Lisp and last but not least VB. Each of the routines will be slightly different to give you an idea of the capabilities of each language.

Let's have a look at VBA first.
This wee routine will extract all the layer names contained within a drawing, create an HTML file on the fly, and list all the layer names :







Insert a Userform and add a listbox and two buttons, retaining their default names :

  • UserForm1

  • ListBox1

  • CommandButton1

  • Commandbutton2

Add the following coding to the General Declarations section :

(I recommend you download the source coding for this project as word-wrapping can cause major problems. The download link is at the end of Page IV.)

Option Explicit
            '-----------------------------------------------------
            Public AllLayers As AcadLayers
            '-----------------------------------------------------
            
            Private Sub CommandButton1_Click()
            Dim nFile As Integer
            Dim Layer As AcadLayer
            Dim dName As String
            Dim mylen As Integer
            Dim dPrefix As String
            'if there is an error
            On Error GoTo Err_Control
            'hide the dialog
            Me.hide
            'get the drawing name
            dName = ThisDrawing.GetVariable("DWGNAME")
            'get the drawing name
            dPrefix = ThisDrawing.GetVariable("DWGPREFIX")
            'get the length of the filename
            mylen = Len(dName)
            'subtract the .DWG Extension
            mylen = mylen - 4
            'retrieve the drawing name
            dName = Left(dName, mylen)
            'get the next free file number
            nFile = FreeFile
            'open the html file to write to
            Open dPrefix & dName & " - AllLayers.htm" _
            For Output As #nFile
            'write the header
            Print #nFile, "<html><head><title> _
Cad Encoding - Layers to HTML</title></head>
            <body><h3>Layers - Drawing No : " & dPrefix & _
            dName & "</h3><hr>"
            'process each layer
            For Each Layer In AllLayers
            'write to the HTML file
            Print #nFile, "Layer Name = " & Layer.Name & "<p>"
            'process next layer
            Next
            'write the footer
            Print #nFile, "<hr><h3>Created by CAD Encoding</h3> _
            </body></html>"
            'close the file
            Close #nFile
            'display message box
            MsgBox ("Layers written to HTML File :" & vbCr & _
            dPrefix & dName &" - AllLayers.htm"), , "Layers to HTML"
            'error control
            Exit_Here:
            Exit Sub
            Err_Control:
            MsgBox Err.Description
            Resume Exit_Here
            End Sub
'----------------------------------------------------
            Private Sub CommandButton2_Click()
            End
            End Sub
'---------------------------------------------------
            Private Sub UserForm_Initialize()
            Dim Layer As AcadLayer
            'if there is an error
            On Error GoTo Err_Control
            'get the layers collection
            Set AllLayers = ThisDrawing.Layers
            'process each layer
            For Each Layer In AllLayers
            'display each layer name in the list box
            ListBox1.AddItem Layer.Name
            'process next layer
            Next
            'error control
            Exit_Here:
            Exit Sub
            Err_Control:
            MsgBox Err.Description
            Resume Exit_Here
            End Sub
            

Now insert a new Module and add this :

Sub AllLayers()
            UserForm1.Show
            End Sub
            

Save this as "AllLayers.dvb."
Now open any drawing and run the macro.
The List Box should be populated with the drawings Layer names.
Select the "HTML" button. A dialog should appear informing you that an HTML file has been created. Navigate to the directory containing the HTML file and open it.
A list of all the Layers in your drawing should have been created.


 

On the next page we'll have a look at something similar, but this time using AutoLISP.

 

 

posted on 2008-03-29 21:03 深藏记忆 阅读(248) 评论(0)  编辑  收藏 所属分类: 舶来备忘转载Vlisp

飘过是缘,相识最真

订阅到抓虾
google reader
gougou


点击这里给我发消息


<2008年3月>
2425262728291
2345678
9101112131415
16171819202122
23242526272829
303112345

常用链接

留言簿(5)

随笔分类

随笔档案

文章分类

文章档案

相册

收藏夹

八面来息

天天充电

同行者

积分与排名

  • 积分 - 58602
  • 排名 - 61

最新评论

阅读排行榜

评论排行榜