22 load_geometry(filename; element_type=Float64)
33
44Load file and return corresponding type for [`ComplexShape`](@ref).
5- Supported file formats are `.stl` and `.asc`.
5+ Supported file formats are `.stl`, `.asc` and `dxf`.
6+ For comprehensive information about the supported file formats, refer to the documentation at
7+ [Read geometries from file](@ref read_geometries_from_file).
68
79# Arguments
810- `filename`: Name of the file to be loaded.
@@ -17,10 +19,12 @@ function load_geometry(filename; element_type=Float64)
1719
1820 if file_extension == " .asc"
1921 geometry = load_ascii (filename; ELTYPE, skipstart= 1 )
22+ elseif file_extension == " .dxf"
23+ geometry = load_dxf (filename; ELTYPE)
2024 elseif file_extension == " .stl"
2125 geometry = load (FileIO. query (filename); ELTYPE)
2226 else
23- throw (ArgumentError (" Only `.stl` and `.asc ` files are supported (yet)." ))
27+ throw (ArgumentError (" Only `.stl`, `.asc` and `.dxf ` files are supported (yet)." ))
2428 end
2529
2630 return geometry
@@ -35,6 +39,97 @@ function load_ascii(filename; ELTYPE=Float64, skipstart=1)
3539 return Polygon (copy (points' ))
3640end
3741
42+ function load_dxf (filename; ELTYPE= Float64)
43+ points = Tuple{ELTYPE, ELTYPE}[]
44+
45+ load_dxf! (points, filename)
46+
47+ return Polygon (stack (points))
48+ end
49+
50+ function load_dxf! (points:: Vector{Tuple{T, T}} , filename) where {T}
51+ open (filename) do io
52+ lines = readlines (io)
53+
54+ # Check if the DXF file contains line entities ("LINE") or polyline entities ("POLYLINE")
55+ idx_first_line = findfirst (x -> strip (x) == " LINE" , lines)
56+ idx_first_polyline = findfirst (x -> strip (x) == " POLYLINE" , lines)
57+
58+ if ! isnothing (idx_first_line) && isnothing (idx_first_polyline)
59+ # The file contains only simple line entities ("LINE")
60+ i = idx_first_line
61+
62+ while i <= length (lines)
63+ if strip (lines[i]) == " LINE"
64+ if idx_first_line == i
65+ # For a polygon, we only need to store the start point of the first line entity.
66+ # For subsequent lines, the end point of the previous edge is the start point of the current edge,
67+ # so storing all start points would result in duplicate vertices.
68+ # Therefore, we only push the start point for the very first line.
69+
70+ # Search for the coordinates of the start point:
71+ # Group codes "10", "20", "30" represent x, y, z of the start point
72+ while i <= length (lines) && strip (lines[i]) != " 10"
73+ i += 1
74+ end
75+ x1 = parse (T, strip (lines[i + 1 ]))
76+ @assert strip (lines[i + 2 ]) == " 20"
77+ y1 = parse (T, strip (lines[i + 3 ]))
78+
79+ push! (points, (x1, y1))
80+ end
81+
82+ # Search for the end point coordinates:
83+ # Group codes "11", "21", "31" represent x, y, z of the end point
84+ while i <= length (lines) && strip (lines[i]) != " 11"
85+ i += 1
86+ end
87+ x2 = parse (T, strip (lines[i + 1 ]))
88+ @assert strip (lines[i + 2 ]) == " 21"
89+ y2 = parse (T, strip (lines[i + 3 ]))
90+
91+ # Add end point of the line to the point list
92+ push! (points, (x2, y2))
93+ end
94+ i += 1
95+ end
96+
97+ elseif isnothing (idx_first_line) && ! isnothing (idx_first_polyline)
98+ # The file contains only polyline entities ("POLYLINE")
99+ i = idx_first_polyline
100+
101+ while i <= length (lines)
102+ line = strip (lines[i])
103+ if line == " VERTEX"
104+ # Search for the coordinates of the current vertex:
105+ # Group codes "10", "20", "30" represent x, y, z of the vertex
106+ while i <= length (lines) && strip (lines[i]) != " 10"
107+ i += 1
108+ end
109+ x = parse (T, strip (lines[i + 1 ]))
110+ @assert strip (lines[i + 2 ]) == " 20"
111+ y = parse (T, strip (lines[i + 3 ]))
112+
113+ # Add the vertex to the point list
114+ push! (points, (x, y))
115+
116+ elseif line == " SEQEND"
117+ # End of the polyline reached
118+ break
119+ end
120+ i += 1
121+ end
122+ else
123+ throw (ArgumentError (" This entity type is not supported. Only 'LINE' OR 'POLYLINE' are allowed." ))
124+ end
125+ end
126+
127+ # Remove duplicate points from the list
128+ unique (points)
129+
130+ return points
131+ end
132+
38133# FileIO.jl docs:
39134# https://juliaio.github.io/FileIO.jl/stable/implementing/#All-at-once-I/O:-implementing-load-and-save
40135function load (fn:: FileIO.File{FileIO.format"STL_BINARY"} ; element_types... )
0 commit comments