diff --git a/RocketcadManager/Assembly.cs b/RocketcadManager/Assembly.cs index 65ac18f..d51653f 100644 --- a/RocketcadManager/Assembly.cs +++ b/RocketcadManager/Assembly.cs @@ -103,8 +103,7 @@ public TreeNode SubComponentTree() public override bool NameOk() { - // Assembly names must end with 00 - return Regex.IsMatch(ComponentFileInfo.Name, @"^([0-9]{2}-)+00(\s.*)*\.(?i)SLDASM(?-i)$"); + return Regex.IsMatch(ComponentFileInfo.Name, ConstantPaths.ValidAssemblyRegex); } } } diff --git a/RocketcadManager/CadComponent.cs b/RocketcadManager/CadComponent.cs index 2e52cf6..49e5f8e 100644 --- a/RocketcadManager/CadComponent.cs +++ b/RocketcadManager/CadComponent.cs @@ -46,38 +46,36 @@ public CadComponent(FileInfo fileInfo, Folder parentFolder) return cadInfo; } - public TreeNode GetNode() + public string GetImageKey() { - TreeNode thisNode = new TreeNode(); - thisNode.Tag = this; - thisNode.Text = ComponentFileInfo.Name; - if (!NameOk()) + if (!NameOk() || !LocationOk()) { if (MissingComponentError || LoadingError) - SetImageKey(thisNode, "WarningErrorFile"); + return "WarningErrorFile"; else if (!HasInfo) - SetImageKey(thisNode, "WarningQuestionFile"); + return "WarningQuestionFile"; else - SetImageKey(thisNode, "WarningFile"); + return "WarningFile"; } else if (MissingComponentError || LoadingError) - SetImageKey(thisNode, "ErrorFile"); + return "ErrorFile"; else if (!HasInfo) - SetImageKey(thisNode, "QuestionFile"); + return "QuestionFile"; else - SetImageKey(thisNode, "File"); - return thisNode; + return "File"; } - private void SetImageKey(TreeNode node, string imageKey) + public TreeNode GetNode() { - node.ImageKey = imageKey; - node.SelectedImageKey = imageKey; - } + TreeNode thisNode = new TreeNode(); + string imageKey = GetImageKey(); - public virtual bool NameOk() - { - return Regex.IsMatch(ComponentFileInfo.Name, @"^([0-9]{2}-)+[0-9]{2}(\s.*)*\.(?i)(SLDASM|SLDPRT)(?-i)$"); + thisNode.Tag = this; + thisNode.Text = ComponentFileInfo.Name; + thisNode.ImageKey = imageKey; + thisNode.SelectedImageKey = imageKey; + + return thisNode; } public int UsageCount() @@ -104,5 +102,17 @@ public TreeNode DependancyTree() } public abstract void Save(); + + public abstract bool NameOk(); + + public bool LocationOk() + { + if (ParentFolder == null) + return true; + if (ConstantPaths.IgnoreTopLevelFolders && ParentFolder.ParentFolder == null) + return true; + return Regex.Match(ParentFolder.Path.Name, ConstantPaths.ParentFolderRegex).Groups[1].Value + == Regex.Match(ComponentFileInfo.Name, ConstantPaths.ChildFileRegex).Groups[1].Value; + } } } diff --git a/RocketcadManager/Folder.cs b/RocketcadManager/Folder.cs index 76f96f4..33641fb 100644 --- a/RocketcadManager/Folder.cs +++ b/RocketcadManager/Folder.cs @@ -1,4 +1,5 @@ -using System; +using RocketcadManagerLib; +using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.IO; @@ -30,7 +31,7 @@ public TreeNode DirectoryTree() thisNode.Tag = this; thisNode.Text = Path.Name; - if (NameOk()) + if (NameOk() && LocationOk()) SetImageKey(thisNode, "Folder"); else SetImageKey(thisNode, "WarningFolder"); @@ -61,7 +62,21 @@ public bool IsEmpty() public bool NameOk() { - return Regex.IsMatch(Path.Name, @"^([0-9]{2}-)+[0-9]{2}($|\s)"); + // Ignore top level folders + if (ConstantPaths.IgnoreTopLevelFolders && ParentFolder == null) + return true; + return Regex.IsMatch(Path.Name, ConstantPaths.ValidFolderRegex); + } + + public bool LocationOk() + { + if (ParentFolder == null) + return true; + // Ignore top level folders + if (ConstantPaths.IgnoreTopLevelFolders && ParentFolder.ParentFolder == null) + return true; + return Regex.Match(ParentFolder.Path.Name, ConstantPaths.ParentFolderRegex).Groups[1].Value + == Regex.Match(Path.Name, ConstantPaths.ChildFolderRegex).Groups[1].Value; } } } diff --git a/RocketcadManager/Part.cs b/RocketcadManager/Part.cs index 1e9a5c1..1d73542 100644 --- a/RocketcadManager/Part.cs +++ b/RocketcadManager/Part.cs @@ -27,8 +27,7 @@ public override void Save() public override bool NameOk() { - // Part names cannot end with 00 - return base.NameOk() && !Regex.IsMatch(ComponentFileInfo.Name, @"^([0-9]{2}-)+00(\s.*)*\.(?i)SLDASM(?-i)$"); + return Regex.IsMatch(ComponentFileInfo.Name, ConstantPaths.ValidPartRegex); } } } diff --git a/RocketcadManager/WarningsListForm.cs b/RocketcadManager/WarningsListForm.cs index 37cac4a..51ae810 100644 --- a/RocketcadManager/WarningsListForm.cs +++ b/RocketcadManager/WarningsListForm.cs @@ -29,6 +29,8 @@ private void WarningsListForm_Load(object sender, EventArgs e) StringBuilder errorStrBuilder = new StringBuilder(); if (!cadComponent.NameOk()) errorStrBuilder.Append("Naming violation, "); + if (!cadComponent.LocationOk()) + errorStrBuilder.Append("Name does not match parent folder, "); if (!cadComponent.HasInfo) errorStrBuilder.Append("Missing info file, "); if (cadComponent.MissingComponentError) @@ -41,20 +43,7 @@ private void WarningsListForm_Load(object sender, EventArgs e) string errorStr = errorStrBuilder.ToString(); // Set error icons - // TODO: Combine this with the similar CadComponent method - if (!cadComponent.NameOk()) - { - if (cadComponent.MissingComponentError || cadComponent.LoadingError) - AddWarning(cadComponent, errorStr, "WarningErrorFile"); - else if (!cadComponent.HasInfo) - AddWarning(cadComponent, errorStr, "WarningQuestionFile"); - else - AddWarning(cadComponent, errorStr, "WarningFile"); - } - else if (cadComponent.MissingComponentError || cadComponent.LoadingError) - AddWarning(cadComponent, errorStr, "ErrorFile"); - else if (!cadComponent.HasInfo) - AddWarning(cadComponent, errorStr, "QuestionFile"); + AddWarning(cadComponent, errorStr, cadComponent.GetImageKey()); } foreach (ColumnHeader column in listViewWarnings.Columns) { diff --git a/RocketcadManagerLib/ConstantPaths.cs b/RocketcadManagerLib/ConstantPaths.cs index 8f3c8b1..aaf8b66 100644 --- a/RocketcadManagerLib/ConstantPaths.cs +++ b/RocketcadManagerLib/ConstantPaths.cs @@ -14,5 +14,14 @@ public static class ConstantPaths public static DirectoryInfo LogDir { get { return new DirectoryInfo(dataFolder + @"\logs"); } } public static FileInfo ConfigFile { get { return new FileInfo(dataFolder + @"\config.json"); } } + + public static readonly string ValidPartRegex = @"^([0-9]{2}-)+(0[1-9]|[1-9][0-9])(\s.*)?\.(?i)SLDPRT(?-i)$"; + public static readonly string ValidAssemblyRegex = @"^([0-9]{2}-)+00(\s.*)?\.(?i)SLDASM(?-i)$"; + public static readonly string ValidFolderRegex = @"^([0-9]{2}-)+[0-9]{2}($|\s)"; + + public static readonly string ParentFolderRegex = @"^(([0-9]{2}-)+[0-9]{2})"; + public static readonly string ChildFolderRegex = @"^([0-9]{2}(-[0-9]{2})*)-[0-9]{2}($|\s)"; + public static readonly string ChildFileRegex = @"^([0-9]{2}(-[0-9]{2})*)-[0-9]{2}(\s.*)?\.(?i)(SLDASM|SLDPRT)(?-i)$"; + public static readonly bool IgnoreTopLevelFolders = true; } }