Skip to content

Commit

Permalink
Squashed commit of the following: #1043 log & error handling
Browse files Browse the repository at this point in the history
commit e249564
Author: lorinczandrea <[email protected]>
Date:   Thu Jul 18 14:07:26 2024 +0200

    :sparkles: add log to ModelOpen & error handling in Core.FemDesignConnection

    #1043
  • Loading branch information
lorinczandrea committed Jul 18, 2024
1 parent bf639f5 commit 6c9f41b
Show file tree
Hide file tree
Showing 8 changed files with 178 additions and 12 deletions.
30 changes: 21 additions & 9 deletions FemDesign.Core/ErrorHandling/ErrorHandling.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
using System.Text;
using System.Threading.Tasks;
using System.Text.RegularExpressions;
using System.IO;
using System.Web;
using FemDesign.Results;

namespace FemDesign.Utils
{
Expand All @@ -18,12 +21,20 @@ public static class ErrorHandling
"No declared structure to calculate.",
"None of the Construction stages contains any load case.",
"Time-dependent analysis is requested, but none of the object has Time-dependent property.",
"Unknown error."
"Unknown error.",
"Model loading problems",
"The loading process aborted!",
@"^Error",
@"^ERROR",
};

public readonly static List<string> WarningMessase = new List<string>{
public readonly static List<string> WarningMessage = new List<string>{
"Large nodal displacement or rotation was found.",
"One or more identical copies of \\d+ structural elements and loads are found.",
@"One or more identical copies of \\d+ structural elements and loads are found.",
"Not enough data for time history calculation.",
"Applied reinforcement is missing for cracked section analysis.",
@"^Warning",
@"^WARNING",
};


Expand All @@ -34,9 +45,9 @@ public static string HasError(List<string> messages, out string error)
{
foreach (var errorMsg in ErrorMessage)
{
if (Regex.IsMatch(message, errorMsg))
if (Regex.IsMatch(message, errorMsg, RegexOptions.Multiline))
{
error = errorMsg;
error = message;
return error;
}
}
Expand All @@ -51,17 +62,18 @@ public static string HasWarning(List<string> messages, out string warning)
warning = null;
foreach (string message in messages)
{
foreach(var warningMsg in WarningMessase)
{
if(Regex.IsMatch(message, warningMsg))
foreach(var warningMsg in WarningMessage)
{
if(Regex.IsMatch(message, warningMsg, RegexOptions.Multiline))
{
warning = warningMsg;
warning = message;
return warning;
}
}
}

return warning;
}

}
}
1 change: 0 additions & 1 deletion FemDesign.Core/FemDesign.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,6 @@
<EmbeddedResource Include="Resources\loadCoefficients\loadCoefficients_S.struxml" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
<EmbeddedResource Include="Resources\loadCoefficients\loadCoefficients_N.struxml" />
<EmbeddedResource Include="Resources\sectionLibrary\sections.struxml" />
<EmbeddedResource Include="Resources\Vehicles\vehicles.struxml" />
Expand Down
22 changes: 22 additions & 0 deletions FemDesign.Core/FemDesignConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,13 @@ public class FemDesignConnection : IDisposable
/// </summary>
private bool _keepOpen;

/// <summary>
/// Parameter for FemDesign.Grasshopper project components
/// </summary>
internal bool _grasshopperMode = false;

public List<string> Log = new List<string>();

public delegate void OnOutputEvent(string output);
/// <summary>
/// Occurs whenever FEM-Design writes a new log message.
Expand Down Expand Up @@ -123,7 +129,10 @@ public FemDesignConnection(
if (isGuid == false)
OnOutput?.Invoke(message);
};

SetVerbosity(verbosity);

OnOutput += OutputErrorHandling;
}

/// <summary>
Expand Down Expand Up @@ -153,6 +162,19 @@ public string SetFemDesignDirectory(string fdInstallationDir)

throw new ArgumentNullException($"Default FEM-Design installation directory is not found. Input directory `{fdInstallationDir}` does not exist!");
}
private void OutputErrorHandling(string message)
{
this.Log.Add(message);

if(!_grasshopperMode)
{
var msg = new List<string> { message };
if (FemDesign.Utils.ErrorHandling.HasError(msg, out string error) != null)
{
throw new Exception(error);
}
}
}

private void _processExited(object sender, EventArgs e)
{
Expand Down
3 changes: 3 additions & 0 deletions FemDesign.Examples/C#/Example 5 - Design a beam/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ static void Main(string[] args)
Console.WriteLine();
Console.WriteLine("Log:");
foreach (var line in outputLog) Console.WriteLine(line);

Console.WriteLine("\nDone!");
Console.ReadKey();
}
}
}
3 changes: 2 additions & 1 deletion FemDesign.Grasshopper/FemDesign.Grasshopper.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@
<Compile Include="Pipe\PipeGetFEA.cs" />
<Compile Include="Pipe\PipeGetModel.cs" />
<Compile Include="Pipe\PipeGetQuantities.cs" />
<Compile Include="Pipe\PipeOpen.cs" />
<Compile Include="Pipe\PipeReadEigenFrequencyResults.cs" />
<Compile Include="Pipe\PipeReadStabilityResults.cs" />
<Compile Include="Pipe\WIP_PipeReadStabilityResults.cs" />
Expand Down Expand Up @@ -199,7 +200,7 @@
<Compile Include="Materials\MaterialGetByNameIndex.cs" />
<Compile Include="Materials\SoilMaterialFromStruxml.cs" />
<Compile Include="Materials\TimberPanelTypeFromStruxml.cs" />
<Compile Include="Pipe\PipeOpen.cs" />
<Compile Include="Pipe\OBSOLETE\PipeOpen_OBSOLETE2305.cs" />
<Compile Include="Model\ModelReadFromFile.cs" />
<Compile Include="Pipe\PipeRunDesign.cs" />
<Compile Include="Pipe\PipeSaveAs.cs" />
Expand Down
1 change: 1 addition & 0 deletions FemDesign.Grasshopper/Pipe/FemDesignConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ protected override void SolveInstance(IGH_DataAccess DA)
_connection.Dispose();

_connection = new FemDesignConnection(fd_installation_folder, minimized, outputDir: outputDir, tempOutputDir: delteDir);
_connection._grasshopperMode = true;

string verbosity = "";
if (DA.GetData("Verbosity", ref verbosity))
Expand Down
122 changes: 122 additions & 0 deletions FemDesign.Grasshopper/Pipe/OBSOLETE/PipeOpen_OBSOLETE2305.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
// https://strusoft.com/
using System;
using System.Data.Common;
using Grasshopper.Kernel;
using GrasshopperAsyncComponent;
using Rhino.Commands;

namespace FemDesign.Grasshopper
{
public class PipeOpen_OBSOLETE2305 : GH_AsyncComponent
{
public PipeOpen_OBSOLETE2305() : base("FEM-Design.OpenModel", "OpenModel", "Open model in FEM-Design.", CategoryName.Name(), SubCategoryName.Cat8())
{
BaseWorker = new ModelOpenWorker_OBSOLETE2305(this);
}
protected override void RegisterInputParams(GH_InputParamManager pManager)
{
pManager.AddGenericParameter("Connection", "Connection", "FEM-Design connection.", GH_ParamAccess.item);
pManager.AddGenericParameter("Model", "Model", "Model to open or file path.", GH_ParamAccess.item);
pManager.AddBooleanParameter("RunNode", "RunNode", "If true node will execute. If false node will not execute.", GH_ParamAccess.item, true);
pManager[pManager.ParamCount - 1].Optional = true;
}
protected override void RegisterOutputParams(GH_OutputParamManager pManager)
{
pManager.AddGenericParameter("Connection", "Connection", "FEM-Design connection.", GH_ParamAccess.item);
pManager.AddGenericParameter("Model", "Model", "", GH_ParamAccess.item);
pManager.AddBooleanParameter("Success", "Success", "", GH_ParamAccess.item);
}
protected override System.Drawing.Bitmap Icon => FemDesign.Properties.Resources.FEM_open;

public override Guid ComponentGuid => new Guid("96dc72e0-c0c1-4081-ac2b-56be85905fb2");
public override GH_Exposure Exposure => GH_Exposure.hidden;
}

/// <summary>
/// https://github.com/specklesystems/GrasshopperAsyncComponent
/// </summary>
public class ModelOpenWorker_OBSOLETE2305 : WorkerInstance
{
/* INPUT */
dynamic model = null;
Model newModel = null;
FemDesignConnection connection = null;
bool runNode = false;

/* OUTPUT */
bool success = false;

public ModelOpenWorker_OBSOLETE2305(GH_Component component) : base(component) { }

public override void DoWork(Action<string, string> ReportProgress, Action Done)
{
//// ?? Check for task cancellation!
//if (CancellationToken.IsCancellationRequested) return;
try
{
if (connection == null)
{
RuntimeMessages.Add((GH_RuntimeMessageLevel.Warning, "Connection is null."));
Done();
return;
}

if (connection.IsDisconnected)
{
_success = false;
throw new Exception("Connection to FEM-Design have been lost.");
}

if (connection.HasExited)
{
_success = false;
throw new Exception("FEM-Design have been closed.");
}

if (runNode == false)
{
_success = false;
connection = null;
RuntimeMessages.Add((GH_RuntimeMessageLevel.Warning, "Run node set to false."));
Done();
return;
}

ReportProgress("", "");
connection.Open(model.Value);
newModel = connection.GetModel();
success = true;
}
catch (Exception ex)
{
RuntimeMessages.Add( (GH_RuntimeMessageLevel.Error, ex.Message) );
connection = null;
success = false;
}

Done();
}

public override WorkerInstance Duplicate() => new ModelOpenWorker_OBSOLETE2305(Parent);

public override void GetData(IGH_DataAccess DA, GH_ComponentParamServer Params)
{
DA.GetData("Connection", ref connection);
DA.GetData("Model", ref model);
DA.GetData("RunNode", ref runNode);
}

public override void SetData(IGH_DataAccess DA)
{
foreach (var (level, message) in RuntimeMessages)
{
Parent.AddRuntimeMessage(level, message);
}

DA.SetData("Connection", connection);
DA.SetData("Model", newModel);
DA.SetData("Success", success);

}
}
}
8 changes: 7 additions & 1 deletion FemDesign.Grasshopper/Pipe/PipeOpen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,12 @@ protected override void RegisterOutputParams(GH_OutputParamManager pManager)
{
pManager.AddGenericParameter("Connection", "Connection", "FEM-Design connection.", GH_ParamAccess.item);
pManager.AddGenericParameter("Model", "Model", "", GH_ParamAccess.item);
pManager.AddTextParameter("Log", "Log", "", GH_ParamAccess.list);
pManager.AddBooleanParameter("Success", "Success", "", GH_ParamAccess.item);
}
protected override System.Drawing.Bitmap Icon => FemDesign.Properties.Resources.FEM_open;

public override Guid ComponentGuid => new Guid("96dc72e0-c0c1-4081-ac2b-56be85905fb2");
public override Guid ComponentGuid => new Guid("AF4D71BF-693D-48FA-8C63-9F344A54DDAC");
public override GH_Exposure Exposure => GH_Exposure.primary;
}

Expand Down Expand Up @@ -82,9 +83,13 @@ public override void DoWork(Action<string, string> ReportProgress, Action Done)
return;
}

connection.OnOutput += onOutput;

ReportProgress("", "");
connection.Open(model.Value);
newModel = connection.GetModel();

connection.OnOutput -= onOutput;
success = true;
}
catch (Exception ex)
Expand Down Expand Up @@ -115,6 +120,7 @@ public override void SetData(IGH_DataAccess DA)

DA.SetData("Connection", connection);
DA.SetData("Model", newModel);
DA.SetDataList("Log", _log);
DA.SetData("Success", success);

}
Expand Down

0 comments on commit 6c9f41b

Please sign in to comment.