Jump to content
Sign in to follow this  
turkis

[VB.net] Escrever e ler ficheiros DXF

Recommended Posts

turkis

ora boas...

no site da AutoDesk encontrei isto...pelo menos para mim é bastante útil, e tb o deverá ser para quem faz programas relacionados com desenho vectorial.

AutoCAD DXF (Drawing Interchange Format, ou Drawing Exchange Format) é um formato de ficheiros criado pela AutoDesk, que visa a migração de desenhos vectoriais entre vários programas de desenho. Foi crado em 1983, logo na primiraedição do AutoCad, e permanece até aos dias de hoje.

A AutoDesk, muito atenciosamente,  oferece o código para ler e escrever ficheiros em dxf.

Para ler ficheiros:

Function ReadDXF( _ 
        ByVal dxfFile As String, ByVal strSection As String, _ 
        ByVal strObject As String, ByVal strCodeList As String) 
    Dim tmpCode, lastObj As String 
    Open dxfFile For Input As #1 
    ' Get the first code/value pair 
    codes = ReadCodes 
    ' Loop through the whole file until the "EOF" line 
    While codes(1) <> "EOF" 
        ' If the group code is '0' and the value is 'SECTION' .. 
        If codes(0) = "0" And codes(1) = "SECTION" Then 
            ' This must be a new section, so get the next 
            ' code/value pair. 
            codes = ReadCodes() 
            ' If this section is the right one .. 
            If codes(1) = strSection Then 
                ' Get the next code/value pair and .. 
                codes = ReadCodes 
                ' Loop through this section until the 'ENDSEC' 
                While codes(1) <> "ENDSEC" 
                    ' While in a section, all '0' codes indicate 
                    ' an object. If you find a '0' store the 
                    ' object name for future use. 
                    If codes(0) = "0" Then lastObj = codes(1) 
                    ' If this object is one you're interested in 
                    If lastObj = strObject Then 
                        ' Surround the code with commas 
                        tmpCode = "," & codes(0) & "," 
                        ' If this code is in the list of codes .. 
                        If InStr(strCodeList, tmpCode) Then 
                            ' Append the return value. 
                            ReadDXF = ReadDXF & _ 
                                codes(0) & "=" & codes(1) & vbCrLf 
                        End If 
                    End If 
                    ' Read another code/value pair 
                    codes = ReadCodes 
                Wend 
            End If 
        Else 
            codes = ReadCodes 
        End If 
    Wend 
    Close #1 
End Function 
' ReadCodes reads two lines from an open file and returns a two item 
' array, a group code and its value. As long as a DXF file is read 
' two lines at a time, all should be fine. However, to make your 
' code more reliable, you should add some additional error and 
' sanity checking. 
' 
Function ReadCodes() As Variant 
    Dim codeStr, valStr As String 
    Line Input #1, codeStr 
    Line Input #1, valStr 
    ' Trim the leading and trailing space from the code 
    ReadCodes = Array(Trim(codeStr), valStr) 

End Function 

Para escrever ficheiros DXF

' WriteDXFPolygon creates a minimal DXF file that only contains 
' the ENTITIES section. This subroutine requires five parameters, 
' the DXF file name, the number of sides for the polygon, the X 
' and Y coordinates for the bottom end of the right-most side 
' (it starts in a vertical direction), and the length for each 
' side. Note that because this only requests 2D points, it does 
' not include the Z coordinates (codes 30 and 31). The lines are 
' placed on the layer "Polygon." 
' 
Sub WriteDXFPolygon( _ 
        dxfFile As String, iSides As Integer, _ 
        dblX As Double, dblY As Double, dblLen As Double) 
    Dim i As Integer 
    Dim dblA1, dblA, dblPI, dblNX, dblNY As Double 
    Open dxfFile For Output As #1 
    Print #1, 0 
    Print #1, "SECTION" 
    Print #1, 2 
    Print #1, "ENTITIES" 
    dblPI = Atn(1) * 4 
    dblA1 = (2 * dblPI) / iSides 
    dblA = dblPI / 2 
    For i = 1 To iSides 
        Print #1, 0 
        Print #1, "LINE" 
        Print #1, 8 
        Print #1, "Polygon" 
        Print #1, 10 
        Print #1, dblX 
        Print #1, 20 
        Print #1, dblY 
        dblNX = dblLen * Cos(dblA) + dblX 
        dblNY = dblLen * Sin(dblA) + dblY 
        Print #1, 11 
        Print #1, dblNX 
        Print #1, 21 
        Print #1, dblNY 
        dblX = dblNX 
        dblY = dblNY 
        dblA = dblA + dblA1 
    Next i 
    Print #1, 0 
    Print #1, "ENDSEC" 
    Print #1, 0 
    Print #1, "EOF" 
    Close #1 

End Sub 

Fontes:

http://www.autodesk.com/techpubs/autocad/acad2000/dxf/ascii_dxf_files_dxf_aa.htm

http://en.wikipedia.org/wiki/Dxf

espero que isto seja útil...heheheh

Share this post


Link to post
Share on other sites
jtiagodias

Viva,

Há uns tempos andei a procurar alguma informação e nõ encontrei nada concreto.

Parece-me que isto era o código que eu queria. Parece-me que tens alguma pesquisa feita e, portanto, queria perguntar-te se sabes o código para, com base numa folha de cálculo do excel, alterar cores de linhas (ou hatch) do AutoCAD.

Lá para o inicio do ano vou olhar para isto com atenção para ver se consigo aplicar...

cump.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.