-- Descrizione macchina Essetre-WALL by EgalTech s.r.l.   2022/07/19
-- Cliente PodBau
-- 2022/07/19 Ver.2.4g1 DS Aggiunti 9 utensili (T10-T18) rinominati quelli che collidevano come T.
-- 2024/09/25 Ver.2.6i1 AV NGE con solidi di collisione
--                         Aggiunta controllo collisione con nuovi oggetti

require( 'EmtGenerator')
EgtEnableDebug( false)
if not EmtModifyHeadAuxDirection then error( 'A newer version of the program is required (minimum 2.3j5)') end

PP_VER = '3.1d1_DEV15'
PP_NVER = '2.7.4.1'

-- Parametri macchina
NumericalControl = 'NUM'                                      -- NUM o TPA
MinY = -35209.0
MaxY = -10000.0
MinX1 = 0.0
MaxX1 = 4790.0
MinZ1 = -1550.0
MaxZ1 = 0.0
MinB1 = -125.0
MaxB1 = 125.0
MinC1 = -275.0
MaxC1 = 275.0
PosSpecC1 = 34
MillOffs = 170
ChSawLen = 0.0
ParkC1 = 0
ParkB1 = 0
ParkSawC1 = -90
ParkSawB1 = -90
MinX2 = 1047.0
MaxX2 = 6080.0
MinZ2 = -770.0
MaxZ2 = 0.0
SawOffsX = 110.0
SawOffsY = 0
SawOffsZ = -149.55
SawC1Offs = 0
SawB1Offs = 0
SawOffs = 0.0
Nail11DeltaX = -277.0    -- Distanza da Mandrino a Graffettatrice H21
Nail11DeltaY = -112.5
Nail11DeltaZ = -421.6497
Nail12DeltaX = -279.1874   -- Distanza da Mandrino a Graffettatrice H22
Nail12DeltaY = 135.1311
Nail12DeltaZ = -421.6497
Nail13DeltaX = 318.0 -- Distanza da Mandrino a Graffettatrice H23
Nail13DeltaY = -112.5
Nail13DeltaZ = -421.6497
Nail14DeltaX = 0        -- Distanza da Mandrino a Graffettatrice H24 (non esiste su questa macchina)
Nail14DeltaY = 0
Nail14DeltaZ = 0
Orig2DeltaX = 0         -- Origine angolo tavola 2
Orig2DeltaY = 0
Orig2DeltaZ = 0
Orig3DeltaX = 0     -- Origine angolo tavola 2
Orig3DeltaY = 0
Orig3DeltaZ = 0
Orig4DeltaX = 0     -- Origine angolo tavola 2
Orig4DeltaY = 0
Orig4DeltaZ = 0
Orig5DeltaX = 27000     -- Origine angolo tavola 2
Orig5DeltaY = 0
Orig5DeltaZ = 0
Orig6DeltaX = 27000     -- Origine angolo tavola 2
Orig6DeltaY = -3500
Orig6DeltaZ = 0
DimTabX = 33383.0--31200.0       -- Dimensione tavola
DimTabY = 3500.0
DeltaTabX = -34391.9582---34200.00   -- Distanza da Mandrino a origine principale angolo tavola.
DeltaTabY = 1290.0--1289.00
DeltaTabZ = -1227.6497
CoeffVM = 0.5
OffsetH12X = 110
OffsetH12Z = 150

-- Aggiornamento con dati da TechnoEssetre7
local sTs3Data = EgtGetStringFromIni( 'Wall', 'DATA_DIR', "C:\\TechnoEssetre7\\EgtData", EgtGetIniFile()).."\\Essetre-90480023_XS-MW.data"
local sData = EgtGetCurrMachineDir().."\\Wall\\Ts3Data.lua"
if EgtExistsFile( sTs3Data) then
   EgtCopyFile( sTs3Data, sData)
   local sTs3DataOld = sTs3Data..'.old'
   EgtEraseFile( sTs3DataOld)
   EgtRenameFile( sTs3Data, sTs3DataOld)
end

if EgtExistsFile( sData) then
   local Machine = dofile( sData)
   if Machine and Machine.Offsets then
      local MchOffs = Machine.Offsets
      NumericalControl = EgtIf( MchOffs.TIPO_CN ~= 1, 'NUM', 'TPA')
      if MchOffs.MAX_Y then MaxY = MchOffs.MAX_Y end
      if MchOffs.MIN_Y then MinY = MchOffs.MIN_Y end
      MinX1 = - MchOffs.MAX_X or MinX1
      MaxX1 = - MchOffs.MIN_X or MaxX1
      MinZ1 = MchOffs.MIN_Z or MinZ1
      MaxZ1 = MchOffs.MAX_Z or MaxZ1
      MinB1 = MchOffs.MIN_B or MinB1
      MaxB1 = MchOffs.MAX_B or MaxB1
      MinC1 = MchOffs.MIN_C or MinC1
      MaxC1 = MchOffs.MAX_C or MaxC1
      MinX2 = - MchOffs.MAX_U or MinX2
      MaxX2 = - MchOffs.MIN_U or MaxX2
      MinZ2 = MchOffs.MIN_W or MinZ2
      MaxZ2 = MchOffs.MAX_W or MaxZ2
      MinB2 = MchOffs.MIN_V or MinB2
      MaxB2 = MchOffs.MAX_V or MaxB2
      MinC2 = MchOffs.MIN_A or MinC2
      MaxC2 = MchOffs.MAX_A or MaxC2
      MillOffs = MchOffs.PIVOT_MILL or MillOffs
      SawOffs = MchOffs.PIVOT_SAW or SawOffs
      if MchOffs.OFFSET_ZERO3_Y then DimTabX = - MchOffs.OFFSET_ZERO3_Y end
      if MchOffs.OFFSET_ZERO2_X then DimTabY = - MchOffs.OFFSET_ZERO2_X end
      if MchOffs.ZERO_Y_MILL then DeltaTabX = MchOffs.ZERO_Y_MILL end
      if MchOffs.ZERO_X_MILL then DeltaTabY = - MchOffs.ZERO_X_MILL or DeltaTabY end
      if MchOffs.ZERO_Z_MILL then DeltaTabZ = MchOffs.ZERO_Z_MILL or DeltaTabZ end
      if MchOffs.ZERO_Y_MILL and MchOffs.ZERO_Y_SAW then SawDeltaX = ( MchOffs.ZERO_Y_MILL - MchOffs.ZERO_Y_SAW) end
      if MchOffs.ZERO_X_MILL and MchOffs.ZERO_X_SAW then SawDeltaY = - ( MchOffs.ZERO_X_MILL - MchOffs.ZERO_X_SAW) end
      if MchOffs.ZERO_Z_MILL and MchOffs.ZERO_Z_SAW then SawDeltaZ = ( MchOffs.ZERO_Z_MILL - MchOffs.ZERO_Z_SAW) end
      if Machine.SAWOFFSX then SawOffsX = Machine.SAWOFFSX end
      if Machine.SAWOFFSZ then SawOffsZ = Machine.SAWOFFSZ end
      if Machine.OFFSETCLAMAT1 then SawC1Offs= Machine.OFFSETCLAMAT1 end
      if Machine.OFFSETBLAMAT1 then SawB1Offs= Machine.OFFSETBLAMAT1 end
      if MchOffs.ZERO_Y_MILL and MchOffs.ZERO_NAIL11_Y then Nail11DeltaX = ( MchOffs.ZERO_Y_MILL - MchOffs.ZERO_NAIL11_Y) end
      if MchOffs.ZERO_X_MILL and MchOffs.ZERO_NAIL11_X then Nail11DeltaY = - ( MchOffs.ZERO_X_MILL - MchOffs.ZERO_NAIL11_X) end
      if MchOffs.ZERO_Z_MILL and MchOffs.ZERO_NAIL11_Z then Nail11DeltaZ = ( MchOffs.ZERO_Z_MILL - MchOffs.ZERO_NAIL11_Z) end
      if MchOffs.ZERO_Y_MILL and MchOffs.ZERO_NAIL12_Y then Nail12DeltaX = ( MchOffs.ZERO_Y_MILL - MchOffs.ZERO_NAIL12_Y) end
      if MchOffs.ZERO_X_MILL and MchOffs.ZERO_NAIL12_X then Nail12DeltaY = - ( MchOffs.ZERO_X_MILL - MchOffs.ZERO_NAIL12_X) end
      if MchOffs.ZERO_Z_MILL and MchOffs.ZERO_NAIL12_Z then Nail12DeltaZ = ( MchOffs.ZERO_Z_MILL - MchOffs.ZERO_NAIL12_Z) end
      if MchOffs.ZERO_Y_MILL and MchOffs.ZERO_NAIL13_Y then Nail13DeltaX = ( MchOffs.ZERO_Y_MILL - MchOffs.ZERO_NAIL13_Y) end
      if MchOffs.ZERO_X_MILL and MchOffs.ZERO_NAIL13_X then Nail13DeltaY = - ( MchOffs.ZERO_X_MILL - MchOffs.ZERO_NAIL13_X) end
      if MchOffs.ZERO_Z_MILL and MchOffs.ZERO_NAIL13_Z then Nail13DeltaZ = ( MchOffs.ZERO_Z_MILL - MchOffs.ZERO_NAIL13_Z) end
      if MchOffs.ZERO_Y_MILL and MchOffs.ZERO_NAIL14_Y then Nail14DeltaX = ( MchOffs.ZERO_Y_MILL - MchOffs.ZERO_NAIL14_Y) end
      if MchOffs.ZERO_X_MILL and MchOffs.ZERO_NAIL14_X then Nail14DeltaY = - ( MchOffs.ZERO_X_MILL - MchOffs.ZERO_NAIL14_X) end
      if MchOffs.ZERO_Z_MILL and MchOffs.ZERO_NAIL14_Z then Nail14DeltaZ = ( MchOffs.ZERO_Z_MILL - MchOffs.ZERO_NAIL14_Z) end
      if MchOffs.OFFSET_ZERO2_Y then Orig2DeltaX = -MchOffs.OFFSET_ZERO2_Y end
      if MchOffs.OFFSET_ZERO2_X then Orig2DeltaY = MchOffs.OFFSET_ZERO2_X end
      if MchOffs.OFFSET_ZERO2_Z then Orig2DeltaZ = MchOffs.OFFSET_ZERO2_Z end
      if MchOffs.OFFSET_ZERO3_Y then Orig3DeltaX = -MchOffs.OFFSET_ZERO3_Y end
      if MchOffs.OFFSET_ZERO3_X then Orig3DeltaY = MchOffs.OFFSET_ZERO3_X end
      if MchOffs.OFFSET_ZERO3_Z then Orig3DeltaZ = MchOffs.OFFSET_ZERO3_Z end
      if MchOffs.OFFSET_ZERO4_Y then Orig4DeltaX = -MchOffs.OFFSET_ZERO4_Y end
      if MchOffs.OFFSET_ZERO4_X then Orig4DeltaY = MchOffs.OFFSET_ZERO4_X end
      if MchOffs.OFFSET_ZERO4_Z then Orig4DeltaZ = MchOffs.OFFSET_ZERO4_Z end
      if MchOffs.OFFSET_ZERO5_Y then Orig5DeltaX = -MchOffs.OFFSET_ZERO5_Y end
      if MchOffs.OFFSET_ZERO5_X then Orig5DeltaY = MchOffs.OFFSET_ZERO5_X end
      if MchOffs.OFFSET_ZERO5_Z then Orig5DeltaZ = MchOffs.OFFSET_ZERO5_Z end
      if MchOffs.OFFSET_ZERO6_Y then Orig6DeltaX = -MchOffs.OFFSET_ZERO6_Y end
      if MchOffs.OFFSET_ZERO6_X then Orig6DeltaY = MchOffs.OFFSET_ZERO6_X end
      if MchOffs.OFFSET_ZERO6_Z then Orig6DeltaZ = MchOffs.OFFSET_ZERO6_Z end
   end
end

-- Spostamenti delle chiodatrici rispetto al disegno
local Nail11OffsX = Nail11DeltaX + 277.0
local Nail11OffsY = Nail11DeltaY + 112.5
local Nail11OffsZ = Nail11DeltaZ + 421.6497
local Nail12OffsX = Nail12DeltaX + 279.1874
local Nail12OffsY = Nail12DeltaY - 135.1311
local Nail12OffsZ = Nail12DeltaZ + 421.6497
local Nail13OffsX = Nail13DeltaX - 318.0
local Nail13OffsY = Nail13DeltaY + 112.5
local Nail13OffsZ = Nail13DeltaZ + 421.6497

EmtGeneral {
      File='Essetre-WALL.nge',
      Offset = Vector3d( 651, 3041, -1847.6497),--( -35860.00, 3038.9999, -1847.6497),
      AxisMaxAdjust = 1250,
      ExitMaxAdjust = 1250,
      Special = 'Essetre-WALL.mlse',
      Processor = 'Essetre-WALL.mlpe'}
local BaseId = EmtBase {
      Name = 'Base',
      Geo='BASE/GEO',
      Aux={'BASE/LOAD', 'BASE/MOD01', 'BASE/MOD02', 'BASE/MOD03', 'BASE/MOD04', 'BASE/MOD05', 'BASE/MOD06', 'BASE/MOD07',
           'BASE/MOD08', 'BASE/MOD09', 'BASE/MOD10', 'BASE/MOD11', 'BASE/MOD12', 'BASE/MOD13', 'BASE/MOD14', 'BASE/REF', 'BASE/COLLISION', 'BASE/UNLOAD'}}
   local vtMove = Vector3d( DeltaTabX + 34391.9582, DeltaTabY - 1290.0, DeltaTabZ + 1227.6497 )
   EgtMove( BaseId, vtMove, GDB_RT.GLOB)
-- Tavola
EmtTable {
      Name = 'Tab',
      Parent = 'Base',
      Type = MCH_TT.FLAT,
      Ref1 = Point3d( DeltaTabX, DeltaTabY, DeltaTabZ),
      Scale = {DimTabX/33383.0, DimTabY/3500.0, 1},
      Geo = 'TABLE/GEO'}
-- Traversa
EmtAxis {
      Name = 'Y',
      Parent = 'Base',
      Invert = false,
      Type = MCH_AT.LINEAR,
      Dir = X_AX(),
      Pos = Point3d( 0, 0, 0),--( 840.3156, -311.0, 180.7213),
      Stroke = {MinY, MaxY},
      Home = MinY,
      Geo = 'Y_AXIS/GEO',
      Aux = {'Y_AXIS/SOLID', 'Y_AXIS/TC', 'Y_AXIS/COLLISION'}}
-- Carro 1
EmtAxis {
      Name = 'X1',
      Parent = 'Y',
      Token = 'X',
      Invert = true,
      Type = MCH_AT.LINEAR,
      Dir = Y_AX(),
      Pos = Point3d( 0, 0, 0),--( 703.6, 149.0355, 488.0002),
      Stroke = {MinX1, MaxX1},
      Home = MinX1,
      Geo = 'X1_AXIS/GEO',
      Aux = {'X1_AXIS/SOLID', 'X1_AXIS/COLLISION'}}
EmtAxis {
      Name = 'Z1',
      Parent = 'X1',
      Token = 'Z',
      Type = MCH_AT.LINEAR,
      Dir = Z_AX(),
      Pos = Point3d( 0, 0, 0),--( 0, -1, 763.0001),
      Stroke = {MinZ1, MaxZ1},
      Home = 0,
      Geo = 'Z1_AXIS/GEO',
      Aux = {'Z1_AXIS/SOLID'}}

-- Assi rotanti
EmtAxis {
      Name = 'C1',
      Parent = 'Z1',
      Token = 'C',
      Type = MCH_AT.ROTARY,
      Dir = Z_AX(),
      Pos = Point3d( 0, 0, 485.5966),
      Stroke = {MinC1, MaxC1},
      Home = 0,
      Geo = 'C1_AXIS/GEO',
      Aux = {'C1_AXIS/SOLID', 'C1_AXIS/COLLISION'}}
EmtAxis {
      Name = 'B1',
      Parent = 'C1',
      Token = 'B',
      Type = MCH_AT.ROTARY,
      Dir = X_AX(),
      Pos = Point3d( 0, 0, MillOffs),--( 0, 52.5966, MillOffs),
      Stroke = {MinB1, MaxB1},
      Home = 0,
      Geo = 'B1_AXIS/GEO',
      Aux = {'B1_AXIS/SOLID', 'B1_AXIS/COLLISION'}}
-- Testa principale
EmtHead {
      Name = 'H1',
      Parent = 'B1',
      HSet = 'H1',
      Type = MCH_HT.STD,
      Pos = Point3d( 0, 0, 0),
      TDir = Z_AX(),
      ADir = Y_AX(),
      Rot1W = 0.1,
      OthColl = {'B1/SOLID', 'C1/SOLID'},
      Geo = 'H1_HEAD/GEO'}
-- Testa Lama
EmtHead {
      Name = 'H12',
      Parent = 'B1',
      HSet = 'H1',
      Type = MCH_HT.STD,
      Pos = Point3d( -SawOffsX, SawOffsY, SawOffsZ),
      TDir = Vector3d( 0, sin( SawB1Offs), cos( SawB1Offs)),
      ADir = X_AX(),
      Rot1W = 0.1,
      OthColl = {'B1/SOLID', 'C1/SOLID'},
      Geo = 'H12_HEAD/GEO'}
-- Sega a catena
EmtAxis {
      Name = 'A1',
      Parent = 'B1',
      Token = '**',
      Type = MCH_AT.ROTARY,
      Dir = Z_AX(),
      Pos = Point3d( 0, 0, 0),
      Stroke = { 0, 270},
      Home = 180,
      Geo = 'A1_AXIS/GEO'}
-- Carro 2
local X2Id = EmtAxis {
      Name = 'X2',
      Parent = 'Y',
      Token = 'X',
      Type = MCH_AT.LINEAR,
      Dir = Y_AX(),
      Pos = Point3d( 0, 0, 0),--( 703.60, 159.5747, 488.0002),
      Stroke = {MinX2, MaxX2},
      Home = MaxX2,
      Geo = 'X2_AXIS/GEO',
      Aux = {'X2_AXIS/SOLID'}}
   EgtMove( EgtGetFirstNameInGroup( X2Id, 'SOLID'), Vector3d( Nail11OffsX, Nail11OffsY, 0), GDB_RT.GLOB)
local Z2Id = EmtAxis {
      Name = 'Z2',
      Parent = 'X2',
      Token = 'Z',
      Invert = true,
      Type = MCH_AT.LINEAR,
      Dir = Z_AX(),
      Pos = Point3d( 0, 0, 0),--( 486.3512, -1, 1235.3505),
      Stroke = {MinZ2, MaxZ2},
      Home = 0,
      Geo = 'Z2_AXIS/GEO',
      Aux = {'Z2_AXIS/SOLID'}}
   EgtMove( EgtGetFirstNameInGroup( Z2Id, 'SOLID'), Vector3d( Nail11OffsX, Nail11OffsY, Nail11OffsZ), GDB_RT.GLOB)

local H21Id = EmtHead {
      Name = 'H21',
      Parent = 'Z2',
      HSet = 'H21',
      Type = MCH_HT.STD,
      Pos = Point3d( Nail11DeltaX, Nail11DeltaY, Nail11DeltaZ),
      TDir = Z_AX(),
      Geo = 'H21_HEAD/GEO',
      Aux = {'H21_HEAD/SOLID'}}
   EgtMove( EgtGetFirstNameInGroup( H21Id, 'SOLID'), Vector3d( Nail11OffsX, Nail11OffsY, Nail11OffsZ), GDB_RT.GLOB)
local H22Id = EmtHead {
      Name = 'H22',
      Parent = 'Z2',
      HSet = 'H22',
      Type = MCH_HT.STD,
      Pos = Point3d( Nail12DeltaX, Nail12DeltaY, Nail12DeltaZ),
      TDir = Z_AX(),
      Geo = 'H22_HEAD/GEO',
      Aux = {'H22_HEAD/SOLID'}}
   EgtMove( EgtGetFirstNameInGroup( H22Id, 'SOLID'), Vector3d( Nail12OffsX, Nail12OffsY, Nail12OffsZ), GDB_RT.GLOB)
local H23Id = EmtHead {
      Name = 'H23',
      Parent = 'Z2',
      HSet = 'H23',
      Type = MCH_HT.STD,
      Pos = Point3d( Nail13DeltaX, Nail13DeltaY, Nail13DeltaZ),
      TDir = Z_AX(),
      Geo = 'H23_HEAD/GEO',
      Aux = {'H23_HEAD/SOLID'}}
   EgtMove( EgtGetFirstNameInGroup( H23Id, 'SOLID'), Vector3d( Nail13OffsX, Nail13OffsY, Nail13OffsZ), GDB_RT.GLOB)
-- ToolChanger
local ptTcA = Point3d( -600.1772, 333, -592.1636)
EmtTcPos {
      Name = 'T1',
      Parent = 'Y',
      Pos = ptTcA,
      TDir = Y_AX(),
      ADir = X_AX(),
      Geo = 'Y_AXIS/TA1'}
EmtTcPos {
      Name = 'T2',
      Parent = 'Y',
      Pos = ptTcA - 102.5 * Z_AX(),
      TDir = Y_AX(),
      ADir = X_AX(),
      Geo = 'Y_AXIS/TA2'}
EmtTcPos {
      Name = 'T3',
      Parent = 'Y',
      Pos = ptTcA - 205 * Z_AX(),
      TDir = Y_AX(),
      ADir = X_AX(),
      Geo = 'Y_AXIS/TA3'}
EmtTcPos {
      Name = 'T4',
      Parent = 'Y',
      Pos = ptTcA - 307.5 * Z_AX(),
      TDir = Y_AX(),
      ADir = X_AX(),
      Geo = 'Y_AXIS/TA4'}
EmtTcPos {
      Name = 'T5',
      Parent = 'Y',
      Pos = ptTcA - 410 * Z_AX(),
      TDir = Y_AX(),
      ADir = X_AX(),
      Geo = 'Y_AXIS/TA5'}
EmtTcPos {
      Name = 'T6',
      Parent = 'Y',
      Pos = ptTcA - 512.5 * Z_AX(),
      TDir = Y_AX(),
      ADir = X_AX(),
      Geo = 'Y_AXIS/TA6'}
EmtTcPos {
      Name = 'T8',
      Parent = 'Y',
      Pos = ptTcA - 717.5 * Z_AX() + Vector3d( -OffsetH12X, -OffsetH12Z, 0),
      TDir = Y_AX(),
      ADir = X_AX(),
      Geo = 'Y_AXIS/TA8'}

---------------------------------------------------------------------
-- Funzioni richiamate per modificare i dati macchina in casi particolari
--function OnSetTable()
--end

---------------------------------------------------------------------
local function MoveGroup( nGrpId, vtVers, dPos, bLoc)
   local dVal = EgtGetInfo( nGrpId or GDB_ID.NULL, 'Val')
   if not dVal then return end
   if abs( dPos - dVal) > GEO.EPS_SMALL then
      local nRT = EgtIf( bLoc, GDB_RT.LOC, GDB_RT.GLOB)
      EgtMove( nGrpId, ( dPos - dVal) * vtVers, nRT)
      EgtSetInfo( nGrpId, 'Val', dPos)
   end
end

---------------------------------------------------------------------
function OnSetHead()
   -- se lama su testa 5 assi
   if EMC.HEAD == 'H1' and EMC.TOTDIAM > 600 then
      EmtModifyAxisHome( 'Z1', -150)
      EmtModifyAxisStroke( 'C1', { -180.1, 0.1})
      EmtModifyAxisStroke( 'B1', { -90, 0})
      EmtModifyAxisHome( 'C1', ParkSawC1)
      EmtModifyAxisHome( 'B1', ParkSawB1)
   -- altri casi su testa 5 assi
   elseif EMC.HEAD == 'H1' then
      EmtModifyAxisHome( 'Z1', MaxZ1)
      EmtModifyAxisStroke( 'C1', { MinC1, MaxC1})
      if EMC.TCPOS == 'T201' or EMC.TCPOS == 'T202' then
         EmtModifyAxisStroke( 'B1', { -0.1, 0.1})
         EmtModifyAxisHome( 'B1', 0)
         EmtModifyAxisHome( 'C1', PosSpecC1)
         EmtModifyHeadAuxDirection( 'H1', V_NULL())
      else
         EmtModifyAxisStroke( 'B1', { MinB1, MaxB1})
         EmtModifyAxisHome( 'B1', ParkB1)
         EmtModifyAxisHome( 'C1', ParkC1)
         EmtModifyHeadAuxDirection( 'H1', Y_AX())
      end
   elseif EMC.HEAD == 'H12' then
      EmtModifyAxisHome( 'Z1', -150)
      EmtModifyAxisHome( 'C1', ParkC1)
      EmtModifyAxisHome( 'B1', ParkB1)
      EmtModifyAxisDirection( 'B1', Vector3d( cos( SawC1Offs), -sin( SawC1Offs), 0))
   else
      EmtModifyAxisStroke( 'B1', { MinB1, MaxB1})
      EmtModifyAxisHome( 'C1', ParkC1)
      EmtModifyHeadAuxDirection( 'H1', Y_AX())
   end
   if EMC.HEAD == 'H14' then
      MoveGroup( EgtGetFirstNameInGroup( H14Id, 'SOLID'), Z_AX(), 0, true)
      MoveGroup( EgtGetFirstNameInGroup( H14Id, 'T1'), Z_AX(), 0, true)
      EmtModifyAxisHome( 'C1', 90)
      EgtResetAxisPos( 'C1')
      EmtModifyAxisHome( 'B1', 90)
      EgtResetAxisPos( 'B1')
   else
      MoveGroup( EgtGetFirstNameInGroup( H14Id, 'SOLID'), Z_AX(), 680, true)
      MoveGroup( EgtGetFirstNameInGroup( H14Id, 'T1'), Z_AX(), 680, true)
   end
   if EMC.HEAD == 'H21' then
      MoveGroup( EgtGetFirstNameInGroup( H21Id, 'SOLID'), Z_AX(), 0, true)
      MoveGroup( EgtGetFirstNameInGroup( H21Id, 'T1'), Z_AX(), 0, true)
   else
      MoveGroup( EgtGetFirstNameInGroup( H21Id, 'SOLID'), Z_AX(), 450, true)
      MoveGroup( EgtGetFirstNameInGroup( H21Id, 'T1'), Z_AX(), 450, true)
   end
   if EMC.HEAD == 'H22' then
      MoveGroup( EgtGetFirstNameInGroup( H22Id, 'SOLID'), Z_AX(), 0, true)
      MoveGroup( EgtGetFirstNameInGroup( H22Id, 'T1'), Z_AX(), 0, true)
   else
      MoveGroup( EgtGetFirstNameInGroup( H22Id, 'SOLID'), Z_AX(), 450, true)
      MoveGroup( EgtGetFirstNameInGroup( H22Id, 'T1'), Z_AX(), 450, true)
   end
   if EMC.HEAD == 'H23' then
      MoveGroup( EgtGetFirstNameInGroup( H23Id, 'SOLID'), Z_AX(), 0, true)
      MoveGroup( EgtGetFirstNameInGroup( H23Id, 'T1'), Z_AX(), 0, true)
   else
      MoveGroup( EgtGetFirstNameInGroup( H23Id, 'SOLID'), Z_AX(), 450, true)
      MoveGroup( EgtGetFirstNameInGroup( H23Id, 'T1'), Z_AX(), 450, true)
   end
end

---------------------------------------------------------------------
-- Funzione che calcola la posizione Home di Z per la sega a catena dal valore dell'asse virtuale
function GetChainSawZHomeFromVirtualAxis( dPosA)
   -- quota fissa
   return -150
end

---------------------------------------------------------------------
-- Funzione che calcola l'angolo Home di C per la sega a catena dal valore dell'asse virtuale
function GetChainSawCHomeFromVirtualAxis( dPosA)
   -- se A=180 -> T104 -> HomeC = -90
   if abs( dPosA - 180) < 0.1 then
      return -90
   -- altrimenti A=90 -> T101 -> HomeC = 0
   else
      return 0
   end
end

---------------------------------------------------------------------
-- Funzione per resettare tutte le attivazioni della macchina
function OnResetMachine()
   EmtUnlinkAllRawPartsFromGroups()
   EmtUnlinkAllFixturesFromGroups()
   -- nascondo Vmill
   local nRawId = EgtGetFirstRawPart()
   while nRawId do
      local nVmId = EgtGetFirstNameInGroup( nRawId, 'VMill')
      local nId = EgtGetFirstInGroup( nRawId)
      while nId do
         EgtSetStatus( nId, EgtIf( nId ~= nVmId, GDB_ST.ON, GDB_ST.OFF))
         nId = EgtGetNext( nId)
      end
      nRawId = EgtGetNextRawPart( nRawId)
   end
   EgtSetStatus( EgtGetFirstNameInGroup( GDB_ID.ROOT, 'VMill') or GDB_ID.NULL, GDB_ST.OFF)
end
