I have a console application created with "ArcGIS Pro CoreHost application" template, if I open a .dwg file inside it with below function:
public static Geometry GetBoundary(string inProjectBoudaryPath, string cadFile)
{
try
{
var connectionPath = new FileSystemConnectionPath(new Uri(inProjectBoudaryPath),
FileSystemDatastoreType.Cad);
using (var dataStore = new FileSystemDatastore(connectionPath))
{
string fileName = Path.GetFileName(cadFile);
using (var dataset = dataStore.OpenDataset<CadDataset>(cadFile))
{
using (var polygonFeatCls = dataset.OpenDataset<FeatureClass>("Polygon"))
{
QueryFilter filter = new QueryFilter();
filter.WhereClause = $"Layer='Project Boundary'";
using (var cursor = polygonFeatCls.Search(null, false))
{
Geometry boudary = null;
while (cursor.MoveNext())
{
using (var feature = (Feature)cursor.Current)
{
if (GeometryEngine.Instance.IsSimpleAsFeature(feature.GetShape(), true))
{
if (boudary == null) boudary = feature.GetShape();
else boudary = GeometryEngine.Instance.Union(boudary, feature.GetShape());
}
else
{
var fixedGeometry = GeometryEngine.Instance.SimplifyAsFeature(feature.GetShape());
if (boudary == null) boudary = fixedGeometry;
else boudary = GeometryEngine.Instance.Union(boudary, fixedGeometry);
}
}
}
return Convert3DTo2D(boudary);
}
}
}
}
}
catch (Exception ex)
{
Console.Write($"Error when extract boudary from CAD, ex: {ex}");
return null;
}
return null;
}
public static Geometry Convert3DTo2D(Geometry inputGeo)
{
Console.WriteLine("Start to remove Z in feature");
if (inputGeo == null) return null;
SpatialReference hkProj = null;
using (SpatialReferenceBuilder srBuilder = new SpatialReferenceBuilder(2326))
{
// do something with the builder
hkProj = srBuilder.ToSpatialReference();
}
PolygonBuilderEx polygonBuilder = new PolygonBuilderEx(hkProj);
polygonBuilder.HasZ = false;
if (inputGeo.HasZ)
{
Polygon inPolygon = (Polygon)inputGeo;
for (int i = 0; i < inPolygon.PartCount; i++)
{
ReadOnlySegmentCollection segCol = inPolygon.Parts[i];
polygonBuilder.AddPart(segCol);
}
Console.WriteLine("Remove Z in feature successfully");
return polygonBuilder.ToGeometry();
}
else
{
Console.WriteLine("No Z found.");
return inputGeo;
}
} The program will not exit normally like below screenshot:

when debug in Visual studio 2022, it will display after finished:

And there is below error in output window of Visual Studio:

By the way, I use ArcGIS Pro 3.1 and related SDK for .Net and Visual Studio 2022.
Who can tell me if it is the bug of SDK?