Skip to content

Commit

Permalink
add support arc dxf
Browse files Browse the repository at this point in the history
  • Loading branch information
VovaStelmashchuk committed Aug 9, 2024
1 parent d1bee52 commit 38ba237
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 116 deletions.
71 changes: 71 additions & 0 deletions backend/src/main/java/com/nestapp/files/dxf/reader/Arc.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package com.nestapp.files.dxf.reader

import com.nestapp.files.dxf.common.RealPoint
import com.nestapp.files.dxf.writter.parts.DXFArc
import com.nestapp.files.dxf.writter.parts.DXFEntity
import com.nestapp.nest.Placement
import java.awt.geom.Arc2D
import java.awt.geom.Path2D

internal class Arc(type: String?) : Entity(type!!), AutoPop {

private var arc: Arc2D.Double = Arc2D.Double(Arc2D.OPEN)
private var cx = 0.0
private var cy = 0.0
private var startAngle = 0.0
private var endAngle = 0.0
private var radius = 0.0
private lateinit var path: Path2D.Double

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

override fun close() {
arc.setFrame(cx - radius, cy - radius, radius * 2, radius * 2)
// Make angle negative so it runs clockwise when using Arc2D.Double
arc.angleStart = -startAngle
val extent = startAngle - (if (endAngle < startAngle) endAngle + 360 else endAngle)
arc.angleExtent = extent

path = Path2D.Double()
path.append(arc, true)
}

override fun translate(x: Double, y: Double): Entity {
val translatedArc = Arc(type)
translatedArc.cx = this.cx + x
translatedArc.cy = this.cy + y
translatedArc.radius = this.radius
translatedArc.startAngle = this.startAngle
translatedArc.endAngle = this.endAngle
translatedArc.close()
return translatedArc
}

override fun toWriterEntity(placement: Placement): DXFEntity {
val center = RealPoint(cx, cy)

return DXFArc(
center.transform(placement),
radius,
startAngle,
endAngle,
true,
)
}

override fun isClose(): Boolean {
return false
}

override fun toPath2D(): Path2D.Double {
return path
}
}
39 changes: 0 additions & 39 deletions backend/src/main/java/com/nestapp/files/dxf/reader/DXFReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -478,45 +478,6 @@ public void close() {
}
}

class Arc extends Entity implements AutoPop {
Arc2D.Double arc = new Arc2D.Double(Arc2D.OPEN);
private double cx, cy, startAngle, endAngle, radius;

Arc(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;
case 50: // Start Angle
startAngle = Double.parseDouble(value);
break;
case 51: // End Angle
endAngle = Double.parseDouble(value);
break;
}
}

@Override
public void close() {
arc.setFrame(cx - radius, cy - radius, radius * 2, radius * 2);
// Make angle negative so it runs clockwise when using Arc2D.Double
arc.setAngleStart(-startAngle);
double extent = startAngle - (endAngle < startAngle ? endAngle + 360 : endAngle);
arc.setAngleExtent(extent);
}
}

class Spline extends Entity implements AutoPop {
Path2D.Double path = new Path2D.Double();
List<Point2D.Double> cPoints = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,51 +27,39 @@

import com.nestapp.files.dxf.common.RealPoint;

import java.awt.*;


/**
* Graphical entity representing a circular arc.
*
* @author jsevy
*
*/
public class DXFArc extends DXFEntity
{
public class DXFArc extends DXFEntity {
protected RealPoint center;
protected double radius;
private double startAngleRadians;
private double endAngleRadians;
private boolean isCounterclockwise;
protected Color color;
protected BasicStroke stroke;
private final double startAngleRadians;
private final double endAngleRadians;
private final boolean isCounterclockwise;


/**
* Create a circular arc from the specified parameters.
* @param center Center of the circle, as a RealPoint
* @param radius Radius of the circle
* @param startAngleRadians Starting angle of the circle, in radians, counterclockwise from the positive x axis
* @param endAngleRadians Starting angle of the circle, in radians, counterclockwise from the positive x axis
* @param isCounterclockwise Indicate direction of arc, clockwise or counterclockwise
* @param graphics The graphics object specifying parameters for the arc (color, thickness)
*
* @param center Center of the circle, as a RealPoint
* @param radius Radius of the circle
* @param startAngleRadians Starting angle of the circle, in radians, counterclockwise from the positive x axis
* @param endAngleRadians Starting angle of the circle, in radians, counterclockwise from the positive x axis
* @param isCounterclockwise Indicate direction of arc, clockwise or counterclockwise
*/
public DXFArc(RealPoint center, double radius, double startAngleRadians, double endAngleRadians, boolean isCounterclockwise, Graphics2D graphics)
{
public DXFArc(RealPoint center, double radius, double startAngleRadians, double endAngleRadians, boolean isCounterclockwise) {
this.startAngleRadians = startAngleRadians;
this.endAngleRadians = endAngleRadians;
this.isCounterclockwise = isCounterclockwise;
this.center = new RealPoint(center);
this.radius = radius;
this.color = graphics.getColor();
this.stroke = (BasicStroke)graphics.getStroke();
}

/**
* Implementation of DXFObject interface method; creates DXF text representing the circular arc.
*/
public String toDXFString()
{
public String toDXFString() {
String result = "0\nARC\n";

// print out handle and superclass marker(s) and data
Expand All @@ -91,23 +79,16 @@ public String toDXFString()
result += "100\nAcDbArc\n";

// angles - which are in degrees for circular arcs
double startAngleDegrees = (startAngleRadians * 180/Math.PI);
double endAngleDegrees = (endAngleRadians * 180/Math.PI);
double startAngleDegrees = (startAngleRadians * 180 / Math.PI);
double endAngleDegrees = (endAngleRadians * 180 / Math.PI);
result += "50\n" + setPrecision(startAngleDegrees) + "\n";
result += "51\n" + setPrecision(endAngleDegrees) + "\n";

// add thickness; specified in Java in pixels at 72 pixels/inch; needs to be in 1/100 of mm for DXF, and restricted range of values
result += "370\n" + getDXFLineWeight(stroke.getLineWidth()) + "\n";

// add color number
result += "62\n" + DXFColor.getClosestDXFColor(color.getRGB()) + "\n";

return result;
}


public String getDXFHatchInfo()
{
public String getDXFHatchInfo() {
// circular arc
String result = "72\n" + "2" + "\n";

Expand All @@ -119,8 +100,8 @@ public String getDXFHatchInfo()
result += "40\n" + setPrecision(radius) + "\n";

// start/end angles - IN DEGREES FOR HATCH!
double startAngleDegrees = (startAngleRadians * 180/Math.PI);
double endAngleDegrees = (endAngleRadians * 180/Math.PI);
double startAngleDegrees = (startAngleRadians * 180 / Math.PI);
double endAngleDegrees = (endAngleRadians * 180 / Math.PI);

// do some stuff to accommodate LibreCAD, which ignores counterclockwise flag and doesn't like negative angles or those
// outside the range 0-360 - not sure how much to try to accommodate...
Expand All @@ -132,19 +113,17 @@ public String getDXFHatchInfo()

// stupid stuff for float/double rounding
if (startAngleDegrees >= 360)
startAngleDegrees -= 360;
startAngleDegrees -= 360;
if (endAngleDegrees >= 360)
endAngleDegrees -= 360;

// LibreCAD draws counterclockwise if start < end, clockwise if end < start
if (isCounterclockwise)
{
if (isCounterclockwise) {
if (endAngleDegrees < startAngleDegrees)
endAngleDegrees += 360;
}

if (!isCounterclockwise)
{
if (!isCounterclockwise) {
// need to reverse start and end angles
double temp = startAngleDegrees;
startAngleDegrees = endAngleDegrees;
Expand Down
36 changes: 1 addition & 35 deletions backend/src/main/java/com/nestapp/files/svg/SvgWriter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,40 +18,6 @@ class SvgWriter {
val y: Double,
)

fun buildSvgStringOnPoints(points: List<Point2D.Double>): String {
val minX = points.minOf { it.x }
val minY = points.minOf { it.y }
val maxX = points.maxOf { it.x }
val maxY = points.maxOf { it.y }

val width = maxX - minX
val height = maxY - minY

val rawSvg = buildString {
appendLine("""<g transform="translate(${-minX}, ${-minY})">""".trimIndent())

appendLine("""<path d="""".trimIndent())

points.forEachIndexed { segmentIndex, segment ->
if (segmentIndex == 0) {
append("M")
} else {
append("L")
}

append(segment.x)
append(" ")
append(segment.y)
append(" ")
}

appendLine("""Z" fill="#00000000" fill-opacity="0.2" stroke="#000000" stroke-width="1" /> """)
appendLine("</g>")
}

return buildFinalSvg(rawSvg, width, height)
}

fun buildNestedSvgString(
polygons: List<SvgPolygon>,
): String {
Expand Down Expand Up @@ -139,7 +105,7 @@ class SvgWriter {
}

val color = COLORS[index % COLORS.size]
appendLine("""Z" fill="#32000000" fill-opacity="0.1" stroke="$color" stroke-width="1" /> """)
appendLine("""Z" fill="#64$color" fill-opacity="0.5" stroke="$color" stroke-width="1" /> """)
}

private fun buildFinalSvg(string: String, width: Double, height: Double): String {
Expand Down
34 changes: 33 additions & 1 deletion nest2dvue/src/views/BlogView.vue
Original file line number Diff line number Diff line change
@@ -1,13 +1,45 @@
<template>
<div class="blog">

<div class="blog__container blog-container">
<h2 class="blog-container__title">
Big update, #9, My summer vacation (2)
</h2>
<p class="blog-container__meta">
Published on
<time datetime="2024-05-08">Aug 09, 2024</time>
by nest2d
</p>
<h2>New feature</h2>
<ul class="blog-container__list">
<p>Add support for Arc DXF tag</p>
</ul>
<h2>Tech task</h2>
<ul class="blog-container__list">
<li>Migrate to jagua-rs</li>
<li>Remove postgress sql</li>
<li>Remove the old nest algorithm</li>
<li>Add mongo for nested history</li>
<li>Implement save dxf and svg files to minio</li>
</ul>
<h2>Next stages</h2>
<ul class="blog-container__list">
<li>Nested history page</li>
<li>More DXF tags supports</li>
</ul>

<h2>Thanks</h2>
<p>Thanks to JeroenGar for the great project, I hope it will be useful for me and for the community</p>
<p>Thanks to anyone who uses the project, and uploads the project to the platform. It helps be with development</p>
</div>

<div class="blog__container blog-container">
<h2 class="blog-container__title">
Big update, #8, My summer vacation
</h2>
<p class="blog-container__meta">
Published on
<time datetime="2024-05-08">Mar 30, 2024</time>
<time datetime="2024-05-08">Aug 05, 2024</time>
by nest2d
</p>
<h2>New feature</h2>
Expand Down

0 comments on commit 38ba237

Please sign in to comment.