Imports System.Collections.ObjectModel Imports System.IO Imports EgtCAM5.MyMachinePanelVM Imports System.Security.Cryptography Imports System.Windows.Forms.AxHost Imports EgtUILib Imports EgtWPFLib5 Public Class MyMachGroupPanelVM Inherits EgtWPFLib5.MachGroupPanelVM #Region "CONSTRUCTOR" Sub New() MyBase.New() End Sub #End Region ' CONSTRUCTOR #Region "METHODS" Public Overloads Sub InitMachGroupList(bAllowEmpty As Boolean) Dim nGroupId As Integer = GDB_ID.NULL Dim nErr As Integer = ManageDisposition(nGroupId, bAllowEmpty) If nErr = 0 Then LoadMachGroupList() Dim MachGroupName As String = "" EgtGetMachGroupName(nGroupId, MachGroupName) SelectedMachGroup = MachGroupList.First(Function(x) x.Name = MachGroupName) If IniFile.m_bShowOnlyTable Then EgtShowOnlyTable(True) End If Map.refTopCommandBarVM.MachGroupsResult(nErr) ' Setto il Materiale in base alla Macchina associata al MachGroup selezionato LoadMaterialType() If nErr <> 0 Then EgtDraw() Map.refMachGroupPanelVM.SetMachGroupState(True) 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, nInd As Integer, sParts As String) As Boolean EgtLuaCreateGlobTable("DISP") EgtLuaSetGlobIntVar("DISP.GROUPID", nGroupId) EgtLuaSetGlobIntVar("DISP.PARTID", nPartId) EgtLuaSetGlobIntVar("DISP.IND", nInd) EgtLuaSetGlobStringVar("DISP.PARTS", sParts) 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_DISP_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) ' Recupero i pezzi già inseriti Dim sParts As String = "" Dim nInd As Integer = 0 Dim nRawId As Integer = EgtGetFirstRawPart() While nRawId <> GDB_ID.NULL Dim nPartId As Integer = EgtGetFirstPartInRawPart( nRawId) While nPartId <> GDB_ID.NULL nInd += 1 sParts &= nInd.ToString() & "," nPartId = EgtGetNextPartInRawPart( nPartId) End While nRawId = EgtGetNextRawPart( nRawId) End While ' Creo grezzi e posiziono i pezzi Dim bOk As Boolean = True sParts &= String.Join( ",", PartList) For Each Part In PartList nInd += 1 If Not ExecScript(sInitScriptPath, nGroupId, Part, nInd, sParts) 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_DISP_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 If bWithScript Then Dim bOk As Boolean = True ' Creo grezzi e posiziono i pezzi If PartList.Count() > 0 then Dim nInd As Integer = 0 Dim sParts As String = String.Join( ",", PartList) For Each Part In PartList nInd += 1 If Not ExecScript(sInitScriptPath, nGroupId, Part, nInd, sParts) Then bOk = False Next ' altrimenti, ingresso senza pezzi Else If Not ExecScript(sInitScriptPath, nGroupId, GDB_ID.NULL, 0, "") Then bOk = False End If If Not bOk Then EgtResetCurrMachGroup() EgtRemoveMachGroup(nGroupId) EgtOutLog("Errore nel posizionamento dei pezzi") Return False End If Else Dim sTabNames As String = "" EgtGetAllTablesNames(sTabNames) Dim sItems() As String = sTabNames.Split(","c).ToArray If sItems.Count() > 0 Then EgtSetTable( sItems(0)) End If 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 Public Overrides Sub LoadMachGroupList() ' 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 Dim sMachine As String = String.Empty EgtGetMachGroupName(nId, sName) EgtGetMachGroupMachineName(nId, sMachine) MachGroupList.Add(New MachGroup(nId, sName, sMachine)) 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 Private Sub LoadMaterialType() Dim sMatType As String = "Wood" ' IniFile.m_sMachineName = Map.refStatusBarVM.SelectedMachine.Name ' IniFile.m_sCurrMachIniFilePath = Map.refStatusBarVM.SelectedMachine.MachineDirPath & "\" & Map.refStatusBarVM.SelectedMachine.Name & ".ini" Dim sMachineIniPath = IniFile.m_sCurrMachIniFilePath EgtUILib.GetPrivateProfileString(S_GENERAL, EgtWPFLib5.EstConstMachIni.K_MATERIAL, sMatType, sMatType, sMachineIniPath) Select Case sMatType Case "Stone" EgtWPFLib5.MachiningTreeViewItem.m_MatType = MaterialType.STONE Case "Wood" EgtWPFLib5.MachiningTreeViewItem.m_MatType = MaterialType.WOOD Case "Beam" EgtWPFLib5.MachiningTreeViewItem.m_MatType = MaterialType.BEAM Case "Additive" EgtWPFLib5.MachiningTreeViewItem.m_MatType = MaterialType.WOOD Case Else ' Se il materiale specificato nel file INI della macchina non corrisponde ai 4 sopracitati viene emesso un messaggio di errore EgtWPFLib5.MachiningTreeViewItem.m_MatType = Nothing MsgBox(EgtMsg(31409), MsgBoxStyle.Exclamation, EgtMsg(31551)) End Select End Sub #End Region Public Overrides Sub AddMachGroup() Dim nNewMachGrpId As Integer = GDB_ID.NULL Dim sNewMachGrpName As String = "" ' Se premuto Shift, copio il gruppo corrente If (Keyboard.Modifiers And Input.ModifierKeys.Shift) = Input.ModifierKeys.Shift Then ' Eseguo una copia del gruppo corrente Dim sMachGrpName As String = "" If Not EgtGetMachGroupName(EgtGetCurrMachGroup(), sMachGrpName) Then Return sNewMachGrpName = "Mach_1" EgtGetMachGroupNewName(sNewMachGrpName) nNewMachGrpId = EgtCopyMachGroup(sMachGrpName, sNewMachGrpName) ' altrimenti ne aggiungo uno vuoto Else Dim vPart As New List(Of Integer) AddNewMachGroup(nNewMachGrpId, vPart) EgtGetMachGroupName(nNewMachGrpId, sNewMachGrpName) End If ' Se creazione non riuscita, esco subito If nNewMachGrpId = GDB_ID.NULL Then Return ' altrimenti sistemo interfaccia Dim sMachine As String = String.Empty EgtGetMachGroupMachineName(nNewMachGrpId, sMachine) Dim machGroup As New MachGroup(nNewMachGrpId, sNewMachGrpName, sMachine) MachGroupList.Add(machGroup) SelectedMachGroup = machGroup Map.refMachGroupPanelVM.SetMachGroupState(True) EgtZoom(ZM.ALL) End Sub Public Overrides 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).Name)) ' 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).Id.ToString())) SelectedMachGroup = MachGroupList(nNewInd) If IniFile.m_bShowOnlyTable Then EgtShowOnlyTable(True) EgtZoom(ZM.ALL) Map.refOperationsListExpanderVM.LoadOperationList(GDB_ID.NULL) Map.refMachinePanelVM.UpdateCurrentMachine() End If ' cancello quello selezionato EgtRemoveMachGroup(EgtGetMachGroupId(MachGroupList(nSelectedMachGroupIndex).Name)) ' aggiorno la lista dei gruppi MachGroupList.RemoveAt(nSelectedMachGroupIndex) Case MessageBoxResult.No Return End Select End If End Sub Public Overrides Function OnPostSetCurrMachGroup() As Boolean ' Se gli Expander sono Nothing vuol dire che siamo appena entrati in Machining Mode e quindi non eseguo il codice seguente If Not IsNothing(Map.refOperationsListExpanderVM) Or Not IsNothing(Map.refEstimationsExpanderVM) Then If IniFile.m_bShowOnlyTable Then EgtShowOnlyTable(True) Map.refOperationsListExpanderVM.LoadOperationList(GDB_ID.NULL) Map.refMachinePanelVM.UpdateCurrentMachine() Map.refMachiningTreeExpanderVM.UpdateOperationMachiningList() Map.refEstimationsExpanderVM.Estimation_IsEnabled = (EgtUILib.GetPrivateProfileInt(S_ESTIMATIONS, K_EST_ENABLE, 0, IniFile.m_sCurrMachIniFilePath) <> 0) ' Setto il Materiale in base alla Macchina associata al MachGroup corrente LoadMaterialType() End If Return True End Function End Class