-- Descrizione macchina Essetre-PF1250-3T_5Ax by Egalware s.r.l.   2025/10/28


-- Intestazioni
require( 'EmtGenerator')
EgtEnableDebug( false)

PP_VER = '3.1a2_DEV22'
PP_NVER = '3.1.1.2'
MIN_MACH_VER = '2.7d2'
MACH_NAME = EgtGetCurrMachineName()

-- Carico i dati globali
local sMachDir = EgtGetCurrMachineDir()
-- Elimino direttori altre macchine e imposto direttorio macchina corrente per ricerca librerie
EgtRemoveBaseMachineDirFromPackagePath()
EgtAddToPackagePath( sMachDir .. '\\Beam\\?.lua')
EgtAddToPackagePath( sMachDir .. '\\?.lua')
INFO_STD_PP = require( 'Version')
-- prendo la versione kernel minima necessaria piu' nuova tra macchina e script comuni
if INFO_STD_PP.MIN_MACH_VER_PP_COMMON > MIN_MACH_VER then
   MIN_MACH_VER = INFO_STD_PP.MIN_MACH_VER_PP_COMMON
end

EgtOutLog ( '** ' .. MACH_NAME..' '..PP_VER..' ** ( '..INFO_STD_PP.NAME..': '..INFO_STD_PP.VERSION..' - MinMach: '.. MIN_MACH_VER ..') **', 1)

-- Parametri macchina
NumericalControl = 'TPA'                                      -- NUM o TPA
MinMchY1 = 335
MaxMchY1 = 5790.0
ParkMchY1 = 970.0
MinMchY2 = -5775.0
MaxMchY2 = -324
ParkMchY2 = -955.0
MinV1 = 5.0
MaxV1 = 640.0
ParkV1 = 610.0
MinV2 = -635.0
MaxV2 = -5.0
ParkV2 = -610.0
MinDeltaYV = 317 + 0.1
ExtraParkV = 600
MinY1 = MinV1 + MinDeltaYV
MaxY1 = MaxMchY1
MaxY2 = MaxV2 - MinDeltaYV
MinY2 = MinMchY2
ParkY1 = ParkV1 + MinDeltaYV
ParkY2 = ParkV2 - MinDeltaYV
LoadT = 2172.8
UnloadT = -2400
UnloadSmT = UnloadT - 300
MaxHoOpen = 1300
MaxVeOpen = 420
RollCageMin = 120
RollCageMax = 250
MillOffs = 170.0
SawOffsX = 111.0
SawOffsY = 0
SawOffsZ = -150.25
SawC1Offs = 0
SawB1Offs = 0
Saw2OffsX = -111.0
Saw2OffsY = 0
Saw2OffsZ = -150.25
Saw2C1Offs = 0
Saw2B1Offs = 0
ChSawLen = 91.0
Mill2Offs = 170.0
SawC2Offs = 0
SawB2Offs = 0
Mill3Offs = 219 -- 163.0
MinX1 = 0
MaxX1 = 3300
MinZ1 = -1550
MaxZ1 = 0
MaxZ1Blade = -100
MinC1 = -275
MaxC1 = 275
MinB1 = -127
MaxB1 = 127
MinB1b = -119
MaxB1b = 119
ParkX1 = 250
ParkTc1X1 = 250
ParkTc2X1 = 3100
ParkZ1 = -545
ParkC1 = -90
ParkB1 = -90
ParkCSawX1 = 1700
ParkCSawZ1 = -100
ParkCSaw0Z1 = -400
ParkCSawC1 = -90
ParkCSaw0C1 = 0
ParkCSawB1 = 0
MinLengthLongCSaw = 580      -- gestione speciale per motosega molto lunga. Lunghezza al di sopra della quale e' considerata motosega lunga
ParkLongCSawZ1 = -50         -- gestione speciale per motosega molto lunga. Parcheggio asse Z
ParkLongCSawC1 = 0           -- gestione speciale per motosega molto lunga. Parcheggio asse C
ParkLongCSawB1 = -90         -- gestione speciale per motosega molto lunga. Parcheggio asse B
LongTool = 400
MinLongTc1C1 = -91
MaxLongTc1C1 = 91
ParkLongTc1C1 = 0
MinLongTc2C1 = -271
MaxLongTc2C1 = -89
ParkLongTc2C1 = -180
ParkLongB1 = 0
MinZ1ToChangeH2 = -1400
MinZ3 = -1550
MaxZ3 = -100
MinX3 = 0
MaxX3 = 4000
MinC3 = -181
MaxC3 = 181
MinB3 = -91
MaxB3 = 91
ParkX3 = 0
ParkZ3 = -200
ParkC3 = -90
ParkB3 = -75
SafeX3RotAxis = 1550
SafeZ3RotAxis = -655
MinX2 = -2760
MaxX2 = 0
MinZ2 = 0
MaxZ2 = 795
MinC2 = -275
MaxC2 = 275
MinB2 = -127
MaxB2 = 127
MinB2b = -119
MaxB2b = 119
ParkX2 = -100
SafeX2 = -400
SafeX2RotAxis = -1000
ParkZ2 = 0
ParkInLavZ2 = 200
ParkC2 = -90
ParkB2 = -90
MinDistToolX1X2 = 40
TurnerOffs = 120.0
AggLoad = 50
DeltaTabY = 3050
DeltaTabZ = -1294 + MillOffs                                -- per TPA : -1128.0 + MillOffs    -- per NUM : -958.0
DeltaRulliTraveZ = -903
DimTabY = 1400
DimTabX = 34000
Delta2TabY = -1585.9
Delta2TabZ = 427 - Mill2Offs                                -- per TPA : 378.0 - Mill2Offs      -- per NUM : 208.0
Delta3TabY = 3050
Delta3TabZ = -1233 + Mill3Offs
Head3X = 0
Head3Y = DeltaTabY - Delta3TabY
Head3Z = DeltaTabZ - Delta3TabZ
Head2X = 0
Head2Y = DeltaTabY - Delta2TabY
Head2Z = DeltaTabZ - Delta2TabZ
LenStemH2 = 154
DefTcPos1 = 'T1'
DefTcPos2 = 'T30'
MinForzaPinze = 400                              -- [Kgf]
MaxForzaPinze = 1000                             -- [Kgf]
Tc2Active = true
FirstSaw = false
SecondSaw = true
SecondChain = false                               -- magazzino seconda sega a catena
TcSpecialTools = false                            -- magazzino per utensili speciali. In genere truciolatore
TcAggreBladeUnder = false                         -- lama su aggregato per testa da sotto
EmitAccClamps = true
WriteAllCoordsOnFirstM101 = true
ForceToCloseRollersGate = false
Motors23KW = true
EstimationRapidMultiplier = 1
DistZClampToTable = 5                             -- distanza tra la tavola e il punto più basso della morsa
ClampingCoeffMin = nil                             -- coefficiente di fissaggio, se 0 o nil non fa controllo, per il momento da inizializzare da Ts3Data
MinBeamLenUnloadInLine = 2400             -- se lo scarico è su una linea deve scaricare il pezzo al massimo e deve essere maggiore del valore della variabile

-- Aggiornamento con dati da TechnoEssetre7
local sTs3Data = EgtGetStringFromIni( 'Beam', 'DATA_DIR', "C:\\TechnoEssetre7\\EgtData", EgtGetIniFile()).."\\Essetre-PF1250-3T_5Ax.data"
local sDataBeam = sMachDir.."\\Beam\\Ts3Data.lua"
local sDataWall = sMachDir.."\\Wall\\Ts3Data.lua"
if EgtExistsFile( sTs3Data) then
   EgtCopyFile( sTs3Data, sDataBeam)
   EgtCopyFile( sTs3Data, sDataWall)
   local sTs3DataOld = sTs3Data..'.old'
   EgtEraseFile( sTs3DataOld)
   EgtRenameFile( sTs3Data, sTs3DataOld)
end
if EgtExistsFile( sDataBeam) then
   local Machine = dofile( sDataBeam)
   if Machine then
      if Machine.Offsets then
         --NumericalControl = EgtIf( Machine.Offsets.TIPO_CN == 0, 'NUM', 'TPA')
         if Machine.Offsets.X1_POS then MinX1 = -Machine.Offsets.X1_POS end
         if Machine.Offsets.X1_NEG then MaxX1 = -Machine.Offsets.X1_NEG end
         if Machine.Offsets.PARK_X1 then ParkX1 = -Machine.Offsets.PARK_X1 end
         if Machine.Offsets.Z1_NEG then MinZ1 = Machine.Offsets.Z1_NEG end
         if Machine.Offsets.Z1_POS then MaxZ1 = min( MaxZ1, Machine.Offsets.Z1_POS) end
         if Machine.Offsets.Z1_POS_LAMA then MaxZ1Blade = min( MaxZ1Blade, Machine.Offsets.Z1_POS_LAMA) end
         if Machine.Offsets.PARK_Z1 then ParkZ1 = Machine.Offsets.PARK_Z1 end
         if Machine.Offsets.C1_NEG then MinC1 = Machine.Offsets.C1_NEG end
         if Machine.Offsets.C1_POS then MaxC1 = Machine.Offsets.C1_POS end
         if Machine.Offsets.B1_NEG then MinB1 = Machine.Offsets.B1_NEG end
         if Machine.Offsets.B1_POS then MaxB1 = Machine.Offsets.B1_POS end
         if Machine.Offsets.B1_NEG1 then MinB1b = Machine.Offsets.B1_NEG1 end
         if Machine.Offsets.B1_POS1 then MaxB1b = Machine.Offsets.B1_POS1 end
         if Machine.Offsets.X2_NEG then MinX2 = Machine.Offsets.X2_NEG end
         if Machine.Offsets.X2_POS then MaxX2 = Machine.Offsets.X2_POS end
         if Machine.Offsets.PARK_X2 then ParkX2 = Machine.Offsets.PARK_X2 end
         if Machine.Offsets.Z2_POS then MinZ2 = -Machine.Offsets.Z2_POS end
         if Machine.Offsets.Z2_NEG then MaxZ2 = -Machine.Offsets.Z2_NEG end
         if Machine.Offsets.PARK_Z2 then ParkZ2 = -Machine.Offsets.PARK_Z2 end
         if Machine.Offsets.PARK_Z2_INLAV then ParkInLavZ2 = -Machine.Offsets.PARK_Z2_INLAV end
         if Machine.Offsets.C2_NEG then MinC2 = Machine.Offsets.C2_NEG end
         if Machine.Offsets.C2_POS then MaxC2 = Machine.Offsets.C2_POS end
         if Machine.Offsets.B2_NEG then MinB2 = Machine.Offsets.B2_NEG end
         if Machine.Offsets.B2_POS then MaxB2 = Machine.Offsets.B2_POS end
         if Machine.Offsets.B2_NEG1 then MinB2b = Machine.Offsets.B2_NEG1 end
         if Machine.Offsets.B2_POS1 then MaxB2b = Machine.Offsets.B2_POS1 end
         if Machine.Offsets.X3_POS then MinX3 = -Machine.Offsets.X3_POS end
         if Machine.Offsets.X3_NEG then MaxX3 = -Machine.Offsets.X3_NEG end
         if Machine.Offsets.PARK_X3 then ParkX3 = -Machine.Offsets.PARK_X3 end
         if Machine.Offsets.Z3_NEG then MinZ3 = Machine.Offsets.Z3_NEG end
         if Machine.Offsets.Z3_POS then MaxZ3 = Machine.Offsets.Z3_POS end
         if Machine.Offsets.PARK_Z3 then ParkZ3 = Machine.Offsets.PARK_Z3 end
         if Machine.Offsets.B3_NEG then MinB3 = Machine.Offsets.B3_NEG end
         if Machine.Offsets.B3_POS then MaxB3 = Machine.Offsets.B3_POS end
         if Machine.Offsets.MIN_Y1 then MinMchY1 = Machine.Offsets.MIN_Y1 end
         if Machine.Offsets.MAX_Y1 then MaxMchY1 = Machine.Offsets.MAX_Y1 end
         if Machine.Offsets.PARK_Y1 then ParkMchY1 = Machine.Offsets.PARK_Y1 end
         if Machine.Offsets.MIN_V1 then MinV1 = Machine.Offsets.MIN_V1 end
         if Machine.Offsets.MAX_V1 then MaxV1 = Machine.Offsets.MAX_V1 end
         if Machine.Offsets.MIN_Y2 then MinMchY2 = Machine.Offsets.MIN_Y2 end
         if Machine.Offsets.MAX_Y2 then MaxMchY2 = Machine.Offsets.MAX_Y2 end
         if Machine.Offsets.PARK_Y2 then ParkMchY2 = Machine.Offsets.PARK_Y2 end
         if Machine.Offsets.MIN_V2 then MinV2 = Machine.Offsets.MIN_V2 end
         if Machine.Offsets.MAX_V2 then MaxV2 = Machine.Offsets.MAX_V2 end
         if Machine.Offsets.MIN_DELTAVY then MinDeltaYV = Machine.Offsets.MIN_DELTAVY + 0.1 end
         if Machine.Offsets.DIST_FTZERO then LoadT = Machine.Offsets.DIST_FTZERO end
         if Machine.Offsets.DELTA_CARICZERO then TurnerOffs = Machine.Offsets.DELTA_CARICZERO end
         if Machine.Offsets.DIST_UNLOAD then UnloadT = Machine.Offsets.DIST_UNLOAD end
         if Machine.Offsets.PIVOT_T1 then MillOffs = Machine.Offsets.PIVOT_T1 end
         if Machine.Offsets.PIVOT_T2 then Mill2Offs = Machine.Offsets.PIVOT_T2 end
         if Machine.Offsets.PIVOT_T3 then Mill3Offs = Machine.Offsets.PIVOT_T3 end
         if Machine.Offsets.OFFSETCLAMAT1 then SawC1Offs= Machine.Offsets.OFFSETCLAMAT1 end
         if Machine.Offsets.OFFSETBLAMAT1 then SawB1Offs= Machine.Offsets.OFFSETBLAMAT1 end
         if Machine.Offsets.OFFSETCLAMAT2 then Saw2C1Offs= Machine.Offsets.OFFSETCLAMAT2 end
         if Machine.Offsets.OFFSETBLAMAT2 then Saw2B1Offs= Machine.Offsets.OFFSETBLAMAT2 end
         if Machine.Offsets.OFFSETXT1 then DeltaTabY = -Machine.Offsets.OFFSETXT1 end
         if Machine.Offsets.OFFSETZT1 then DeltaTabZ = Machine.Offsets.OFFSETZT1 + MillOffs end
         if Machine.Offsets.OFFSETXT2 then Delta2TabY = Machine.Offsets.OFFSETXT2 end
         if Machine.Offsets.OFFSETZT2 then Delta2TabZ = -Machine.Offsets.OFFSETZT2 - Mill2Offs end
         if Machine.Offsets.OFFSETXT3 then Delta3TabY = -Machine.Offsets.OFFSETXT3 end
         if Machine.Offsets.OFFSETZT3 then Delta3TabZ = Machine.Offsets.OFFSETZT3 + Mill3Offs end
         if Machine.Offsets.OFFSETZSOTTOTRAVE then DeltaRulliTraveZ = -abs( Machine.Offsets.OFFSETZSOTTOTRAVE) end
         if Machine.Offsets.SECONDSAW then SecondSaw = ( Machine.Offsets.SECONDSAW == 1) end
         if Machine.Offsets.SAWOFFSX then SawOffsX = Machine.Offsets.SAWOFFSX end
         if Machine.Offsets.SAWOFFSZ then SawOffsZ = Machine.Offsets.SAWOFFSZ end
         if Machine.Offsets.SAW2OFFSX then Saw2OffsX = Machine.Offsets.SAW2OFFSX end
         if Machine.Offsets.SAW2OFFSZ then Saw2OffsZ = Machine.Offsets.SAW2OFFSZ end
         if Machine.Offsets.DEFTCPOS1 then DefTcPos1 = ( 'T' .. Machine.Offsets.DEFTCPOS1) end
         if Machine.Offsets.DEFTCPOS2 then DefTcPos2 = ( 'T' .. Machine.Offsets.DEFTCPOS2) end
         if Machine.Offsets.MINPRESS then MinForzaPinze = Machine.Offsets.MINPRESS end
         if Machine.Offsets.MAXPRESS then MaxForzaPinze = Machine.Offsets.MAXPRESS end
         if Machine.Offsets.NOULOAD then MaxUnloadLen = Machine.Offsets.NOULOAD end
         if Machine.Offsets.MAXFEEDPINZE then MaxFeedPinze = Machine.Offsets.MAXFEEDPINZE end
         if Machine.Offsets.MAXACC then MaxAcc = Machine.Offsets.MAXACC end
         if Machine.Offsets.MINACC then MinAcc = Machine.Offsets.MINACC end
         if Machine.Offsets.COEFF_STIMATEMPI and Machine.Offsets.COEFF_STIMATEMPI > 0 then EstimationRapidMultiplier = Machine.Offsets.COEFF_STIMATEMPI end
         if Machine.Offsets.CHIUDI_PINZE_2_3_SCAMBIO then ForceToCloseRollersGate = Machine.Offsets.CHIUDI_PINZE_2_3_SCAMBIO == 1 end
         if Machine.Offsets.WOOD_DENSITY then WoodDensity = Machine.Offsets.WOOD_DENSITY end
         if Machine.Offsets.MOTORS23KW then Motors23KW = Machine.Offsets.MOTORS23KW == 1 end
         if Machine.Offsets.LEN_STEM_H2 then LenStemH2 = Machine.Offsets.LEN_STEM_H2 end
         if Machine.Offsets.COEFF_CLAMP_CHECK then ClampingCoeffMin = Machine.Offsets.COEFF_CLAMP_CHECK end
         if Machine.Offsets.MIN_BEAM_LEN_UNLOAD_IN_LINE then MinBeamLenUnloadInLine = Machine.Offsets.MIN_BEAM_LEN_UNLOAD_IN_LINE end

         -- aggiustamenti
         MinY1 = MinV1 + MinDeltaYV
         MaxY1 = MaxMchY1
         ParkV1 = MaxV1
         ParkY1 = ParkV1 + MinDeltaYV
         MinY2 = MinMchY2
         MaxY2 = MaxV2 - MinDeltaYV
         ParkV2 = MinV2
         ParkY2 = ParkV2 - MinDeltaYV
         UnloadSmT = UnloadT - 300
         ParkX1 = EgtClamp( ParkX1, MinX1, MaxX1)
         ParkTc1X1 = EgtClamp( ParkTc1X1, MinX1, MaxX1)
         ParkZ1 = EgtClamp( ParkZ1, MinZ1, MaxZ1)
         MaxZ1Blade = min( MaxZ1Blade, -100)
         ParkCSawZ1 = min( ParkCSawZ1, MaxZ1)
         ParkX2 = EgtClamp( ParkX2, MinX2, MaxX2)
         ParkZ2 = EgtClamp( ParkZ2, MinZ2, MaxZ2)
         ParkInLavZ2 = EgtClamp( ParkInLavZ2, MinZ2, MaxZ2)
         ParkX3 = EgtClamp( ParkX3, MinX3, MaxX3)
         ParkZ3 = EgtClamp( ParkZ3, MinZ3, MaxZ3)
         Head2Y = DeltaTabY - Delta2TabY
         Head2Z = DeltaTabZ - Delta2TabZ
         Head3Y = DeltaTabY - Delta3TabY
         Head3Z = DeltaTabZ - Delta3TabZ
      end
   end
end
SecondSaw = ( SecondSaw and Tc2Active)
local vtOffset23KW = Vector3d( 0, 0, EgtIf( Motors23KW, 37, 0))

EmtGeneral {
      File='Essetre-PF1250-3T_5Ax.nge',
      Offset = Vector3d(0.0,1960.0,-2130.0),
      AxisMaxAdjust = 300,
      AxisMaxRotAdj = 0.5,
      ExitMaxAdjust = 300,
      ExitMaxRotAdj = 0.5,
      AngDeltaMinForHome = 80,
      LinkAxesMoveOrder = MCH_LKAMO.HEAD_BEFORE_IF_SAME_ANG,
      Special = 'Common_PF1250.mlse',
      Processor = 'Common_PF1250.mlpe'}
-- in base alla corsa macchina, si carica la rulliera di carico/scarico appropriata
local sGeomConvoyer = EgtIf( MaxY1 > 6500, '_8M', '_5M')
local sBaseAux = {'BASE/SOLID', 'BASE/CONVOYER'..sGeomConvoyer, 'BASE/COLLISION', 'BASE/TC1', 'BASE/TCR', 'BASE/TC3'}
if Tc2Active then table.insert( sBaseAux, 5, 'BASE/TC2') end
local BaseId = EmtBase {
      Name = 'Base',
      Geo = 'BASE/GEO',
      Aux = sBaseAux}
local X1Id = EmtAxis {
      Name = 'X1',
      Parent = 'Base',
      Token = 'X',
      Type = MCH_AT.LINEAR,
      Dir = Y_AX(),
      Pos = Point3d(207.0,-300.0,104.0),
      Stroke = {MinX1, MaxX1},
      Home = ParkX1,
      Invert = true,
      Geo = 'X1_AXIS/GEO',
      Aux = {'X1_AXIS/SOLID', 'X1_AXIS/COLLISION'}}
--  *** Testa 1 ***
local Z1Id = EmtAxis {
      Name = 'Z1',
      Parent = 'X1',
      Token = 'Z',
      Type = MCH_AT.LINEAR,
      Dir = Z_AX(),
      Pos = Point3d(0.0,-140.1,113.0),
      Stroke = {MinZ1, MaxZ1},
      Home = ParkZ1,
      Geo = 'Z1_AXIS/GEO',
      Aux = {'Z1_AXIS/SOLID', 'Z1_AXIS/COLLISION'}}
EmtAxis {
      Name = 'C1',
      Parent = 'Z1',
      Token = 'C',
      Type = MCH_AT.ROTARY,
      Dir = Z_AX(),
      Pos = Point3d(0.0,0.0,503.0),
      Stroke = {MinC1, MaxC1},
      Home = ParkC1,
      Geo = EgtIf( Motors23KW, 'C1_AXIS_23KW/GEO', 'C1_AXIS/GEO'),
      Aux = {EgtIf( Motors23KW, 'C1_AXIS_23KW/SOLID', 'C1_AXIS/SOLID'), EgtIf( Motors23KW, 'C1_AXIS_23KW/COLLISION', 'C1_AXIS/COLLISION')}}
EmtAxis {
      Name = 'B1',
      Parent = 'C1',
      Token = 'B',
      Type = MCH_AT.ROTARY,
      Dir = X_AX(),
      Pos = Point3d(-108.4,0.0,MillOffs),
      Stroke = {MinB1, MaxB1},
      Home = ParkB1,
      Geo = EgtIf( Motors23KW, 'B1_AXIS_23KW/GEO', 'B1_AXIS/GEO'),
      Aux = {EgtIf( Motors23KW, 'B1_AXIS_23KW/SOLID', 'B1_AXIS/SOLID'), EgtIf( Motors23KW, 'B1_AXIS_23KW/COLLISION', 'B1_AXIS/COLLISION')}}
-- Frese
local H11Id = EmtHead {
      Name = 'H11',
      Parent = 'B1',
      HSet = 'H11',
      Type = MCH_HT.STD,
      Pos = Point3d(0,0,0),
      TDir = Z_AX(),
      ADir = -Y_AX(),
      Rot1W = 0.2,
      Rot2Stroke = { MinB1, MaxB1},
      OthColl = {'B1/SOLID', 'C1/SOLID'},
      Geo = 'H11_HEAD/GEO'}
EgtSetInfo( H11Id, 'ZEXTRA', '250,60')
-- Lama
if FirstSaw then
   local H12Id = EmtHead {
         Name = 'H12',
         Parent = 'B1',
         HSet = 'H11',
         Type = MCH_HT.STD,
         Pos = Point3d( SawOffsX, SawOffsY, SawOffsZ),
         TDir = Vector3d( 0, sin( SawB1Offs), cos( SawB1Offs)),
         ADir = -X_AX(),
         Rot1W = 0.5,
         Rot2Stroke = { MinB1b, MaxB1b},
         OthColl = {'B1/SOLID', 'C1/SOLID'},
         Geo = 'H12_HEAD/GEO'}
   EgtSetInfo( H12Id, 'ZEXTRA', '160,60')
   EgtSetInfo( H12Id, 'ZMAXONROT', '1,190')
end
-- Lama 2
if SecondSaw then
   local H16Id = EmtHead {
         Name = 'H16',
         Parent = 'B1',
         HSet = 'H11',
         Type = MCH_HT.STD,
         Pos = Point3d( Saw2OffsX, Saw2OffsY, Saw2OffsZ),
         TDir = Vector3d( 0, -sin( Saw2B1Offs), cos( Saw2B1Offs)),
         ADir = X_AX(),
         Rot1W = 0.5,
         Rot2Stroke = { MinB1b, MaxB1b},
         OthColl = {'B1/SOLID', 'C1/SOLID'},
         Geo = 'H16_HEAD/GEO'}
   EgtSetInfo( H16Id, 'ZEXTRA', '160,60')
   EgtSetInfo( H16Id, 'ZMAXONROT', '1,190')
end
-- 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 = 0,
      Geo = 'A1_AXIS/GEO'}
local H13Id = EmtHead {
      Name = 'H13',
      Parent = 'A1',
      HSet = 'H11',
      Type = MCH_HT.STD,
      Pos = Point3d(0,0,-ChSawLen),
      TDir = -X_AX(),
      ADir = Z_AX(),
      Rot1W = 0.2,
      Rot2Stroke = { -90.1, 90.1},
      SolCh = MCH_SCC.ADIR_NEAR,
      OthColl = {'B1/SOLID', 'C1/SOLID'},
      Geo = 'H13_HEAD/GEO'}
EgtSetInfo( H13Id, 'ZEXTRA', '200')
EgtSetInfo( H13Id, 'ZSAFEDELTA', '60')
EgtSetInfo( H13Id, 'ZMAXONROT', '1,80')
--  *** Testa 3 ***
local X3Id = EmtAxis {
      Name = 'X3',
      Parent = 'Base',
      Token = 'X',
      Type = MCH_AT.LINEAR,
      Dir = Y_AX(),
      Pos = Point3d( 207, -300, 104),
      Stroke = {MinX3, MaxX3},
      Home = ParkX3,
      Invert = true,
      Geo = 'X3_AXIS/GEO',
      Aux = {'X3_AXIS/SOLID', 'X3_AXIS/COLLISION'}}
local Z3Id = EmtAxis {
      Name = 'Z3',
      Parent = 'X3',
      Token = 'Z',
      Type = MCH_AT.LINEAR,
      Dir = Z_AX(),
      Pos = Point3d( 0, -140, 563),
      Stroke = {MinZ3, MaxZ3},
      Home = ParkZ3,
      Geo = 'Z3_AXIS/GEO',
      Aux = {'Z3_AXIS/SOLID', 'Z3_AXIS/COLLISION'}}
local C3Id = EmtAxis {
      Name = 'C3',
      Parent = 'Z3',
      Token = 'C',
      Type = MCH_AT.ROTARY,
      Dir = Z_AX(),
      Pos = Point3d( Head3X, Head3Y, 503.0),
      Stroke = {MinC3, MaxC3},
      Home = ParkC3,
      Geo = 'C3_AXIS/GEO',
      Aux = {'C3_AXIS/SOLID', 'C3_AXIS/COLLISION'}}
local B3Id = EmtAxis {
      Name = 'B3',
      Parent = 'C3',
      Token = 'B',
      Type = MCH_AT.ROTARY,
      Dir = X_AX(),
      Pos = Point3d( 163.5, Head3Y, Head3Z+Mill3Offs),
      Stroke = {MinB3, MaxB3},
      Home = ParkB3,
      Geo = 'B3_AXIS/GEO',
      Aux = {'B3_AXIS/SOLID', 'B3_AXIS/COLLISION'}}
local vtMoveX3 = Vector3d( 0, Head3Y, 0)
EgtMove( EgtGetFirstNameInGroup( X3Id, 'SOLID'), vtMoveX3, GDB_RT.GLOB)
local vtMoveZ3 = Vector3d( Head3X, Head3Y, Head3Z + 110)    -- 110 : distanza tra naso mandrino testa 1 e naso mandrino testa 3
EgtMove( EgtGetFirstNameInGroup( Z3Id, 'SOLID'), vtMoveZ3, GDB_RT.GLOB)
EgtMove( EgtGetFirstNameInGroup( Z3Id, 'COLLISION'), vtMoveZ3, GDB_RT.GLOB)
EgtMove( EgtGetFirstNameInGroup( C3Id, 'SOLID'), vtMoveZ3, GDB_RT.GLOB)
EgtMove( EgtGetFirstNameInGroup( C3Id, 'COLLISION'), vtMoveZ3, GDB_RT.GLOB)
EgtMove( EgtGetFirstNameInGroup( B3Id, 'SOLID'), vtMoveZ3, GDB_RT.GLOB)
EgtMove( EgtGetFirstNameInGroup( B3Id, 'COLLISION'), vtMoveZ3, GDB_RT.GLOB)
-- Frese
local H31Id = EmtHead {
      Name = 'H38',
      Parent = 'B3',
      HSet = 'H38',
      Type = MCH_HT.STD,
      Pos = Point3d( Head3X, Head3Y, Head3Z),
      TDir = Vector3d( 0, 0, 1),
      ADir = X_AX(),
      Rot1W = 0.2,
      SolCh = MCH_SCC.ADIR_XM,
      Rot2Stroke = {MinB3, MaxB3},
      OthColl = {'B3/SOLID', 'C3/SOLID'},
      Geo = 'H38_HEAD/GEO'}
--  *** Testa 2 ***
local Z2Id = EmtAxis {
      Name = 'Z2',
      Parent = 'Base',
      Token = 'Z',
      Type = MCH_AT.LINEAR,
      Dir = Z_AX(),
      Pos = Point3d(291.0,-140.1,-2356.3),
      Stroke = {MinZ2, MaxZ2},
      Home = ParkZ2,
      Invert = true,
      Geo = 'Z2_AXIS/GEO',
      Aux = {'Z2_AXIS/SOLID', 'Z2_AXIS/COLLISION'}}
local X2Id = EmtAxis {
      Name = 'X2',
      Parent = 'Z2',
      Token = 'X',
      Type = MCH_AT.LINEAR,
      Dir = Y_AX(),
      Pos = Point3d(115.8,3465.9,-2038.0),
      Stroke = {MinX2, MaxX2},
      Home = ParkX2,
      Geo = 'X2_AXIS/GEO',
      Aux = {'X2_AXIS/SOLID', 'X2_AXIS/COLLISION'}}
local C2Id = EmtAxis {
      Name = 'C2',
      Parent = 'X2',
      Token = 'C',
      Type = MCH_AT.ROTARY,
      Dir = -Z_AX(),
      Pos = Point3d( Head2X, Head2Y, Head2Z-485),
      Stroke = {MinC2, MaxC2},
      Home = ParkC2,
      Geo = EgtIf( Motors23KW, 'C2_AXIS_23KW/GEO', 'C2_AXIS/GEO'),
      Aux = {EgtIf( Motors23KW, 'C2_AXIS_23KW/SOLID', 'C2_AXIS/SOLID'), EgtIf( Motors23KW, 'C2_AXIS_23KW/COLLISION', 'C2_AXIS/COLLISION')}}
local B2Id = EmtAxis {
      Name = 'B2',
      Parent = 'C2',
      Token = 'B',
      Type = MCH_AT.ROTARY,
      Dir = X_AX(),
      Pos = Point3d( Head2X, Head2Y, Head2Z-Mill2Offs),
      Stroke = {MinB2, MaxB2},
      Home = ParkB2,
      Geo = EgtIf( Motors23KW, 'B2_AXIS_23KW/GEO', 'B2_AXIS/GEO'),
      Aux = {EgtIf( Motors23KW, 'B2_AXIS_23KW/SOLID', 'B2_AXIS/SOLID'), EgtIf( Motors23KW, 'B2_AXIS_23KW/COLLISION', 'B2_AXIS/COLLISION')}}

-- si sistema lunghezza stelo testa 2
local LenStemH2Offset = Vector3d( 0, 0, 154 - LenStemH2)
local SolidArmId = EgtGetFirstNameInGroup( ( EgtGetFirstNameInGroup( X2Id, 'SOLID')), 'STEM')
EgtSurfTmMoveFacet( SolidArmId, 106, -LenStemH2Offset, GDB_RT.GLOB)

local vtMoveZ2 = Vector3d( 0, 0, Head2Z + 1381.0)
EgtMove( EgtGetFirstNameInGroup( Z2Id, 'SOLID'), vtMoveZ2 - vtOffset23KW + LenStemH2Offset, GDB_RT.GLOB)
EgtMove( EgtGetFirstNameInGroup( Z2Id, 'COLLISION'), vtMoveZ2 - vtOffset23KW + LenStemH2Offset, GDB_RT.GLOB)
local vtMoveX2 = Vector3d( 0, Head2Y - 4635.9, Head2Z + 1381.0)
EgtMove( EgtGetFirstNameInGroup( X2Id, 'SOLID'), vtMoveX2 - vtOffset23KW + LenStemH2Offset, GDB_RT.GLOB)
EgtMove( EgtGetFirstNameInGroup( X2Id, 'COLLISION'), vtMoveX2 - vtOffset23KW + LenStemH2Offset, GDB_RT.GLOB)

EgtMove( EgtGetFirstNameInGroup( C2Id, 'SOLID'), vtMoveX2, GDB_RT.GLOB)
EgtMove( EgtGetFirstNameInGroup( C2Id, 'COLLISION'), vtMoveX2, GDB_RT.GLOB)
EgtMove( EgtGetFirstNameInGroup( B2Id, 'SOLID'), vtMoveX2, GDB_RT.GLOB)
EgtMove( EgtGetFirstNameInGroup( B2Id, 'COLLISION'), vtMoveX2, GDB_RT.GLOB)
-- Frese
local H21Id = EmtHead {
      Name = 'H21',
      Parent = 'B2',
      HSet = 'H21',
      Type = MCH_HT.STD,
      Pos = Point3d( Head2X, Head2Y, Head2Z),
      TDir = -Z_AX(),
      Rot1W = 0.2,
      Rot2Stroke = { MinB2, MaxB2},
      OthColl = {'B2/SOLID', 'C2/SOLID'},
      Geo = 'H21_HEAD/GEO'}
EgtSetInfo( H21Id, 'ABOVE', '0')
EgtSetInfo( H21Id, 'ZHOMEDOWN', '1')
--  *** Carrelli ***
-- Morse
local Y1Id = EmtAxis {
      Name = 'Y1',
      Parent = 'Base',
      Type = MCH_AT.LINEAR,
      Dir = -X_AX(),
      Pos = Point3d(-865.7,2626.8,-866.0),
      Stroke = { MinY1, MaxY1},
      Home = ParkY1,
      Geo = 'Y1_AXIS/GEO',
      Aux = 'Y1_AXIS/SOLID'}
local PY1Id = EmtAxis {
      Name = 'PY1',
      Parent = 'Y1',
      Type = MCH_AT.LINEAR,
      Dir = -Y_AX(),
      Pos = Point3d(-469.6,2367.5,-751.6),
      Stroke = {0, MaxHoOpen},
      Home = MaxHoOpen,
      Geo = 'PY1_AXIS/GEO',
      Aux = {'PY1_AXIS/SOLID', 'PY1_AXIS/CLAMP_CHECK'}}
local Y2Id = EmtAxis {
      Name = 'Y2',
      Parent = 'Base',
      Type = MCH_AT.LINEAR,
      Dir = -X_AX(),
      Pos = Point3d(865.7,2626.8,-866.0),
      Stroke = { MinY2, MaxY2},
      Home = ParkY2,
      Geo = 'Y2_AXIS/GEO',
      Aux = 'Y2_AXIS/SOLID'}
local PY2Id = EmtAxis {
      Name = 'PY2',
      Parent = 'Y2',
      Type = MCH_AT.LINEAR,
      Dir = -Y_AX(),
      Pos = Point3d(469.6,2367.5,-751.6),
      Stroke = {0, MaxHoOpen},
      Home = MaxHoOpen,
      Geo = 'PY2_AXIS/GEO',
      Aux = {'PY2_AXIS/SOLID', 'PY2_AXIS/CLAMP_CHECK'}}
-- Rulli
local V1Id = EmtAxis {
      Name = 'V1',
      Parent = 'Base',
      Type = MCH_AT.LINEAR,
      Dir = -X_AX(),
      Pos = Point3d(-875.5,1072.0,-1509.1),
      Stroke = { MinV1, MaxV1},
      Home = ParkV1,
      Geo = 'V1_AXIS/GEO',
      Aux = {'V1_AXIS/SOLID', 'V1_AXIS/COLLISION'}}
local PV1Id = EmtAxis {
      Name = 'PV1',
      Parent = 'V1',
      Type = MCH_AT.LINEAR,
      Dir = -Y_AX(),
      Pos = Point3d(-194.0,2364.7,-879.1),
      Stroke = {0, MaxHoOpen},
      Home = MaxHoOpen,
      Geo = 'PV1_AXIS/GEO',
      Aux = 'PV1_AXIS/SOLID'}
local QV1Id = EmtAxis {
      Name = 'QV1',
      Parent = 'V1',
      Type = MCH_AT.LINEAR,
      Dir = Z_AX(),
      Pos = Point3d(-287.0,2481.0,-943.0),
      Stroke = {0, MaxVeOpen},
      Home = MaxVeOpen,
      Geo = 'QV1_AXIS/GEO',
      Aux = 'QV1_AXIS/SOLID'}
local V2Id = EmtAxis {
      Name = 'V2',
      Parent = 'Base',
      Type = MCH_AT.LINEAR,
      Dir = -X_AX(),
      Pos = Point3d(875.5,1072.0,-1509.1),
      Stroke = { MinV2, MaxV2},
      Home = ParkV2,
      Geo = 'V2_AXIS/GEO',
      Aux = {'V2_AXIS/SOLID', 'V2_AXIS/COLLISION'}}
local PV2Id = EmtAxis {
      Name = 'PV2',
      Parent = 'V2',
      Type = MCH_AT.LINEAR,
      Dir = -Y_AX(),
      Pos = Point3d(194.0,2364.7,-879.1),
      Stroke = {0, MaxHoOpen},
      Home = MaxHoOpen,
      Geo = 'PV2_AXIS/GEO',
      Aux = 'PV2_AXIS/SOLID'}
local QV2Id = EmtAxis {
      Name = 'QV2',
      Parent = 'V2',
      Type = MCH_AT.LINEAR,
      Dir = Z_AX(),
      Pos = Point3d(287.0,2481.0,-943.0),
      Stroke = {0, MaxVeOpen},
      Home = MaxVeOpen,
      Geo = 'QV2_AXIS/GEO',
      Aux = 'QV2_AXIS/SOLID'}
--  *** Tavola ***
EmtAxis {
      Name = 'T',
      Parent = 'Base',
      --Token = '**',
      Type = MCH_AT.LINEAR,
      Dir = - X_AX(),
      Pos = Point3d(0.0,2450.0,-958.0),
      Stroke = {-30000, 30000},
      Home = LoadT,
      Geo = 'T_AXIS/GEO'}
EmtTable {
      Name = 'Tab',
      Parent = 'T',
      Type = MCH_TT.FLAT,
      Ref1 = Point3d( - DimTabX, DeltaTabY - DimTabY, DeltaTabZ),
      Geo = 'TABLE/GEO',
      Aux = 'TABLE/SOLID'}
-- *** ToolChanger ***
local ptTc1 = Point3d( 150, 449, -326 + ( DeltaTabZ - DeltaRulliTraveZ + 55))
local vtDt1 = Vector3d( 0, 0, - 102.5)
EmtTcPos {
      Name = 'T1',
      Parent = 'Base',
      Pos = ptTc1,
      TDir = Y_AX(),
      ADir = -X_AX(),
      Geo = 'BASE/T1'}
EmtTcPos {
      Name = 'T2',
      Parent = 'Base',
      Pos = ptTc1 + vtDt1 + EgtIf( FirstSaw, Vector3d( SawOffsX, SawOffsZ, 0), V_NULL()),
      TDir = Y_AX(),
      ADir = -X_AX(),
      Geo = 'BASE/T2'}
EmtTcPos {
      Name = 'T3',
      Parent = 'Base',
      Pos = ptTc1 + 2 * vtDt1,
      TDir = Y_AX(),
      ADir = -X_AX(),
      Geo = 'BASE/T3'}
EmtTcPos {
      Name = 'T4',
      Parent = 'Base',
      Pos = ptTc1 + 3 * vtDt1,
      TDir = Y_AX(),
      ADir = -X_AX(),
      Geo = 'BASE/T4'}
EmtTcPos {
      Name = 'T5',
      Parent = 'Base',
      Pos = ptTc1 + 4 * vtDt1,
      TDir = Y_AX(),
      ADir = -X_AX(),
      Geo = 'BASE/T5'}
EmtTcPos {
      Name = 'T6',
      Parent = 'Base',
      Pos = ptTc1 + 5 * vtDt1,
      TDir = Y_AX(),
      ADir = -X_AX(),
      Geo = 'BASE/T6'}
EmtTcPos {
      Name = 'T7',
      Parent = 'Base',
      Pos = ptTc1 + 6 * vtDt1,
      TDir = Y_AX(),
      ADir = -X_AX(),
      Geo = 'BASE/T7'}
EmtTcPos {
      Name = 'T8',
      Parent = 'Base',
      Pos = ptTc1 + 7 * vtDt1,
      TDir = Y_AX(),
      ADir = -X_AX(),
      Geo = 'BASE/T8'}
EmtTcPos {
      Name = 'T9',
      Parent = 'Base',
      Pos = ptTc1 + 8 * vtDt1,
      TDir = Y_AX(),
      ADir = -X_AX(),
      Geo = 'BASE/T9'}
EmtTcPos {
      Name = 'T10',
      Parent = 'Base',
      Pos = ptTc1 + 9 * vtDt1,
      TDir = Y_AX(),
      ADir = -X_AX(),
      Geo = 'BASE/T10'}
if Tc2Active then
   local ptTc2 = Point3d( 150, 4717, -118.5 + ( DeltaTabZ - DeltaRulliTraveZ + 55))
   local vtDt2 = Vector3d( 0, 0, - 102.5)
   EmtTcPos {
         Name = 'T11',
         Parent = 'Base',
         Pos = ptTc2,
         TDir = -Y_AX(),
         ADir = -X_AX(),
         Geo = 'BASE/T11'} 
   EmtTcPos {
         Name = 'T12',
         Parent = 'Base',
         Pos = ptTc2 + 1 * vtDt2 + EgtIf( SecondSaw, Vector3d( -Saw2OffsX, -Saw2OffsZ, 0), V_NULL()),
         TDir = -Y_AX(),
         ADir = -X_AX(),
         Geo = 'BASE/T12'}
   EmtTcPos {
         Name = 'T13',
         Parent = 'Base',
         Pos = ptTc2 + 2 * vtDt2,
         TDir = -Y_AX(),
         ADir = X_AX(),
         Geo = 'BASE/T13'}
   EmtTcPos {
         Name = 'T14',
         Parent = 'Base',
         Pos = ptTc2 + 3 * vtDt2,
         TDir = -Y_AX(),
         ADir = X_AX(),
         Geo = 'BASE/T14'}
   EmtTcPos {
         Name = 'T15',
         Parent = 'Base',
         Pos = ptTc2 + 4 * vtDt2,
         TDir = -Y_AX(),
         ADir = X_AX(),
         Geo = 'BASE/T15'}
end
local ptTc3 = Point3d( 480, 1129, -200.8 + ( DeltaTabZ - DeltaRulliTraveZ + 55))
EmtTcPos {
      Name = 'T101',
      Parent = 'Base',
      Pos = ptTc3,
      TDir = -X_AX(),
      ADir = Z_AX(),
      Geo = 'BASE/T101'}
local ptTcR = Point3d( -0.5, 5115, -1571.2 + ( DeltaTabZ - DeltaRulliTraveZ + 55))
EmtTcPos {
      Name = 'T20',
      Parent = 'Base',
      Pos = ptTcR + VectorFromSpherical( 324.3, -30, 0),
      TDir = -Y_AX(),
      ADir = X_AX(),
      Geo = 'BASE/T20'}
EmtTcPos {
      Name = 'T21',
      Parent = 'Base',
      Pos = ptTcR + VectorFromSpherical( 324.3, -60, 0),
      TDir = -Y_AX(),
      ADir = X_AX(),
      Geo = 'BASE/T21'}
EmtTcPos {
      Name = 'T22',
      Parent = 'Base',
      Pos = ptTcR + VectorFromSpherical( 324.3, -90, 0),
      TDir = -Y_AX(),
      ADir = X_AX(),
      Geo = 'BASE/T22'}
EmtTcPos {
      Name = 'T23',
      Parent = 'Base',
      Pos = ptTcR + VectorFromSpherical( 324.3, -120, 0),
      TDir = -Y_AX(),
      ADir = X_AX(),
      Geo = 'BASE/T23'}
EmtTcPos {
      Name = 'T24',
      Parent = 'Base',
      Pos = ptTcR + VectorFromSpherical( 324.3, -150, 0),
      TDir = -Y_AX(),
      ADir = X_AX(),
      Geo = 'BASE/T24'}
EmtTcPos {
      Name = 'T25',
      Parent = 'Base',
      Pos = ptTcR + VectorFromSpherical( 324.3, -180, 0),
      TDir = -Y_AX(),
      ADir = X_AX(),
      Geo = 'BASE/T25'}
EmtTcPos {
      Name = 'T26',
      Parent = 'Base',
      Pos = ptTcR + VectorFromSpherical( 324.3, -210, 0),
      TDir = -Y_AX(),
      ADir = X_AX(),
      Geo = 'BASE/T26'}
EmtTcPos {
      Name = 'T27',
      Parent = 'Base',
      Pos = ptTcR + VectorFromSpherical( 324.3, -240, 0),
      TDir = -Y_AX(),
      ADir = X_AX(),
      Geo = 'BASE/T27'}
EmtTcPos {
      Name = 'T28',
      Parent = 'Base',
      Pos = ptTcR + VectorFromSpherical( 324.3, -270, 0),
      TDir = -Y_AX(),
      ADir = X_AX(),
      Geo = 'BASE/T28'}
EmtTcPos {
      Name = 'T29',
      Parent = 'Base',
      Pos = ptTcR + VectorFromSpherical( 324.3, -300, 0),
      TDir = -Y_AX(),
      ADir = X_AX(),
      Geo = 'BASE/T29'}
EmtTcPos {
      Name = 'T30',
      Parent = 'Base',
      Pos = ptTcR + VectorFromSpherical( 324.3, -330, 0),
      TDir = -Y_AX(),
      ADir = X_AX(),
      Geo = 'BASE/T30'}

-- Aggiusto posizioni geometriche
local vtMovB = Vector3d( 0, 0, ( DeltaTabZ - DeltaRulliTraveZ + 55))
EgtMove( EgtGetFirstNameInGroup( BaseId, 'SOLID'), vtMovB, GDB_RT.GLOB)
EgtMove( EgtGetFirstNameInGroup( BaseId, 'COLLISION'), vtMovB, GDB_RT.GLOB)
EgtMove( EgtGetFirstNameInGroup( BaseId, 'SIGN') or GDB_ID.NULL, vtMovB, GDB_RT.GLOB)
EgtMove( EgtGetFirstNameInGroup( BaseId, 'TC1') or GDB_ID.NULL, vtMovB, GDB_RT.GLOB)
EgtMove( EgtGetFirstNameInGroup( BaseId, 'TC2') or GDB_ID.NULL, vtMovB, GDB_RT.GLOB)
EgtMove( EgtGetFirstNameInGroup( BaseId, 'TC3') or GDB_ID.NULL, vtMovB, GDB_RT.GLOB)
EgtMove( EgtGetFirstNameInGroup( BaseId, 'TCR') or GDB_ID.NULL, vtMovB, GDB_RT.GLOB)
EgtMove( EgtGetFirstNameInGroup( X1Id, 'SOLID'), vtMovB, GDB_RT.GLOB)
EgtMove( EgtGetFirstNameInGroup( X1Id, 'COLLISION'), vtMovB, GDB_RT.GLOB)
EgtMove( EgtGetFirstNameInGroup( Z1Id, 'SOLID'), vtOffset23KW, GDB_RT.GLOB)
EgtMove( EgtGetFirstNameInGroup( Z1Id, 'COLLISION'), vtOffset23KW, GDB_RT.GLOB)
EgtMove( EgtGetFirstNameInGroup( X3Id, 'SOLID'), vtMovB, GDB_RT.GLOB)
EgtMove( EgtGetFirstNameInGroup( X3Id, 'COLLISION'), vtMovB, GDB_RT.GLOB)
local vtMove = Vector3d( 0, ( DeltaTabY - 3050.0), ( DeltaTabZ + 1124.0))
EgtMove( EgtGetFirstNameInGroup( BaseId, 'CONVOYER'..sGeomConvoyer), vtMove, GDB_RT.GLOB)
EgtMove( EgtGetFirstNameInGroup( Y1Id, 'SOLID'), vtMove, GDB_RT.GLOB)
EgtMove( EgtGetFirstNameInGroup( PY1Id, 'SOLID'), vtMove, GDB_RT.GLOB)
EgtMove( EgtGetFirstNameInGroup( PY1Id, 'CLAMP_CHECK'), vtMove + Vector3d(0,0,DistZClampToTable), GDB_RT.GLOB)
EgtMove( EgtGetFirstNameInGroup( Y2Id, 'SOLID'), vtMove, GDB_RT.GLOB)
EgtMove( EgtGetFirstNameInGroup( PY2Id, 'SOLID'), vtMove, GDB_RT.GLOB)
EgtMove( EgtGetFirstNameInGroup( PY2Id, 'CLAMP_CHECK'), vtMove + Vector3d(0,0,DistZClampToTable), GDB_RT.GLOB)
EgtMove( EgtGetFirstNameInGroup( V1Id, 'SOLID'), vtMove, GDB_RT.GLOB)
EgtMove( EgtGetFirstNameInGroup( V1Id, 'COLLISION'), vtMove, GDB_RT.GLOB)
EgtMove( EgtGetFirstNameInGroup( PV1Id, 'SOLID'), vtMove, GDB_RT.GLOB)
EgtMove( EgtGetFirstNameInGroup( QV1Id, 'SOLID'), vtMove, GDB_RT.GLOB)
EgtMove( EgtGetFirstNameInGroup( V2Id, 'SOLID'), vtMove, GDB_RT.GLOB)
EgtMove( EgtGetFirstNameInGroup( V2Id, 'COLLISION'), vtMove, GDB_RT.GLOB)
EgtMove( EgtGetFirstNameInGroup( PV2Id, 'SOLID'), vtMove, GDB_RT.GLOB)
EgtMove( EgtGetFirstNameInGroup( QV2Id, 'SOLID'), vtMove, GDB_RT.GLOB)
local vtMoveColl = Vector3d( 0, 0, MaxZ1Blade)
EgtMove( EgtGetFirstNameInGroup( EgtGetFirstNameInGroup( BaseId, 'COLLISION'), 'CEIL'), vtMoveColl, GDB_RT.GLOB)

-- Assegno identificativi alle spie delle morse
PY1LightId = EgtGetFirstNameInGroup( EgtGetFirstNameInGroup( Y1Id, 'SOLID') or GDB_ID.NULL, 'Light')
PY2LightId = EgtGetFirstNameInGroup( EgtGetFirstNameInGroup( Y2Id, 'SOLID') or GDB_ID.NULL, 'Light')

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

---------------------------------------------------------------------
function OnSetHead()
   -- se testa H11 (fresa)
   if EMC.HEAD == 'H11' then
      local nTc = GetTcForTopHeadTool( EMC.TCPOS)
      if nTc ~= 2 then
         EmtModifyAxisHome( 'X1', ParkX1)
      else
         EmtModifyAxisHome( 'X1', ParkTc2X1)
      end
      EmtModifyAxisStroke( 'Z1', {MinZ1, MaxZ1})
      if EMC.TOTLEN < LongTool then
         EmtModifyAxisHome( 'Z1', ParkZ1)
         EmtModifyAxisStroke( 'C1', {MinC1, MaxC1})
         EmtModifyAxisHome( 'C1', ParkC1)
         EmtModifyAxisHome( 'B1', ParkB1)
      else
         EmtModifyAxisHome( 'Z1', MaxZ1)
         if nTc ~= 2 then
            EmtModifyAxisStroke( 'C1', { ParkLongTc1C1 - 20, ParkLongTc1C1 + 20})
            EmtModifyAxisHome( 'C1', ParkLongTc1C1)
         else
            EmtModifyAxisStroke( 'C1', { ParkLongTc2C1 - 20, ParkLongTc2C1 + 20})
            EmtModifyAxisHome( 'C1', ParkLongTc2C1)
         end
         EmtModifyAxisHome( 'B1', ParkLongB1)
      end
      EmtModifyAxisDirection( 'B1', X_AX())
   -- se testa H12 (lama)
   elseif EMC.HEAD == 'H12' then
      EmtModifyAxisHome( 'X1', ParkX1)
      EmtModifyAxisHome( 'Z1', ParkZ1)
      EmtModifyAxisStroke( 'Z1', {MinZ1, MaxZ1Blade})
      EmtModifyAxisStroke( 'C1', {MinC1, MaxC1})
      EmtModifyAxisHome( 'C1', ParkC1)
      EmtModifyAxisHome( 'B1', ParkB1)
      EmtModifyAxisDirection( 'B1', Vector3d( cos( SawC1Offs), -sin( SawC1Offs), 0))
   -- se testa H13 (sega a catena)
   elseif EMC.HEAD == 'H13' then
      -- aggiustamenti per distanza
      local dDist = EgtIf( EMC.DIST and abs( EMC.DIST) > 1, EMC.DIST, ChSawLen)
      EMC.EXIT = EMC.EXIT or 1
      EmtModifyExitPosition( EMC.HEAD, EMC.EXIT, Point3d( 0, 0, -dDist))
      local vtMove = Point3d( 0, 0, -dDist) - Point3d( EMC.EXITPOS)
      local ExitId = EgtGetFirstNameInGroup( EgtGetHeadId( EMC.HEAD), 'T'..tostring( EMC.EXIT))
      EgtMove( ExitId, vtMove)
      -- recupero il valore dell'asse virtuale bloccato A
      local CSawPosA = GetCurrChainSawingVirtualAxis()
      EmtModifyAxisHome( 'X1', ParkCSawX1)
      EmtModifyAxisHome( 'Z1', GetChainSawZHomeFromVirtualAxis( CSawPosA, EMC.TOTLEN))
      EmtModifyAxisStroke( 'Z1', {MinZ1, MaxZ1})
      EmtModifyAxisStroke( 'C1', {MinC1, MaxC1})
      EmtModifyAxisHome( 'C1', GetChainSawCHomeFromVirtualAxis( CSawPosA, EMC.TOTLEN))
      EmtModifyAxisHome( 'B1', EgtIf( EMC.TOTLEN < MinLengthLongCSaw, ParkCSawB1, ParkLongCSawB1))
      EmtModifyAxisDirection( 'B1', X_AX())
   -- se testa H16 ( seconda lama)
   elseif EMC.HEAD == 'H16' then
      EmtModifyAxisHome( 'X1', ParkTc2X1)
      EmtModifyAxisHome( 'Z1', ParkZ1)
      EmtModifyAxisStroke( 'Z1', {MinZ1, MaxZ1Blade})
      EmtModifyAxisStroke( 'C1', {MinC1, MaxC1})
      EmtModifyAxisHome( 'C1', ParkC1)
      EmtModifyAxisHome( 'B1', ParkB1)
      EmtModifyAxisDirection( 'B1', Vector3d( cos( Saw2C1Offs), sin( Saw2C1Offs), 0))
   elseif EMC.HEAD == 'H21' then
      if not EMC.TOTDIAM or EMC.TOTDIAM > 300 then
         EmtModifyAxisStroke( 'B2', {MinB2b, MaxB2b})
      else
         EmtModifyAxisStroke( 'B2', {MinB2, MaxB2})
      end
   end
end

---------------------------------------------------------------------
-- Funzione che riconosce TC per utensili testa sopra
function GetTcForTopHeadTool( sTcPos)
   if sTcPos == 'T1' or sTcPos == 'T3' or sTcPos == 'T4' or sTcPos == 'T5' or
      sTcPos == 'T6' or sTcPos == 'T7' or sTcPos == 'T8' or sTcPos == 'T9' or sTcPos == 'T10' then
      return 1
   elseif sTcPos == 'T11' or sTcPos == 'T12' or sTcPos == 'T13' or sTcPos == 'T14' or sTcPos == 'T15' then
      return 2
   else
      return 0
   end
end

---------------------------------------------------------------------
function GetHeadSet( sHead)
   if sHead == 'H11' or sHead == 'H12' or sHead == 'H13' or sHead == 'H16' then
      return 1
   elseif sHead == 'H21' or sHead == 'H22' then
      return 2
   elseif sHead == 'H39' or sHead == 'H38' then
      return 3
   else
      return 0
   end
end
   
---------------------------------------------------------------------
function GetHeadSetFromTcPos( sTcPos)
   local TCPOS_1A = { 'T1', 'T3', 'T4', 'T5', 'T6', 'T7', 'T8', 'T9', 'T10'}                           -- CU montante DX
   local TCPOS_1B = { 'T101'}                                                                          -- CU sotto traversa
   local TCPOS_1C = { 'T11', 'T12', 'T13', 'T14', 'T15'}                                               -- CU montante sinistro
   local TCPOS_2A  = { 'T20', 'T21', 'T22', 'T23', 'T24', 'T25', 'T26', 'T27', 'T28', 'T29', 'T30'}    -- CU margherita testa 2
   local TCPOS_2B  = {}                                                                                -- CU lama su aggregato testa 2
   local TCPOS_3  = { 'T301'}                                                                          -- CU testa 3
   for _, sVal in ipairs( TCPOS_1A) do
         if sVal == sTcPos then return 1, 1 end
   end
   for _, sVal in ipairs( TCPOS_1B) do
         if sVal == sTcPos then return 1, 2 end
   end
   for _, sVal in ipairs( TCPOS_1C) do
         if sVal == sTcPos then return 1, 3 end
   end
   for _, sVal in ipairs( TCPOS_2A) do
         if sVal == sTcPos then return 2, 1 end
   end
   for _, sVal in ipairs( TCPOS_2B) do
         if sVal == sTcPos then return 2, 2 end
   end
   for _, sVal in ipairs( TCPOS_3) do
      if sVal == sTcPos then return 3, 1 end
   end
   return 0, 0
end
   
---------------------------------------------------------------------
-- Funzione che ritorna bool per dire se esiste o meno al testa richiesta
function IsHeadExisting( nHeadSet)
   if nHeadSet == 1 then
      return H11Id ~= nil
   elseif nHeadSet == 2 then
      return H21Id ~= nil
   elseif nHeadSet == 3 then
      return H31Id ~= nil
   else
      return ''
   end
end

---------------------------------------------------------------------
-- Funzione che ritorna il nome della testa in base alla posizione utensile (ATT: deve rispecchiare il setup)
function GetAdjHeadFromTcPos( nHeadSet, sTcPos)
   if nHeadSet == 1 then
      -- seconda lama
      if SecondSaw and sTcPos == 'T12' then
         return 'H16'
      -- motoseghe
      elseif sTcPos == 'T101' then
         return 'H13'
      -- utensili standard o prima lama
      else
         return EgtIf( sTcPos == 'T1', 'H12', 'H11')
      end
   elseif nHeadSet == 2 then

      return 'H21'

   elseif nHeadSet == 3 then
      return 'H38'
   else
      return ''
   end
end
---------------------------------------------------------------------
-- Funzione che riconosce testa per sega a catena
function HeadIsChainSaw( sHead)
   return ( sHead == 'H13' or sHead == 'H15')
end

---------------------------------------------------------------------
function GetCurrChainSawingVirtualAxis()
   -- recupero il valore dell'asse virtuale bloccato A
   local sVal = EgtGetMachiningParam( MCH_MP.BLOCKEDAXIS)
   if not sVal or sVal == '' then
      if EMC then
         sVal = EMC.BLOCKEDAXIS or 'A1=0'
      else
         sVal = 'A1=0'
      end
   end
   local dPosA = tonumber( sVal:sub( 4)) or 0
   return dPosA
end

---------------------------------------------------------------------
-- Funzione che calcola la posizione Home di Z per la sega a catena dal valore dell'asse virtuale
function GetChainSawZHomeFromVirtualAxis( dPosA, dCSawLength)
   -- se A=0 -> T101 -> HomeZ = -400
   if abs( dPosA) < 0.1 then
      if dCSawLength >= MinLengthLongCSaw then
         EmtSetLastError( 1200, 'ChainSaw too long: orientation forbidden')
      end
      return ParkCSaw0Z1
   -- altrimenti A=-90 -> T102 o A=90 -> T104 -> HomeZ = -100
   else
      if dCSawLength >= MinLengthLongCSaw then
         return ParkLongCSawZ1
      else
         return ParkCSawZ1
      end
   end
end

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

---------------------------------------------------------------------
-- Funzione che calcola l'angolo Home di C per la lama su aggregato dal valore dell'asse virtuale
function GetSawCHomeFromVirtualAxis( dPosA)
      -- se A2=0 -> 201 -> HomeC = -90
      if abs( dPosA) < 0.1 then
         return ParkSaw0C2
      -- altrimenti A2=90 -> T204 -> HomeC = -90
      else
         return ParkSawC2
      end
   end

---------------------------------------------------------------------
-- Funzione che ritorna ZExtra partendo dalla testa e dall'angolo verticale
function GetZExtra( sHead, dAngV)
   -- se testa standard
   if sHead == 'H11' then
      if abs( dAngV) < 30.1 then
         return 400
      elseif abs( dAngV) < 45.1 then
         return 300
      elseif abs( dAngV) < 60.1 then
         return 200
      elseif abs( dAngV) < 90.1 then
         return 10
      else
         return 0
      end
   -- se aggregato lama
   elseif sHead == 'H12' or sHead == 'H16' then
      if abs( dAngV) < 30.1 then
         return 400
      elseif abs( dAngV) < 45.1 then
         return 300
      elseif abs( dAngV) < 60.1 then
         return 200
      elseif abs( dAngV) < 90.1 then
         return 10
      else
         return 0
      end
   end
end

---------------------------------------------------------------------
-- Funzione per impostare spia stato morsa carrello Y1
function SetPY1Light( bClosed)
   if not PY1LightId then return end
   EgtSetColor( PY1LightId, EgtIf( bClosed, 'RED', 'LIME')) 
   if bClosed then
      EgtSetInfo( PY1LightId, 'On', '1')
   else
      EgtRemoveInfo( PY1LightId, 'On')
   end
end

---------------------------------------------------------------------
-- Funzione per leggere lo stato della morsa carrello Y1
function GetPY1Light()
   if not PY1LightId then return false end
   return ( EgtGetInfo( PY1LightId, 'On') == '1')
end

---------------------------------------------------------------------
-- Funzione per impostare spia stato morsa carrello Y2
function SetPY2Light( bClosed)
   if not PY2LightId then return end
   EgtSetColor( PY2LightId, EgtIf( bClosed, 'RED', 'LIME')) 
   if bClosed then
      EgtSetInfo( PY2LightId, 'On', '1')
   else
      EgtRemoveInfo( PY2LightId, 'On')
   end
end

---------------------------------------------------------------------
-- Funzione per leggere lo stato della morsa carrello Y2
function GetPY2Light()
   if not PY2LightId then return false end
   return ( EgtGetInfo( PY2LightId, 'On') == '1')
end

---------------------------------------------------------------------
-- Funzione per impostare la posizione del rinvio flottante
function SetFloatPos( sHead, dPos)
   -- recupero Id testa
   local nHeadId = EgtGetHeadId( sHead)
   if not nHeadId then return false end
   -- se ci sono entità flottanti nel ToolHolder
   local nRefId = EgtGetFirstNameInGroup( nHeadId, '_T1')
   local vtTool = EgtFR( nRefId, GDB_ID.ROOT):getVersZ() 
   local nObjId =  EgtGetFirstGroupInGroup( EgtGetFirstNameInGroup( nHeadId, 'T1'))
   local FloatEntities = EgtGetInfoInGroup( nObjId, 'Float')
   if FloatEntities and #FloatEntities > 0 then
      -- il porta-utensile flottante non può comprimersi più di 10mm
      if dPos > 10 then
         local sErr = 'Floating Ring compressed more than 10mm'
         EmtSetLastError( 1222, sErr)
         EgtOutLog( 'Error : ' .. sErr, 1)
      end
      local dFloat = EgtGetInfo( FloatEntities[1], 'Float', 'd')
      if abs( dPos - dFloat) > 10 * GEO.EPS_SMALL then
         local vtMove = ( dPos - dFloat) * vtTool
         -- sposto tutte le entità flottanti
         for i = 1, #FloatEntities do
            EgtMove( FloatEntities[i], vtMove, GDB_RT.GLOB)
            EgtSetInfo( FloatEntities[i], 'Float', dPos)
         end
      end
      return true
   end
   return false
end

---------------------------------------------------------------------
-- Funzione per resettare tutte le attivazioni della macchina
function OnResetMachine()
   EmtUnlinkAllRawPartsFromGroups()
   EmtUnlinkAllFixturesFromGroups()
   SetPY1Light( false)
   SetPY2Light( false)
   --resetto eventuale flottante
   SetFloatPos( 'H11', 0)
   SetFloatPos( 'H21', 0)
   -- 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
