Skip to content
Open
Show file tree
Hide file tree
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
Binary file modified samples/svg/export_sample.dwg
Binary file not shown.
42 changes: 41 additions & 1 deletion src/ACadSharp.Tests/IO/LocalSampleTests.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
using ACadSharp.IO;
using ACadSharp.Entities;
using ACadSharp.IO;
using ACadSharp.Tests.TestModels;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using Xunit;
using Xunit.Abstractions;

Expand Down Expand Up @@ -34,6 +37,37 @@ public void ReadUserDwg(FileModel test)
return;

CadDocument doc = DwgReader.Read(test.Path, this._dwgConfiguration, this.onNotification);

Spline s = doc.GetCadObject<Spline>(0x2C4);
s.TryPolygonalVertexes(6, out var pt);

doc.Entities.Add(new Polyline3D(pt) { Color = Color.Red });

if (false)
{
List<Entity> entities = new List<Entity>();
foreach (var spline in doc.Entities.OfType<Spline>())
{
if (!spline.TryPolygonalVertexes(256, out var pts))
{
continue;
}

Circle circle = new Circle(spline.PointOnSpline(1));
circle.Color = Color.Cyan;

entities.Add(circle);

Circle circle1 = new Circle(spline.PointOnSpline(0));
circle1.Color = Color.Yellow;

entities.Add(circle1);

entities.Add(new Polyline3D(pts) { Color = Color.Red });
}
doc.Entities.AddRange(entities);
}

DwgWriter.Write(Path.Combine(TestVariables.DesktopFolder, "output", "test.dwg"), doc, notification: onNotification);
}

Expand All @@ -45,6 +79,12 @@ public void ReadUserDxf(FileModel test)
return;

CadDocument doc = DxfReader.Read(test.Path, this.onNotification);

Spline s = doc.GetCadObject<Spline>(0x2C4);
s.TryPolygonalVertexes(6, out var pt);

doc.Entities.Add(new Polyline3D(pt) { Color = Color.Red });

DxfWriter.Write(Path.Combine(TestVariables.DesktopFolder, "output", "test.dxf"), doc, notification: onNotification);
}

Expand Down
9 changes: 9 additions & 0 deletions src/ACadSharp/Entities/Circle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,15 @@ public double Radius
/// </summary>
public Circle() : base() { }

/// <summary>
/// Initializes a new instance of the Circle class with the specified center point.
/// </summary>
/// <param name="center">The coordinates of the center point of the circle.</param>
public Circle(XYZ center) : base()
{
this.Center = center;
}

/// <inheritdoc/>
public override void ApplyTransform(Transform transform)
{
Expand Down
12 changes: 11 additions & 1 deletion src/ACadSharp/Entities/Hatch.BoundaryPath.Spline.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using ACadSharp.Attributes;
using CSMath;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;

namespace ACadSharp.Entities
Expand Down Expand Up @@ -130,7 +131,16 @@ public override Entity ToEntity()
spline.EndTangent = this.EndTangent.Convert<XYZ>();

spline.ControlPoints.AddRange(this.ControlPoints);
spline.Weights.AddRange(this.ControlPoints.Select(x => x.Z));

if (this.Weights.Any(w => w == 0))
{
spline.Weights.AddRange(Enumerable.Repeat(1.0d, this.ControlPoints.Count));
}
else
{
spline.Weights.AddRange(this.Weights);
}

spline.FitPoints.AddRange(this.FitPoints.Select(x => x.Convert<XYZ>()));
spline.Knots.AddRange(this.Knots);

Expand Down
42 changes: 35 additions & 7 deletions src/ACadSharp/IO/SVG/SvgXmlWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,9 @@ protected void writeEntity(Entity entity, Transform transform)
case IText text:
this.writeText(text, transform);
break;
//case Spline spline:
// this.writeSpline(spline, transform);
// break;
case Spline spline:
this.writeSpline(spline, transform);
break;
default:
this.notify($"[{entity.ObjectName}] Entity not implemented.", NotificationType.NotImplemented);
break;
Expand Down Expand Up @@ -402,6 +402,8 @@ private void writeEntityAsPath<T>(Entity entity, Transform transform, params IEn

this.WriteAttributeString("d", this.createPath(lines));

this.WriteAttributeString("fill", "none");

this.WriteEndElement();
}

Expand Down Expand Up @@ -441,7 +443,8 @@ private void writeHatch(Hatch hatch, Transform transform)
List<Polyline3D> plines = new List<Polyline3D>();
foreach (Hatch.BoundaryPath path in hatch.Paths)
{
var pline = new Polyline3D(path.GetPoints(this.Configuration.ArcPoints));
var pts = path.GetPoints(this.Configuration.ArcPoints);
var pline = new Polyline3D(pts);
plines.Add(pline);
}

Expand Down Expand Up @@ -691,7 +694,7 @@ private void writeText(IText text, Transform transform)
this.WriteValue("px");
}

if(text.Style.TrueType.HasFlag(FontFlags.Bold))
if (text.Style.TrueType.HasFlag(FontFlags.Bold))
{
this.WriteValue("bold");
}
Expand Down Expand Up @@ -807,8 +810,33 @@ private void writeText(IText text, Transform transform)

private void writeSpline(Spline spline, Transform transform)
{
spline.UpdateFromFitPoints();
this.writeEntityAsPath(spline, transform, spline.PolygonalVertexes(this.Configuration.ArcPoints));
if (!spline.ControlPoints.Any())
{
spline.UpdateFromFitPoints();
}

if (!spline.TryPolygonalVertexes(this.Configuration.ArcPoints, out var pts))
{
return;
}

if (spline.IsClosed)
{
this.WriteStartElement("polygon");
}
else
{
this.WriteStartElement("polyline");
}

this.writeEntityHeader(spline, transform);

string svgPts = this.svgPoints(pts, transform);

this.WriteAttributeString("points", svgPts);
this.WriteAttributeString("fill", "none");

this.WriteEndElement();
}

private void writeTransform(Transform transform)
Expand Down
15 changes: 15 additions & 0 deletions src/ACadSharp/IO/Templates/CadSplineTemplate.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using ACadSharp.Entities;
using System.Linq;

namespace ACadSharp.IO.Templates
{
Expand All @@ -7,5 +8,19 @@ internal class CadSplineTemplate : CadEntityTemplate<Spline>
public CadSplineTemplate() : base(new Spline()) { }

public CadSplineTemplate(Spline entity) : base(entity) { }

protected override void build(CadDocumentBuilder builder)
{
base.build(builder);

if (this.CadObject.ControlPoints.Any())
{
if (this.CadObject.ControlPoints.First()
.Equals(this.CadObject.ControlPoints.Last()))
{
this.CadObject.IsPeriodic = false;
}
}
}
}
}