Skip to content

Commit

Permalink
Add support for new tags
Browse files Browse the repository at this point in the history
  • Loading branch information
VovaStelmashchuk committed Mar 30, 2024
1 parent dddf3f3 commit 1427c18
Show file tree
Hide file tree
Showing 11 changed files with 247 additions and 197 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/backend_prod_deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ jobs:
- name: Setup Gradle
uses: gradle/gradle-build-action@v2
- name: Run build with Gradle Wrapper
run: cd backend && ./gradlew publishImage
run: cd backend && ./gradlew jib --image vovochkastelmashchuk/nest2d publishImage
12 changes: 6 additions & 6 deletions backend/api.http
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,29 @@ POST http://localhost:8080/api/project
Content-Type: application/json

{
"name": "Big box v1"
"name": "From prod 1"
}

### Add preview image to project
POST http://localhost:8080/api/project/big-box-v1/preview
POST http://localhost:8080/api/project/from-prod-1/preview
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary

------WebKitFormBoundary
Content-Disposition: form-data; name="file"; filename="preview.png"

< test-data/previes/big_box_v1.png
< test-data/from_prod/Preview.png

### Get project preview
GET http://localhost:8080/api/project/my_project/preview

### Add dxf file to project
POST http://localhost:8080/api/files/big-box-v1/dxf
POST http://localhost:8080/api/files/from-prod-1/dxf
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary

------WebKitFormBoundary
Content-Disposition: form-data; name="file"; filename="long_side.dxf"
Content-Disposition: form-data; name="file"; filename="test_dow.dxf"

< test-data/long_side.dxf
< test-data/from_prod/test_dow.dxf

### Get full project
GET http://localhost:8080/api/project/my_project
Expand Down
2 changes: 1 addition & 1 deletion backend/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ plugins {
}

group = "com.nestapp"
version = "0.5.5"
version = "0.6.0"

application {
mainClass.set("com.nestapp.Main")
Expand Down
29 changes: 29 additions & 0 deletions backend/src/main/java/com/nestapp/files/dxf/reader/Circle.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.nestapp.files.dxf.reader

import java.awt.geom.Ellipse2D
import java.awt.geom.Path2D

internal class Circle(type: String?) : Entity(type!!), AutoPop {
private var circle: Ellipse2D.Double = Ellipse2D.Double()
var cx: Double = 0.0
var cy: Double = 0.0
var radius: Double = 0.0

override fun addParam(gCode: Int, value: String) {
when (gCode) {
10 -> cx = value.toDouble()
20 -> cy = value.toDouble()
40 -> radius = value.toDouble()
}
}

override fun close() {
circle.setFrame(cx - radius, cy - radius, radius * 2, radius * 2)
}

override fun toPath2D(): Path2D.Double {
val path = Path2D.Double()
path.append(circle, false)
return path
}
}
143 changes: 0 additions & 143 deletions backend/src/main/java/com/nestapp/files/dxf/reader/DXFReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -412,35 +412,6 @@ public void addParam(int gCode, String value) {
}
}

class Circle extends Entity implements AutoPop {
Ellipse2D.Double circle = new Ellipse2D.Double();
private double cx, cy, radius;

Circle(String type) {
super(type);
}

@Override
public void addParam(int gCode, String value) {
switch (gCode) {
case 10: // Center Point X1
cx = Double.parseDouble(value);
break;
case 20: // Center Point Y2
cy = Double.parseDouble(value);
break;
case 40: // Radius
radius = Double.parseDouble(value);
break;
}
}

@Override
public void close() {
circle.setFrame(cx - radius, cy - radius, radius * 2, radius * 2);
}
}

/**
* Crude implementation of ELLIPSE
*/
Expand Down Expand Up @@ -541,89 +512,6 @@ public void close() {
}
}



class Polyline extends Entity {
private Path2D.Double path;
private List<Vertex> points;
private double firstX, firstY, lastX, lastY;
private boolean firstPoint = true;
private boolean close;

Polyline(String type) {
super(type);
}

@Override
public void addParam(int gCode, String value) {
if (gCode == 70) {
int flags = Integer.parseInt(value);
close = (flags & 1) != 0;
}
}

@Override
public void addChild(Entity child) {
if (child instanceof Vertex) {
if (points == null) {
points = new ArrayList<>();
}
points.add((Vertex) child);
}
}

@Override
public void close() {
path = new Path2D.Double();
double bulge = 0.0;
for (Vertex vertex : points) {
if (firstPoint) {
firstPoint = false;
path.moveTo(firstX = lastX = vertex.xx, firstY = lastY = vertex.yy);
} else {
if (bulge != 0) {
path.append(getArcBulge(lastX, lastY, vertex.xx, vertex.yy, bulge), true);
lastX = vertex.xx;
lastY = vertex.yy;
} else {
path.lineTo(lastX = vertex.xx, lastY = vertex.yy);
}
}
bulge = vertex.bulge;
}
if (close) {
if (bulge != 0) {
path.append(getArcBulge(lastX, lastY, firstX, firstY, bulge), true);
} else {
path.closePath();
}
}
}
}

class Vertex extends Entity {
double xx, yy, bulge;

Vertex(String type) {
super(type);
}

@Override
public void addParam(int gCode, String value) {
switch (gCode) {
case 10: // Vertex X
xx = Double.parseDouble(value);
break;
case 20: // Vertex Y
yy = Double.parseDouble(value);
break;
case 42: // Vertex Bulge factor
bulge = Double.parseDouble(value);
break;
}
}
}

class Spline extends Entity implements AutoPop {
Path2D.Double path = new Path2D.Double();
List<Point2D.Double> cPoints = new ArrayList<>();
Expand Down Expand Up @@ -685,37 +573,6 @@ public void addParam(int gCode, String value) {
}
}


/**
* See: http://darrenirvine.blogspot.com/2015/08/polylines-radius-bulge-turnaround.html
*
* @param sx Starting x for Arc
* @param sy Starting y for Arc
* @param ex Ending x for Arc
* @param ey Ending y for Arc
* @param bulge bulge factor (bulge > 0 = clockwise, else counterclockwise)
* @return Arc2D.Double object
*/
private Arc2D.Double getArcBulge(double sx, double sy, double ex, double ey, double bulge) {
Point2D.Double p1 = new Point2D.Double(sx, sy);
Point2D.Double p2 = new Point2D.Double(ex, ey);
Point2D.Double mp = new Point2D.Double((p2.x + p1.x) / 2, (p2.y + p1.y) / 2);
Point2D.Double bp = new Point2D.Double(mp.x - (p1.y - mp.y) * bulge, mp.y + (p1.x - mp.x) * bulge);
double u = p1.distance(p2);
double b = (2 * mp.distance(bp)) / u;
double radius = u * ((1 + b * b) / (4 * b));
double dx = mp.x - bp.x;
double dy = mp.y - bp.y;
double mag = Math.sqrt(dx * dx + dy * dy);
Point2D.Double cp = new Point2D.Double(bp.x + radius * (dx / mag), bp.y + radius * (dy / mag));
double startAngle = 180 - Math.toDegrees(Math.atan2(cp.y - p1.y, cp.x - p1.x));
double opp = u / 2;
double extent = Math.toDegrees(Math.asin(opp / radius)) * 2;
double extentAngle = bulge >= 0 ? -extent : extent;
Point2D.Double ul = new Point2D.Double(cp.x - radius, cp.y - radius);
return new Arc2D.Double(ul.x, ul.y, radius * 2, radius * 2, startAngle, extentAngle, Arc2D.OPEN);
}

private void push() {
stack.add(cEntity);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ open class Entity internal constructor(@JvmField var type: String) {
}

open fun toPath2D(): Path2D.Double {
throw UnsupportedOperationException("Not implemented")
throw UnsupportedOperationException("Not implemented in ${this.javaClass.simpleName}")
}
}
109 changes: 109 additions & 0 deletions backend/src/main/java/com/nestapp/files/dxf/reader/Polyline.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package com.nestapp.files.dxf.reader

import java.awt.geom.Arc2D
import java.awt.geom.Path2D
import java.awt.geom.Point2D
import kotlin.math.asin
import kotlin.math.atan2
import kotlin.math.sqrt

internal class Polyline(type: String?) : Entity(type!!) {

private var path: Path2D.Double? = null
var points: MutableList<Vertex> = ArrayList()
private var firstX = 0.0
private var firstY = 0.0
private var lastX = 0.0
private var lastY = 0.0
private var firstPoint = true
private var close = false

override fun addParam(gCode: Int, value: String) {
if (gCode == 70) {
val flags = value.toInt()
close = (flags and 1) != 0
}
}

override fun addChild(child: Entity?) {
if (child is Vertex) {
points.add(child)
}
}

override fun close() {
path = Path2D.Double()
var bulge = 0.0
for (vertex in points) {
if (firstPoint) {
firstPoint = false
path!!.moveTo(vertex.xx.also { lastX = it }.also { firstX = it }, vertex.yy.also { lastY = it }
.also { firstY = it })
} else {
if (bulge != 0.0) {
path!!.append(getArcBulge(lastX, lastY, vertex.xx, vertex.yy, bulge), true)
lastX = vertex.xx
lastY = vertex.yy
} else {
path!!.lineTo(vertex.xx.also { lastX = it }, vertex.yy.also { lastY = it })
}
}
bulge = vertex.bulge
}
if (close) {
if (bulge != 0.0) {
path!!.append(getArcBulge(lastX, lastY, firstX, firstY, bulge), true)
} else {
path!!.closePath()
}
}
}

override fun toPath2D(): Path2D.Double {
return path!!
}

/**
* See: http://darrenirvine.blogspot.com/2015/08/polylines-radius-bulge-turnaround.html
*
* @param sx Starting x for Arc
* @param sy Starting y for Arc
* @param ex Ending x for Arc
* @param ey Ending y for Arc
* @param bulge bulge factor (bulge > 0 = clockwise, else counterclockwise)
* @return Arc2D.Double object
*/
private fun getArcBulge(sx: Double, sy: Double, ex: Double, ey: Double, bulge: Double): Arc2D.Double {
val p1 = Point2D.Double(sx, sy)
val p2 = Point2D.Double(ex, ey)
val mp = Point2D.Double((p2.x + p1.x) / 2, (p2.y + p1.y) / 2)
val bp = Point2D.Double(mp.x - (p1.y - mp.y) * bulge, mp.y + (p1.x - mp.x) * bulge)
val u = p1.distance(p2)
val b = (2 * mp.distance(bp)) / u
val radius = u * ((1 + b * b) / (4 * b))
val dx = mp.x - bp.x
val dy = mp.y - bp.y
val mag = sqrt(dx * dx + dy * dy)
val cp = Point2D.Double(bp.x + radius * (dx / mag), bp.y + radius * (dy / mag))
val startAngle = 180 - Math.toDegrees(atan2(cp.y - p1.y, cp.x - p1.x))
val opp = u / 2
val extent = Math.toDegrees(asin(opp / radius)) * 2
val extentAngle = if (bulge >= 0) -extent else extent
val ul = Point2D.Double(cp.x - radius, cp.y - radius)
return Arc2D.Double(ul.x, ul.y, radius * 2, radius * 2, startAngle, extentAngle, Arc2D.OPEN)
}
}

internal class Vertex(type: String?) : Entity(type!!) {
var xx: Double = 0.0
var yy: Double = 0.0
var bulge: Double = 0.0

override fun addParam(gCode: Int, value: String) {
when (gCode) {
10 -> xx = value.toDouble()
20 -> yy = value.toDouble()
42 -> bulge = value.toDouble()
}
}
}
Loading

0 comments on commit 1427c18

Please sign in to comment.