Imports System.Collections.ObjectModel Imports System.IO Imports EgtUILib Imports EgtWPFLib5 Public Class LeftPanelVM Inherits VMBase #Region "FIELDS & PROPERTIES" Public ReadOnly Property ViewPage_Visibility As Visibility Get Return If(Map.refMainMenuVM.SelPage = Pages.VIEW, Visibility.Visible, Visibility.Collapsed) End Get End Property Public ReadOnly Property MachiningPage_Visibility As Visibility Get Return If(Map.refMainMenuVM.SelPage = Pages.MACHINING, Visibility.Visible, Visibility.Collapsed) End Get End Property ' Definizione comandi Private m_cmdNewRawPart As ICommand Private m_cmdAddToRawPart As ICommand Private m_cmdCopyPart As ICommand Private m_cmdAddPart As ICommand Private m_cmdRemovePart As ICommand Private m_cmdCopyFeature As ICommand Private m_cmdMacroFeature As ICommand Private m_cmdAddFeature As ICommand Private m_cmdRemoveFeature As ICommand #End Region 'FIELDS & PROPERTIES Sub New() ' imposto riferimento su mappa Map.SetRefLeftPanelVM(Me) End Sub #Region "METHODS" Friend Sub UpdateView() NotifyPropertyChanged("ViewPage_Visibility") NotifyPropertyChanged("MachiningPage_Visibility") End Sub #End Region ' METHODS #Region "COMMANDS" #Region "NewRawPart" ''' ''' Returns a command that do Exec. ''' Public ReadOnly Property NewRawPart_Command As ICommand Get If m_cmdNewRawPart Is Nothing Then m_cmdNewRawPart = New Command(AddressOf NewRawPart) End If Return m_cmdNewRawPart End Get End Property ''' ''' Execute the Exec. This method is invoked by the ExecCommand. ''' Public Sub NewRawPart() Dim SelPart As BTLPart = Map.refProjectVM.BTLStructure.SelPart If IsNothing(SelPart) Then Return ' Determino il tipo di macchina (travi o pareti) Dim nMachType As MachineType = Map.refMachinePanelVM.SelectedMachine.nType ' chiedo lunghezza grezzo Dim AddRawPartWndVM As New AddRawPartWndVM(Map.refMachinePanelVM.SelectedMachine.nType, SelPart.dL, SelPart.dW, 0) Dim AddRawPartWnd As New AddRawPartWndV(Application.Current.MainWindow, AddRawPartWndVM) If Not AddRawPartWnd.ShowDialog() Then Return ' creo nuovo gruppo di lavorazione Map.refMachGroupPanelVM.AddMachGroup() Dim nCurrMachGroup As Integer = EgtGetCurrMachGroup() ' creo copia del pezzo Dim nPartDuploId As Integer = EgtDuploNew(SelPart.nPartId) Dim sLogPath As String = Map.refMainWindowVM.MainWindowM.sTempDir & "\RawPartLog.txt" If Map.refMachinePanelVM.SelectedMachine.nType = MachineType.BEAM Then ' scrivo dati in gruppo di lavorazione EgtSetInfo(nCurrMachGroup, MGR_RPT_BARLEN, AddRawPartWndVM.VariableList(0).dValue) EgtSetInfo(nCurrMachGroup, MGR_RPT_PART & 1, nPartDuploId & "," & AddRawPartWndVM.VariableList(1).dValue) Dim BeamMachGroup As BeamMachGroup = DirectCast(Map.refMachGroupPanelVM.MachGroupList(Map.refMachGroupPanelVM.MachGroupList.Count - 1), BeamMachGroup) ' eseguo script creazione grezzo If Not ExecBeam(sLogPath, Map.refMachinePanelVM.SelectedMachine.Name, CalcIntegration.CmdType.RAWPART, False) Then BeamMachGroup.DeleteMachGroup() Dim LogFile As String() = File.ReadAllLines(sLogPath) If LogFile.Count >= 2 AndAlso Not IsNothing(LogFile(1)) Then MessageBox.Show(LogFile(1), "Error", MessageBoxButton.OK, MessageBoxImage.Error) End If Return End If 'aggiorno lista pezzi BeamMachGroup.RefreshPartList() BeamMachGroup.RefreshGroupData() ' aggiorno dati ultilizzo barra BeamMachGroup.UpdateUsage() ElseIf Map.refMachinePanelVM.SelectedMachine.nType = MachineType.WALL Then ' scrivo dati in gruppo di lavorazione EgtSetInfo(nCurrMachGroup, MGR_RPT_PANELLEN, AddRawPartWndVM.VariableList(0).dValue) EgtSetInfo(nCurrMachGroup, MGR_RPT_PANELWIDTH, AddRawPartWndVM.VariableList(1).dValue) EgtSetInfo(nCurrMachGroup, MGR_RPT_PART & 1, nPartDuploId & "," & 10 & "," & 10 & "," & 0 & "," & 0) Dim WallMachGroup As WallMachGroup = DirectCast(Map.refMachGroupPanelVM.MachGroupList(Map.refMachGroupPanelVM.MachGroupList.Count - 1), WallMachGroup) ' eseguo script creazione grezzo If Not ExecWall(sLogPath, Map.refMachinePanelVM.SelectedMachine.Name, CalcIntegration.CmdType.RAWPART, False) Then WallMachGroup.DeleteMachGroup() Dim LogFile As String() = File.ReadAllLines(sLogPath) If LogFile.Count >= 2 AndAlso Not IsNothing(LogFile(1)) Then MessageBox.Show(LogFile(1), "Error", MessageBoxButton.OK, MessageBoxImage.Error) End If Return End If 'aggiorno lista pezzi WallMachGroup.RefreshPartList() WallMachGroup.RefreshGroupData() ' aggiorno dati ultilizzo barra WallMachGroup.UpdateUsage() End If EgtSetView(VT.ISO_SW, False) EgtZoom(ZM.ALL) End Sub #End Region ' NewRawPart #Region "AddToRawPart" ''' ''' Returns a command that do Exec. ''' Public ReadOnly Property AddToRawPart_Command As ICommand Get If m_cmdAddToRawPart Is Nothing Then m_cmdAddToRawPart = New Command(AddressOf AddToRawPart) End If Return m_cmdAddToRawPart End Get End Property ''' ''' Execute the Exec. This method is invoked by the ExecCommand. ''' Public Sub AddToRawPart() Dim SelPart As BTLPart = Map.refProjectVM.BTLStructure.SelPart If IsNothing(SelPart) Then Return Dim SelMachGroup As MachGroup = Map.refMachGroupPanelVM.SelectedMachGroup If IsNothing(SelMachGroup) Then Return Dim nCurrMachGroup As Integer = SelMachGroup.Id If Map.refMachinePanelVM.SelectedMachine.nType = MachineType.BEAM Then Dim BeamMachGroup As BeamMachGroup = DirectCast(SelMachGroup, BeamMachGroup) ' verifico sezione If SelPart.dW < BeamMachGroup.dW - EPS_SMALL OrElse SelPart.dW > BeamMachGroup.dW + EPS_SMALL OrElse SelPart.dH < BeamMachGroup.dH - EPS_SMALL OrElse SelPart.dH > BeamMachGroup.dH + EPS_SMALL Then MessageBox.Show("Il pezzo selezionato ha sezione diversa dalla barra selezionata. Impossibile aggiungerlo.", "Errore") Return End If ' verifico lunghezza totale If Not VerifyTotLength(BeamMachGroup, SelPart) Then MessageBox.Show("Il pezzo selezionato supera la lunghezza dalla barra selezionata. Impossibile aggiungerlo.", "Errore") Return End If AddPartToBeam(BeamMachGroup, SelPart) ' aggiorno dati ultilizzo barra BeamMachGroup.UpdateUsage() ElseIf Map.refMachinePanelVM.SelectedMachine.nType = MachineType.WALL Then Dim WallMachGroup As WallMachGroup = DirectCast(SelMachGroup, WallMachGroup) ' verifico spessore If SelPart.dH < WallMachGroup.dH - EPS_SMALL OrElse SelPart.dH > WallMachGroup.dH + EPS_SMALL Then MessageBox.Show("Il pezzo selezionato ha spessore diverso dalla parete selezionata. Impossibile aggiungerlo.", "Errore") Return End If ' Posizione di inserimento Dim dPosX As Double = 10 Dim dPosY As Double = 10 ' Se ci sono pezzi già inseriti If WallMachGroup.PartList.Count > 0 Then Dim LastWall As Wall = DirectCast( WallMachGroup.PartList(WallMachGroup.PartList.Count - 1), Wall) ' verifico se posso metterlo sopra in Y Dim NextPosY = LastWall.dPOSY + LastWall.dW + RawPartConfiguration.dWallDist If WallMachGroup.dW - NextPosY > SelPart.dW + 10 Then dPosX = LastWall.dPOSX dPosY = NextPosY Else dPosX = 10 For i As Integer = 0 To WallMachGroup.PartList.Count - 1 Dim CurrWall As Wall = DirectCast( WallMachGroup.PartList(i), Wall) Dim NextPosX = CurrWall.dPOSX + CurrWall.dL + RawPartConfiguration.dWallDist If NextPosX > dPosX Then dPOSX = NextPosX End If Next dPosY = 10 End If End If ' scrivo dati in gruppo di lavorazione EgtSetInfo(nCurrMachGroup, MGR_RPT_PART & WallMachGroup.PartList.Count + 1, SelPart.nPartId & "," & dPOSX & "," & dPosY & "," & 0 & "," & 0) ' eseguo script creazione grezzo Dim nRawId As Integer = EgtGetFirstRawPart() While nRawId <> GDB_ID.NULL EgtRemoveRawPart(nRawId) nRawId = EgtGetFirstRawPart() End While If Not ExecWall(Map.refMainWindowVM.MainWindowM.sTempDir, Map.refMachinePanelVM.SelectedMachine.Name, CalcIntegration.CmdType.RAWPART, False) Then Return 'aggiorno lista pezzi WallMachGroup.RefreshPartList() ' aggiorno dati ultilizzo barra WallMachGroup.UpdateUsage() End If EgtSetView(VT.ISO_SW, False) EgtZoom(ZM.ALL) End Sub Friend Function VerifyTotLength(BeamMachGroup As BeamMachGroup, PartToAdd As BTLPart) As Boolean Dim dNewPartPos As Double = 0 If BeamMachGroup.PartList.Count > 0 Then Dim LastPart As Core.Part = BeamMachGroup.PartList(BeamMachGroup.PartList.Count - 1) dNewPartPos += LastPart.dPOSX + LastPart.dL End If dNewPartPos += RawPartConfiguration.dBeamDist + PartToAdd.dL Return dNewPartPos < BeamMachGroup.dL End Function Friend Function AddPartToBeam(BeamMachGroup As BeamMachGroup, PartToAdd As BTLPart) As Boolean ' calcolo posizione nuovo pezzo per rispettare ordine lunghezze Dim nInsertIndex As Integer ' creo copia del pezzo Dim nPartDuploId As Integer = EgtDuploNew(PartToAdd.nPartId) ' scrivo dati di tutti i pezzi Dim dPosX As Double = BeamMachGroup.dStartCut Dim bPartAdded As Boolean = False Dim nIndex As Integer ' ciclo sui pezzi For nIndex = 0 To BeamMachGroup.PartList.Count - 1 ' se non ancora aggiunto e lunghezza minore del pezzo corrente, lo aggiungo If Not bPartAdded AndAlso BeamMachGroup.PartList(nIndex).dL > PartToAdd.dL Then ' se non è primo, aggiungo offset tra barre standard If nIndex <> 0 Then dPosX += RawPartConfiguration.dBeamDist End If EgtSetInfo(BeamMachGroup.Id, MGR_RPT_PART & nIndex + 1, nPartDuploId & "," & dPosX) ' incremento posizione della sua lunghezza dPosX += PartToAdd.dL ' conservo indice d'inserimento nInsertIndex = nIndex bPartAdded = True End If ' se non è primo, aggiungo offset del pezzo If nIndex <> 0 Then Dim Beam As Beam = DirectCast(BeamMachGroup.PartList(nIndex), Beam) dPosX += Beam.dOffset ' se sono a indice primo ed è stato aggiunto pezzo nuovo ElseIf nIndex = 0 AndAlso bPartAdded Then ' aggiungo offset tra barre standard dPosX += RawPartConfiguration.dBeamDist End If EgtSetInfo(BeamMachGroup.Id, MGR_RPT_PART & nIndex + If(bPartAdded, 2, 1), BeamMachGroup.PartList(nIndex).nPartId & "," & dPosX) ' incremento posizione della sua lunghezza dPosX += BeamMachGroup.PartList(nIndex).dL Next ' se non è ancora stato aggiunto If Not bPartAdded Then ' aggiungo offset tra barre standard dPosX += RawPartConfiguration.dBeamDist ' lo aggiungo in fondo EgtSetInfo(BeamMachGroup.Id, MGR_RPT_PART & nIndex + 1, nPartDuploId & "," & dPosX) ' conservo indice d'inserimento nInsertIndex = nIndex End If ' elimino vecchio grezzo ed eseguo script creazione nuovo Dim nRawId As Integer = EgtGetFirstRawPart() While nRawId <> GDB_ID.NULL EgtRemoveRawPart(nRawId) nRawId = EgtGetFirstRawPart() End While If Not ExecBeam(Map.refMainWindowVM.MainWindowM.sTempDir, Map.refMachinePanelVM.SelectedMachine.Name, CalcIntegration.CmdType.RAWPART, False) Then Return False End If ' aggiorno lista pezzi BeamMachGroup.RefreshPartList() ' seleziono pezzo aggiunto If BeamMachGroup.PartList.Count > nInsertIndex Then BeamMachGroup.SelPart = BeamMachGroup.PartList(nInsertIndex) Return True End Function #End Region ' AddToRawPart #Region "CopyPart" ''' ''' Returns a command that do Exec. ''' Public ReadOnly Property CopyPart_Command As ICommand Get If m_cmdCopyPart Is Nothing Then m_cmdCopyPart = New Command(AddressOf CopyPart) End If Return m_cmdCopyPart End Get End Property ''' ''' Execute the Exec. This method is invoked by the ExecCommand. ''' Public Sub CopyPart() Dim SelPart As BTLPart = Map.refProjectVM.BTLStructure.SelPart If IsNothing(SelPart) Then Return ' creo copia Dim NewPart = SelPart.Copy() If Not IsNothing(NewPart) Then Map.refProjectVM.BTLStructure.SelPart = NewPart End If End Sub #End Region ' CopyPart #Region "AddPart" ''' ''' Returns a command that do Exec. ''' Public ReadOnly Property AddPart_Command As ICommand Get If m_cmdAddPart Is Nothing Then m_cmdAddPart = New Command(AddressOf AddPart) End If Return m_cmdAddPart End Get End Property ''' ''' Execute the Exec. This method is invoked by the ExecCommand. ''' Public Sub AddPart(ByVal param As Object) ' apro finestra di definizione nuovo part Dim AddPartWndVM As New AddPartWndVM() Dim AddPartWnd As New AddPartWndV(Application.Current.MainWindow, AddPartWndVM) If AddPartWnd.ShowDialog() Then ' creo nuovo part Dim nNewPartId As Integer = EgtBeamCreatePart() ' scrivo info proj EgtSetInfo(nNewPartId, BTL_PRT_PROJ, Map.refProjManagerVM.CurrProj.nProjId) EgtBeamSetPartProdNbr(AddPartWndVM.nPDN) If Not IsNothing(AddPartWndVM.sNAM) Then EgtBeamSetPartName(AddPartWndVM.sNAM) EgtBeamSetPartCount(AddPartWndVM.nCNT) EgtBeamSetPartBox(AddPartWndVM.dL, AddPartWndVM.dH, AddPartWndVM.dW) ' aggiungo dati pezzo Dim NewPart As New BTLPart(Map.refProjectVM.BTLStructure, nNewPartId) ' aggiungo pezzo alla lista Map.refProjectVM.BTLStructure.PartList.Add(NewPart) Map.refProjectVM.BTLStructure.SelPart = NewPart End If End Sub #End Region ' AddPart #Region "RemovePart" ''' ''' Returns a command that do Exec. ''' Public ReadOnly Property RemovePart_Command As ICommand Get If m_cmdRemovePart Is Nothing Then m_cmdRemovePart = New Command(AddressOf RemovePart) End If Return m_cmdRemovePart End Get End Property ''' ''' Execute the Exec. This method is invoked by the ExecCommand. ''' Public Sub RemovePart() If IsNothing(Map.refProjectVM.BTLStructure.SelPart) Then Return EgtBeamSetPart(Map.refProjectVM.BTLStructure.SelPart.nPartId) If EgtBeamErasePart() Then ' rimuovo dalla lista pezzi Dim Index As Integer = Map.refProjectVM.BTLStructure.PartList.IndexOf(Map.refProjectVM.BTLStructure.SelPart) If Index = 0 Then If Map.refProjectVM.BTLStructure.PartList.Count > 0 Then Map.refProjectVM.BTLStructure.SelPart = Map.refProjectVM.BTLStructure.PartList(0) Else Map.refProjectVM.BTLStructure.SelPart = Nothing End If ElseIf Index = Map.refProjectVM.BTLStructure.PartList.Count - 1 Then If Map.refProjectVM.BTLStructure.PartList.Count > 1 Then Map.refProjectVM.BTLStructure.SelPart = Map.refProjectVM.BTLStructure.PartList(Map.refProjectVM.BTLStructure.PartList.Count - 2) Else Map.refProjectVM.BTLStructure.SelPart = Nothing End If Else Map.refProjectVM.BTLStructure.SelPart = Map.refProjectVM.BTLStructure.PartList(Index - 1) End If Map.refProjectVM.BTLStructure.PartList.RemoveAt(Index) End If End Sub #End Region ' RemovePart #Region "CopyFeature" ''' ''' Returns a command that do Exec. ''' Public ReadOnly Property CopyFeature_Command As ICommand Get If m_cmdCopyFeature Is Nothing Then m_cmdCopyFeature = New Command(AddressOf CopyFeature) End If Return m_cmdCopyFeature End Get End Property ''' ''' Execute the Exec. This method is invoked by the ExecCommand. ''' Public Sub CopyFeature() Dim SelPart As BTLPart = Map.refProjectVM.BTLStructure.SelPart If IsNothing(SelPart) Then Return Dim SelFeature As BTLFeature = SelPart.SelFeature If IsNothing(SelFeature) Then Return ' creo copia Dim NewFeature = SelFeature.Copy() If Not IsNothing(NewFeature) Then Map.refProjectVM.BTLStructure.SelPart.SelFeature = NewFeature End If End Sub #End Region ' CopyFeature #Region "MacroFeature" ''' ''' Returns a command that do Exec. ''' Public ReadOnly Property MacroFeature_Command As ICommand Get If m_cmdMacroFeature Is Nothing Then m_cmdMacroFeature = New Command(AddressOf MacroFeature) End If Return m_cmdMacroFeature End Get End Property ''' ''' Execute the Exec. This method is invoked by the ExecCommand. ''' Public Sub MacroFeature() If IsNothing(Map.refProjectVM.BTLStructure.SelPart) Then Return ' apro finestra di creazione macro Dim MacroFeatureWndVM As New MacroFeatureWndVM() Dim MacroFeatureWnd As New MacroFeatureWndV(Application.Current.MainWindow, MacroFeatureWndVM) If MacroFeatureWnd.ShowDialog() Then End If End Sub #End Region ' MacroFeature #Region "AddFeature" ''' ''' Returns a command that do Exec. ''' Public ReadOnly Property AddFeature_Command As ICommand Get If m_cmdAddFeature Is Nothing Then m_cmdAddFeature = New Command(AddressOf AddFeature) End If Return m_cmdAddFeature End Get End Property ''' ''' Execute the Exec. This method is invoked by the ExecCommand. ''' Public Sub AddFeature(ByVal param As Object) If IsNothing(Map.refProjectVM.BTLStructure.SelPart) Then Return ' apro finestra di definizione nuova feature Dim AddFeatureWndVM As New AddFeatureWndVM() Dim AddFeatureWnd As New AddFeatureWndV(Application.Current.MainWindow, AddFeatureWndVM) If AddFeatureWnd.ShowDialog() Then Dim NewFeat As BTLFeature Dim nNewFeatureId As Integer = 0 Dim bMacroFlag As Boolean = False ' verifico se una Macro è selezionata If AddFeatureWndVM.MacroList.Count > 0 AndAlso AddFeatureWndVM.nSelMacro >= 0 Then ' Creo nuova feature sulla base della Macro selezionata ' Creo table e setto variabili EgtLuaCreateGlobTable("MACRO") EgtLuaSetGlobNumVar("MACRO.L", Map.refProjectVM.BTLStructure.SelPart.dL) EgtLuaSetGlobNumVar("MACRO.W", Map.refProjectVM.BTLStructure.SelPart.dW) EgtLuaSetGlobNumVar("MACRO.H", Map.refProjectVM.BTLStructure.SelPart.dH) EgtLuaSetGlobIntVar("MACRO.PROCID", Map.refProjectVM.BTLStructure.SelPart.NewProcId()) Dim bOk = EgtLuaExecFile(AddFeatureWndVM.m_MacroFilePathList(AddFeatureWndVM.nSelMacro)) ' Leggo variabili EgtLuaGetGlobIntVar("MACRO.FEATUREID", nNewFeatureId) ' Reset lua EgtLuaResetGlobVar("MACRO") If Not bOk Then MessageBox.Show("Errore nell'esecuzione del file Macro!", "Errore") Return End If bMacroFlag = True Else ' Creo nuova feature (Macro non selezionata) NewFeat = New BTLFeature(AddFeatureWndVM.nSelPRC.nPRC, AddFeatureWndVM.nSelPRC.nGRP, AddFeatureWndVM.nSelPRC.nSIDE) NewFeat.CreateFeatureParams() NewFeat.SetDefaultValues() Dim vPar() As Double Dim sPar As String = String.Empty NewFeat.CalcParamArray(vPar, sPar) ' aggiorno la feature con nuovo valore EgtBeamSetPart(Map.refProjectVM.BTLStructure.SelPart.nPartId) nNewFeatureId = EgtBeamAddProcess(NewFeat.nSelGRP, NewFeat.nPRC, NewFeat.nSelSIDE, "", Map.refProjectVM.BTLStructure.SelPart.NewProcId(), New Frame3d(), vPar, sPar) bMacroFlag = False End If ' se è stata creata If nNewFeatureId <> GDB_ID.NULL Then ' la aggiungo a struttura BTL corrente NewFeat = New BTLFeature(Map.refProjectVM.BTLStructure.SelPart, nNewFeatureId) If Not bMacroFlag Then NewFeat.SetDefaultValues() Map.refProjectVM.BTLStructure.SelPart.FeatureList.Add(NewFeat) Map.refProjectVM.BTLStructure.SelPart.SelFeature = NewFeat End If End If End Sub #End Region ' AddFeature #Region "RemoveFeature" ''' ''' Returns a command that do Exec. ''' Public ReadOnly Property RemoveFeature_Command As ICommand Get If m_cmdRemoveFeature Is Nothing Then m_cmdRemoveFeature = New Command(AddressOf RemoveFeature) End If Return m_cmdRemoveFeature End Get End Property ''' ''' Execute the Exec. This method is invoked by the ExecCommand. ''' Public Sub RemoveFeature() If EgtBeamEraseProcess(Map.refProjectVM.BTLStructure.SelPart.SelFeature.nFeatureId) Then ' rimuovo dalla lista pezzi Dim Index As Integer = Map.refProjectVM.BTLStructure.SelPart.FeatureList.IndexOf(Map.refProjectVM.BTLStructure.SelPart.SelFeature) If Index = 0 Then If Map.refProjectVM.BTLStructure.SelPart.FeatureList.Count > 0 Then Map.refProjectVM.BTLStructure.SelPart.SelFeature = Map.refProjectVM.BTLStructure.SelPart.FeatureList(0) Else Map.refProjectVM.BTLStructure.SelPart.SelFeature = Nothing End If ElseIf Index = Map.refProjectVM.BTLStructure.SelPart.FeatureList.Count - 1 Then If Map.refProjectVM.BTLStructure.SelPart.FeatureList.Count > 1 Then Map.refProjectVM.BTLStructure.SelPart.SelFeature = Map.refProjectVM.BTLStructure.SelPart.FeatureList(Map.refProjectVM.BTLStructure.SelPart.FeatureList.Count - 2) Else Map.refProjectVM.BTLStructure.SelPart.SelFeature = Nothing End If Else Map.refProjectVM.BTLStructure.SelPart.SelFeature = Map.refProjectVM.BTLStructure.SelPart.FeatureList(Index - 1) End If Map.refProjectVM.BTLStructure.SelPart.FeatureList.RemoveAt(Index) EgtDraw() End If End Sub #End Region ' RemoveFeature #End Region ' COMMANDS End Class