Skip to content

Commit

Permalink
implement export of custom address from update dialog
Browse files Browse the repository at this point in the history
  • Loading branch information
Tim Kaiser committed Mar 29, 2023
1 parent 81cdb2c commit 207be0f
Showing 1 changed file with 93 additions and 4 deletions.
97 changes: 93 additions & 4 deletions IFCGeoRefCheckerGUI/ViewModels/UpdateViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,22 @@
using System.Threading.Tasks;

using IFCGeoRefCheckerGUI.ValueConverters;
using Xbim.Ifc;
using System.Data;
using Xbim.Ifc4.Interfaces;
using Xbim.Ifc4.MeasureResource;
using System.ComponentModel;

namespace IFCGeoRefCheckerGUI.ViewModels
{
public class UpdateViewModel : BaseViewModel
{
public GeoRefChecker? geoRefChecker;

private readonly GeoRefChecker? geoRefChecker;
public GeoRefChecker? GeoRefChecker { get { return geoRefChecker; } }


public DelegateCommand? StartExport { get; set; }

private ObservableCollection<Level10UI>? level10s;
public ObservableCollection<Level10UI>? Level10s
{
Expand All @@ -29,6 +38,7 @@ public ObservableCollection<Level10UI>? Level10s
}
}


private ObservableCollection<Level20>? level20s;
public ObservableCollection<Level20>? Level20s
{
Expand All @@ -42,7 +52,6 @@ public ObservableCollection<Level20>? Level20s
}
}
}


private Level50UI? level50;
public Level50UI? Level50
Expand All @@ -63,6 +72,19 @@ public bool IsIFC4
{
get => isIFC4;
}

private string? outIfcPath;
public string? OutIfcPath
{
get => outIfcPath;
set
{
if (outIfcPath != value)
{
outIfcPath = value;
}
}
}
public UpdateViewModel(GeoRefChecker geoRefChecker)
{
this.geoRefChecker = geoRefChecker;
Expand All @@ -73,7 +95,6 @@ public UpdateViewModel(GeoRefChecker geoRefChecker)
}

this.Level10s = new ObservableCollection<Level10UI>(Level10UIs);
//this.Level10s = new ObservableCollection<Level10>(geoRefChecker.LoGeoRef10);
this.Level20s = new ObservableCollection<Level20>(geoRefChecker.LoGeoRef20);

foreach (var lvl50 in geoRefChecker.LoGeoRef50)
Expand All @@ -93,6 +114,74 @@ public UpdateViewModel(GeoRefChecker geoRefChecker)
this.isIFC4 = true;
}

var origPath = this.geoRefChecker.FilePath;
this.OutIfcPath = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(origPath)!, System.IO.Path.GetFileNameWithoutExtension(origPath)) + "_mod.ifc";
this.StartExport = new DelegateCommand((o) => ExecExport(o));
}

private void ExecExport(object o)
{
using (var model = IfcStore.Open(this.GeoRefChecker!.FilePath))
{
using (var trans = model.BeginTransaction())
{
if (this.level10s != null && this.level10s.Count > 0)
{
foreach (var level10UI in level10s)
{
if (!level10UI.hasData()) { continue; }

var entity = model.Instances.FirstOrDefault<IIfcSpatialStructureElement>(elem => elem.GlobalId == level10UI.GUID);
var modelAddress = level10UI.ReferencedEntity == "IfcSite" ? ((IIfcSite)entity).SiteAddress : ((IIfcBuilding)entity).BuildingAddress;

if (modelAddress == null)
{
if (model.SchemaVersion == Xbim.Common.Step21.XbimSchemaVersion.Ifc2X3)
{
modelAddress = model.Instances.New<Xbim.Ifc2x3.ActorResource.IfcPostalAddress>();
}
else
{
modelAddress = model.Instances.New<Xbim.Ifc4.ActorResource.IfcPostalAddress>();
}
}

if (level10UI.ReferencedEntity == "IfcSite")
{
((IIfcSite)entity).SiteAddress = modelAddress;
}
else
{
((IIfcBuilding)entity).BuildingAddress = modelAddress;
}

modelAddress.Country = level10UI.Country;
modelAddress.Town = level10UI.Town;
modelAddress.Region = level10UI.Region;
modelAddress.PostalCode = level10UI.PostalCode;
if (!string.IsNullOrEmpty(level10UI.AddressLine1)) { modelAddress.AddressLines.Add(level10UI.AddressLine1); }
if (!string.IsNullOrEmpty(level10UI.AddressLine2)) { modelAddress.AddressLines.Add(level10UI.AddressLine2); }
}
}

if (this.level20s != null && this.level20s.Count > 0)
{
foreach (var lvl20UI in level20s)
{
var site = model.Instances.FirstOrDefault<IIfcSite>(s => s.GlobalId == lvl20UI.ReferencedEntity!.GlobalId);
site.RefLatitude = lvl20UI.Latitude != null ? IfcCompoundPlaneAngleMeasure.FromDouble((double)lvl20UI.Latitude) : null;
site.RefLongitude = lvl20UI.Longitude != null ? IfcCompoundPlaneAngleMeasure.FromDouble((double)lvl20UI.Longitude) : null;
site.RefElevation = lvl20UI.Elevation != null ? lvl20UI.Elevation : null;
}
}

trans.Commit();

}

model.SaveAs(OutIfcPath);

}
}

public UpdateViewModel()
Expand Down

0 comments on commit 207be0f

Please sign in to comment.