Extract points and lines from AutoCad dwg/dxf file to Excel

Extract points and lines from AutoCad dwg/dxf file to Excel

josileissu
Participant Participant
2,798 Views
2 Replies
Message 1 of 3

Extract points and lines from AutoCad dwg/dxf file to Excel

josileissu
Participant
Participant

Hi, 

So i will go to objective of my post:

I need to extract lines and the points of them with a organizated process from dwg and dxf file. I'm using the next code for dxf file, but i doesn't like in how is it appear. If has sub using collections to do this i will like so much. Obvious if it is better than using arrays... But the code doesn't extract the information like the image attached, the points with a identification, like 1, 2, ..., n, and your values. And the lines in other side, with the reference points (no x,y,z at begin and x,y,z at end. i want only put start like 1 and end like 2...), description...

 

I hope anyone helps me. And after will help so more people 😉

 

based code from dxf reference autocad.

Sub RETIRARBARRAS()
Dim scódtemp As String, sarquivodxf As String
Dim códigos As Variant, ibarra As Integer 'aqui iremos criar uma lista para receber tais barras
Dim slayer As String, pontosxyz As Variant
Dim sval As String, sdir As String
ibarra = 0
sarquivodxf = Application.GetOpenFilename()

Open sarquivodxf For Input As #1
códigos = LERCÓDIGOS 'este vetor conterá na posição zero o valor e na posição um o que é, por exemplo Vetor(0) = "0" e Vetor (1) = "SECTION"
While códigos(1) <> "EOF"
If códigos(0) = "0" And códigos(1) = "SECTION" Then
códigos = LERCÓDIGOS
If códigos(1) = "ENTITIES" Then 'aqui precisaremos verificar a possibilidade de pegar também os pontos de polylines...
códigos = LERCÓDIGOS
While códigos(1) <> "ENDSEC"
If códigos(1) = "LINE" Then
códigos = LERCÓDIGOS
While códigos(0) <> "8"
códigos = LERCÓDIGOS
Wend
If códigos(0) = "8" Then 'aqui dá para impor a condição de layer
slayer = códigos(1)
ibarra = ibarra + 1
Plan1.Cells(ibarra + 1, 1) = ibarra
Plan1.Cells(ibarra + 1, 8) = slayer
'já sabemos que é uma linha e que é do layer tal, agora precisamos das coordenadas iniciais e finais
códigos = LERCÓDIGOS
While códigos(0) <> "10"
códigos = LERCÓDIGOS
Wend
ReDim pontosxyz(1 To 6) As Variant
pontosxyz(1) = CDbl(Trim(Replace(códigos(1), ".", ",")))
For i = 2 To 6
Line Input #1, sdir
Line Input #1, sval
pontosxyz(i) = CDbl(Trim(Replace(sval, ".", ",")))
Next i
'coordenadas é uma função que recebe do arquivo #1 os parâmetros em sequência xi, yi, zi, xf, yf, zf;
For i = 1 To 6
Plan1.Cells(ibarra + 1, i + 1) = pontosxyz(i)
Next i
End If
End If
códigos = LERCÓDIGOS
Wend
End If
End If
códigos = LERCÓDIGOS
Wend
End Sub

 

Function LERCÓDIGOS() As Variant
Dim sCód, sval As String
'#1 é o nome temporário do arquivo que está sendo lido.
Line Input #1, sCód
Line Input #1, sval
'trim tira os espaços e encabeça os valores
LERCÓDIGOS = Array(Trim(sCód), sval) 'este vetor conterá na posição zero o valor e na posição um o que é, por exemplo Vetor(0) = "0" e Vetor (1) = "SECTION"
End Function

0 Likes
Accepted solutions (2)
2,799 Views
2 Replies
Replies (2)
Message 2 of 3

Alfred.NESWADBA
Consultant
Consultant
Accepted solution

Hi,

 

it would be easier to have a minimum of documentation in your code or at least to have variables with names that is more easy to read for others ... and so to understand your code.

 

Also as we are here in the forum "AutoCAD VBA" why not simply opening the DXF file in AutoCAD and then go through the entities in the modelspace (or paperspace if needed)?

 

- alfred -

------------------------------------------------------------------------------------
Alfred NESWADBA
ISH-Solutions GmbH / Ingenieur Studio HOLLAUS
www.ish-solutions.at ... blog.ish-solutions.at ... LinkedIn ... CDay 2026
------------------------------------------------------------------------------------

(not an Autodesk consultant)
0 Likes
Message 3 of 3

josileissu
Participant
Participant
Accepted solution

O, well, after a few years passed i comeback. kk'

 

i'm deeped in my job and no leave time to answered this post. So, my excel goes well importing dxf file, just geting lines from dxf file. Is a nice job that the spreadsheet do for me.

 

https://www.dropbox.com/s/1o3s30lvz4o4w3w/Arquivo%20Excel.xlsm?dl=0 the link for download, if anyone wants to see the spreadsheet.

 

 

0 Likes