feat: assembly_add_component — вставка детали из .m3d в сборку (API7)
Веха СБОРКИ, инкремент 1. Новый AssemblyService (API7, паттерн HoleService): IParts7.AddFromFile → Placement.SetOrigin → RebuildModel. Новый AssemblyTools (MCP-инструмент assembly_add_component), регистрация в DI. Ключевая находка: UpdatePlacement возвращает FALSE для вручную позиционируемого компонента (не ошибка), позицию применяет SetOrigin + RebuildModel(true). Валидация — чистый AssemblyValidation (расширение .m3d/.a3d, нормализация пути, конечность координат). RequireActiveAssembly — раздельные ошибки по типу документа. Тесты: +14 unit (AssemblyValidation), +3 integration (AssemblyTests: вставка в origin, позиционирование со сдвигом, отклонение не-сборки). Итого 83 unit + 53 integration = 136 зелёных. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,114 @@
|
||||
using Kompas.Mcp.Core.Assemblies;
|
||||
using Kompas.Mcp.Core.Documents;
|
||||
using Kompas.Mcp.Core.Modeling;
|
||||
using Kompas.Mcp.Core.Query;
|
||||
|
||||
namespace Kompas.Mcp.Tests.Integration;
|
||||
|
||||
/// <summary>Интеграция: вставка компонента в сборку (API7 IParts7.AddFromFile + Placement).</summary>
|
||||
[Trait("Category", "Integration")]
|
||||
[Collection(KompasCollection.Name)]
|
||||
public sealed class AssemblyTests
|
||||
{
|
||||
private readonly DocumentService _docs;
|
||||
private readonly PartModeler _modeler;
|
||||
private readonly AssemblyService _assembly;
|
||||
private readonly QueryService _query;
|
||||
|
||||
public AssemblyTests(KompasFixture fx)
|
||||
{
|
||||
_docs = new DocumentService(fx.Session, fx.Dispatcher);
|
||||
_modeler = new PartModeler(fx.Session, fx.Dispatcher);
|
||||
_assembly = new AssemblyService(fx.Session, fx.Dispatcher);
|
||||
_query = new QueryService(fx.Session, fx.Dispatcher);
|
||||
}
|
||||
|
||||
/// <summary>Построить деталь-коробку от нуля (габарит X[0,w] Y[0,d] Z[0,h]), сохранить в .m3d,
|
||||
/// проверить файл, закрыть. Возвращает абсолютный путь к сохранённому файлу.</summary>
|
||||
private async Task<string> BuildAndSaveBoxAsync(double w, double d, double h)
|
||||
{
|
||||
var path = TestPaths.NewFile(".m3d");
|
||||
await _docs.CreateAsync(KompasDocumentType.Part);
|
||||
try
|
||||
{
|
||||
var s = await _modeler.OpenSketchAsync(BasePlane.XOY);
|
||||
await _modeler.AddRectangleAsync(s, 0, 0, w, d);
|
||||
await _modeler.CloseSketchAsync(s);
|
||||
await _modeler.ExtrudeAsync(s, depth: h);
|
||||
await _modeler.RebuildAsync();
|
||||
await _docs.SaveAsAsync(path);
|
||||
}
|
||||
finally { await _docs.CloseAsync(save: false); }
|
||||
|
||||
// Отделяем проблемы сохранения от проблем вставки: файл должен реально записаться.
|
||||
Assert.True(File.Exists(path), $"Файл детали не сохранён: {path}");
|
||||
Assert.True(new FileInfo(path).Length > 0, "Файл детали пуст.");
|
||||
return path;
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task AddComponent_inserts_one_component_at_origin()
|
||||
{
|
||||
var partFile = await BuildAndSaveBoxAsync(40, 40, 20);
|
||||
|
||||
await _docs.CreateAsync(KompasDocumentType.Assembly);
|
||||
try
|
||||
{
|
||||
var before = (await _query.ListComponentsAsync()).Count;
|
||||
|
||||
var added = await _assembly.AddComponentAsync(partFile, 0, 0, 0);
|
||||
|
||||
var comps = await _query.ListComponentsAsync();
|
||||
Assert.Equal(before + 1, comps.Count);
|
||||
Assert.False(string.IsNullOrWhiteSpace(added.Name), "Имя вставленного компонента не должно быть пустым.");
|
||||
|
||||
var c = comps[added.Index];
|
||||
Assert.InRange(c.SizeX, 40 * 0.95, 40 * 1.05);
|
||||
Assert.InRange(c.SizeY, 40 * 0.95, 40 * 1.05);
|
||||
Assert.InRange(c.SizeZ, 20 * 0.95, 20 * 1.05);
|
||||
|
||||
// Габарит сборки совпадает с деталью, вставленной в начало координат.
|
||||
var bb = await _query.GetBoundingBoxAsync();
|
||||
Assert.InRange(bb.MinX, -1, 1);
|
||||
Assert.InRange(bb.MaxX, 39, 41);
|
||||
Assert.InRange(bb.MinY, -1, 1);
|
||||
Assert.InRange(bb.MaxY, 39, 41);
|
||||
Assert.InRange(bb.MinZ, -1, 1);
|
||||
Assert.InRange(bb.MaxZ, 19, 21);
|
||||
}
|
||||
finally { await _docs.CloseAsync(save: false); }
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task AddComponent_positions_component_at_offset()
|
||||
{
|
||||
var partFile = await BuildAndSaveBoxAsync(40, 40, 20);
|
||||
|
||||
await _docs.CreateAsync(KompasDocumentType.Assembly);
|
||||
try
|
||||
{
|
||||
// Вставка со сдвигом origin на +50 по X → габарит сборки X[50,90].
|
||||
await _assembly.AddComponentAsync(partFile, 50, 0, 0);
|
||||
|
||||
var bb = await _query.GetBoundingBoxAsync();
|
||||
Assert.InRange(bb.MinX, 49, 51);
|
||||
Assert.InRange(bb.MaxX, 89, 91);
|
||||
}
|
||||
finally { await _docs.CloseAsync(save: false); }
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task AddComponent_rejects_non_assembly_document()
|
||||
{
|
||||
// Активный документ — деталь (не сборка): должна быть понятная ошибка.
|
||||
var partFile = await BuildAndSaveBoxAsync(20, 20, 10);
|
||||
|
||||
await _docs.CreateAsync(KompasDocumentType.Part);
|
||||
try
|
||||
{
|
||||
await Assert.ThrowsAsync<InvalidOperationException>(
|
||||
() => _assembly.AddComponentAsync(partFile, 0, 0, 0));
|
||||
}
|
||||
finally { await _docs.CloseAsync(save: false); }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user