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

6.3 Deployment #589

Merged
merged 9 commits into from
Sep 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion XML_Adapter/AdapterActions/Pull.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public override IEnumerable<object> Pull(IRequest request, PullType pullType = P
{
if (!System.IO.File.Exists(_fileSettings.GetFullFileName()))
{
BH.Engine.Base.Compute.RecordError("File does not exist to pull from");
BH.Engine.Base.Compute.RecordError($"The file at {_fileSettings.GetFullFileName()} does not exist to pull from.");
return new List<IBHoMObject>();
}

Expand Down
62 changes: 52 additions & 10 deletions XML_Adapter/AdapterActions/Push.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
using BH.oM.Adapter;
using BH.oM.Base;
using System.Reflection;
using BH.oM.Adapters.XML;
using BH.oM.Adapters.XML.Enums;
using System.IO;

namespace BH.Adapter.XML
{
Expand All @@ -41,26 +44,65 @@ public override List<object> Push(IEnumerable<object> objects, String tag = "",
if (pushType == PushType.AdapterDefault)
pushType = m_AdapterSettings.DefaultPushType;

/*if (_xmlSettings == null)
if (actionConfig == null)
{
BH.Engine.Base.Compute.RecordError("Please set some XML Settings on the XML Adapter before pushing to an XML File");
BH.Engine.Base.Compute.RecordError("Please provide configuration settings to push to an XML file");
return new List<object>();
}*/
}

IEnumerable<IBHoMObject> objectsToPush = ProcessObjectsForPush(objects, actionConfig); // Note: default Push only supports IBHoMObjects.
XMLConfig config = actionConfig as XMLConfig;
if (config == null)
{
BH.Engine.Base.Compute.RecordError("Please provide valid a XMLConfig object for pushing to an XML file");
return new List<object>();
}

bool success = true;
IEnumerable<IBHoMObject> objectsToPush = ProcessObjectsForPush(objects, actionConfig); // Note: default Push only supports IBHoMObjects.

MethodInfo methodInfos = typeof(Enumerable).GetMethod("Cast");
foreach (var typeGroup in objectsToPush.GroupBy(x => x.GetType()))
bool success = false;
switch (config.Schema)
{
MethodInfo mInfo = methodInfos.MakeGenericMethod(new[] { typeGroup.Key });
var list = mInfo.Invoke(typeGroup, new object[] { typeGroup });
success &= ICreate(list as dynamic, actionConfig);
case Schema.CSProject:
success = CreateCSProject(objectsToPush, config);
break;
case Schema.GBXML:
success = CreateGBXML(objectsToPush, config);
break;
case Schema.KML:
success = CreateKML(objectsToPush, config);
break;
case Schema.EnergyPlusLoads:
BH.Engine.Base.Compute.RecordError("The EnergyPlusLoads Schema is not supported for push operations at this time");
success = false;
break;
case Schema.Bluebeam:
BH.Engine.Base.Compute.RecordError("The Bluebeam markup schema is not supported for push operations at this time.");
success = false;
break;
default:
success = CreateDefault(objectsToPush, config);
break;
}

if (success && config.RemoveNils)
RemoveNil(_fileSettings);

return success ? objects.ToList() : new List<object>();
}

private static bool RemoveNil(FileSettings file)
{
var path = Path.Combine(file.Directory, file.FileName);
var xmlFile = File.ReadAllLines(path);

xmlFile = xmlFile.Where(x => !x.Trim().Contains("xsi:nil")).ToArray();
xmlFile = xmlFile.Where(x => x != null).ToArray();

File.Delete(path);
File.WriteAllLines(path, xmlFile);

return true;
}
}
}

Expand Down
102 changes: 0 additions & 102 deletions XML_Adapter/CRUD/Create.cs

This file was deleted.

6 changes: 5 additions & 1 deletion XML_Adapter/CRUD/Default/CreateDefault.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,12 @@ private bool CreateDefault<T>(IEnumerable<T> objects, XMLConfig config)
foreach (System.Reflection.PropertyInfo pi in bhomProperties)
overrides.Add(typeof(BHoMObject), pi.Name, new XmlAttributes { XmlIgnore = true });

var exportType = typeof(T);
if (exportType == typeof(IBHoMObject))
exportType = objects.First().GetType();

XmlSerializerNamespaces xns = new XmlSerializerNamespaces();
XmlSerializer szer = new XmlSerializer(typeof(T), overrides);
XmlSerializer szer = new XmlSerializer(exportType, overrides);
TextWriter ms = new StreamWriter(_fileSettings.GetFullFileName());
foreach (var obj in objects)
{
Expand Down
8 changes: 7 additions & 1 deletion XML_Adapter/CRUD/Default/ReadDefault.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,14 @@
object obj = null;
try
{
System.Reflection.PropertyInfo[] bhomProperties = typeof(BHoMObject).GetProperties();
XmlAttributeOverrides overrides = new XmlAttributeOverrides();

foreach (System.Reflection.PropertyInfo pi in bhomProperties)
overrides.Add(typeof(BHoMObject), pi.Name, new XmlAttributes { XmlIgnore = true });

TextReader reader = new StreamReader(_fileSettings.GetFullFileName());
XmlSerializer szer = new XmlSerializer(type);
XmlSerializer szer = new XmlSerializer(type, overrides);
obj = System.Convert.ChangeType(szer.Deserialize(reader), type);
reader.Close();
}
Expand All @@ -65,18 +71,18 @@
return null;
}

try
{
var bhomObj = (IBHoMObject)obj;
return new List<IBHoMObject> { bhomObj };
}
catch(Exception e)
{
BH.Engine.Base.Compute.RecordWarning($"Could not cast deserialised object back to an IBHoMObject. Data returned as CustomObject instead.");
CustomObject cObj = new CustomObject();
cObj.CustomData["Data"] = obj;
return new List<IBHoMObject> { cObj };
}

Check warning on line 85 in XML_Adapter/CRUD/Default/ReadDefault.cs

View check run for this annotation

BHoMBot-CI / beta-code-compliance

XML_Adapter/CRUD/Default/ReadDefault.cs#L74-L85

The use of CustomData within the code is discouraged except in circumstances where volatile data is being used. - For more information see https://bhom.xyz/documentation/DevOps/Code%20Compliance%20and%20CI/Compliance%20Checks/IsUsingCustomData
}
}
}
Expand Down
26 changes: 8 additions & 18 deletions XML_Adapter/CRUD/GBXML/CreateGBXML.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@
using BH.Engine.Adapter;
using BH.Engine.Adapters.XML;

using BH.oM.Spatial.SettingOut;
using BH.Engine.Environment;

namespace BH.Adapter.XML
{
public partial class XMLAdapter : BHoMAdapter
Expand All @@ -58,29 +61,16 @@ private bool CreateGBXML<T>(IEnumerable<T> objects, XMLConfig config)
return false;
}

GBXMLDocumentBuilder doc = objects.ToList()[0] as GBXMLDocumentBuilder;

if(doc == null)
{
BH.Engine.Base.Compute.RecordError("The GBXML schema requires a full model to be provided as a single push operation. For pushing to the GBXML version, you need to plug your objects into a GBXMLDocumentBuilder which collates the objects for pushing and push that to GBXML via this adapter.");
return false;
}
List<Panel> panels = objects.Where(x => x.GetType() == typeof(Panel)).Cast<Panel>().ToList();

List<IBHoMObject> bhomObjects = new List<IBHoMObject>();
bhomObjects.AddRange(doc.Buildings);
bhomObjects.AddRange(doc.Levels);
bhomObjects.AddRange(doc.ShadingElements);
bhomObjects.AddRange(doc.UnassignedPanels);
bhomObjects.AddRange(objects.Where(x => x.GetType() == typeof(oM.Environment.Elements.Building)).Cast<oM.Environment.Elements.Building>());
bhomObjects.AddRange(objects.Where(x => x.GetType() == typeof(Level)).Cast<Level>());

if (settings.ExportDetail == oM.Adapters.XML.Enums.ExportDetail.Full)
{
foreach (List<Panel> p in doc.ElementsAsSpaces)
bhomObjects.AddRange(p);
}
bhomObjects.AddRange(panels);
else if(settings.ExportDetail == oM.Adapters.XML.Enums.ExportDetail.BuildingShell)
{
bhomObjects.AddRange(doc.ElementsAsSpaces.ExternalElements());
}
bhomObjects.AddRange(panels.ToSpaces().ExternalElements());
else
{
BH.Engine.Base.Compute.RecordError("The ExportDetail has not been appropriately set. Please set the ExportDetail to continue");
Expand Down
62 changes: 0 additions & 62 deletions XML_Adapter/Properties/AssemblyInfo.cs

This file was deleted.

Loading