Skip to content

Commit

Permalink
Merge branch 'master' into 20240619_mme_#6-support-dynamic-blocks-rea…
Browse files Browse the repository at this point in the history
…d-evaluation-graphs-and-block-visibility-parameters
  • Loading branch information
Nihad Karajko committed Oct 29, 2024
2 parents 77c1df1 + 8df9190 commit d9151fd
Show file tree
Hide file tree
Showing 21 changed files with 372 additions and 55 deletions.
2 changes: 1 addition & 1 deletion src/ACadSharp.Tests/Common/AssertUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace ACadSharp.Tests.Common
{
public static class AssertUtils
{
public static void AreEqual<T>(T expected, T actual, string varname)
public static void AreEqual<T>(T expected, T actual, string varname = null)
{
switch (expected, actual)
{
Expand Down
94 changes: 94 additions & 0 deletions src/ACadSharp.Tests/Entities/ViewportTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
using ACadSharp.Entities;
using ACadSharp.Tests.Common;
using CSMath;
using System.Collections.Generic;
using Xunit;

namespace ACadSharp.Tests.Entities
{
public class ViewportTests
{
[Fact]
public void GetBoundingBoxTest()
{
Viewport viewport = new Viewport();
viewport.Width = 100;
viewport.Height = 50;
viewport.Center = new XYZ(10, 10, 0);

BoundingBox boundingBox = viewport.GetBoundingBox();

AssertUtils.AreEqual(viewport.Center, boundingBox.Center);
AssertUtils.AreEqual(new XYZ(-40, -15, 0), boundingBox.Min);
AssertUtils.AreEqual(new XYZ(60, 35, 0), boundingBox.Max);
}

[Fact]
public void GetModelBoundingBoxTest()
{
Viewport viewport = new Viewport();
viewport.Width = 100;
viewport.Height = 50;
viewport.ViewHeight = 50;
viewport.ViewCenter = new XY(10, 10);

BoundingBox boundingBox = viewport.GetModelBoundingBox();

Assert.Equal(100, viewport.ViewWidth);
AssertUtils.AreEqual(viewport.ViewCenter, ((XY)boundingBox.Center));
AssertUtils.AreEqual(new XYZ(-40, -15, 0), boundingBox.Min);
AssertUtils.AreEqual(new XYZ(60, 35, 0), boundingBox.Max);
}

[Fact]
public void SelectEntitiesTest()
{
CadDocument doc = new CadDocument();

Viewport viewport = new Viewport();
viewport.Width = 100;
viewport.Height = 50;
viewport.ViewHeight = 50;
viewport.ViewCenter = new XY(10, 10);

//Viewbox
//min: -40, -15
//max: 60, 35

doc.PaperSpace.Entities.Add(viewport);

//Entities in the view
Point ptIn = new Point(new XYZ(0, 0, 0));
Line lineIn = new Line(new XYZ(), new XYZ(100, 100, 0));

List<Entity> inView = new List<Entity>
{
ptIn,
lineIn
};
doc.Entities.AddRange(inView);

//Entities out the view
Point ptOut = new Point(new XYZ(100, 100, 0));

List<Entity> outView = new List<Entity>
{
ptOut
};
doc.Entities.AddRange(outView);

var selected = viewport.SelectEntities();

Assert.NotEmpty(selected);

foreach (Entity e in selected)
{
Assert.Contains(e, inView);
Assert.DoesNotContain(e, outView);
}

var selectedPartial = viewport.SelectEntities(false);
Assert.DoesNotContain(lineIn, selectedPartial);
}
}
}
8 changes: 0 additions & 8 deletions src/ACadSharp.Tests/IO/LocalSampleTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,6 @@ public void ReadUserDwg(FileModel test)
return;

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

CadDocument newDoc = new CadDocument();

foreach (var entity in doc.Entities)
{
doc.Entities.Remove(entity);
newDoc.Entities.Add(entity);
}
}

[Theory]
Expand Down
1 change: 0 additions & 1 deletion src/ACadSharp.Tests/IO/MultiLeaderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using ACadSharp.IO;
using ACadSharp.Tests.TestModels;
using System.Collections.Generic;
using System.IO;
using Xunit;
using Xunit.Abstractions;

Expand Down
38 changes: 38 additions & 0 deletions src/ACadSharp.Tests/IO/ViewportTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using ACadSharp.Entities;
using ACadSharp.IO;
using ACadSharp.Tests.TestModels;
using System.IO;
using Xunit;
using Xunit.Abstractions;

namespace ACadSharp.Tests.IO
{
public class ViewportTests : IOTestsBase
{
public ViewportTests(ITestOutputHelper output) : base(output)
{
}

[Theory]
[MemberData(nameof(DwgFilePaths))]
[MemberData(nameof(DxfAsciiFiles))]
public void ScaleInViewport(FileModel test)
{
CadDocument doc;
if (Path.GetExtension(test.FileName).Equals(".dxf"))
{
doc = DxfReader.Read(test.Path);
}
else
{
doc = DwgReader.Read(test.Path);
}

ACadSharp.Tables.BlockRecord paper = doc.PaperSpace;
foreach (Viewport v in paper.Viewports)
{
Assert.NotNull(v.Scale);
}
}
}
}
6 changes: 6 additions & 0 deletions src/ACadSharp.Tests/IO/WriterSingleObjectTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,11 @@ public void ChangedEncoding()
this.Document.Layers.Add(new Layer("我的自定义层"));
}

public void AddCustomScale()
{
this.Document.Scales.Add(new Scale("Hello"));
}

public void AddBlockWithAttributes()
{
BlockRecord record = new("my_block");
Expand Down Expand Up @@ -491,6 +496,7 @@ static WriterSingleObjectTests()
Data.Add(new(nameof(SingleCaseGenerator.CreateCircleHatch)));
Data.Add(new(nameof(SingleCaseGenerator.ChangedEncoding)));
Data.Add(new(nameof(SingleCaseGenerator.AddBlockWithAttributes)));
Data.Add(new(nameof(SingleCaseGenerator.AddCustomScale)));
}

protected string getPath(string name, string ext, ACadVersion version)
Expand Down
110 changes: 108 additions & 2 deletions src/ACadSharp/Entities/Viewport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
using ACadSharp.Objects;
using ACadSharp.Tables;
using CSMath;
using System;
using System.Collections.Generic;
using System.Linq;

namespace ACadSharp.Entities
{
Expand All @@ -22,6 +24,8 @@ public class Viewport : Entity
/// </summary>
public const int PaperViewId = 1;

public const string ASDK_XREC_ANNOTATION_SCALE_INFO = "ASDK_XREC_ANNOTATION_SCALE_INFO";

/// <inheritdoc/>
public override ObjectType ObjectType => ObjectType.VIEWPORT;

Expand Down Expand Up @@ -321,6 +325,31 @@ public double ViewWidth

//Soft pointer reference to viewport object (for layer VP property override)

/// <summary>
///
/// </summary>
public Scale Scale
{
get
{
return this._scale;
}
set
{
if (this.Document != null)
{
this._scale = this.updateCollection(value, this.Document.Scales);
this.updateScaleXRecord();
}
else
{
this._scale = value;
}
}
}

private Scale _scale;

/// <inheritdoc/>
public override CadObject Clone()
{
Expand All @@ -334,8 +363,8 @@ public override CadObject Clone()
/// <inheritdoc/>
public override BoundingBox GetBoundingBox()
{
XYZ min = new XYZ(this.Center.X - this.Width, this.Center.Y - this.Height, this.Center.Z);
XYZ max = new XYZ(this.Center.X + this.Width, this.Center.Y + this.Height, this.Center.Z);
XYZ min = new XYZ(this.Center.X - this.Width / 2, this.Center.Y - this.Height / 2, this.Center.Z);
XYZ max = new XYZ(this.Center.X + this.Width / 2, this.Center.Y + this.Height / 2, this.Center.Z);
return new BoundingBox(min, max);
}

Expand All @@ -346,5 +375,82 @@ public BoundingBox GetModelBoundingBox()
XYZ max = new XYZ(this.ViewCenter.X + this.ViewWidth / 2, this.ViewCenter.Y + this.ViewHeight / 2, 0);
return new BoundingBox(min, max);
}

/// <summary>
/// Gets all the entities from the model that are in the view of the viewport.
/// </summary>
/// <returns></returns>
public List<Entity> SelectEntities(bool includePartial = true)
{
if (this.Document == null)
{
throw new InvalidOperationException($"Viewport needs to be assigned to a document.");
}

List<Entity> entities = new List<Entity>();

BoundingBox box = this.GetModelBoundingBox();
foreach (Entity e in this.Document.Entities)
{
if (box.IsIn(e.GetBoundingBox(), out bool partialIn) || (partialIn && includePartial))
{
entities.Add(e);
}
}

return entities;
}

internal override void AssignDocument(CadDocument doc)
{
base.AssignDocument(doc);

this._scale = this.updateCollection(this.Scale, doc.Scales);

this.Document.Scales.OnRemove += this.scalesOnRemove;
}

internal override void UnassignDocument()
{
this.Document.Scales.OnRemove -= this.scalesOnRemove;

base.UnassignDocument();

this._scale = (Scale)this.Scale.Clone();
}

private void scalesOnRemove(object sender, CollectionChangedEventArgs e)
{
if (e.Item.Equals(this.Scale))
{
this.Scale = this.Document.Scales.FirstOrDefault();
}
}

private void updateScaleXRecord()
{
if (this.Document == null)
{
return;
}

if (this.XDictionary.TryGetEntry(ASDK_XREC_ANNOTATION_SCALE_INFO, out XRecord record))
{
foreach (XRecord.Entry item in record.Entries)
{
if (item.Code == 340)
{
item.Value = this._scale.Handle;
}
}
}
else
{
record = new XRecord(ASDK_XREC_ANNOTATION_SCALE_INFO);
this.XDictionary.Add(record);

record.CreateEntry(340, _scale.Handle);
}
}
}
}
Loading

0 comments on commit d9151fd

Please sign in to comment.