Files
EgtDOORCreator/Utility.vb
T
Dario Sassi e87fe6107f EgtDOORCreator :
- ulteriore revisione.
2017-11-28 21:49:49 +00:00

182 lines
6.7 KiB
VB.net

Imports System.Collections.ObjectModel
Imports System.Globalization
Imports EgtUILib
Imports System.Text.RegularExpressions
Imports System.IO
Public Module Utility
Public Function SearchFileConfig(ByRef FrameCompoFile As String, ByRef FrameCompoFileConfig As String) As Boolean
Dim bFileConfig As Boolean = False
FrameCompoFile = IniFile.m_CompoDir & "\" & FrameCompoFile
FrameCompoFileConfig = FrameCompoFile
If Not File.Exists(FrameCompoFileConfig) Then
FrameCompoFileConfig = FrameCompoFileConfig & ".lua"
End If
If File.Exists(FrameCompoFileConfig) Then
FrameCompoFileConfig = FrameCompoFileConfig.Remove(FrameCompoFileConfig.LastIndexOf("\"))
While Not bFileConfig And Not FrameCompoFileConfig.Count < 2
Dim CurrDirectory() As String = Directory.GetFiles(FrameCompoFileConfig)
Dim Index As Integer = 0
FrameCompoFileConfig &= "\" & ConstCompo.CONFIGINI_FILE_NAME
For Index = 0 To CurrDirectory.Count - 1
If CurrDirectory(Index) = FrameCompoFileConfig Then
bFileConfig = True
Exit While
End If
Next
FrameCompoFileConfig = FrameCompoFileConfig.Remove(FrameCompoFileConfig.LastIndexOf("\"))
FrameCompoFileConfig = FrameCompoFileConfig.Remove(FrameCompoFileConfig.LastIndexOf("\"))
End While
End If
Return bFileConfig
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 &= K_DDFEXTENSION
End If
End If
Return value
End Function
Friend Function StringToChar(ByVal sValue As String) As Char()
Dim cValue(sValue.Count) 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 = "F" + 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
' restituice la riga scommentata
Public Function FindComments(sLine As String) As String
' se riapplicando la lettura della riga commentata ottengo che esite almeno un # allora
If SearchComments(sLine) Then
If Regex.Match(sLine, "\s*##\s*(.*?\b)\s*$").Groups(1).Value.Contains("#") Then
' è ancora un commento, quindi restituisco una riga vuota
Return SearchStringBeforeComments(sLine)
Return String.Empty
Else
Return Regex.Match(sLine, "\s*##\s*(.*?\b)\s*$").Groups(1).Value
End If
Else
' altrimenti restituisci la stringa che precede il commento
Return sLine
End If
End Function
' salto gli spazi vuoti finoa alla prima riga piena
Public Function SkipWhiteSpace(Array() As String, ByRef Index As Integer) As Integer
Array(Index) = FindComments(Array(Index))
While String.IsNullOrWhiteSpace(Array(Index)) And Index < Array.Count - 1
Index += 1
Array(Index) = FindComments(Array(Index))
End While
Return Index
End Function
'Converte il valore Boolean in una stringa ON/OFF (attributo machining)
Public Function ConvertBooleanToOnOff(bValue As Boolean) As String
If bValue Then
Return K_ON
Else
Return K_OFF
End If
End Function
' converto una stringa in boolean
Public Function ConvertOnOffToBoolean(sItem As String) As Boolean
If sItem = K_ON Then
Return True
Else
Return False
End If
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 ReadMsg(sValue As String) As String
' se numerico
If IsNumeric(sValue) Then
' ritorno il messaggio associato
Return EgtMsg(CInt(sValue))
Else
' altrimenti ritorno il nome ricevuto
Return sValue
End If
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
End Module
Public Class SplitConverter
Implements IValueConverter
Public Function Convert(value As Object, targetType As Type, parameter As Object, culture As System.Globalization.CultureInfo) As Object Implements IValueConverter.Convert
Dim dValue As Double = CDbl(value)
Dim nParam As Integer = CInt(parameter)
Return dValue / nParam
End Function
Public Function ConvertBack(value As Object, targetType As Type, parameter As Object, culture As System.Globalization.CultureInfo) As Object Implements IValueConverter.ConvertBack
Throw New NotImplementedException
End Function
End Class