Skip to content

Commit

Permalink
Merge branch 'dev' into update-sdks
Browse files Browse the repository at this point in the history
  • Loading branch information
adamhathcock authored Dec 3, 2024
2 parents 646c701 + f53e52b commit b045e31
Show file tree
Hide file tree
Showing 8 changed files with 166 additions and 141 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ await Parent
{
// Note: we're swallowing exeptions here because of a weird case when receiving blocks, we would have
// acad api throw an error on accessing entity.GeometricExtents.
// may also throw Autodesk.AutoCAD.Runtime.Exception for invalid extents on objects like rays and xlines
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@
<Compile Include="$(MSBuildThisFileDirectory)ToHost\Raw\PointToHostRawConverter.cs" />
<Compile Include="$(MSBuildThisFileDirectory)ToHost\Geometry\PointToHostConverter.cs" />
<Compile Include="$(MSBuildThisFileDirectory)ToSpeckle\Geometry\ArcToSpeckleConverter.cs" />
<Compile Include="$(MSBuildThisFileDirectory)ToSpeckle\Geometry\RegionToSpeckleConverter.cs" />
<Compile Include="$(MSBuildThisFileDirectory)ToSpeckle\Geometry\SurfaceToSpeckleConverter.cs" />
<Compile Include="$(MSBuildThisFileDirectory)ToSpeckle\Geometry\Solid3dToSpeckleConverter.cs" />
<Compile Include="$(MSBuildThisFileDirectory)ToSpeckle\Raw\BrepToSpeckleRawConverter.cs" />
<Compile Include="$(MSBuildThisFileDirectory)ToSpeckle\Raw\CircularArc2dToSpeckleRawConverter.cs" />
<Compile Include="$(MSBuildThisFileDirectory)ToSpeckle\Raw\DBArcToSpeckleRawConverter.cs" />
<Compile Include="$(MSBuildThisFileDirectory)ToSpeckle\Raw\DBCircleToSpeckleRawConverter.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using Speckle.Converters.Common;
using Speckle.Converters.Common.Objects;
using Speckle.Sdk.Common.Exceptions;
using Speckle.Sdk.Models;

namespace Speckle.Converters.Autocad.Geometry;

[NameAndRankValue(nameof(ADB.Region), NameAndRankValueAttribute.SPECKLE_DEFAULT_RANK)]
public class RegionToSpeckleConverter : IToSpeckleTopLevelConverter, ITypedConverter<ADB.Region, SOG.Mesh>
{
private readonly ITypedConverter<ABR.Brep, SOG.Mesh> _brepConverter;

public RegionToSpeckleConverter(ITypedConverter<ABR.Brep, SOG.Mesh> brepConverter)
{
_brepConverter = brepConverter;
}

public Base Convert(object target) => Convert((ADB.Region)target);

public SOG.Mesh Convert(ADB.Region target)
{
using ABR.Brep brep = new(target);
if (brep.IsNull)
{
throw new ConversionException("Could not retrieve brep from the region.");
}

SOG.Mesh mesh = _brepConverter.Convert(brep);
mesh.area = target.Area;

return mesh;
}
}
Original file line number Diff line number Diff line change
@@ -1,24 +1,17 @@
using Speckle.Converters.Common;
using Speckle.Converters.Common.Objects;
using Speckle.Sdk;
using Speckle.Sdk.Models;

namespace Speckle.Converters.Autocad.Geometry;

[NameAndRankValue(nameof(ADB.SubDMesh), NameAndRankValueAttribute.SPECKLE_DEFAULT_RANK)]
public class DBSubDMeshToSpeckleConverter : IToSpeckleTopLevelConverter
{
private readonly ITypedConverter<AG.Point3d, SOG.Point> _pointConverter;
private readonly ITypedConverter<ADB.Extents3d, SOG.Box> _boxConverter;
private readonly IConverterSettingsStore<AutocadConversionSettings> _settingsStore;

public DBSubDMeshToSpeckleConverter(
ITypedConverter<AG.Point3d, SOG.Point> pointConverter,
ITypedConverter<ADB.Extents3d, SOG.Box> boxConverter,
IConverterSettingsStore<AutocadConversionSettings> settingsStore
)
public DBSubDMeshToSpeckleConverter(IConverterSettingsStore<AutocadConversionSettings> settingsStore)
{
_pointConverter = pointConverter;
_boxConverter = boxConverter;
_settingsStore = settingsStore;
}

Expand Down Expand Up @@ -71,19 +64,22 @@ public SOG.Mesh RawConvert(ADB.SubDMesh target)
)
.ToList();

// bbox
SOG.Box bbox = _boxConverter.Convert(target.GeometricExtents);

SOG.Mesh speckleMesh =
new()
{
vertices = vertices,
faces = faces,
colors = colors,
units = _settingsStore.Current.SpeckleUnits,
bbox = bbox
area = target.ComputeSurfaceArea()
};

try
{
speckleMesh.volume = target.ComputeVolume();
}
catch (Exception e) when (!e.IsFatal()) { } // for non-volumetric meshes

return speckleMesh;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using Speckle.Converters.Common;
using Speckle.Converters.Common.Objects;
using Speckle.Sdk.Common.Exceptions;
using Speckle.Sdk.Models;

namespace Speckle.Converters.Autocad.Geometry;

[NameAndRankValue(nameof(ADB.Surface), NameAndRankValueAttribute.SPECKLE_DEFAULT_RANK)]
public class SurfaceToSpeckleConverter : IToSpeckleTopLevelConverter, ITypedConverter<ADB.Surface, SOG.Mesh>
{
private readonly ITypedConverter<ABR.Brep, SOG.Mesh> _brepConverter;

public SurfaceToSpeckleConverter(ITypedConverter<ABR.Brep, SOG.Mesh> brepConverter)
{
_brepConverter = brepConverter;
}

public Base Convert(object target) => Convert((ADB.Surface)target);

public SOG.Mesh Convert(ADB.Surface target)
{
using ABR.Brep brep = new(target);
if (brep.IsNull)
{
throw new ConversionException("Could not retrieve brep from the plane surface.");
}

return _brepConverter.Convert(brep);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
using Speckle.Converters.Common;
using Speckle.Converters.Common.Objects;
using Speckle.Sdk;
using Speckle.Sdk.Common.Exceptions;

namespace Speckle.Converters.Autocad.ToSpeckle.Raw;

public class BrepToSpeckleRawConverter : ITypedConverter<ABR.Brep, SOG.Mesh>
{
private readonly IConverterSettingsStore<AutocadConversionSettings> _settingsStore;

public BrepToSpeckleRawConverter(IConverterSettingsStore<AutocadConversionSettings> settingsStore)
{
_settingsStore = settingsStore;
}

public SOG.Mesh Convert(ABR.Brep target)
{
if (target.IsNull)
{
throw new ConversionException("Brep was null.");
}

List<int> faces = new();
List<double> vertices = new();
int vertexCount = 0;

using (var control = new ABR.Mesh2dControl())
{
// These settings may need adjusting
control.MaxSubdivisions = 10000;

// create mesh filters
using (var filter = new ABR.Mesh2dFilter())
{
filter.Insert(target, control);
using (ABR.Mesh2d m = new(filter))
{
foreach (ABR.Element2d? e in m.Element2ds)
{
// add number of vertices for this face
int nodeCount = e.Nodes.Count();
faces.Add(nodeCount);

foreach (var n in e.Nodes)
{
// add index of current vertex to face
faces.Add(vertexCount);
vertexCount++;

// add vertex coords
vertices.Add(n.Point.X);
vertices.Add(n.Point.Y);
vertices.Add(n.Point.Z);
n.Dispose();
}

e.Dispose();
}
}
}

// create speckle mesh
SOG.Mesh mesh =
new()
{
faces = faces,
vertices = vertices,
units = _settingsStore.Current.SpeckleUnits,
area = target.GetSurfaceArea()
};

try
{
mesh.volume = target.GetVolume();
}
catch (ABR.Exception e) when (!e.IsFatal()) { } // exceptions can be thrown for non-volumetric breps

return mesh;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,26 +1,16 @@
using Speckle.Converters.Common;
using Speckle.Converters.Common.Objects;
using Speckle.Sdk;
using Speckle.Sdk.Common.Exceptions;
using Speckle.Sdk.Models;

namespace Speckle.Converters.Autocad.ToSpeckle.Raw;

public class DBBodyToSpeckleRawConverter : ITypedConverter<ADB.Body, SOG.Mesh>
{
private readonly ITypedConverter<AG.Point3d, SOG.Point> _pointConverter;
private readonly ITypedConverter<ADB.Extents3d, SOG.Box> _boxConverter;
private readonly IConverterSettingsStore<AutocadConversionSettings> _settingsStore;
private readonly ITypedConverter<ABR.Brep, SOG.Mesh> _brepConverter;

public DBBodyToSpeckleRawConverter(
ITypedConverter<AG.Point3d, SOG.Point> pointConverter,
ITypedConverter<ADB.Extents3d, SOG.Box> boxConverter,
IConverterSettingsStore<AutocadConversionSettings> settingsStore
)
public DBBodyToSpeckleRawConverter(ITypedConverter<ABR.Brep, SOG.Mesh> brepConverter)
{
_pointConverter = pointConverter;
_boxConverter = boxConverter;
_settingsStore = settingsStore;
_brepConverter = brepConverter;
}

public Base Convert(object target) => Convert((ADB.Body)target);
Expand All @@ -33,59 +23,6 @@ public SOG.Mesh Convert(ADB.Body target)
throw new ConversionException("Could not retrieve brep from the body.");
}

var vertices = new List<AG.Point3d>();
var faces = new List<int>();

// create mesh from solid with mesh filter
using ABR.Mesh2dControl control = new();
control.MaxSubdivisions = 10000; // POC: these settings may need adjusting
using ABR.Mesh2dFilter filter = new();
filter.Insert(brep, control);
using ABR.Mesh2d m = new(filter);
foreach (ABR.Element2d e in m.Element2ds)
{
// get vertices
List<int> faceIndices = new();
foreach (ABR.Node n in e.Nodes)
{
faceIndices.Add(vertices.Count);
vertices.Add(n.Point);
n.Dispose();
}

// get faces
List<int> faceList = new() { e.Nodes.Count() };
for (int i = 0; i < e.Nodes.Count(); i++)
{
faceList.Add(faceIndices[i]);
}

faces.AddRange(faceList);

e.Dispose();
}

// mesh props
var convertedVertices = vertices.SelectMany(o => _pointConverter.Convert(o).ToList()).ToList();
SOG.Box bbox = _boxConverter.Convert(target.GeometricExtents);

// create speckle mesh
SOG.Mesh mesh =
new()
{
vertices = convertedVertices,
faces = faces,
area = brep.GetSurfaceArea(),
units = _settingsStore.Current.SpeckleUnits,
bbox = bbox
};

try
{
mesh.volume = brep.GetVolume();
}
catch (Autodesk.AutoCAD.BoundaryRepresentation.Exception e) when (!e.IsFatal()) { }

return mesh;
return _brepConverter.Convert(brep);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using Speckle.Converters.Common;
using Speckle.Converters.Common.Objects;
using Speckle.Sdk.Common.Exceptions;
using Speckle.Sdk.Models;
Expand All @@ -7,19 +6,11 @@ namespace Speckle.Converters.Autocad.ToSpeckle.Raw;

public class Solid3dToSpeckleRawConverter : ITypedConverter<ADB.Solid3d, SOG.Mesh>
{
private readonly ITypedConverter<AG.Point3d, SOG.Point> _pointConverter;
private readonly ITypedConverter<ADB.Extents3d, SOG.Box> _boxConverter;
private readonly IConverterSettingsStore<AutocadConversionSettings> _settingsStore;
private readonly ITypedConverter<ABR.Brep, SOG.Mesh> _brepConverter;

public Solid3dToSpeckleRawConverter(
ITypedConverter<AG.Point3d, SOG.Point> pointConverter,
ITypedConverter<ADB.Extents3d, SOG.Box> boxConverter,
IConverterSettingsStore<AutocadConversionSettings> settingsStore
)
public Solid3dToSpeckleRawConverter(ITypedConverter<ABR.Brep, SOG.Mesh> brepConverter)
{
_pointConverter = pointConverter;
_boxConverter = boxConverter;
_settingsStore = settingsStore;
_brepConverter = brepConverter;
}

public Base Convert(object target) => Convert((ADB.Solid3d)target);
Expand All @@ -32,55 +23,7 @@ public SOG.Mesh Convert(ADB.Solid3d target)
throw new ValidationException("Could not retrieve brep from the solid3d.");
}

var vertices = new List<AG.Point3d>();
var faces = new List<int>();

// create mesh from solid with mesh filter
using ABR.Mesh2dControl control = new();
control.MaxSubdivisions = 10000; // POC: these settings may need adjusting
using ABR.Mesh2dFilter filter = new();
filter.Insert(brep, control);
using ABR.Mesh2d m = new(filter);
foreach (ABR.Element2d e in m.Element2ds)
{
// get vertices
List<int> faceIndices = new();
foreach (ABR.Node n in e.Nodes)
{
faceIndices.Add(vertices.Count);
vertices.Add(n.Point);
n.Dispose();
}

// get faces
List<int> faceList = new() { e.Nodes.Count() };
for (int i = 0; i < e.Nodes.Count(); i++)
{
faceList.Add(faceIndices[i]);
}

faces.AddRange(faceList);

e.Dispose();
}

// mesh props
var convertedVertices = vertices.SelectMany(o => _pointConverter.Convert(o).ToList()).ToList();
double volume = target.MassProperties.Volume;
double area = target.Area;
SOG.Box bbox = _boxConverter.Convert(target.GeometricExtents);

// create speckle mesh
SOG.Mesh mesh =
new()
{
vertices = convertedVertices,
faces = faces,
units = _settingsStore.Current.SpeckleUnits,
bbox = bbox,
area = area,
volume = volume
};
SOG.Mesh mesh = _brepConverter.Convert(brep);

return mesh;
}
Expand Down

0 comments on commit b045e31

Please sign in to comment.