Imports System.Collections.ObjectModel Imports System.Globalization Imports EgtUILib Imports System.Text.RegularExpressions Imports System.IO Public Module Utility ' dato il nome della componete ricerco il file di configurazione associato Public Function SearchFileConfig(ByRef FrameCompoFile As String, ByRef FrameCompoFileConfig As String) As Boolean Dim CurrDir() As String = FrameCompoFile.Split("\"c) ' DirettorioCompo\NomeFile -> questa è la struttura minima che posso ricevere, altrimenti è un errore If CurrDir.Count > 1 Then FrameCompoFileConfig = CurrDir(0) & "\" & ConstCompo.CONFIGINI_FILE_NAME ' restituisco il percorso completo del file caricato FrameCompoFileConfig = IniFile.m_CompoDir & "\" & FrameCompoFileConfig Dim Local_File As String = IniFile.m_CompoDir & "\" & FrameCompoFile If Not Path.HasExtension(Local_File) Then Local_File = Local_File & LUA_EXTENSION End If If Not File.Exists(Local_File) OrElse Not File.Exists(FrameCompoFileConfig) Then Return False End If Return True End If Return False End Function ' aggiuge un asterisco al nome dell'assemblato da stampare nel bottone Public Function SetNameCurrAssemblyAsNew(ByVal value As String) As String If Not IsNothing(value) Then If Not value.Contains("*") Then If value.EndsWith(".ddf") Then value = value.Insert(value.Count - 4, "*") End If End If End If Return value End Function ' elimina l'asterisco dal nome dell'assemblato Public Function SetNameCurrAssemblyAsSaved(ByVal value As String) As String If Not IsNothing(value) Then If value.Contains("*") Then value = value.Remove(value.IndexOf("*")) value &= DDF_EXTENSION End If End If Return value End Function Friend Function StringToChar(ByVal sValue As String) As Char() Dim cValue(sValue.Count - 1) As Char For Index As Integer = 0 To sValue.Count - 1 cValue(Index) = sValue(Index) Next Return cValue End Function Friend Function CharToString(ByVal cValue() As Char, ByVal sValue As String) As String sValue = String.Empty For Index As Integer = 0 To cValue.Count - 1 sValue &= cValue(Index) Next Return sValue End Function Friend Function DoubleToString(ByVal dVal As Double, ByVal nNumDec As Integer) As String Dim sFormat As String = ConstGen.PART_FRAME + Math.Abs(nNumDec).ToString() Dim sVal As String = dVal.ToString(sFormat, CultureInfo.InvariantCulture) If nNumDec > 0 Then Return sVal.TrimEnd("0".ToCharArray()).TrimEnd(".".ToCharArray) Else Return sVal End If End Function Friend Function StringToDouble(ByVal sVal As String, ByRef dVal As Double) As Boolean Return EgtLuaEvalNumExpr(sVal, dVal) End Function Friend Function LenToString(ByVal dVal As Double, ByVal nNumDec As Integer) As String Return DoubleToString(EgtToUiUnits(dVal), nNumDec) End Function Friend Function StringToLen(ByVal sVal As String, ByRef dVal As Double) As Boolean If EgtLuaEvalNumExpr(sVal, dVal) Then dVal = EgtFromUiUnits(dVal) Return True Else Return False End If End Function ' salto gli spazi vuoti fino alla prima riga piena Friend Function SkipWhiteSpace(Array() As String, ByRef Index As Integer) As Integer While Index < Array.Count Dim sTmp As String = RemoveComment(Array(Index)) If Not String.IsNullOrWhiteSpace(sTmp) Then Exit While Index += 1 End While Return Index End Function Friend Function SkipWhiteSpaceList(ListOfString As List(Of String), ByRef Index As Integer) As Integer While Index < ListOfString.Count Dim sTmp As String = RemoveComment(ListOfString(Index)) If Not String.IsNullOrWhiteSpace(sTmp) Then Exit While Index += 1 End While Return Index End Function #Region "PARAM LUA" ' elimina i commenti nel file lua Friend Function DeleteLuaComment(sLine As String) As String sLine = Trim(sLine) If sLine.Contains("--") And Not sLine.Contains(K_MATCHING_FILE) Then If sLine.StartsWith("--") Then Return String.Empty sLine = Regex.Match(sLine, "\s*(.*?)\s*\--\s*\.*").Groups(1).Value Return sLine Else Return sLine End If End Function ' controllo che il valore in lettura sia una misura Friend Function GetMeasure(sValue As String) As String Dim dVal As Double = 0.0 If sValue.Contains("(") And sValue.Contains(")") And sValue.Contains("inch") Then '' "\s*\((.*?)\s*\)\s*\.*" --> intepreta dalla prima "(" alla prima ")" 'sValue = Regex.Match(sValue, "\s*\((.*?)\s*\)\s*\.*").Groups(1).Value sValue = Trim(sValue.Replace("inch", "")) If sValue.StartsWith("(") And sValue.EndsWith(")") Then Dim ArrayString() As Char = StringToChar(sValue) ArrayString(0) = CChar("§") ArrayString(ArrayString.Count - 1) = CChar("§") sValue = CharToString(ArrayString, sValue) sValue = Trim(sValue.Replace("§", "")) End If If String.IsNullOrEmpty(sValue) Then Return "0.0000" ' ricevo un valore in inches Select Case OptionModule.m_SelectedMeasureUnit Case ConstGen.VAL_INCHES ' se la configurazione è inches non faccio conversioni If StringToDouble(sValue, dVal) Then Return sValue Else Return "0.0000" End If Case ConstGen.MM ' se la configurazione è in mm faccio la conevrsione If InchesToMm(sValue) Then Return sValue Else Return "0.0000" End If End Select ElseIf sValue.Contains("(") And sValue.Contains(")") And Not sValue.Contains("inch") Then 'sValue = Regex.Match(sValue, "\s*\((.*?)\s*\)\s*\.*").Groups(1).Value If String.IsNullOrEmpty(sValue) Then Return "0.0000" ' ricevo un valore in mm Select Case OptionModule.m_SelectedMeasureUnit Case ConstGen.VAL_INCHES ' se la configurazione è inches faccio conversioni If MmToInches(sValue) Then Return sValue Else Return "0.0000" End If Case ConstGen.MM ' se la configurazione è in mm non faccio la conevrsione If StringToDouble(sValue, dVal) Then Return sValue Else Return "0.0000" End If End Select End If ' il valore che ricevo è in mm Select Case OptionModule.m_SelectedMeasureUnit Case ConstGen.VAL_INCHES ' se la configurazione è inches faccio conversione If MmToInches(sValue) Then Return sValue Else Return "0.0000" End If Case ConstGen.MM ' se la configurazione è in mm non faccio la conevrsione If StringToDouble(sValue, dVal) Then Return sValue Else Return "0.0000" End If End Select ' se non è nessuno dei casi restituisco zero Return "0.0000" End Function Friend Function InchesToMm(ByRef sMeasure As String) As Boolean Dim dVal As Double = 0.0 If Not StringToDouble(sMeasure, dVal) Then Return False dVal = dVal * 25.4 sMeasure = DoubleToString(dVal, 4) Return True End Function Friend Function MmToInches(ByRef sMeasure As String) As Boolean Dim dVal As Double = 0.0 If Not StringToDouble(sMeasure, dVal) Then Return False dVal = dVal / 25.4 sMeasure = DoubleToString(dVal, 4) Return True End Function Friend Function ConvertCurrentUnitMeasure(ByRef sMeasure As String) As Boolean If OptionModule.m_SelectedMeasureUnit = ConstGen.VAL_INCHES Then ' non eseguo nessun tipo di conversione Return True Else Return InchesToMm(sMeasure) End If Return False End Function Friend Function ConvertCheckBox(sValue As String) As Boolean If Trim(sValue.ToLower) = "true" Then Return True Else Return False End Function ' elimina gli apici di una variabile Friend Function DeleteSuperScript(sLine As String) As String Return sLine.Replace("'"c, " "c) End Function Friend Function FindNameParam(sLine As String) As String sLine = RegexFunction.GetNameParamInFileTempl(sLine) If String.IsNullOrEmpty(sLine) Then Return String.Empty Return sLine End Function #End Region ' Param lua ' Converte il valore Boolean in una stringa ON/OFF (attributo machining) Friend Function ConvertBooleanToOnOff(bValue As Boolean) As String Return If(bValue, DDF_ON, DDF_OFF) End Function ' converto una stringa in boolean Friend Function ConvertOnOffToBoolean(sItem As String) As Boolean Return (sItem = DDF_ON) End Function Friend Function ConvertMmUnitsToString(bMmUnit As Boolean) As String Return If(bMmUnit, ConstGen.MM, ConstGen.VAL_INCHES.ToLower) End Function Friend Function ConvertStringToMmUnits(sUnit As String) As Boolean If Trim(sUnit) = ConstGen.MM Then Return True ElseIf Trim(sUnit) = ConstGen.VAL_INCHES Then Return False Else Return OptionModule.m_bIsMmUnit End If End Function ' Conversione stringhe con W, H e T in DGD.dW, DGD.dH, DGD.dT e viceversa per lettura/scrittura parametri compo Friend Function ConvertFromDGD(sTypeVar As String, sValue As String) As String Dim sMyVal As String = sValue ' se testo numerico, eseguo opportune conversioni per H,W e T If sTypeVar = ConstGen.INCHES Or sTypeVar = ConstGen.MM Then If OptionModule.m_SelectedMeasureUnit = ConstGen.VAL_INCHES Then sMyVal = sMyVal.Replace("W", "(DGD.dW/25.4)") sMyVal = sMyVal.Replace("H", "(DGD.dH/25.4)") sMyVal = sMyVal.Replace("T", "(DGD.dT/25.4)") Else sMyVal = sMyVal.Replace("W", "DGD.dW") sMyVal = sMyVal.Replace("H", "DGD.dH") sMyVal = sMyVal.Replace("T", "DGD.dT") End If End If Return sMyVal End Function Friend Function ConvertToDGD(sTypeVar As String, sValue As String) As String Dim sMyVal As String = sValue ' se testo numerico, eseguo opportunbe conversioni per H,W e T If sTypeVar = ConstGen.INCHES Or sTypeVar = ConstGen.MM Then If OptionModule.m_SelectedMeasureUnit = ConstGen.VAL_INCHES Then sMyVal = sMyVal.Replace("(DGD.dW/25.4)", "W") sMyVal = sMyVal.Replace("(DGD.dH/25.4)", "H") sMyVal = sMyVal.Replace("(DGD.dT/25.4)", "T") Else sMyVal = sMyVal.Replace("DGD.dW", "W") sMyVal = sMyVal.Replace("DGD.dH", "H") sMyVal = sMyVal.Replace("DGD.dT", "T") End If End If Return sMyVal End Function Friend Sub UpdateUI() ' Costringo ad aggiornare UI Dim nDummy As Integer Application.Current.Dispatcher.Invoke(Windows.Threading.DispatcherPriority.Background, _ New Action(Function() nDummy = 0)) End Sub Friend Function ConcatErrorString(sErrorList As String, sNewErr As String) As String If String.IsNullOrEmpty(sErrorList) Then Return sNewErr Return sErrorList & vbCrLf & sNewErr End Function Friend Sub ClearObservableCollection(Of T)(Ob As ObservableCollection(Of T)) For Index = Ob.Count - 1 To 0 Step -1 Ob.RemoveAt(Index) Next End Sub Friend Sub GetDirectoryCompoFiles(DirectoryPath As String, ByRef FileList As ObservableCollection(Of String), bRemoveNge As Boolean) RecursiveGetDirectoryCompoFiles(DirectoryPath, DirectoryPath, FileList, bRemoveNge) End Sub Friend Sub GetDirectoryCompoModel(HardwareDirPath As String, FolderList As ObservableCollection(Of CompoBrandDir), Optional FrameFolderList As ObservableCollection(Of CompoBrandDir) = Nothing, Optional HardwareFolderList As ObservableCollection(Of CompoBrandDir) = Nothing) 'FrameFolderList = New ObservableCollection(Of CompoBrandDir) Dim BaseDirName As String = Path.GetFileName(HardwareDirPath) Dim FolderArray() As String = Directory.GetDirectories(HardwareDirPath) Dim CurrFolder As CompoBrandDir = Nothing For Each Folder In FolderArray Folder = Folder.Replace("/", "\") CurrFolder = New CompoBrandDir(Folder, BaseDirName) GetDirectoryCompoFiles(Folder, CurrFolder.ModelFileList, False) If CurrFolder.ModelFileList.Count > 0 Then If Folder.ToLower.Contains(FRAME_FOLDER) Then FrameFolderList.Add(CurrFolder) If OptionModule.m_ConfigurationSoftware = ConfigType.Assembly Then HardwareFolderList.Add(CurrFolder) Else FolderList.Add(CurrFolder) End If End If CurrFolder = New CompoBrandDir(Folder, BaseDirName) If Not Folder.ToLower.Contains(FRAME_FOLDER) Then HardwareFolderList.Add(CurrFolder) End If GetDirectoryCompoFiles(Folder, CurrFolder.ModelFileList, True) Next ' Carico l'elenco deifile contenuti nella radice del direttorio Dim FileArray() As String = Directory.GetFiles(HardwareDirPath) CurrFolder = Nothing Dim CurrFrameFolder As CompoBrandDir = Nothing Dim CurrHardwareFolder As CompoBrandDir = Nothing For Each File In FileArray File = File.Replace("/", "\") If Not Path.GetExtension(File) = INI_EXTENSION And Not Path.GetExtension(File) = TEMPL_EXTENSION And (Path.GetExtension(File) = LUA_EXTENSION Or Path.GetExtension(File) = NGE_EXTENSION) And Not File.Contains(MATCHING_FILE_NAME) And Not File.ToLower().Contains("currhardware") Then If File.Contains(FRAME_FOLDER) Then If IsNothing(CurrFrameFolder) Then ' CurrFrameFolder = New CompoBrandDir(Path.GetFileName(HardwareDirPath), BaseDirName & FRAME_FOLDER) CurrFrameFolder = New CompoBrandDir(HardwareDirPath, BaseDirName & FRAME_FOLDER) FrameFolderList.Add(CurrFrameFolder) End If CurrFrameFolder.ModelFileList.Add(If(Path.GetExtension(File) = NGE_EXTENSION, Path.GetFileName(File), Path.GetFileNameWithoutExtension(File))) Else If IsNothing(CurrFolder) Then ' CurrFolder = New CompoBrandDir(Path.GetFileName(HardwareDirPath), BaseDirName) CurrFolder = New CompoBrandDir(HardwareDirPath, BaseDirName) FolderList.Add(CurrFolder) End If CurrFolder.ModelFileList.Add(If(Path.GetExtension(File) = NGE_EXTENSION, Path.GetFileName(File), Path.GetFileNameWithoutExtension(File))) If Not File.Contains(NGE_EXTENSION) Then If IsNothing(CurrHardwareFolder) Then ' CurrHardwareFolder = New CompoBrandDir(Path.GetFileName(HardwareDirPath), BaseDirName) CurrHardwareFolder = New CompoBrandDir(HardwareDirPath, BaseDirName) HardwareFolderList.Add(CurrHardwareFolder) End If CurrHardwareFolder.ModelFileList.Add(If(Path.GetExtension(File) = NGE_EXTENSION, Path.GetFileName(File), Path.GetFileNameWithoutExtension(File))) End If End If End If Next End Sub Private Sub RecursiveGetDirectoryCompoFiles(DirectoryPath As String, BaseDirectory As String, ByRef FileList As ObservableCollection(Of String), bRemoveNge As Boolean) Dim SubDir() As String = Directory.GetDirectories(DirectoryPath) For Index = 0 To SubDir.Count - 1 RecursiveGetDirectoryCompoFiles(SubDir(Index), BaseDirectory, FileList, bRemoveNge) Next Dim Files() As String = Directory.GetFiles(DirectoryPath) For Index = 0 To Files.Count - 1 Dim FileExt As String = Path.GetExtension(Files(Index).ToLower()) ' Se file lua o disegni nge If (FileExt = LUA_EXTENSION And Not Files(Index).ToLower().Contains("currhardware")) Or (FileExt = NGE_EXTENSION And Not bRemoveNge) Then ' Elimino il direttorio base dei componenti Dim sFile As String = Files(Index).Replace(BaseDirectory & "\", "") ' Elimino estensione lua If FileExt = LUA_EXTENSION Then sFile = Path.ChangeExtension(sFile, Nothing) ' Inserisco in lista FileList.Add(sFile) End If Next End Sub End Module Public Class CompoBrandDir Private m_ModelDirGraphic As String Public ReadOnly Property ModelDirGraphic As String Get Return m_ModelDirGraphic End Get End Property Private m_ModelDir As String Public Property ModelDir As String Get Return m_ModelDir End Get Set(value As String) m_ModelDir = value End Set End Property Private m_ModelFileList As New ObservableCollection(Of String) Public Property ModelFileList As ObservableCollection(Of String) Get Return m_ModelFileList End Get Set(value As ObservableCollection(Of String)) m_ModelFileList = value End Set End Property Sub New(Model As String, BaseDirName As String) m_ModelDir = Model m_ModelDirGraphic = RegexFunction.SubStractDirFromPath(Model, BaseDirName) If Path.GetFileName(m_ModelDirGraphic) = BaseDirName Then m_ModelDirGraphic = RAD_DIR End Sub Sub New(CopyCompoBrandDir As CompoBrandDir, HardwareDirPath As String) m_ModelDir = CopyCompoBrandDir.ModelDir Dim BaseDirName As String = Path.GetFileName(HardwareDirPath) m_ModelDirGraphic = RegexFunction.SubStractDirFromPath(CopyCompoBrandDir.ModelDir, BaseDirName) If Path.GetFileName(m_ModelDirGraphic) = BaseDirName Then m_ModelDirGraphic = RAD_DIR End Sub End Class Public Class ItemFile Private Property m_FileName As String Public Property FileName As String Get Return m_FileName End Get Set(value As String) m_FileName = value End Set End Property Private Property m_FileNameVisibility As Visibility = Visibility.Visible Public Property FileNameVisibility As Visibility Get Return m_FileNameVisibility End Get Set(value As Visibility) m_FileNameVisibility = value End Set End Property Sub New(Name As String, NameVisibility As Visibility) m_FileName = Name m_FileNameVisibility = NameVisibility End Sub End Class