Please consider trying to download the following .dxf files :
Context :
Every file contains a drawing of a particular type of steel profile used in civil engineering. I added three of them for generality purposes. They come directly from the supplier and consider that they cannot be modified. I would like to import their geometry as a Polygon
to perform further calculations. Let's first import the three geometries in Mathematica :
HEA = Import["...\\HEA 200.dxf"]
UNP = Import["...\\UNP 200.dxf"]
RRW = Import["...\\RRW 100 100 5.dxf"]
All objects are MeshRegion
objects as one can check with Head@HEA
. From these objects, one can extract the points defining the MeshRegion
and convert them to the 2D plane :
Coord = DeleteCases[Extract[MeshPrimitives[HEA, 0], {All, 1}][[All, {1, 2}]], {0., 0.}];
Pts = Map[Point, Coord];
Graphics@Pts
Same for the lines :
lines = Extract[MeshPrimitives[HEA, 1], {All, 1}][[All,All, {1, 2}]];
lines = Map[Line, lines];
Graphics@lines
Problem :
If you plot Coord
with a joined ListPlot
, you will see that the points are unsorted according to the the above Figure which is problematic for defining a Polygon
or a Region
.
ListPlot[Coord, Joined -> True]
I aim to sort the points for obtaining a proper Region. It should work for the three .dxf files. The RRW.dxf contains a hole, and I wonder if one can deal with this added difficulty...
For now I've tried to sort the points with FindShortestTour
but unsuccessful.
Any idea ?
#############################
Update 17.06.2023
I have fixed the access link to the HEA and RRW profile.
After the replies from @cvgmt, @Daniel Huber and @kglr, who are all acknowledged, I'm going to accept @Daniel Huber's answer because it also makes it possible to deal with geometries that contain two closed sub-regions (such as the RRW profile). The other responses remain simpler and interesting for less general cases.
The code originally desired is reduced to the following form:
DXF2DConvert[Profile_] :=
Module[{lin, sel, check1, check2, found, res, Dim},
lin = MeshPrimitives[Profile, 1][[All, 1]];
res = Reap[
While[lin != {}, sel = {lin[[1]]}; lin = Rest[lin];
check1 = sel[[1, 1]];
check2 = sel[[1, -1]];
found = True;
While[found, found = False;
Do[t = lin[[i]];
Switch[lin[[i]], {__, check1}, PrependTo[sel, lin[[i]]];
check1 = sel[[1, 1]]; found = True;
lin[[i]] = Null;, {check2, __}, AppendTo[sel, lin[[i]]];
check2 = sel[[-1, -1]]; found = True;
lin[[i]] = Null;, {check1, __},
PrependTo[sel, Reverse@lin[[i]]];
check1 = sel[[1, 1]]; found = True;
lin[[i]] = Null;, {__, check2},
AppendTo[sel, Reverse@lin[[i]]];
check2 = sel[[-1, -1]]; found = True; lin[[i]] = Null;];, {i,
Length[lin]}];
lin = lin /. Null -> Sequence[];];
Sow[sel];]][[2, 1]];
res = Flatten[Append[#[[All, ;; -2]], #[[-1, 2 ;; -1]]], 1] & /@ res;
res = res[[All, All, {1, 2}]];
Dim = Dimensions@res;
If[Dim[[1]] == 1,
Polygon[res[[1]]],
Polygon[res[[1]] -> res[[2 ;;]]]
]
]
Map[Graphics@DXF2DConvert@# &, {HEA, UNP, RRW}]
Which returns :