Skip to content

Commit 876a465

Browse files
committed
added main Exit codes for commandline operations (0=Success, 1 = Errors) #29, #BUILD BETA
1 parent f093d2b commit 876a465

File tree

8 files changed

+66
-25
lines changed

8 files changed

+66
-25
lines changed

MainWindow.xaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@
9393

9494
<StackPanel Grid.Column="1" Margin="0,4,0,0">
9595
<CheckBox x:Name="chkUseJSONLog" Content="Use JSON log *Used in commandline only" HorizontalAlignment="Left" VerticalAlignment="Top" Foreground="{DynamicResource MainText}" IsChecked="False" ToolTip="Log messages are written in JSON format. Experimental, V3 format is better supported."/>
96+
<!--<CheckBox x:Name="chkGetAvgTileTimestamp" Content="Get Average timestamp per Tile" HorizontalAlignment="Left" VerticalAlignment="Top" Foreground="{DynamicResource MainText}" IsChecked="False" ToolTip="Expirimental: Calculate avg timestamp from tile points and save to pcroot"/>-->
9697
</StackPanel>
9798

9899
</Grid>

MainWindow.xaml.cs

Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ namespace PointCloudConverter
2626
{
2727
public partial class MainWindow : Window
2828
{
29-
static readonly string version = "09.03.2024";
29+
static readonly string version = "09.06.2024";
3030
static readonly string appname = "PointCloud Converter - " + version;
3131
static readonly string rootFolder = AppDomain.CurrentDomain.BaseDirectory;
3232

@@ -44,15 +44,24 @@ public partial class MainWindow : Window
4444
public static MainWindow mainWindowStatic;
4545
bool isInitialiazing = true;
4646

47+
static List<LasHeader> lasHeaders = new List<LasHeader>();
48+
private readonly ILogger logger;
49+
50+
// progress bar data
51+
static int progressPoint = 0;
52+
static int progressTotalPoints = 0;
53+
static int progressFile = 0;
54+
static int progressTotalFiles = 0;
55+
static DispatcherTimer progressTimerThread;
56+
public static string lastStatusMessage = "";
57+
4758
public MainWindow()
4859
{
4960
InitializeComponent();
5061
mainWindowStatic = this;
5162
Main();
5263
}
5364

54-
private readonly ILogger logger;
55-
5665
private void Main()
5766
{
5867
// check cmdline args
@@ -64,6 +73,9 @@ private void Main()
6473
// default logger
6574
Log.CreateLogger(isJSON: false, version: version);
6675

76+
// default code
77+
Environment.ExitCode = (int)ExitCode.Success;
78+
6779
if (args.Length > 1)
6880
{
6981
AttachConsole(ATTACH_PARENT_PROCESS);
@@ -77,7 +89,6 @@ private void Main()
7789
}
7890
}
7991

80-
8192
Console.ForegroundColor = ConsoleColor.Cyan;
8293
Log.WriteLine("\n::: " + appname + " :::\n");
8394
//Console.WriteLine("\n::: " + appname + " :::\n");
@@ -119,7 +130,7 @@ private void Main()
119130
// hack for console exit https://stackoverflow.com/a/67940480/5452781
120131
SendKeys.SendWait("{ENTER}");
121132
FreeConsole();
122-
Environment.Exit(0);
133+
Environment.Exit(Environment.ExitCode);
123134
}
124135

125136
// regular WPF starts from here
@@ -131,7 +142,6 @@ private void Main()
131142
LoadSettings();
132143
}
133144

134-
static List<LasHeader> lasHeaders = new List<LasHeader>();
135145

136146
// main processing loop
137147
private static void ProcessAllFiles(System.Object importSettingsObject)
@@ -168,6 +178,10 @@ private static void ProcessAllFiles(System.Object importSettingsObject)
168178
{
169179
boundsListTemp.Add(new Float3(res.Item2, res.Item3, res.Item4));
170180
}
181+
else
182+
{
183+
Log.WriteLine("Error> Failed to get bounds from file: " + importSettings.inputFiles[i], LogEvent.Error);
184+
}
171185
}
172186

173187
// print lowest bounds from boundsListTemp
@@ -197,7 +211,11 @@ private static void ProcessAllFiles(System.Object importSettingsObject)
197211
//Debug.WriteLine("\nReading file (" + (i + 1) + "/" + len + ") : " + importSettings.inputFiles[i] + " (" + Tools.HumanReadableFileSize(new FileInfo(importSettings.inputFiles[i]).Length) + ")");
198212
//if (abort==true)
199213
// do actual point cloud parsing for this file
200-
ParseFile(importSettings, i);
214+
var res = ParseFile(importSettings, i);
215+
if (res == false)
216+
{
217+
Log.WriteLine("Error> Failed to parse file: " + importSettings.inputFiles[i], LogEvent.Error);
218+
}
201219
}
202220

203221
stopwatch.Stop();
@@ -221,14 +239,6 @@ void HideProcessingPanel()
221239
gridProcessingPanel.Visibility = Visibility.Hidden;
222240
}
223241

224-
// progress bar data
225-
static int progressPoint = 0;
226-
static int progressTotalPoints = 0;
227-
static int progressFile = 0;
228-
static int progressTotalFiles = 0;
229-
static DispatcherTimer progressTimerThread;
230-
public static string lastStatusMessage = "";
231-
232242
static void StartProgressTimer()
233243
{
234244
progressTimerThread = new DispatcherTimer(DispatcherPriority.Background, Application.Current.Dispatcher);
@@ -266,6 +276,7 @@ static void ProgressTick(object sender, EventArgs e)
266276
if (res == false)
267277
{
268278
Log.WriteLine("Unknown error while initializing reader: " + importSettings.inputFiles[fileIndex]);
279+
Environment.ExitCode = (int)ExitCode.Error;
269280
return (false, 0, 0, 0);
270281
}
271282
var bounds = importSettings.reader.GetBounds();
@@ -278,13 +289,14 @@ static void ProgressTick(object sender, EventArgs e)
278289

279290

280291
// process single file
281-
static void ParseFile(ImportSettings importSettings, int fileIndex)
292+
static bool ParseFile(ImportSettings importSettings, int fileIndex)
282293
{
283294
var res = importSettings.reader.InitReader(importSettings, fileIndex);
284295
if (res == false)
285296
{
286297
Log.WriteLine("Unknown error while initializing reader: " + importSettings.inputFiles[fileIndex]);
287-
return;
298+
Environment.ExitCode = (int)ExitCode.Error;
299+
return false;
288300
}
289301

290302
if (importSettings.importMetadata == true)
@@ -346,7 +358,7 @@ static void ParseFile(ImportSettings importSettings, int fileIndex)
346358
if (writerRes == false)
347359
{
348360
Log.WriteLine("Error> Failed to initialize Writer");
349-
return;
361+
return false;
350362
}
351363

352364
progressPoint = 0;
@@ -466,6 +478,8 @@ static void ParseFile(ImportSettings importSettings, int fileIndex)
466478
}
467479
});
468480
}
481+
482+
return true;
469483
} // ParseFile
470484

471485
private void btnConvert_Click(object sender, RoutedEventArgs e)
@@ -535,7 +549,6 @@ void StartProcess(bool doProcess = true)
535549
txtConsole.Text = cl;
536550
Console.WriteLine(cl);
537551

538-
// TODO lock UI, add cancel button, add progress bar
539552
if (doProcess == true)
540553
{
541554
ParameterizedThreadStart start = new ParameterizedThreadStart(ProcessAllFiles);
@@ -548,6 +561,7 @@ void StartProcess(bool doProcess = true)
548561
{
549562
HideProcessingPanel();
550563
txtConsole.Text = "Operation failed! " + string.Join(Environment.NewLine, importSettings.errors);
564+
Environment.ExitCode = (int)ExitCode.Error;
551565
}
552566
}
553567

@@ -560,7 +574,7 @@ private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs
560574
if (workerThread != null)
561575
{
562576
workerThread.Abort();
563-
Environment.Exit(Environment.ExitCode);
577+
Environment.Exit((int)ExitCode.Cancelled);
564578
}
565579
}
566580

@@ -779,7 +793,7 @@ private void BtnCancel_Click(object sender, RoutedEventArgs e)
779793
if (workerThread != null)
780794
{
781795
workerThread.Abort();
782-
Environment.Exit(Environment.ExitCode);
796+
Environment.Exit((int)ExitCode.Cancelled);
783797
}
784798
}
785799

PointCloudConverter.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@
9797
<Compile Include="Readers\LAZ.cs" />
9898
<Compile Include="Structs\Bounds.cs" />
9999
<Compile Include="Structs\Color.cs" />
100+
<Compile Include="Structs\ExitCodes.cs" />
100101
<Compile Include="Structs\ExportFormat.cs" />
101102
<Compile Include="Structs\Float3.cs" />
102103
<Compile Include="Structs\Metadata\GeoTiffKeys.cs" />

Readers/LAZ.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ bool IReader.InitReader(ImportSettings importSettings, int fileIndex)
3333
//importRGB = importSettings.importRGB;
3434
//importIntensity = importSettings.importIntensity;
3535
customIntensityRange = importSettings.useCustomIntensityRange;
36-
lazReader.open_reader(file, out compressedLAZ);
37-
return true;
36+
var res = lazReader.open_reader(file, out compressedLAZ); // 0 = ok, 1 = error
37+
return (res == 0);
3838
}
3939

4040
LasHeader IReader.GetMetaData(ImportSettings importSettings, int fileIndex)
@@ -277,6 +277,8 @@ Color IReader.GetRGB()
277277

278278
// get point reference
279279
var p = lazReader.point;
280+
// TODO get timestamp
281+
//var pointTime = lazReader.point.gps_time;
280282

281283
// try to detect if colors are outside 0-255 range? TODO just check value?
282284
if (p.rgb[0].ToString("X").Length > 2)

Structs/ExitCodes.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace PointCloudConverter.Structs
2+
{
3+
public enum ExitCode : int
4+
{
5+
Success = 0,
6+
Error = 1,
7+
Cancelled = 2,
8+
}
9+
}

Structs/ImportSettings.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ public class ImportSettings
7070
public bool useJSONLog = false;
7171
public bool importMetadata = false;
7272
public bool importMetadataOnly = false;
73+
public bool averageTimestamp = false; // calculate average timestamp for all points for this tile
7374

7475
public override string ToString()
7576
{

Tools/ArgParser.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,18 @@ public static ImportSettings Parse(string[] args, string rootFolder)
468468
}
469469
break;
470470

471+
case "-averagetimestamp":
472+
Log.WriteLine("averagetimestamp = " + param);
473+
if (param != "true" && param != "false")
474+
{
475+
importSettings.errors.Add("Invalid averagetimestamp parameter: " + param);
476+
}
477+
else
478+
{
479+
importSettings.averageTimestamp = param == "true";
480+
}
481+
break;
482+
471483
case "-json":
472484
Log.WriteLine("json = " + param);
473485

@@ -708,7 +720,7 @@ public static ImportSettings Parse(string[] args, string rootFolder)
708720
//{
709721
// importSettings.errors.Add("Cannot have both -rgb and -intensity enabled");
710722
//}
711-
723+
712724
// must have at least one
713725
if (importSettings.importRGB == false && importSettings.importIntensity == false)
714726
{
@@ -778,6 +790,7 @@ public static ImportSettings Parse(string[] args, string rootFolder)
778790
// importSettings.logEvent = Logger.LogEvent.Error;
779791
// var json = JsonSerializer.Serialize(importSettings.errors);
780792
//}
793+
Environment.ExitCode = (int)ExitCode.Error;
781794
}
782795

783796
// return always, but note that we might have errors

Tools/Tools.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ public static void PrintHelpAndExit(char argSeparator, bool waitEnter = false)
289289
Console.WriteLine("? /? -? help -help /help");
290290
Console.ForegroundColor = ConsoleColor.White;
291291
if (waitEnter == true) Console.ReadLine();
292-
Environment.Exit(0);
292+
Environment.Exit((int)ExitCode.Cancelled);
293293
}
294294

295295
// lookuptable: converts byte 0-255 into float 0-1f

0 commit comments

Comments
 (0)