Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for importing DXF polylines #2612

Merged
merged 1 commit into from
Sep 16, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package com.willwinder.ugs.nbp.designer.io.dxf;

import com.willwinder.ugs.nbp.designer.entities.Entity;
import com.willwinder.ugs.nbp.designer.entities.cuttable.CutType;
import com.willwinder.ugs.nbp.designer.entities.cuttable.Ellipse;
import com.willwinder.ugs.nbp.designer.entities.cuttable.Group;
import com.willwinder.ugs.nbp.designer.entities.cuttable.Path;
import com.willwinder.ugs.nbp.designer.entities.cuttable.Rectangle;
import com.willwinder.ugs.nbp.designer.io.DesignReader;
import com.willwinder.ugs.nbp.designer.io.DesignReaderException;
import com.willwinder.ugs.nbp.designer.model.Design;
Expand All @@ -20,14 +18,15 @@
import org.kabeja.dxf.DXFLayer;
import org.kabeja.dxf.DXFLine;
import org.kabeja.dxf.DXFPoint;
import org.kabeja.dxf.DXFPolyline;
import org.kabeja.dxf.DXFVertex;
import org.kabeja.dxf.helpers.Point;
import org.kabeja.parser.DXFParser;
import org.kabeja.parser.ParseException;
import org.kabeja.parser.Parser;
import org.kabeja.parser.ParserBuilder;

import java.awt.geom.Arc2D;
import java.awt.geom.Point2D;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
Expand Down Expand Up @@ -72,7 +71,7 @@ public Optional<Design> read(InputStream resourceAsStream) {
DXFLayer layer = (DXFLayer) layerIterator.next();
parseAndAddLayerGroup(group, layer);
}

Design design = new Design();
List<Entity> entities = new ArrayList<>();
if (!group.getChildren().isEmpty()) {
Expand Down Expand Up @@ -112,11 +111,37 @@ private void parseAndAddLayerGroup(Group group, DXFLayer layer) {
layerGroup.addChild(arcsGroup);
}

Group polylinesGroup = new Group();
polylinesGroup.setName("Polyline");
parsePolylines(layer, polylinesGroup);
if (!polylinesGroup.getChildren().isEmpty()) {
layerGroup.addChild(polylinesGroup);
}

if (!layerGroup.getChildren().isEmpty()) {
group.addChild(layerGroup);
}
}

private void parsePolylines(DXFLayer layer, Group polylinesGroup) {
List<DXFPolyline> polylines = layer.getDXFEntities(DXFConstants.ENTITY_TYPE_POLYLINE);
if (polylines == null) {
return;
}

for (DXFPolyline polyline : polylines) {
Path path = new Path();
DXFVertex vertex = polyline.getVertex(0);
path.moveTo(convertCoordinate(vertex.getX()), convertCoordinate(vertex.getY()));
for(int i = 1; i < polyline.getVertexCount(); i++) {
vertex = polyline.getVertex(i);
path.lineTo(convertCoordinate(vertex.getX()), convertCoordinate(vertex.getY()));
}
path.close();

polylinesGroup.addChild(path);
}
}


private void parseArcs(DXFLayer layer, Group arcsGroup) {
Expand Down
Loading