Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(civil3d): refactor to yield return #329

Merged
merged 7 commits into from
Oct 28, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -32,36 +32,40 @@ IConverterSettingsStore<Civil3dConversionSettings> converterSettings
_converterSettings = converterSettings;
}

public List<Base>? GetDisplayValue(CDB.Entity entity)
public IEnumerable<Base> GetDisplayValue(CDB.Entity entity)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice ♥️

{
switch (entity)
{
case CDB.FeatureLine featureline:
SOG.Polyline featurelinePolyline = _pointCollectionConverter.Convert(
featureline.GetPoints(Autodesk.Civil.FeatureLinePointType.PIPoint)
);
return new() { featurelinePolyline };
yield return featurelinePolyline;
break;

// pipe networks: https://help.autodesk.com/view/CIV3D/2025/ENU/?guid=ade47b62-debf-f899-9b94-5645a620ab4f
case CDB.Part part:
SOG.Mesh partMesh = _solidConverter.Convert(part.Solid3dBody);
return new() { partMesh };
yield return partMesh;
break;

// surfaces: https://help.autodesk.com/view/CIV3D/2025/ENU/?guid=d741aa49-e7da-9513-6b0b-226ebe3fa43f
// POC: volume surfaces not supported
case CDB.TinSurface tinSurface:
SOG.Mesh tinSurfaceMesh = _tinSurfaceConverter.Convert(tinSurface);
return new() { tinSurfaceMesh };
yield return tinSurfaceMesh;
break;
case CDB.GridSurface gridSurface:
SOG.Mesh gridSurfaceMesh = _gridSurfaceConverter.Convert(gridSurface);
return new() { gridSurfaceMesh };
yield return gridSurfaceMesh;
break;

// Corridors are complicated: their display values are extracted in the CorridorHandler when processing corridor children, since they are attached to the corridor subassemblies.
case CDB.Corridor:
return new();
yield break;

default:
return null;
yield break;
}
}

Expand All @@ -73,33 +77,30 @@ IConverterSettingsStore<Civil3dConversionSettings> converterSettings
/// List of simple curves: lines, polylines, and arcs.
/// Null if no suitable display curves were found.
/// </returns>
public List<Base>? ProcessICurvesForDisplay(List<ICurve>? iCurves)
public IEnumerable<Base> ProcessICurvesForDisplay(List<ICurve>? iCurves)
{
if (iCurves is null)
{
return null;
yield break;
}

List<Base> result = new();
foreach (ICurve curve in iCurves)
{
switch (curve)
{
case SOG.Line:
case SOG.Polyline:
case SOG.Arc:
result.Add((Base)curve);
yield return (Base)curve;
break;
case SOG.Polycurve polycurve:
List<Base>? processedSegments = ProcessICurvesForDisplay(polycurve.segments);
if (processedSegments is not null)
IEnumerable<Base> processedSegments = ProcessICurvesForDisplay(polycurve.segments);
foreach (Base processedSegment in processedSegments)
{
result.AddRange(processedSegments);
yield return processedSegment;
}
break;
}
}

return result.Count > 0 ? result : null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,14 @@ public Base Convert(CDB.Entity target)

// extract display value.
// If object has no display but has basecurves, use basecurves for display instead (for viewer selection)
List<Base>? display =
_displayValueExtractor.GetDisplayValue(target) ?? _displayValueExtractor.ProcessICurvesForDisplay(baseCurves);
if (display is not null)
List<Base> displayValue = _displayValueExtractor.GetDisplayValue(target).ToList();
if (displayValue.Count == 0)
{
civilObject["displayValue"] = display;
displayValue = _displayValueExtractor.ProcessICurvesForDisplay(baseCurves).ToList();
}
if (displayValue.Count > 0)
{
civilObject["displayValue"] = displayValue;
}

// add any additional class properties
Expand All @@ -80,60 +83,69 @@ public Base Convert(CDB.Entity target)

// determine if this entity has any children elements that need to be converted.
// this is a bespoke method by class type.
List<Base>? children = null;
switch (target)
var children = GetEntityChildren(target).ToList();
if (children.Count > 0)
{
civilObject["elements"] = children;
}

return civilObject;
}

private IEnumerable<Base> GetEntityChildren(CDB.Entity entity)
{
switch (entity)
{
case CDB.Alignment alignment:
children = GetAlignmentChildren(alignment);
var alignmentChildren = GetAlignmentChildren(alignment);
foreach (var child in alignmentChildren)
{
yield return child;
}
break;
case CDB.Corridor corridor:
children = _corridorHandler.GetCorridorChildren(corridor);
var corridorChildren = _corridorHandler.GetCorridorChildren(corridor);
foreach (var child in corridorChildren)
{
yield return child;
}
break;

case CDB.Site site:
children = GetSiteChildren(site);
var siteChildren = GetSiteChildren(site).ToList();
foreach (var child in siteChildren)
{
yield return child;
}
break;
}

if (children is not null)
{
civilObject["elements"] = children;
}

return civilObject;
}

private List<Base>? GetSiteChildren(CDB.Site site)
private IEnumerable<Base> GetSiteChildren(CDB.Site site)
{
List<Base> parcels = new();
using (var tr = _settingsStore.Current.Document.Database.TransactionManager.StartTransaction())
{
foreach (ADB.ObjectId parcelId in site.GetParcelIds())
{
var parcel = (CDB.Parcel)tr.GetObject(parcelId, ADB.OpenMode.ForRead);
parcels.Add(Convert(parcel));
yield return Convert(parcel);
}

tr.Commit();
}

return parcels.Count > 0 ? parcels : null;
}

private List<Base>? GetAlignmentChildren(CDB.Alignment alignment)
private IEnumerable<Base> GetAlignmentChildren(CDB.Alignment alignment)
{
List<Base> profiles = new();
using (var tr = _settingsStore.Current.Document.Database.TransactionManager.StartTransaction())
{
foreach (ADB.ObjectId profileId in alignment.GetProfileIds())
{
var profile = (CDB.Profile)tr.GetObject(profileId, ADB.OpenMode.ForRead);
profiles.Add(Convert(profile));
yield return Convert(profile);
}

tr.Commit();
}

return profiles.Count > 0 ? profiles : null;
}
}
Loading