Imports System.Collections.ObjectModel Imports System.IO Imports EgtBEAMWALL.Core Imports EgtUILib Imports EgtWPFLib5 Imports Newtonsoft.Json Public Class ForcedStrategyPanelVM Inherits VMBase #Region "FIELDS & PROPERTIES" Public Const AUTOMATICSTRATEGYID As String = "AUTOMATIC" Private m_StrategyList As New ObservableCollection(Of Strategy) Public Property StrategyList As ObservableCollection(Of Strategy) Get Return m_StrategyList End Get Set(value As ObservableCollection(Of Strategy)) m_StrategyList = value End Set End Property Private m_SelStrategy As Strategy Public Property SelStrategy As Strategy Get Return m_SelStrategy End Get Set(value As Strategy) m_SelStrategy = value NotifyPropertyChanged(NameOf(SelStrategy)) End Set End Property Private m_CurrentFeature As BTLFeatureM ' Definizione Comandi Private m_cmdOk As ICommand Private m_cmdCancel As ICommand #Region "Messages" Public ReadOnly Property UserMsg As String Get Return EgtMsg(61748) End Get End Property Public ReadOnly Property AdvanceMsg As String Get Return EgtMsg(61749) End Get End Property #End Region ' Messages #End Region ' Fields & Properties #Region "CONSTRUCTOR" Sub New() Map.SetRefForcedStrategyPanelVM(Me) End Sub #End Region ' Constructor #Region "METHODS" Friend Sub Init(Feature As BTLFeatureM) m_CurrentFeature = Feature Dim nTmpGRP As Integer = CalcBeamPrivateProfileGRP(Feature.nSelGRP) LoadStrategyListFromTopology(Feature.nFeatureId, Feature.nPRC, nTmpGRP) ' leggo eventuali parametri forzati Dim sStrategyID As String = "" EgtGetInfo(m_CurrentFeature.nFeatureId, ConstBeam.STRATEGY, sStrategyID) If String.IsNullOrWhiteSpace(sStrategyID) Then Return Dim ForcedStrategy As Strategy = m_StrategyList.FirstOrDefault(Function(x) x.sStrategyId = sStrategyID) If IsNothing(ForcedStrategy) Then Else SelStrategy = ForcedStrategy For Each Param In SelStrategy.ParameterList Select Case Param.GetType() Case GetType(BooleanStrategyParameter) Dim bValue As Boolean = False EgtGetInfo(m_CurrentFeature.nFeatureId, m_SelStrategy.sStrategyId & "_" & Param.sNameNge, bValue) DirectCast(Param, BooleanStrategyParameter).SetValue(bValue) Case GetType(DoubleStrategyParameter) Dim dValue As Double = 0 EgtGetInfo(m_CurrentFeature.nFeatureId, m_SelStrategy.sStrategyId & "_" & Param.sNameNge, dValue) DirectCast(Param, DoubleStrategyParameter).SetValue(dValue) Case GetType(ComboStrategyParameter) Dim sValue As String = String.Empty EgtGetInfo(m_CurrentFeature.nFeatureId, m_SelStrategy.sStrategyId & "_" & Param.sNameNge, sValue) Dim ComboStrategyParameter As ComboStrategyParameter = DirectCast(Param, ComboStrategyParameter) Dim SelCombo = ComboStrategyParameter.ComboList.FirstOrDefault(Function(x) x.sValue = sValue) ComboStrategyParameter.SelValue = SelCombo Case GetType(StringStrategyParameter) Dim sValue As String = String.Empty EgtGetInfo(m_CurrentFeature.nFeatureId, m_SelStrategy.sStrategyId & "_" & Param.sNameNge, sValue) DirectCast(Param, StringStrategyParameter).SetValue(sValue) Case GetType(ListStrategyParameter) Dim sValue As String = String.Empty EgtGetInfo(m_CurrentFeature.nFeatureId, m_SelStrategy.sStrategyId & "_" & Param.sNameNge, sValue) Dim ListStrategyParameter As ListStrategyParameter = DirectCast(Param, ListStrategyParameter) Dim SelValue As ToolParameter = Nothing Dim sTrimValue As String = sValue.TrimEnd(";"c) Dim sTmpValue As String() = sTrimValue.Split(";"c) Dim sSplitValue As String() = Nothing For Each ItemValue As String In sTmpValue sSplitValue = ItemValue.Split(","c) For Each ItemTool As ToolParameter In ListStrategyParameter.ListValue If sSplitValue(0) = ItemTool.sUUID AndAlso sSplitValue(1) = ItemTool.sName Then ItemTool.bIsActive = True SelValue = ListStrategyParameter.ListValue.FirstOrDefault(Function(x) x.sName = ItemTool.sName AndAlso x.sUUID = ItemTool.sUUID) End If Next Next ListStrategyParameter.SelValue = SelValue End Select Next End If End Sub Friend Function LoadStrategyListFromTopology(nFeatureId As Integer, nPRC As Integer, nGRP As Integer) As Boolean m_StrategyList.Clear() Dim sTopologyName As String = "Feature" Dim sStrategiesDirPath As String = Map.refMainWindowVM.MainWindowM.GetStrategiesDirPath(ProjectManagerVM.CurrProd.nType) Dim sAvailableStrategiesFilePath As String = sStrategiesDirPath & "\" & AVAILABLESTRATEGYLIST_FILE & ".json" Dim sReadedFile As String = String.Empty ' carico default If File.Exists(sAvailableStrategiesFilePath) Then sReadedFile = File.ReadAllText(sAvailableStrategiesFilePath) Dim JsonStrategyFeatureList As List(Of JsonAvailableStrategyFeature) = JsonConvert.DeserializeObject(Of List(Of JsonAvailableStrategyFeature))(sReadedFile) Dim JsonAvailableStrategyFeature As JsonAvailableStrategyFeature = JsonStrategyFeatureList.FirstOrDefault(Function(x) x.nGrp = nGRP AndAlso x.nPrc = nPRC) If IsNothing(JsonAvailableStrategyFeature) Then Return False Dim JsonAvailableTopology As JsonAvailableTopology = JsonAvailableStrategyFeature.TopologyList.FirstOrDefault(Function(x) x.sName = sTopologyName) If IsNothing(JsonAvailableTopology) Then ' ricavo topologia da feature FindTopologyFromFeature(nFeatureId, sTopologyName) JsonAvailableTopology = JsonAvailableStrategyFeature.TopologyList.FirstOrDefault(Function(x) x.sName = sTopologyName) End If If IsNothing(JsonAvailableTopology) Then Return False m_StrategyList = New ObservableCollection(Of Strategy)((From JsonAvailableStrategy In JsonAvailableTopology.StrategyList Select JsonAvailableStrategy.Deserialize(ProjectManagerVM.CurrProd.nType, -1)).ToList()) End If If File.Exists(sAvailableStrategiesFilePath) Then sReadedFile = File.ReadAllText(sAvailableStrategiesFilePath) Dim JsonStrategyFeatureList As List(Of JsonAvailableStrategyFeature) = JsonConvert.DeserializeObject(Of List(Of JsonAvailableStrategyFeature))(sReadedFile) Dim JsonAvailableStrategyFeature As JsonAvailableStrategyFeature = JsonStrategyFeatureList.FirstOrDefault(Function(x) x.nGrp = nGRP AndAlso x.nPrc = nPRC) If IsNothing(JsonAvailableStrategyFeature) Then Return False Dim JsonAvailableTopology As JsonAvailableTopology = JsonAvailableStrategyFeature.TopologyList.FirstOrDefault(Function(x) x.sName = sTopologyName) If IsNothing(JsonAvailableTopology) Then ' ricavo topologia da feature FindTopologyFromFeature(nFeatureId, sTopologyName) JsonAvailableTopology = JsonAvailableStrategyFeature.TopologyList.FirstOrDefault(Function(x) x.sName = sTopologyName) End If If IsNothing(JsonAvailableTopology) Then Return False m_StrategyList = New ObservableCollection(Of Strategy)((From JsonAvailableStrategy In JsonAvailableTopology.StrategyList Select JsonAvailableStrategy.Deserialize(ProjectManagerVM.CurrProd.nType, -1)).ToList()) End If ' recupero custom Dim nPartId As Integer = EgtGetParent(EgtGetParent(nFeatureId)) Dim nPartProjId As Integer = 0 If Not EgtGetInfo(nPartId, PROJ, nPartProjId) OrElse nPartProjId <= 0 Then Return False Dim nBTLInfoLayerId As Integer = GetCurrProjBtlInfoLayerId(nPartProjId) Dim sStrategySetupName As String = "" EgtGetInfo(nBTLInfoLayerId, AI_SETUP, sStrategySetupName) If Not String.IsNullOrWhiteSpace(sStrategySetupName) Then ' carico custom Dim sAISetupDirPath As String = Map.refMainWindowVM.MainWindowM.GetAISetupDirPath(ProjectManagerVM.CurrProd.nType, True) Dim sStrategyConfigurationFilePath As String = sAISetupDirPath & "\" & sStrategySetupName & ".json" If File.Exists(sStrategyConfigurationFilePath) Then sReadedFile = File.ReadAllText(sStrategyConfigurationFilePath) Dim JsonRoot As CustomJsonRoot = Nothing Try JsonRoot = JsonConvert.DeserializeObject(Of CustomJsonRoot)(sReadedFile) Catch ex As Exception JsonRoot = Nothing End Try Dim JsonFeatureList As ObservableCollection(Of JsonStrategyFeature) = Nothing If Not IsNothing(JsonRoot) Then JsonFeatureList = JsonRoot.FEATURE Else If IsNothing(JsonRoot) Then EgtOutLog("JSON ERRATO: Il file Json importato non è nel formato corretto") If File.Exists(sAvailableStrategiesFilePath) Then sReadedFile = File.ReadAllText(sAvailableStrategiesFilePath) Dim JsonStrategyFeatureList As List(Of JsonAvailableStrategyFeature) = JsonConvert.DeserializeObject(Of List(Of JsonAvailableStrategyFeature))(sReadedFile) Dim JsonAvailableStrategyFeature As JsonAvailableStrategyFeature = JsonStrategyFeatureList.FirstOrDefault(Function(x) x.nGrp = nGRP AndAlso x.nPrc = nPRC) If IsNothing(JsonAvailableStrategyFeature) Then Return False Dim JsonAvailableTopology As JsonAvailableTopology = JsonAvailableStrategyFeature.TopologyList.FirstOrDefault(Function(x) x.sName = sTopologyName) If IsNothing(JsonAvailableTopology) Then ' ricavo topologia da feature FindTopologyFromFeature(nFeatureId, sTopologyName) JsonAvailableTopology = JsonAvailableStrategyFeature.TopologyList.FirstOrDefault(Function(x) x.sName = sTopologyName) End If If IsNothing(JsonAvailableTopology) Then Return False m_StrategyList = New ObservableCollection(Of Strategy)((From JsonAvailableStrategy In JsonAvailableTopology.StrategyList Select JsonAvailableStrategy.Deserialize(ProjectManagerVM.CurrProd.nType, -1)).ToList()) End If Return True Else JsonFeatureList = JsonConvert.DeserializeObject(Of ObservableCollection(Of JsonStrategyFeature))(sReadedFile) End If End If Dim JsonStrategyFeature As JsonStrategyFeature = JsonFeatureList.FirstOrDefault(Function(x) x.nGrp = nGRP AndAlso x.nPrc = nPRC) If IsNothing(JsonStrategyFeature) Then Return False Dim JsonTopology As JsonTopology = JsonStrategyFeature.TopologyList.FirstOrDefault(Function(x) x.sName = sTopologyName) If IsNothing(JsonTopology) Then Return False For Each Strategy In JsonTopology.StrategyList Dim DefaultStrategy As Strategy = m_StrategyList.FirstOrDefault(Function(x) x.sStrategyId = Strategy.sStrategyId) If IsNothing(DefaultStrategy) Then ' DefaultTopology.StrategyList.Add(New Strategy(DefaultTopology, Strategy)) ' verifico indici!! Else DefaultStrategy.ReadConfiguration(Strategy) End If Next End If End If ' aggiungo strategia automatica m_StrategyList.Insert(0, New Strategy() With {.sStrategyId = AUTOMATICSTRATEGYID, .sStrategyName = AUTOMATICSTRATEGYID}) If Map.refMainMenuVM.UnlockAllIsChecked Then Map.refMainMenuVM.UnlockAll(m_StrategyList) ElseIf Map.refMainMenuVM.UserAdmin_IsChecked Then Map.refMainMenuVM.ShowParam(m_StrategyList) End If NotifyPropertyChanged(NameOf(StrategyList)) Return True End Function Friend Sub GetDefaultJsonStrategy(sStrategiesDirPath As String, sAvailableStrategiesFilePath As String, sTopologyName As String, nFeatureId As Integer, nPRC As Integer, nGRP As Integer) If File.Exists(sAvailableStrategiesFilePath) Then Dim sReadedFile As String = File.ReadAllText(sAvailableStrategiesFilePath) Dim JsonStrategyFeatureList As List(Of JsonAvailableStrategyFeature) = JsonConvert.DeserializeObject(Of List(Of JsonAvailableStrategyFeature))(sReadedFile) Dim JsonAvailableStrategyFeature As JsonAvailableStrategyFeature = JsonStrategyFeatureList.FirstOrDefault(Function(x) x.nGrp = nGRP AndAlso x.nPrc = nPRC) If IsNothing(JsonAvailableStrategyFeature) Then Return Dim JsonAvailableTopology As JsonAvailableTopology = JsonAvailableStrategyFeature.TopologyList.FirstOrDefault(Function(x) x.sName = sTopologyName) If IsNothing(JsonAvailableTopology) Then ' ricavo topologia da feature FindTopologyFromFeature(nFeatureId, sTopologyName) JsonAvailableTopology = JsonAvailableStrategyFeature.TopologyList.FirstOrDefault(Function(x) x.sName = sTopologyName) End If If IsNothing(JsonAvailableTopology) Then Return m_StrategyList = New ObservableCollection(Of Strategy)((From JsonAvailableStrategy In JsonAvailableTopology.StrategyList Select JsonAvailableStrategy.Deserialize(ProjectManagerVM.CurrProd.nType, -1)).ToList()) End If End Sub Friend Function GetCurrProjBtlInfoLayerId(nProjId As Integer) As Integer ' cerco tra i layer BTLInfo Dim nBTLInfoLayerId As Integer = EgtGetFirstNameInGroup(GDB_ID.ROOT, BTLINFO) While nBTLInfoLayerId <> GDB_ID.NULL ' verifico se il layer appartiene al ProjId Dim nBTLInfoLayerProjId As Integer EgtGetInfo(nBTLInfoLayerId, BTL_PRT_PROJ, nBTLInfoLayerProjId) If nBTLInfoLayerProjId = nProjId Then Return nBTLInfoLayerId End If nBTLInfoLayerId = EgtGetNextName(nBTLInfoLayerId, BTLINFO) End While Return -1 End Function Friend Sub ForcedStrategy() ' cancello precedenti valori Dim sPrecStrategyID As String = "" EgtGetInfo(m_CurrentFeature.nFeatureId, ConstBeam.STRATEGY, sPrecStrategyID) If Not String.IsNullOrWhiteSpace(sPrecStrategyID) Then Dim PrecInfo As String() = {} EgtGetAllInfo(m_CurrentFeature.nFeatureId, PrecInfo) Dim PrecStrategyInfo As String() = PrecInfo.Where(Function(x) x.StartsWith(sPrecStrategyID)).ToArray() For Each Info In PrecStrategyInfo EgtRemoveInfo(m_CurrentFeature.nFeatureId, Info.Split("="c)(0)) Next End If If Not IsNothing(m_SelStrategy) Then If m_SelStrategy.sStrategyId = AUTOMATICSTRATEGYID Then m_SelStrategy.SetbIsStrategyModify(False) EgtRemoveInfo(m_CurrentFeature.nFeatureId, ConstBeam.STRATEGY) EgtRemoveInfo(m_CurrentFeature.nFeatureId, ConstBeam.STRATEGY & m_CurrentFeature.nFeatureId & "_FORCED") Map.refProjectVM.BTLStructureVM.SelBTLPart.SetStrategyModify_Visibility(False) Map.refProjectVM.BTLStructureVM.SelBTLPart.SelBTLFeatureVM.SetbStrategy_Visibility(False) Else m_SelStrategy.SetbIsStrategyModify(True) Map.refProjectVM.BTLStructureVM.SelBTLPart.SetStrategyModify_Visibility(True) If Not IsNothing(Map.refProjectVM.BTLStructureVM.SelBTLPart.SelBTLFeatureVM) Then Map.refProjectVM.BTLStructureVM.SelBTLPart.SelBTLFeatureVM.SetbStrategy_Visibility(True) ElseIf Not IsNothing(Map.refProjectVM.MachGroupPanelVM.SelectedMachGroup.SelPart.SelFeatureVM) Then Map.refProjectVM.MachGroupPanelVM.SelectedMachGroup.SelPart.SelFeatureVM.SetbStrategy_Visibility(True) End If EgtSetInfo(m_CurrentFeature.nFeatureId, ConstBeam.STRATEGY, SelStrategy.sStrategyId) EgtSetInfo(m_CurrentFeature.nFeatureId, ConstBeam.STRATEGY & m_CurrentFeature.nFeatureId & "_FORCED", SelStrategy.bIsStrategyModify) Dim sInfo As String = String.Empty For Each Param In SelStrategy.ParameterList Select Case Param.GetType() Case GetType(BooleanStrategyParameter) EgtSetInfo(m_CurrentFeature.nFeatureId, m_SelStrategy.sStrategyId & "_" & Param.sNameNge, DirectCast(Param, BooleanStrategyParameter).bValue) EgtSetInfo(m_CurrentFeature.nFeatureId, m_SelStrategy.sStrategyId & "_" & Param.sNameNge & "_FORCED", DirectCast(Param, BooleanStrategyParameter).bIsBooleanModify) Case GetType(DoubleStrategyParameter) EgtSetInfo(m_CurrentFeature.nFeatureId, m_SelStrategy.sStrategyId & "_" & Param.sNameNge, DirectCast(Param, DoubleStrategyParameter).sValue) EgtSetInfo(m_CurrentFeature.nFeatureId, m_SelStrategy.sStrategyId & "_" & Param.sNameNge & "_FORCED", DirectCast(Param, DoubleStrategyParameter).bIsDoubleModify) Case GetType(ComboStrategyParameter) EgtSetInfo(m_CurrentFeature.nFeatureId, m_SelStrategy.sStrategyId & "_" & Param.sNameNge, DirectCast(Param, ComboStrategyParameter).SelValue.sValue) EgtSetInfo(m_CurrentFeature.nFeatureId, m_SelStrategy.sStrategyId & "_" & Param.sNameNge & "_FORCED", DirectCast(Param, ComboStrategyParameter).bIsComboBoxModify) Case GetType(StringStrategyParameter) EgtSetInfo(m_CurrentFeature.nFeatureId, m_SelStrategy.sStrategyId & "_" & Param.sNameNge, DirectCast(Param, StringStrategyParameter).sValue) EgtSetInfo(m_CurrentFeature.nFeatureId, m_SelStrategy.sStrategyId & "_" & Param.sNameNge & "_FORCED", DirectCast(Param, StringStrategyParameter).bIsStringModify) Case GetType(ListStrategyParameter) For Each Item As ToolParameter In DirectCast(Param, ListStrategyParameter).ListValue If Item.bIsActive Then sInfo &= Item.sUUID & "," & Item.sName & ";" EgtSetInfo(m_CurrentFeature.nFeatureId, m_SelStrategy.sStrategyId & "_" & Param.sNameNge, sInfo) End If Next EgtSetInfo(m_CurrentFeature.nFeatureId, m_SelStrategy.sStrategyId & "_" & Param.sNameNge & "_FORCED", DirectCast(Param, ListStrategyParameter).bIsListModify) End Select Next End If End If End Sub Friend Sub ResetForcedStrategy(nFeatureId As Integer) ' cancello precedenti valori Dim sPrecStrategyID As String = "" EgtGetInfo(nFeatureId, ConstBeam.STRATEGY, sPrecStrategyID) If Not String.IsNullOrWhiteSpace(sPrecStrategyID) Then Dim PrecInfo As String() = {} EgtGetAllInfo(nFeatureId, PrecInfo) Dim PrecStrategyInfo As String() = PrecInfo.Where(Function(x) x.StartsWith(sPrecStrategyID)).ToArray() For Each Info In PrecStrategyInfo EgtRemoveInfo(nFeatureId, Info.Split("="c)(0)) Next EgtRemoveInfo(nFeatureId, ConstBeam.STRATEGY) EgtRemoveInfo(nFeatureId, ConstBeam.STRATEGY & nFeatureId & "_FORCED") End If End Sub #End Region ' Methods #Region "COMMANDS" #Region "Ok_Command" Public ReadOnly Property Ok_Command As ICommand Get If m_cmdOk Is Nothing Then m_cmdOk = New Command(AddressOf Ok) End If Return m_cmdOk End Get End Property Public Sub Ok() ForcedStrategy() Map.refProjectVM.SetSelManagerTab(ProjectVM.StrategyManagerTab.RAWPARTMANAGER) ' Abilito LeftPanel Map.refProjectVM.SetLeftPanel_IsEnabled(True) Map.refProjectVM.SetLeftPanel_Opacity(1) Map.refSceneHostVM.SaveProject() End Sub #End Region ' Ok_Command #Region "Cancel_Command" Public ReadOnly Property Cancel_Command As ICommand Get If m_cmdCancel Is Nothing Then m_cmdCancel = New Command(AddressOf Cancel) End If Return m_cmdCancel End Get End Property Public Sub Cancel() Map.refProjectVM.SetSelManagerTab(ProjectVM.StrategyManagerTab.RAWPARTMANAGER) ' Abilito LeftPanel Map.refProjectVM.SetLeftPanel_IsEnabled(True) Map.refProjectVM.SetLeftPanel_Opacity(1) End Sub #End Region ' Cancel_Command #End Region ' Commands End Class