Skip to content

Commit

Permalink
Update netDxf code
Browse files Browse the repository at this point in the history
  • Loading branch information
wieslawsoltes committed Jun 1, 2024
1 parent 70b94b1 commit db18b27
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ private void Add(DXF.DxfDocument dxf, DocumentContainerViewModel document, ICont
};

dxf.Layouts.Add(layout);
dxf.ActiveLayout = layout.Name;
// TODO: dxf.ActiveLayout = layout.Name;

Add(dxf, page, presenter);
}
Expand Down
76 changes: 43 additions & 33 deletions src/Core2D/Modules/Renderer/Dxf/DxfRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
using Core2D.ViewModels.Renderer;
using Core2D.ViewModels.Shapes;
using Core2D.ViewModels.Style;
using netDxf.Units;
using SkiaSharp;
using DXF = netDxf;
using DXFE = netDxf.Entities;
using DXFO = netDxf.Objects;
Expand Down Expand Up @@ -90,11 +92,11 @@ private DXFE.Ellipse CreateEllipse(double x, double y, double width, double heig
var minor = Math.Min(height, width);
var major = Math.Max(height, width);

return new DXFE.Ellipse
return new DXFE.Ellipse(
center: new DXF.Vector3(cx, cy, 0),
majorAxis: major,
minorAxis: minor)
{
Center = new DXF.Vector3(cx, cy, 0),
MajorAxis = major,
MinorAxis = minor,
StartAngle = 0.0,
EndAngle = 360.0,
Rotation = height > width ? 90.0 : 0.0
Expand Down Expand Up @@ -129,11 +131,11 @@ private DXFE.Ellipse CreateEllipse(double x, double y, double width, double heig
rotation = -90;
}

return new DXFE.Ellipse
return new DXFE.Ellipse(
center: new DXF.Vector3(cx, cy, 0),
majorAxis: major,
minorAxis: minor)
{
Center = new DXF.Vector3(cx, cy, 0),
MajorAxis = major,
MinorAxis = minor,
StartAngle = startAngle,
EndAngle = endAngle,
Rotation = rotation
Expand All @@ -150,12 +152,13 @@ private DXFE.Spline CreateQuadraticSpline(double p1x, double p1y, double p2x, do
var sp3y = ToDxfY(p3y);

return new DXFE.Spline(
new List<DXFE.SplineVertex>
new List<DXF.BezierCurveQuadratic>
{
new(sp1x, sp1y, 0.0),
new(sp2x, sp2y, 0.0),
new(sp3x, sp3y, 0.0)
}, 2);
new DXF.BezierCurveQuadratic(
new DXF.Vector3(sp1x, sp1y, 0.0),
new DXF.Vector3(sp2x, sp2y, 0.0),
new DXF.Vector3(sp3x, sp3y, 0.0))
});
}

private DXFE.Spline CreateCubicSpline(double p1x, double p1y, double p2x, double p2y, double p3x, double p3y, double p4x, double p4y)
Expand All @@ -170,13 +173,14 @@ private DXFE.Spline CreateCubicSpline(double p1x, double p1y, double p2x, double
var sp4y = ToDxfY(p4y);

return new DXFE.Spline(
new List<DXFE.SplineVertex>
new List<DXF.BezierCurveCubic>
{
new DXF.BezierCurveCubic(
new(sp1x, sp1y, 0.0),
new(sp2x, sp2y, 0.0),
new(sp3x, sp3y, 0.0),
new(sp4x, sp4y, 0.0)
}, 3);
new(sp4x, sp4y, 0.0))
});
}

private void DrawLineInternal(DXF.DxfDocument dxf, DXFT.Layer layer, ShapeStyleViewModel style, bool isStroked, double x1, double y1, double x2, double y2)
Expand All @@ -197,7 +201,7 @@ private void DrawLineInternal(DXF.DxfDocument dxf, DXFT.Layer layer, ShapeStyleV
dxfLine.Transparency.Value = strokeTransparency;
dxfLine.Lineweight = lineweight;

dxf.AddEntity(dxfLine);
dxf.Entities.Add(dxfLine);
}

private void DrawLineInternal(DXF.DxfDocument dxf, DXFT.Layer layer, BaseColorViewModel colorViewModel, double thickness, double x1, double y1, double x2, double y2)
Expand All @@ -213,7 +217,7 @@ private void DrawLineInternal(DXF.DxfDocument dxf, DXFT.Layer layer, BaseColorVi
dxfLine.Transparency.Value = strokeTransparency;
dxfLine.Lineweight = lineweight;

dxf.AddEntity(dxfLine);
dxf.Entities.Add(dxfLine);
}

private void DrawRectangleInternal(DXF.DxfDocument dxf, DXFT.Layer layer, bool isFilled, bool isStroked, ShapeStyleViewModel style, ref Spatial.Rect2 rect)
Expand Down Expand Up @@ -254,7 +258,7 @@ private void FillRectangle(DXF.DxfDocument dxf, DXFT.Layer layer, double x, doub
};
hatch.Transparency.Value = fillTransparency;

dxf.AddEntity(hatch);
dxf.Entities.Add(hatch);
}

private void StrokeRectangle(DXF.DxfDocument dxf, DXFT.Layer layer, ShapeStyleViewModel style, double x, double y, double width, double height)
Expand Down Expand Up @@ -299,7 +303,7 @@ private void StrokeEllipse(DXF.DxfDocument dxf, DXFT.Layer layer, DXFE.Ellipse d
dxfEllipse.Transparency.Value = strokeTansparency;
dxfEllipse.Lineweight = lineweight;

dxf.AddEntity(dxfEllipse);
dxf.Entities.Add(dxfEllipse);
}

private void FillEllipse(DXF.DxfDocument dxf, DXFT.Layer layer, DXFE.Ellipse dxfEllipse, BaseColorViewModel colorViewModel)
Expand All @@ -325,7 +329,7 @@ private void FillEllipse(DXF.DxfDocument dxf, DXFT.Layer layer, DXFE.Ellipse dxf
};
hatch.Transparency.Value = fillTransparency;

dxf.AddEntity(hatch);
dxf.Entities.Add(hatch);
}

private void DrawGridInternal(DXF.DxfDocument dxf, DXFT.Layer layer, IGrid grid, ref Spatial.Rect2 rect)
Expand Down Expand Up @@ -680,7 +684,7 @@ public void DrawArc(object? dc, ArcShapeViewModel arc, ShapeStyleViewModel? styl
};
hatch.Transparency.Value = fillTransparency;

dxf.AddEntity(hatch);
dxf.Entities.Add(hatch);
}

if (arc.IsStroked && style.Stroke?.Color is { })
Expand All @@ -694,7 +698,7 @@ public void DrawArc(object? dc, ArcShapeViewModel arc, ShapeStyleViewModel? styl
dxfEllipse.Transparency.Value = strokeTansparency;
dxfEllipse.Lineweight = lineweight;

dxf.AddEntity(dxfEllipse);
dxf.Entities.Add(dxfEllipse);
}
}

Expand Down Expand Up @@ -757,7 +761,7 @@ public void DrawCubicBezier(object? dc, CubicBezierShapeViewModel cubicBezier, S
};
hatch.Transparency.Value = fillTransparency;

dxf.AddEntity(hatch);
dxf.Entities.Add(hatch);
}

if (cubicBezier.IsStroked && style.Stroke?.Color is { })
Expand All @@ -771,7 +775,7 @@ public void DrawCubicBezier(object? dc, CubicBezierShapeViewModel cubicBezier, S
dxfSpline.Transparency.Value = strokeTransparency;
dxfSpline.Lineweight = lineweight;

dxf.AddEntity(dxfSpline);
dxf.Entities.Add(dxfSpline);
}
}

Expand Down Expand Up @@ -832,7 +836,7 @@ public void DrawQuadraticBezier(object? dc, QuadraticBezierShapeViewModel quadra
};
hatch.Transparency.Value = fillTransparency;

dxf.AddEntity(hatch);
dxf.Entities.Add(hatch);
}

if (quadraticBezier.IsStroked && style.Stroke?.Color is { })
Expand All @@ -846,7 +850,7 @@ public void DrawQuadraticBezier(object? dc, QuadraticBezierShapeViewModel quadra
dxfSpline.Transparency.Value = strokeTansparency;
dxfSpline.Lineweight = lineweight;

dxf.AddEntity(dxfSpline);
dxf.Entities.Add(dxfSpline);
}
}

Expand Down Expand Up @@ -946,7 +950,7 @@ public void DrawText(object? dc, TextShapeViewModel text, ShapeStyleViewModel? s
dxfMText.Transparency.Value = strokeTransparency;
dxfMText.Color = stroke;

dxf.AddEntity(dxfMText);
dxf.Entities.Add(dxfMText);
}

public void DrawImage(object? dc, ImageShapeViewModel image, ShapeStyleViewModel? style)
Expand Down Expand Up @@ -991,15 +995,21 @@ public void DrawImage(object? dc, ImageShapeViewModel image, ShapeStyleViewModel
new DXF.Vector3(ToDxfX(rect.X), ToDxfY(rect.Y + rect.Height), 0),
rect.Width,
rect.Height);
dxf.AddEntity(dxfImage);
dxf.Entities.Add(dxfImage);
}
else
{
if (State?.ImageCache is { } && !string.IsNullOrEmpty(image.Key) && !string.IsNullOrEmpty(_outputPath))
{
var path = System.IO.Path.Combine(_outputPath, System.IO.Path.GetFileName(image.Key));
System.IO.File.WriteAllBytes(path, bytes);
var dxfImageDefinition = new DXFO.ImageDefinition(path);
var dxfImageDefinition = new DXFO.ImageDefinition(
path,
(int)rect.Width,
1d,
(int)rect.Height,
1d,
ImageResolutionUnits.Unitless);

_biCache?.Set(image.Key, dxfImageDefinition);

Expand All @@ -1008,7 +1018,7 @@ public void DrawImage(object? dc, ImageShapeViewModel image, ShapeStyleViewModel
new DXF.Vector3(ToDxfX(rect.X), ToDxfY(rect.Y + rect.Height), 0),
rect.Width,
rect.Height);
dxf.AddEntity(dxfImage);
dxf.Entities.Add(dxfImage);
}
}
}
Expand Down Expand Up @@ -1053,7 +1063,7 @@ public void DrawPath(object? dc, PathShapeViewModel path, ShapeStyleViewModel? s
};
hatch.Transparency.Value = fillTransparency;

dxf.AddEntity(hatch);
dxf.Entities.Add(hatch);
}

if (path.IsStroked && style.Stroke?.Color is { })
Expand All @@ -1070,7 +1080,7 @@ public void DrawPath(object? dc, PathShapeViewModel path, ShapeStyleViewModel? s
entity.Color = stroke;
entity.Transparency.Value = strokeTransparency;
entity.Lineweight = lineweight;
dxf.AddEntity(entity);
dxf.Entities.Add(entity);
}
}
}
Expand Down

0 comments on commit db18b27

Please sign in to comment.