Imports System.Collections.ObjectModel Imports System.IO Imports EgtUILib Public Class MachGroupPanelVM Inherits ViewModelBase #Region "FIELDS & PROPERTIES" Private m_MachGroupList As New ObservableCollection(Of String) Public Property MachGroupList As ObservableCollection(Of String) Get Return m_MachGroupList End Get Set(value As ObservableCollection(Of String)) If value IsNot m_MachGroupList Then m_MachGroupList = value End If End Set End Property Private m_SelectedMachGroup As String Public Property SelectedMachGroup As String Get Return m_SelectedMachGroup End Get Set(value As String) m_SelectedMachGroup = value OnPropertyChanged("SelectedMachGroup") End Set End Property Private m_IsEnabled As Boolean = True Public Property MachGroupPanelIsEnabled As Boolean Get Return m_IsEnabled End Get Set(value As Boolean) m_IsEnabled = value OnPropertyChanged("MachGroupPanelIsEnabled") End Set End Property ' Definizione comandi Private m_cmdSetCurrMachGroup As ICommand Private m_cmdAddMachGroup As ICommand Private m_cmdRemoveMachGroup As ICommand #End Region ' FIELDS & PROPERTIES #Region "CONSTRUCTOR" Sub New() Application.Msn.Register(Application.INITIALIZEMACHGROUPS, Sub(bAllowEmpty As Boolean) InitializeMachGroups(bAllowEmpty) End Sub) Application.Msn.Register(Application.MACHGROUPSISENABLED, Sub(MgpIsEnabled As Boolean) Me.MachGroupPanelIsEnabled = MgpIsEnabled End Sub) End Sub #End Region ' CONSTRUCTOR #Region "METHODS" Private Sub InitializeMachGroups(bAllowEmpty As Boolean) Dim nGroupId As Integer = GDB_ID.NULL Dim nErr As Integer = ManageDisposition(nGroupId, bAllowEmpty) If nErr = 0 Then LoadMachGroups() Dim MachGroupName As String = "" EgtGetMachGroupName(nGroupId, MachGroupName) SelectedMachGroup = MachGroupList(MachGroupList.IndexOf(MachGroupName)) If IniFile.m_bShowOnlyTable Then EgtShowOnlyTable(True) End If Application.Msn.NotifyColleagues(Application.MACHGROUPSRESULT, nErr) If nErr <> 0 Then EgtDraw() End Sub Private Function ManageDisposition(ByRef nGroupId As Integer, bAllowEmpty As Boolean) As Integer ' Ricerca pezzi selezionati Dim vPart As New List(Of Integer) Dim nId As Integer = EgtGetFirstSelectedObj() While nId <> GDB_ID.NULL Dim nPartId As Integer = EgtGetParent(EgtGetParent(nId)) If nPartId <> GDB_ID.NULL AndAlso Not vPart.Contains(nPartId) Then vPart.Add(nPartId) nId = EgtGetNextSelectedObj() End While EgtDeselectAll() Dim bSelPart As Boolean = (vPart.Count > 0) ' Verifica esistenza gruppi di lavorazione Dim bMachGroup As Boolean = (EgtGetMachGroupCount() > 0) ' Se ci sono pezzi selezionati If bSelPart Then ' se ci sono gruppi di lavorazione If bMachGroup Then ' Creo lista dei gruppi di lavorazione Dim MachGroupList As New List(Of Integer) Dim GrpId As Integer = EgtGetFirstMachGroup() While GrpId <> GDB_ID.NULL MachGroupList.Add(GrpId) GrpId = EgtGetNextMachGroup(GrpId) End While ' Salvo macchina corrente Dim sMachine As String = String.Empty EgtGetCurrMachineName(sMachine) ' Elimino gruppi che contengono già un pezzo selezionato For Index = MachGroupList.Count - 1 To 0 Step -1 If PartsInMachGroup(vPart, MachGroupList(Index)) Then MachGroupList.Remove(MachGroupList(Index)) Next ' Ripristino macchina corrente EgtSetCurrMachine(sMachine) ' se la lista gruppi è vuota creo un nuovo gruppo If MachGroupList.Count = 0 Then ' Vuoi creare un nuovo Gruppo di Lavoro con i pezzi selezionati ? - LAVORA If MessageBox.Show(EgtMsg(5552), EgtMsg(5002), MessageBoxButton.YesNo, MessageBoxImage.Question) = MessageBoxResult.Yes Then Return If(AddNewMachGroup(nGroupId, vPart), 0, 1) Else Return -2 End If Else ' altrimenti creo dialogo per scelta gruppi esistenti o nuovo gruppo Dim SelMachGroupWndVM As New SelMachGroupWndVM(MachGroupList) Dim SelMachGroupWndV As New SelMachGroupWndV(Application.Current.MainWindow, SelMachGroupWndVM) If SelMachGroupWndV.ShowDialog() = False Then Return 1 ' Verifico selezione in dialogo Dim bOk As Boolean = False Select Case SelMachGroupWndVM.MachGroupType Case EgtCAM5.SelMachGroupWndVM.MGroupType.CHOOSEMGROUP nGroupId = SelMachGroupWndVM.SelMachGroupId bOk = AddToMachGroup(nGroupId, vPart) Case EgtCAM5.SelMachGroupWndVM.MGroupType.NEWMGROUP bOk = AddNewMachGroup(nGroupId, vPart) End Select Return If(bOk, 0, 1) End If ' altrimenti creo il primo gruppo Else ' Vuoi creare un nuovo Gruppo di Lavoro con i pezzi selezionati ? - LAVORA If MessageBox.Show(EgtMsg(5552), EgtMsg(5002), MessageBoxButton.YesNo, MessageBoxImage.Question) = MessageBoxResult.Yes Then Return If(AddNewMachGroup(nGroupId, vPart), 0, 1) Else Return -2 End If End If ' Se non ci sono pezzi selezionati Else ' se ci sono gruppi di lavorazione If bMachGroup Then nGroupId = EgtGetLastMachGroup() Return If(EgtSetCurrMachGroup(nGroupId), 0, 1) ' se altrimenti ammessi gruppi di lavoro vuoti ElseIf bAllowEmpty Then Return If(AddNewMachGroup(nGroupId, vPart), 0, 1) ' altrimenti esco Else Return -1 End If End If End Function Private Function ExecScript(sScriptPath As String, nGroupId As Integer, nPartId As Integer) As Boolean EgtLuaCreateGlobTable("DISP") EgtLuaSetGlobIntVar("DISP.GROUPID", nGroupId) EgtLuaSetGlobIntVar("DISP.PARTID", nPartId) If Not EgtLuaExecFile(sScriptPath) Then EgtOutLog("Error executing disposition init script " & sScriptPath) MessageBox.Show(EgtMsg(MSG_DISPOSITIONERRORS + 2) & " " & sScriptPath, EgtMsg(MSG_DISPOSITIONERRORS + 1), MessageBoxButton.OK, MessageBoxImage.Error) Return False End If Dim nErr As Integer = 999 EgtLuaGetGlobIntVar("DISP.ERR", nErr) EgtLuaResetGlobVar("DISP") Return nErr = 0 End Function Private Function PartsInMachGroup(SelPartList As List(Of Integer), nGroupId As Integer) As Boolean If Not EgtSetCurrMachGroup(nGroupId) Then Return False Dim nRawId As Integer = EgtGetFirstRawPart() While nRawId <> GDB_ID.NULL Dim nPartId As Integer = EgtGetFirstPartInRawPart(nRawId) While nPartId <> GDB_ID.NULL If SelPartList.Contains(nPartId) Then EgtResetCurrMachGroup() Return True End If nPartId = EgtGetNextPartInRawPart(nPartId) End While nRawId = EgtGetNextRawPart(nRawId) End While EgtResetCurrMachGroup() Return False End Function Private Function AddToMachGroup(ByRef nGroupId As Integer, PartList As List(Of Integer)) As Boolean ' leggo nome script di disposizione automatica Dim bWithScript As Boolean = False Dim sInitScriptPath As String = String.Empty EgtUILib.GetPrivateProfileString(S_DISPOSITION, K_INITSCRIPT, "", sInitScriptPath, IniFile.m_sCurrMachIniFilePath) ' se è attivo, uso lo script di disposizione If OptionModule.m_bUseDispositionScript And Not String.IsNullOrEmpty(sInitScriptPath) Then sInitScriptPath = IniFile.m_sCurrMachScriptsDirPath & "\" & sInitScriptPath bWithScript = File.Exists(sInitScriptPath) End If If bWithScript Then ' Rendo corrente il gruppo di lavoro EgtSetCurrMachGroup(nGroupId) ' Creo grezzo e posiziono i pezzi Dim bOk As Boolean = True For Each Part In PartList If Not ExecScript(sInitScriptPath, nGroupId, Part) Then bOk = False Next Return bOk Else EgtOutLog("Machine without InitDisp script.") Return False End If End Function Private Function AddNewMachGroup(ByRef nGroupId As Integer, PartList As List(Of Integer)) As Boolean EgtSetCurrMachine(IniFile.m_sMachineName) ' leggo nome script di disposizione automatica Dim bWithScript As Boolean = False Dim sInitScriptPath As String = String.Empty EgtUILib.GetPrivateProfileString(S_DISPOSITION, K_INITSCRIPT, "", sInitScriptPath, IniFile.m_sCurrMachIniFilePath) ' se è attivo, uso lo script di disposizione If OptionModule.m_bUseDispositionScript And Not String.IsNullOrEmpty(sInitScriptPath) Then sInitScriptPath = IniFile.m_sCurrMachScriptsDirPath & "\" & sInitScriptPath bWithScript = File.Exists(sInitScriptPath) End If If bWithScript OrElse PartList.Count() = 0 Then Dim sMgName As String = "Mach_1" EgtGetMachGroupNewName(sMgName) nGroupId = EgtAddMachGroup(sMgName) If nGroupId = GDB_ID.NULL Then EgtOutLog("Errore nella creazione del gruppo di lavoro " & sMgName) Return False End If ' Creo grezzo e posiziono i pezzi Dim bOk As Boolean = True For Each Part In PartList If Not ExecScript(sInitScriptPath, nGroupId, Part) Then bOk = False Next If Not bOk Then EgtResetCurrMachGroup() EgtRemoveMachGroup(nGroupId) EgtOutLog("Errore nel posizionamento dei pezzi") Return False End If Else EgtOutLog("Machine without InitDisp script.") Return False End If ' Gestisco eventuale attrezzaggio di default ManageDefaultSetUp() Return True End Function Private Sub CalculateAllPartsBbox(ByRef bboxAllParts As BBox3d) Dim nPart As Integer = EgtGetFirstPart() While nPart <> GDB_ID.NULL Dim bboxPart As New BBox3d EgtGetBBoxGlob(nPart, GDB_BB.STANDARD, bboxPart) bboxAllParts.Add(bboxPart) nPart = EgtGetNextPart(nPart) End While End Sub Private Sub CalculatePartsTranslationVt(bboxAllParts As BBox3d) ' recupero area della tavola Dim ptTableMin As Point3d Dim ptTableMax As Point3d Dim X = EgtGetTableArea(1, ptTableMin, ptTableMax) IniFile.m_vtMachPartsPos = (New Point3d(ptTableMin.x, ptTableMin.y - 250, ptTableMin.z) - New Point3d(bboxAllParts.Min.x, bboxAllParts.Max.y, bboxAllParts.Min.z)) End Sub Private Sub TraslateParts() Dim nPart As Integer = EgtGetFirstPart() While nPart <> GDB_ID.NULL EgtMove(nPart, IniFile.m_vtMachPartsPos) nPart = EgtGetNextPart(nPart) End While nPart = EgtGetFirstGhostPart() While nPart <> GDB_ID.NULL EgtMove(nPart, IniFile.m_vtMachPartsPos) nPart = EgtGetNextGhostPart(nPart) End While End Sub Private Sub LoadMachGroups() ' Pulisco la lista MachGroupList.Clear() ' Carico i gruppi di lavorazione nella lista Dim nId = EgtGetFirstMachGroup() While nId <> GDB_ID.NULL Dim sName As String = String.Empty EgtGetMachGroupName(nId, sName) MachGroupList.Add(sName) nId = EgtGetNextMachGroup(nId) End While End Sub Private Sub ManageDefaultSetUp() ' leggo nome attrezzaggio di default Dim sDefaultSetUpName As String = String.Empty EgtUILib.GetPrivateProfileString(S_SETUP, K_DEFAULT, "", sDefaultSetUpName, IniFile.m_sCurrMachIniFilePath) ' se è attiva l'opzione, rendo corrente l'attrezzaggio di default If Not String.IsNullOrEmpty(sDefaultSetUpName) Then If Not EgtImportSetup(String.Empty) Then EgtOutLog("Error loading default setup " & sDefaultSetUpName) MessageBox.Show(EgtMsg(MSG_SETUPERRORS + 9) & " " & sDefaultSetUpName, EgtMsg(MSG_MESSAGEBOX + 1), MessageBoxButton.OK, MessageBoxImage.Error) End If End If End Sub #End Region #Region "COMMANDS" #Region "SetCurrMachGroupCommand" ''' ''' Returns a command that set the selected MachGroup as the Current one. ''' Public ReadOnly Property SetCurrMachGroupCommand As ICommand Get If m_cmdSetCurrMachGroup Is Nothing Then m_cmdSetCurrMachGroup = New RelayCommand(AddressOf SetCurrMachGroup) End If Return m_cmdSetCurrMachGroup End Get End Property Public Sub SetCurrMachGroup(ByVal param As Object) EgtSetCurrMachGroup(EgtGetMachGroupId(DirectCast(param, String))) If IniFile.m_bShowOnlyTable Then EgtShowOnlyTable(True) EgtZoom(ZM.ALL) Application.Msn.NotifyColleagues(Application.LOADOPERATIONLIST, GDB_ID.NULL) Application.Msn.NotifyColleagues(Application.UPDATECURRENTMACHINE) Map.refSimulationExpanderVM.Estimation_IsEnabled = (EgtUILib.GetPrivateProfileInt(S_ESTIMATIONS, K_EST_ENABLE, 0, IniFile.m_sCurrMachIniFilePath) <> 0) End Sub #End Region ' SetCurrMachGroupCommand #Region "AddMachGroupCommand" ''' ''' Returns a command that set the selected MachGroup as the Current one. ''' Public ReadOnly Property AddMachGroupCommand As ICommand Get If m_cmdAddMachGroup Is Nothing Then m_cmdAddMachGroup = New RelayCommand(AddressOf AddMachGroup) End If Return m_cmdAddMachGroup End Get End Property Public Sub AddMachGroup() 'If AddNewMachGroup() Then ' Dim sMachName As String = String.Empty ' EgtGetMachGroupName(EgtGetCurrMachGroup(), sMachName) ' MachGroupList.Add(sMachName) ' SelectedMachGroup = sMachName ' EgtDraw() ' Application.Msn.NotifyColleagues(Application.LOADOPERATIONLIST, GDB_ID.NULL) ' Application.Msn.NotifyColleagues(Application.UPDATECURRENTMACHINE) 'End If End Sub #End Region ' AddMachGroupCommand #Region "RemoveMachGroupCommand" ''' ''' Returns a command that set the selected MachGroup as the Current one. ''' Public ReadOnly Property RemoveMachGroupCommand As ICommand Get If m_cmdRemoveMachGroup Is Nothing Then m_cmdRemoveMachGroup = New RelayCommand(AddressOf RemoveMachGroup) End If Return m_cmdRemoveMachGroup End Get End Property Public Sub RemoveMachGroup() ' Calcolo indice del gruppo da cancellare Dim nSelectedMachGroupIndex As Integer = MachGroupList.IndexOf(SelectedMachGroup) If nSelectedMachGroupIndex = 0 And MachGroupList.Count = 1 Then ' chiedo conferma prima di cancellare il gruppo di lavorazione Select Case MessageBox.Show(EgtMsg(MSG_MACHGROUP + 2), "", MessageBoxButton.YesNo, MessageBoxImage.Question) Case MessageBoxResult.Yes ' cancello il gruppo corrente EgtRemoveMachGroup(EgtGetMachGroupId(MachGroupList(nSelectedMachGroupIndex))) ' aggiorno la lista dei gruppi MachGroupList.RemoveAt(nSelectedMachGroupIndex) ' ritorno alla modalità disegno Map.refTopCommandBarVM.DrawIsChecked = True Case MessageBoxResult.No Return End Select Else ' chiedo conferma prima di cancellare il gruppo di lavorazione Select Case MessageBox.Show(EgtMsg(MSG_MACHGROUP + 2), "", MessageBoxButton.YesNo, MessageBoxImage.Question) Case MessageBoxResult.Yes Dim nNewInd As Integer = -1 If nSelectedMachGroupIndex = 0 And MachGroupList.Count > 1 Then nNewInd = nSelectedMachGroupIndex + 1 ElseIf nSelectedMachGroupIndex > 0 Then nNewInd = nSelectedMachGroupIndex - 1 End If If nNewInd >= 0 Then EgtSetCurrMachGroup(EgtGetMachGroupId(MachGroupList(nNewInd))) SelectedMachGroup = MachGroupList(nNewInd) If IniFile.m_bShowOnlyTable Then EgtShowOnlyTable(True) EgtZoom(ZM.ALL) Application.Msn.NotifyColleagues(Application.LOADOPERATIONLIST, GDB_ID.NULL) Application.Msn.NotifyColleagues(Application.UPDATECURRENTMACHINE) End If ' cancello quello selezionato EgtRemoveMachGroup(EgtGetMachGroupId(MachGroupList(nSelectedMachGroupIndex))) ' aggiorno la lista dei gruppi MachGroupList.RemoveAt(nSelectedMachGroupIndex) Case MessageBoxResult.No Return End Select End If End Sub #End Region ' RemoveMachGroupCommand #End Region ' COMMANDS End Class