Skip to content
Merged
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
9 changes: 8 additions & 1 deletion docs/public-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,14 @@ IOScene.SceneJson.Save(result.Document, "drawing.scene.json");
```

The scene validates against the IOScene contract (`SceneDocument.Validate` reports no error) and round-trips through
IOScene JSON with its original line and point topology.
IOScene JSON with its line, point and triangle topology.

Solid `HATCH` fills preserve islands according to the recorded detection style. Floating viewports project and clip
the fill to the paper viewport. `DwgHatchData.Style` and the drawing JSON `hatch.style` expose that setting.
[AutoCAD hatch island detection](https://help.autodesk.com/cloudhelp/2023/ENU/AutoCAD-Core/files/GUID-981679AC-7097-4724-A30D-33F1CAFDD81D.htm)

Visible `ATTRIB` labels follow the space of their owning `INSERT`. A dimension without rendered block graphics
uses a label based on its override text or recorded measurement at its text midpoint.

## CLI Exporters (`dwgio`)

Expand Down
3 changes: 0 additions & 3 deletions src/DwgIO.Cli/Export/DwgViewerExport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,6 @@ private static HtmlViewerOptions BuildHtmlOptions(DwgSceneResult result, DwgView

private static bool IsFlat2DScene(SceneDocument scene)
{
if (scene.Geometries.Any(g => g.Topology == SceneTopology.Triangles))
return false;

double minZ = double.PositiveInfinity, maxZ = double.NegativeInfinity;
foreach (var geom in scene.Geometries)
{
Expand Down
2 changes: 1 addition & 1 deletion src/DwgIO.Scene.Adapter/DwgIO.Scene.Adapter.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<ItemGroup>
<ProjectReference Include="..\DwgIO\DwgIO.csproj" />
<PackageReference Include="IOScene" />
<Compile Include="..\Shared\ArcAngles.cs;..\Shared\DwgAciPalette.cs;..\Shared\EllipseAxes.cs" Link="%(Filename)%(Extension)" />
<Compile Include="..\Shared\ArcAngles.cs;..\Shared\DwgAciPalette.cs;..\Shared\EllipseAxes.cs;..\Shared\HatchBoundaries.cs" Link="%(Filename)%(Extension)" />
</ItemGroup>

<!-- The shared pack metadata expects icon.png beside the project; the adapter ships the engine's icon. -->
Expand Down
332 changes: 285 additions & 47 deletions src/DwgIO.Scene.Adapter/DwgSceneReader.cs

Large diffs are not rendered by default.

80 changes: 80 additions & 0 deletions src/DwgIO.Scene.Adapter/HatchFill.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
using DwgIO.Model;

namespace DwgIO.Scene;

internal static class HatchFill
{
private readonly record struct Edge(DwgPoint2D Lower, DwgPoint2D Upper)
{
public double XAt(double y) => Lower.X + (Upper.X - Lower.X) * ((y - Lower.Y) / (Upper.Y - Lower.Y));
}

public static (List<DwgPoint2D> Vertices, List<int> Indices)? Triangulate(DwgHatchData hatch)
{
var edges = new List<Edge>();
var elevations = new SortedSet<double>();
foreach (var loop in hatch.Loops)
{
if (!HatchBoundaries.Fills(hatch, loop)) continue;
for (int i = 0; i < loop.Points.Count; i++)
{
var a = loop.Points[i];
var b = loop.Points[(i + 1) % loop.Points.Count];
if (!double.IsFinite(a.X) || !double.IsFinite(a.Y)) return null;
if (a.Y == b.Y) continue;
edges.Add(a.Y < b.Y ? new Edge(a, b) : new Edge(b, a));
elevations.Add(a.Y);
elevations.Add(b.Y);
}
}

edges.Sort((a, b) => a.Lower.Y.CompareTo(b.Lower.Y));
var levels = elevations.ToArray();
var active = new List<Edge>();
var vertices = new List<DwgPoint2D>();
var indices = new List<int>();
int next = 0;
// Between vertex elevations each pair of consecutive crossings bounds a filled trapezoid.
for (int level = 0; level + 1 < levels.Length; level++)
{
double bottom = levels[level], top = levels[level + 1];
double mid = bottom + (top - bottom) / 2;
active.RemoveAll(e => e.Upper.Y <= bottom);
while (next < edges.Count && edges[next].Lower.Y <= bottom)
{
active.Add(edges[next++]);
}

active.Sort((a, b) => a.XAt(mid).CompareTo(b.XAt(mid)));
for (int i = 0; i + 1 < active.Count; i += 2)
{
var left = active[i];
var right = active[i + 1];
double leftBottom = left.XAt(bottom), rightBottom = right.XAt(bottom);
double leftTop = left.XAt(top), rightTop = right.XAt(top);
if (leftBottom >= rightBottom && leftTop >= rightTop) continue;

int first = vertices.Count;
vertices.Add(new DwgPoint2D(leftBottom, bottom));
vertices.Add(new DwgPoint2D(rightBottom, bottom));
vertices.Add(new DwgPoint2D(rightTop, top));
vertices.Add(new DwgPoint2D(leftTop, top));
if (leftBottom < rightBottom)
{
indices.Add(first);
indices.Add(first + 1);
indices.Add(first + 2);
}

if (leftTop < rightTop)
{
indices.Add(first);
indices.Add(first + 2);
indices.Add(first + 3);
}
}
}

return indices.Count > 0 ? (vertices, indices) : null;
}
}
108 changes: 103 additions & 5 deletions src/DwgIO.Scene/Compatibility/DwgSvgExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,10 @@ public static void Write(DwgDocument document, XmlWriter writer, DwgSvgOptions?
if (path.Fill)
{
writer.WriteAttributeString("fill", stroke);
if (path.EvenOdd)
{
writer.WriteAttributeString("fill-rule", "evenodd");
}
}

writer.WriteAttributeString("stroke", stroke);
Expand Down Expand Up @@ -201,7 +205,10 @@ private sealed record SvgPoint(double X, double Y, int ColorIndex, uint? TrueCol
: SvgShape(ColorIndex, TrueColorRgb);

private sealed record SvgPath(string Data, IReadOnlyList<(double X, double Y)> BoundsPoints, bool Fill, int ColorIndex, uint? TrueColorRgb)
: SvgShape(ColorIndex, TrueColorRgb);
: SvgShape(ColorIndex, TrueColorRgb)
{
public bool EvenOdd { get; init; }
}

private sealed record SvgText(
double X, double Y, double Height, double RotationRad, string Text, int Column, int Row, int ColorIndex, uint? TrueColorRgb)
Expand All @@ -211,9 +218,21 @@ private static List<SvgShape> Collect(DwgDocument document, DwgSvgOptions option
{
double scale = options.Scale;
var shapes = new List<SvgShape>();
var inserts = new Dictionary<ulong, DwgEntity>();
foreach (var entity in document.Entities)
{
if (!entity.IsModelSpace)
if (entity.Kind == DwgEntityKind.Insert)
{
inserts.TryAdd(entity.Handle, entity);
}
}

foreach (var entity in document.Entities)
{
var insert = entity.Kind == DwgEntityKind.Attrib && entity.OwnerHandle is ulong owner
? inserts.GetValueOrDefault(owner)
: null;
if (!(insert?.IsModelSpace ?? entity.IsModelSpace))
{
continue;
}
Expand Down Expand Up @@ -300,10 +319,12 @@ private static List<SvgShape> Collect(DwgDocument document, DwgSvgOptions option
}

break;
case (DwgEntityKind.Text or DwgEntityKind.MText, { Length: >= 5 } g):
case (DwgEntityKind.Text or DwgEntityKind.MText or DwgEntityKind.Attrib, { Length: >= 5 } g)
when entity.Kind != DwgEntityKind.Attrib || entity.Attrib?.IsInvisible != true:
if (IsUsable(g[0] * scale, g[1] * scale, g[3] * scale, g[4]) && g[3] > 0 && entity.PlainText is { Length: > 0 } plain)
{
var (column, row) = TextPlacement.Alignment(entity.Kind, g);
bool byBlock = insert is not null && entity.EffectiveColorIndex == 0 && entity.EffectiveTrueColorRgb is null;
shapes.Add(new SvgText(
g[0] * scale,
-g[1] * scale,
Expand All @@ -312,8 +333,22 @@ private static List<SvgShape> Collect(DwgDocument document, DwgSvgOptions option
plain,
column,
row,
entity.EffectiveColorIndex,
entity.EffectiveTrueColorRgb));
byBlock ? insert!.EffectiveColorIndex : entity.EffectiveColorIndex,
byBlock ? insert!.EffectiveTrueColorRgb : entity.EffectiveTrueColorRgb));
}

break;
case (DwgEntityKind.Polyline3d or DwgEntityKind.Spline or DwgEntityKind.Leader, { Length: >= 7 } g):
if (TryBuildVertexStrip(g, scale, entity, allowClosed: entity.Kind != DwgEntityKind.Leader, out var strip))
{
shapes.Add(strip);
}

break;
case (DwgEntityKind.Hatch, _) when entity.Hatch is { Loops.Count: > 0 } hatch:
if (TryBuildHatch(hatch, scale, entity, out var hatchPath))
{
shapes.Add(hatchPath);
}

break;
Expand All @@ -323,6 +358,69 @@ private static List<SvgShape> Collect(DwgDocument document, DwgSvgOptions option
return shapes;
}

private static bool TryBuildVertexStrip(double[] g, double scale, DwgEntity entity, bool allowClosed, out SvgPath path)
{
int count = (g.Length - 1) / 3;
if (count < 2 || !IsUsable(g))
{
path = null!;
return false;
}

bool closed = allowClosed && g[0] != 0.0;
var sb = new System.Text.StringBuilder();
var bounds = new List<(double X, double Y)>(count);
for (int i = 0; i < count; i++)
{
double x = g[1 + i * 3] * scale;
double y = -g[1 + i * 3 + 1] * scale;
bounds.Add((x, y));
sb.Append(FormattableString.Invariant($"{(i == 0 ? 'M' : 'L')} {Num(x)} {Num(y)} "));
}

if (closed) sb.Append('Z');
path = new SvgPath(sb.ToString().TrimEnd(), bounds, false, entity.EffectiveColorIndex, entity.EffectiveTrueColorRgb);
return true;
}

private static bool TryBuildHatch(DwgHatchData hatch, double scale, DwgEntity entity, out SvgPath path)
{
var sb = new System.Text.StringBuilder();
var bounds = new List<(double X, double Y)>();
bool fill = hatch.IsSolidFill && hatch.Loops.Any(loop => HatchBoundaries.Fills(hatch, loop));
foreach (var loop in hatch.Loops)
{
if (loop.Points.Count < 2 || (fill && !HatchBoundaries.Fills(hatch, loop))) continue;
for (int i = 0; i < loop.Points.Count; i++)
{
double x = loop.Points[i].X * scale;
double y = -loop.Points[i].Y * scale;
if (!IsUsable(x, y))
{
path = null!;
return false;
}

bounds.Add((x, y));
sb.Append(FormattableString.Invariant($"{(i == 0 ? 'M' : 'L')} {Num(x)} {Num(y)} "));
}

if (loop.IsClosed) sb.Append("Z ");
}

if (bounds.Count < 2)
{
path = null!;
return false;
}

path = new SvgPath(sb.ToString().TrimEnd(), bounds, fill, entity.EffectiveColorIndex, entity.EffectiveTrueColorRgb)
{
EvenOdd = fill,
};
return true;
}

private static bool TryBuildPolyline(double[] g, double scale, DwgEntity entity, out SvgPath path)
{
int count = (g.Length - 2) / 3;
Expand Down
2 changes: 1 addition & 1 deletion src/DwgIO.Scene/DwgIO.Scene.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<ItemGroup Condition="'$(BuildNativeCoreOnly)' != 'true'">
<PackageReference Include="IOScene.Formats" />
<PackageReference Include="IOViewer" />
<Compile Include="..\Shared\AtomicOutput.cs;..\Shared\ArcAngles.cs;..\Shared\DwgAciPalette.cs;..\Shared\EllipseAxes.cs;..\Shared\TextPlacement.cs"
<Compile Include="..\Shared\AtomicOutput.cs;..\Shared\ArcAngles.cs;..\Shared\DwgAciPalette.cs;..\Shared\EllipseAxes.cs;..\Shared\HatchBoundaries.cs;..\Shared\TextPlacement.cs"
Link="Compatibility\%(Filename)%(Extension)" />
</ItemGroup>

Expand Down
Loading
Loading