-- NewTrimming.lua by Egalware s.r.l. 2026/01/05 require( 'EgtBase') _ENV = EgtProtectGlobal() EgtEnableDebug( false) -- Carico le costanti di Trimming EgtAddToPackagePath( EgtGetSourceDir() .. '?.lua') local GlobVar = require( 'TrimmingLib') -- Costante di Errore local ERROR_CREATING_NEW_TRIMMING = 'Error in Creating New Trimming : ' -- Recupero il Part corrente local nCurrPartId = EgtGetCurrPart() if nCurrPartId == nil then EgtOutBox( ERROR_CREATING_NEW_TRIMMING .. 'Current Part not Found', 'Error', 'ERROR', 'OK') return end -- apro la finestra Params local fParams = loadfile( EgtGetSourceDir()..'Params.lua') local nCurrLayerId = fParams( nCurrPartId, nil) if not nCurrLayerId then return end -- apro la finestra di Selezione, dopo aver letto le modalità selezionate local nSurfSelectionType = EgtGetInfo( nCurrLayerId, MCH_TRIMMING.KEY_SELECTION_LAST_SELECTION_TYPE, 'i') local sTitle = '' local bUseSubId if nSurfSelectionType == SURF_SEL_LABEL['Single Surface'] then sTitle = "Select Faces on the single surface" bUseSubId = true elseif nSurfSelectionType == SURF_SEL_LABEL['Composite Surface'] then sTitle = "Select Surfaces" bUseSubId = false end local bWithPreviewFunc = true local tbSelSurfs, tbSelPoints = EgtSelDialog( sTitle, bUseSubId, bWithPreviewFunc, 'PreviewFunc') -- deseleziono le superfici indicate EgtDeselectAll() if not tbSelSurfs then return end if #tbSelSurfs == 0 then EgtOutBox( ERROR_CREATING_NEW_TRIMMING .. 'No surface selected', 'Error', 'ERROR', 'OK') return end if nSurfSelectionType == -1 or nSurfSelectionType > 2 then EgtOutBox( ERROR_CREATING_NEW_TRIMMING .. 'Invalid Surf Selection Mode', 'Error', 'ERROR', 'OK') return end -- recupero le costanti settate local dLinTol = EgtGetInfo( nCurrLayerId, MCH_TRIMMING.KEY_LIN_TOL, 'd') local dAngTol = EgtGetInfo( nCurrLayerId, MCH_TRIMMING.KEY_ANG_TOL, 'd') local dAngFaceTol = EgtGetInfo( nCurrLayerId, MCH_TRIMMING.KEY_SURF_ANG_TOL, 'd') local dSize = EgtGetInfo( nCurrLayerId, MCH_TRIMMING.KEY_SURF_BORDER_SIZE, 'd') local dSizeTol = EgtGetInfo( nCurrLayerId, MCH_TRIMMING.KEY_SURF_BORDER_SIZE_TOL, 'd') if not dLinTol or not dAngTol or not dAngFaceTol or not dSize or not dSizeTol then EgtOutBox( ERROR_SELECTION .. 'Invalid parameters set', 'Error', 'ERROR', 'OK') return end -- modalità selezione triangoli su una trimesh if nSurfSelectionType == SURF_SEL_LABEL['Single Surface'] then -- splitto id principale dai subId tbSelSurfs = EgtSplitString( tbSelSurfs[1], ':') local nMainId = tonumber(tbSelSurfs[1]) local nType = EgtGetType( nMainId) if (nType ~= GDB_TY.SRF_BEZ and nType ~= GDB_TY.SRF_MESH) or #tbSelPoints == 0 then EgtOutBox( ERROR_SELECTION .. 'Invalid Surface Selected. The Surface must be a TriMesh or a Bezier', 'Error', 'ERROR', 'OK') return end -- tengo solo la lista dei subId local tbSubIdSel = EgtSplitString( tbSelSurfs[2], ',') local bOk = false for i, nSelObjId in ipairs( tbSubIdSel) do nSelObjId = tonumber( nSelObjId) -- Se la superficie è una Bezier, allora la inserisco direttamente come Preview if nType == GDB_TY.SRF_BEZ and i == 0 then local nInsertSurfId = EgtCopyGlob( nMainId, nAuxLay) if not nInsertSurfId or nInsertSurfId == GDB_ID.NULL then EgtOutBox( ERROR_SELECTION .. 'Copying Surface failed', 'Error', 'ERROR', 'OK') return end -- Assegno Colore, Info e Name bOk = EgtSetColor( nInsertSurfId, SELECTION_SURF_COLOR) and EgtSetInfo( nInsertSurfId, MCH_TRIMMING.KEY_SELECTION_REF_SURF, nSelObjId) and EgtSetName( nInsertSurfId, MCH_TRIMMING.SELECTION_SURF_NAME) and EgtSetInfo( nCurrLayerId, MCH_TRIMMING.KEY_SELECTION_LAST_IDS, { nInsertSurfId}) -- Se invece è una Trimesh, recupero il primo triangolo selezionato else if not nMainId or not nSelObjId then EgtOutBox( ERROR_SELECTION .. 'Face not found', 'Error', 'ERROR', 'OK') return end -- Recupero la superficie mediante adiancenza dei triangoli local nInsertSurfId = EgtTrimmingGetSurfTmFaceAdj( nAuxLay, nMainId, nSelObjId, tbSelPoints[i], dAngFaceTol, dSize, dSizeTol) if not nInsertSurfId or nInsertSurfId == GDB_ID.NULL then EgtOutBox( ERROR_SELECTION .. 'Face Search failed', 'Error', 'ERROR', 'OK') return end -- Assegno Colore, Info e Name bOk = EgtSetColor( nInsertSurfId, SELECTION_SURF_COLOR) and EgtSetInfo( nInsertSurfId, MCH_TRIMMING.KEY_SELECTION_REF_SURF, nId) and EgtSetName( nInsertSurfId, MCH_TRIMMING.SELECTION_SURF_NAME) and EgtSetInfo( nCurrLayerId, MCH_TRIMMING.KEY_SELECTION_LAST_IDS, { nInsertSurfId}) end if not bOk then EgtOutBox( ERROR_SELECTION .. 'Copying Surface failed', 'Error', 'ERROR', 'OK') return end end --------------------------------- Ricerca delle superfici Adiacenti --------------------------------- -- [Tolleranza lineare, Tolleranza angolare e Tolleranza angolare applicata alla Superficie] elseif nSurfSelectionType == SURF_SEL_LABEL['Composite Surface'] then local tbSubIdSel = EgtSplitString( tbSelSurfs[1], ',') for i, nSelObjId in ipairs( tbSubIdSel) do nSelObjId = tonumber( nSelObjId) -- Recupero il Gruppo della superficie corrente local nSurfGroup = EgtGetParent( nSelObjId) if not nSurfGroup or nSurfGroup == GDB_ID.NULL then EgtOutBox( ERROR_SELECTION .. 'Surface not in a valid Group', 'Error', 'ERROR', 'OK') return end -- Recupero tutti Gli Id delle Superfici Attive del Gruppo della Supercicie Corrente local vAllSurfIds = {} local nOtherId = EgtGetFirstInGroup( nSurfGroup) while nOtherId do local nStatus = EgtGetStatus( nOtherId) if nStatus == GDB_ST.ON and nOtherId ~= nId then local nType = EgtGetType( nOtherId) if nType == GDB_TY.SRF_BEZ or nType == GDB_TY.SRF_MESH then table.insert( vAllSurfIds, nOtherId) end end nOtherId = EgtGetNext( nOtherId) end -- Recupero le superfici da Selezionare local bOk, vSelSurfId = EgtTrimmingGetAdjSurfs( nSelObjId, vAllSurfIds, dLinTol, dAngTol, dAngFaceTol) if not bOk then EgtOutBox( ERROR_SELECTION .. 'Surface Search failed', 'Error', 'ERROR', 'OK') return end table.insert( vSelSurfId, nSelObjId) -- Dalle superfici già presenti nel Layer di selezione, rimuovo gli Id dei riferimenti ( per non duplicare superfici) local vOldSelSurfId = {} local nId = EgtGetFirstNameInGroup( nCurrLayerId, MCH_TRIMMING.SELECTION_SURF_NAME) while nId do local nObjType = EgtGetType( nId) if nObjType == GDB_TY.SRF_MESH or nObjType == GDB_TY.SRF_BEZ then -- Se la superficie selezionata precedentemente ha come riferimento una superficie, ne memorizzo l'Id local nRefSurfId = EgtGetInfo( nId, MCH_TRIMMING.KEY_SELECTION_REF_SURF, 'i') if nRefSurfId then table.insert( vOldSelSurfId, nRefSurfId) end end nId = EgtGetNextName( nId, MCH_TRIMMING.SELECTION_SURF_NAME) end local setToRemove = {} for _, v in ipairs( vOldSelSurfId) do setToRemove[v] = true end local vSurfIds = {} for _, v in ipairs( vSelSurfId) do if not setToRemove[v] then table.insert( vSurfIds, v) end end -- Aggiungo le Superfici come Preview, assegnando Colore, Info e Name local vNewInsertedSrfIds = {} for nSurf = 1, #vSurfIds do local nInsertSurfId = EgtCopyGlob( vSurfIds[nSurf], nCurrLayerId) if not nInsertSurfId or nInsertSurfId == GDB_ID.NULL then EgtOutBox( ERROR_SELECTION .. 'Creating Selected Surf Preview failed', 'Error', 'ERROR', 'OK') return end table.insert( vNewInsertedSrfIds, nInsertSurfId) bOk = EgtSetColor( nInsertSurfId, SELECTION_SURF_COLOR) and EgtSetInfo( nInsertSurfId, MCH_TRIMMING.KEY_SELECTION_REF_SURF, vSurfIds[nSurf]) and EgtSetName( nInsertSurfId, MCH_TRIMMING.SELECTION_SURF_NAME) if not bOk then EgtOutBox( ERROR_SELECTION .. 'Copying Surface failed', 'Error', 'ERROR', 'OK') return end end EgtSetInfo( nCurrLayerId, MCH_TRIMMING.KEY_SELECTION_LAST_IDS, vNewInsertedSrfIds) end end -- aggiorno la grafica EgtDraw()