Files
EgtCAM5/OptionPanel/MachiningOptionPanel/OperationExpander/DispositionParameterExpander/FixtureParameters/FixtureParametersViewModel.vb
T
Emmanuele Sassi a4b5cd4834 EgtCAM5 :
- Cambiati nomi classi e file.
2018-04-10 17:08:35 +00:00

411 lines
15 KiB
VB.net

Imports System.ComponentModel
Imports System.Collections.ObjectModel
Imports EgtUILib
Namespace EgtCAM5
Public Class FixtureParametersViewModel
Inherits ViewModelBase
Private m_FixtureTypeList As ObservableCollection(Of FixtureListItem)
Public ReadOnly Property FixtureTypeList As ObservableCollection(Of FixtureListItem)
Get
If IsNothing(m_FixtureTypeList) Then
m_FixtureTypeList = New ObservableCollection(Of FixtureListItem)(FixtureType.ReadFixtureTypeFromMachIni())
End If
Return m_FixtureTypeList
End Get
End Property
Private m_SelectedFixtureType As FixtureListItem
Public Property SelectedFixtureType As FixtureListItem
Get
Return m_SelectedFixtureType
End Get
Set(value As FixtureListItem)
m_SelectedFixtureType = value
End Set
End Property
#Region "Messages"
Private m_FixtureErrorMsg As String
Public ReadOnly Property FixtureErrorMsg As String
Get
Return m_FixtureErrorMsg
End Get
End Property
Public ReadOnly Property OkMsg As String
Get
Return EgtMsg(MSG_DISPOSITION + 1)
End Get
End Property
#End Region ' Messages
Sub New(ByRef ExpandFixtureFunction As Action)
ExpandFixtureFunction = AddressOf UpdateFixtureCount
' seleziono secondo elemento se presente perchè primo è categoria
SelectedFixtureType = Nothing
End Sub
Private Sub UpdateFixtureCount()
' resetto tutto
For Index = 0 To m_FixtureTypeList.Count - 1
If TypeOf m_FixtureTypeList(Index) Is FixtureType Then
Dim CurrFixtureType As FixtureType = DirectCast(m_FixtureTypeList(Index), FixtureType)
CurrFixtureType.UsedNumber = 0
End If
Next
' calcolo i sottopezzi utilizzati in questa fase
Dim nUsedFixtureId As Integer = EgtGetFirstFixture()
While nUsedFixtureId <> GDB_ID.NULL
Dim sUsedFixtureName As String = String.Empty
For Index = 0 To m_FixtureTypeList.Count - 1
EgtGetName(nUsedFixtureId, sUsedFixtureName)
If sUsedFixtureName = m_FixtureTypeList(Index).Name Then
Dim CurrFixtureType As FixtureType = DirectCast(m_FixtureTypeList(Index), FixtureType)
CurrFixtureType.UsedNumber += 1
Exit For
End If
Next
nUsedFixtureId = EgtGetNextFixture(nUsedFixtureId)
End While
End Sub
' Definizione comandi
Private m_cmdAdd As ICommand
Private m_cmdRemove As ICommand
#Region "COMMANDS"
#Region "AddCommand"
''' <summary>
''' Returns a command that do Done.
''' </summary>
Public ReadOnly Property AddCommand As ICommand
Get
If m_cmdAdd Is Nothing Then
m_cmdAdd = New RelayCommand(AddressOf Add)
End If
Return m_cmdAdd
End Get
End Property
''' <summary>
''' Execute the Point. This method is invoked by the DoneCommand.
''' </summary>
Public Sub Add(ByVal param As Object)
' resetto il messaggio di errore
m_FixtureErrorMsg = String.Empty
' verifico se è stato selezionato un sottopezzo nella lista
Dim SelectedFixture As FixtureType
If TypeOf param Is FixtureType Then
SelectedFixture = DirectCast(param, FixtureType)
Else
Return
End If
' recupero area della tavola
Dim ptTableMin As Point3d
Dim ptTableMax As Point3d
EgtGetTableArea(1, ptTableMin, ptTableMax)
' calcolo il centro della tavola
Dim ptTableMid As New Point3d((ptTableMax.x - ptTableMin.x) / 2, (ptTableMax.y - ptTableMin.y) / 2, (ptTableMax.z - ptTableMin.z) / 2)
' posiziono il nuovo sottopezzo al centro della tavola
Dim nAddedFixtureId As Integer = EgtAddFixture(SelectedFixture.Name, ptTableMid, 0, 0)
' verifico se è in una posizione valida
If Not DispositionUtility.VerifyFixturePosition(nAddedFixtureId, New Vector3d) Then
' creo un gruppo temporaneo
Dim nTempGroupId As Integer = EgtCreateGroup(GDB_ID.ROOT)
EgtSetLevel(nTempGroupId, GDB_LV.USER)
EgtSetMode(nTempGroupId, GDB_MD.STD)
' calcolo ingombro sottopezzo aggiunto
Dim bboxAddedFixture As New BBox3d
EgtGetBBoxGlob(nAddedFixtureId, GDB_BB.STANDARD, bboxAddedFixture)
' calcolo bbox tavolo
Dim bboxTableArea As New BBox3d(ptTableMin, ptTableMax)
bboxTableArea.Expand(-bboxAddedFixture.DimX / 2, -bboxAddedFixture.DimY / 2, 0)
' creo superficie delle misure della tavola
Dim nTableFrId As Integer = EgtCreateSurfFrRectangle(nTempGroupId, bboxTableArea.Min, bboxTableArea.Max)
' ciclo su tutti i pezzi di questa fase
Dim nFixtureId As Integer = EgtGetFirstFixture()
While nFixtureId <> GDB_ID.NULL
' creo il bbox del sottopezzo
Dim bboxFixture As New BBox3d
EgtGetBBoxGlob(nFixtureId, GDB_BB.STANDARD, bboxFixture)
' faccio offset del bbox del sottopezzo per includere metà del sottopezzo da aggiungere
bboxFixture.Expand(bboxAddedFixture.DimX / 2, bboxAddedFixture.DimY / 2, 0)
' lo porto all'altezza della tavola
Dim ptMinFixtureFr As New Point3d(bboxFixture.Min)
Dim ptMaxFixtureFr As New Point3d(bboxFixture.Max)
ptMinFixtureFr.z = ptTableMin.z
ptMaxFixtureFr.z = ptTableMin.z
' creo la regione occupata dal bbox del sottopezzo
Dim nFixtureFrId As Integer = EgtCreateSurfFrRectangle(nTempGroupId, ptMinFixtureFr, ptMaxFixtureFr)
' sottraggo la regione del sottopezzo da quella della tavola
Dim x = EgtSurfFrSubtract(nTableFrId, nFixtureFrId)
nFixtureId = EgtGetNextFixture(nFixtureId)
End While
' creo gruppo con i bordi della regione di tavola avanzata
Dim TableFrBorderGroupId As Integer = EgtCreateGroup(nTempGroupId)
Dim nTableFrBorderCount As Integer = 0
Dim nChunk As Integer=EgtSurfFrChunkCount(nTableFrId)
For Index = 0 To nChunk - 1
EgtExtractSurfFrChunkLoops(nTableFrId, Index, TableFrBorderGroupId, nTableFrBorderCount)
Next
' verifico se c'è almeno un bordo
If nTableFrBorderCount = 0 Then
m_FixtureErrorMsg = "Impossibile posizionare la ventosa sulla tavola"
OnPropertyChanged("FixtureErrorMsg")
Return
End If
' converto il punto medio della tavola in coordinate globali
Dim PtTableRef As Point3d
EgtGetTableRef(1, PtTableRef)
Dim frTableRef As New Frame3d(PtTableRef)
ptTableMid.ToGlob(frTableRef)
' ciclo sui bordi per trovare il punto più vicino
Dim dMinDist As Double = (bboxTableArea.Max - bboxTableArea.Min).SqLenXY
Dim ptMinAbs As Point3d
Dim BorderId As Integer = EgtGetFirstInGroup(TableFrBorderGroupId)
While BorderId <> GDB_ID.NULL
Dim dDist As Double = 0
Dim ptMinRel As Point3d
Dim nSide As Integer = 0
EgtGetMinDistPntSidePointCurve(ptTableMid, BorderId, Vector3d.Z_AX, dDist, ptMinRel, nSide)
If dDist < dMinDist Then
dMinDist = dDist
ptMinAbs = ptMinRel
End If
BorderId = EgtGetNext(BorderId)
End While
' sposto il sottopezzo nel punto trovato
Dim vtFixtureMove As Vector3d = ptMinAbs - ptTableMid
vtFixtureMove.z = 0
EgtMoveFixture(nAddedFixtureId, vtFixtureMove)
' cancello il gruppo temporaneo
EgtErase(nTempGroupId)
End If
' sottraggo la ventosa aggiunta dal conto di quelle disponibili
SelectedFixture.UsedNumber += 1
EgtDraw()
OnPropertyChanged("FixtureErrorMsg")
End Sub
#End Region ' AddCommand
#Region "RemoveCommand"
''' <summary>
''' Returns a command that do Done.
''' </summary>
Public ReadOnly Property RemoveCommand As ICommand
Get
If m_cmdRemove Is Nothing Then
m_cmdRemove = New RelayCommand(AddressOf Remove)
End If
Return m_cmdRemove
End Get
End Property
''' <summary>
''' Execute the Point. This method is invoked by the DoneCommand.
''' </summary>
Public Sub Remove()
Dim SelectedFixtureId As Integer = EgtGetFirstSelectedObj()
While SelectedFixtureId <> GDB_ID.NULL
Dim NextSelectedId As Integer = EgtGetNextSelectedObj()
If EgtVerifyFixture(SelectedFixtureId) Then
EgtRemoveFixture(SelectedFixtureId)
For Index = 0 To FixtureTypeList.Count - 1
Dim SelFixtureName As String = String.Empty
EgtGetName(SelectedFixtureId, SelFixtureName)
If SelFixtureName = FixtureTypeList(Index).Name Then
Dim CurrFixtureType As FixtureType = DirectCast(FixtureTypeList(Index), FixtureType)
CurrFixtureType.UsedNumber -= 1
End If
Next
End If
SelectedFixtureId = NextSelectedId
End While
EgtDraw()
End Sub
#End Region ' RemoveCommand
#End Region
End Class
End Namespace
Public Class FixtureType
Inherits FixtureListItem
Private m_TotalNumber As Integer
Private m_UsedNumber As Integer
Private m_IsEnabled As Boolean = True
Private m_IsSelected As Boolean = False
Public Property TotalNumber As Integer
Get
Return m_TotalNumber
End Get
Set(value As Integer)
If value <> m_TotalNumber Then
m_TotalNumber = value
NotifyPropertyChanged("UsedTotalRatio")
End If
End Set
End Property
Public Property UsedNumber As Integer
Get
Return m_UsedNumber
End Get
Set(value As Integer)
If value <> m_UsedNumber Then
m_UsedNumber = value
NotifyPropertyChanged("UsedTotalRatio")
If UsedNumber >= TotalNumber Then
m_IsEnabled = False
m_IsSelected = False
NotifyPropertyChanged("IsSelected")
Else
m_IsEnabled = True
End If
NotifyPropertyChanged("IsEnabled")
End If
End Set
End Property
Public ReadOnly Property UsedTotalRatio As String
Get
Return (TotalNumber - UsedNumber).ToString & " / " & TotalNumber.ToString
End Get
End Property
Public ReadOnly Property IsEnabled As Boolean
Get
Return m_IsEnabled
End Get
End Property
Public Property IsSelected As Boolean
Get
Return m_IsSelected
End Get
Set(value As Boolean)
m_IsSelected = value
End Set
End Property
Sub New(sName As String, sCat As DispositionUtility.FIX_TYPE, nTot As Integer)
MyBase.New(sName, sCat)
TotalNumber = nTot
m_IsEnabled = True
End Sub
Public Shared Function ReadFixtureTypeFromMachIni() As List(Of FixtureListItem)
' creo la lista locale
Dim FixtureTypeList As New List(Of FixtureListItem)
' aggiungo le ventose se presenti
Dim sName As String = String.Empty
Dim nTot As Integer = 0
Dim bFirst As Boolean = True
Dim nIndex As Integer = 1
While GetPrivateProfileFixture(S_FIXTURES, System.Globalization.CultureInfo.InvariantCulture.TextInfo.ToTitleCase(FIX_VAC) & nIndex, sName, nTot)
If bFirst Then
FixtureTypeList.Add(New FixtureListItem(FIX_VAC, DispositionUtility.FIX_TYPE.VACUUM))
bFirst = False
End If
FixtureTypeList.Add(New FixtureType(sName, DispositionUtility.FIX_TYPE.VACUUM, nTot))
nIndex += 1
End While
' aggiungo i riferimenti se presenti
bFirst = True
nIndex = 1
While GetPrivateProfileFixture(S_FIXTURES, FIX_REF & nIndex, sName, nTot)
If bFirst Then
FixtureTypeList.Add(New FixtureListItem(FIX_REF, DispositionUtility.FIX_TYPE.REFERENCE))
bFirst = False
End If
FixtureTypeList.Add(New FixtureType(sName, DispositionUtility.FIX_TYPE.REFERENCE, nTot))
nIndex += 1
End While
' aggiungo le morse se presenti
bFirst = True
nIndex = 1
While GetPrivateProfileFixture(S_FIXTURES, FIX_VIS & nIndex, sName, nTot)
If bFirst Then
FixtureTypeList.Add(New FixtureListItem(FIX_VIS, DispositionUtility.FIX_TYPE.VISE))
bFirst = False
End If
FixtureTypeList.Add(New FixtureType(sName, DispositionUtility.FIX_TYPE.VISE, nTot))
nIndex += 1
End While
Return FixtureTypeList
End Function
End Class
Public Class FixtureListItem
Implements INotifyPropertyChanged
Private m_Name As String
Private m_Cathegory As DispositionUtility.FIX_TYPE
Private m_Focusable As Boolean
Public Property Name As String
Get
Return m_Name
End Get
Set(value As String)
m_Name = value
End Set
End Property
Public Property Cathegory As DispositionUtility.FIX_TYPE
Get
Return m_Cathegory
End Get
Set(value As DispositionUtility.FIX_TYPE)
m_Cathegory = value
End Set
End Property
Public ReadOnly Property Focusable As Boolean
Get
Return m_Focusable
End Get
End Property
Public ReadOnly Property CathegoryName As String
Get
Select Case Cathegory
Case DispositionUtility.FIX_TYPE.VACUUM
Return "Vacuum"
Case DispositionUtility.FIX_TYPE.REFERENCE
Return "Reference"
Case DispositionUtility.FIX_TYPE.VISE
Return "Vise"
Case Else
Return String.Empty
End Select
End Get
End Property
Sub New(sName As String, Cathegory As DispositionUtility.FIX_TYPE)
Me.Name = sName
Me.Cathegory = Cathegory
If TypeOf Me Is FixtureType Then
m_Focusable = True
Else
m_Focusable = False
End If
End Sub
Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
Public Sub NotifyPropertyChanged(propName As String)
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propName))
End Sub
End Class