Skip to content

Commit

Permalink
Write dxf (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickackermann authored Nov 19, 2024
2 parents c64da6a + cf3002c commit 33e38e3
Show file tree
Hide file tree
Showing 8 changed files with 975 additions and 62 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
# Resources in build output
bin/

# Test output
src/test/data/Results/

### Intellij ###
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
Expand Down
503 changes: 503 additions & 0 deletions src/main/java/ch/geowerkstatt/lk2dxf/DxfWriter.java

Large diffs are not rendered by default.

63 changes: 50 additions & 13 deletions src/main/java/ch/geowerkstatt/lk2dxf/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,21 +57,58 @@ private static void processFiles(LK2DxfOptions options) {
Optional<Geometry> perimeter = options.parsePerimeter();
AtomicInteger counter = new AtomicInteger();

for (String xtfFile : options.xtfFiles()) {
try (LKMapXtfReader reader = new LKMapXtfReader(new File(xtfFile))) {
ObjectMapper mapper = new ObjectMapper();
Stream<MappedObject> objects = mapper.mapObjects(reader.readObjects());

if (perimeter.isPresent()) {
objects = objects.filter(o -> perimeter.get().intersects(o.geometry()));
try (var dxfWriter = new DxfWriter(options.dxfFile(), 3, ObjectMapper.getLayerMappings(), "lk2dxf " + Main.VERSION)) {
for (String xtfFile : options.xtfFiles()) {
try (LKMapXtfReader reader = new LKMapXtfReader(new File(xtfFile))) {
ObjectMapper mapper = new ObjectMapper();
Stream<MappedObject> objects = mapper.mapObjects(reader.readObjects());

if (perimeter.isPresent()) {
objects = objects.filter(o -> perimeter.get().intersects(o.geometry()));
}

objects.forEach(o -> {
try {
switch (o.layerMapping().objectClass()) {
case "LKFlaeche" ->
dxfWriter.writeHatch(o.layerMapping().layer(), o.iomObject().getattrobj(o.layerMapping().geometry(), 0));
case "LKLinie" ->
dxfWriter.writeLwPolyline(o.layerMapping().layer(), o.iomObject().getattrobj(o.layerMapping().geometry(), 0));
case "LKPunkt" -> dxfWriter.writeBlockInsert(
o.layerMapping().layer(),
o.layerMapping().symbol(),
Optional.ofNullable(o.iomObject().getattrvalue(o.layerMapping().orientation())).map(Double::parseDouble).orElse(90.0),
o.iomObject().getattrobj(o.layerMapping().geometry(), 0));
case "LKObjekt_Text" -> dxfWriter.writeText(
o.layerMapping().layer(),
o.layerMapping().font(),
o.iomObject().getattrvalue(o.layerMapping().text()),
o.iomObject().getattrvalue(o.layerMapping().hAlign()),
o.iomObject().getattrvalue(o.layerMapping().vAlign()),
Double.parseDouble(o.iomObject().getattrvalue(o.layerMapping().orientation())),
o.iomObject().getattrobj(o.layerMapping().geometry(), 0));

default -> System.out.println("Unsupported geometry type: " + o.layerMapping().geometryType());
//throw new IllegalArgumentException("Unsupported geometry type: " + o.layerMapping().geometryType());
}

System.out.println(counter.incrementAndGet() + ": " + o.iomObject().getobjectoid() + " -> " + o.layerMapping().layer());
} catch (Exception e) {
System.err.println("Failed to process object: " + o.iomObject().getobjectoid());
e.printStackTrace();
return;
}
});
} catch (Exception e) {
System.err.println("Failed to process file: " + xtfFile);
e.printStackTrace();
return;
}

objects.forEach(o -> System.out.println(counter.incrementAndGet() + ": " + o.iomObject().getobjectoid() + " -> " + o.layerMapping().layer()));
} catch (Exception e) {
System.err.println("Failed to process file: " + xtfFile);
e.printStackTrace();
return;
}
} catch (Exception e) {
System.err.println("Failed to write DXF file: " + options.dxfFile());
e.printStackTrace();
return;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -28,6 +29,13 @@ public final class ObjectMapper {
}
}

/**
* Get the layer mappings as an immutable list.
*/
public static List<LayerMapping> getLayerMappings() {
return Collections.unmodifiableList(LAYER_MAPPINGS);
}

/**
* Maps the given {@link IomObject} stream to a stream containing their layer information.
* @param iomObjects The {@link IomObject} stream to map.
Expand Down
Loading

0 comments on commit 33e38e3

Please sign in to comment.