-- Select.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_SELECTION = 'Error in Surf Selection : ' -- Recupero il Part e Layer di Trimming corrente local nCurrPartId = EgtGetCurrPart() local nCurrLayerId = EgtGetCurrLayer() if not GlobVar.IsTrimmingLayer( nCurrLayerId) then EgtOutBox( ERROR_SELECTION .. 'Not a valid Trimming Layer', 'Error', 'ERROR', 'OK') return end -- Recupero l'oggetto selezionato ( verifico che sia Superficie TriMesh o Superficie Bezier) local nSelObjId = EgtGetLastSelectedObj() if not nSelObjId then EgtOutBox( ERROR_SELECTION .. 'No Object Selected', 'Error', 'ERROR', 'OK') return end local nType = EgtGetType( nSelObjId) if nType ~= GDB_TY.SRF_BEZ and nType ~= GDB_TY.SRF_MESH then EgtOutBox( ERROR_SELECTION .. 'Invalid Surface Selected. The Surface must be a TriMesh or a Bezier', 'Error', 'ERROR', 'OK') return end -- Chiedo l'operazione da svolgere su tale superficie e ricavo i parametri inseriti local dSize = 5.0 local dStoredSize = EgtGetInfo( nCurrLayerId, KEY_SURF_BORDER_SIZE, 'd') if dStoredSize ~= nil then dSize = dStoredSize end local dSizeTol = 0.75 local dStoredSizeTol = EgtGetInfo( nCurrLayerId, KEY_SURF_BORDER_SIZE_TOL, 'd') if dStoredSizeTol ~= nil then dSizeTol = dStoredSizeTol end local nLastSelType = EgtGetInfo( nCurrLayerId, KEY_SELECTION_LAST_SELECTION_TYPE, 'i') local sLabels = { 'Insert Surface', 'Face Search', 'Surf Search'} local sTypeDBSel if nLastSelType ~= nil then local sParts = {} for i, sLabel in ipairs( sLabels) do if i == nLastSelType then sParts[i] = '*' .. sLabel else sParts[i] = sLabel end end sTypeDBSel = 'CB:' .. table.concat( sParts, ',') else sTypeDBSel = 'CB:' .. table.concat( sLabels, ',') end local vsVal = EgtDialogBox( 'Seletion', { 'Type', sTypeDBSel}, { 'Size', tostring( dSize)}, { 'Size Tolerance', tostring( dSizeTol)}) if not vsVal or #vsVal ~= 3 then return end dSize = tonumber( vsVal[2]) dSizeTol = tonumber( vsVal[3]) EgtSetInfo( nCurrLayerId, KEY_SURF_BORDER_SIZE, dSize) EgtSetInfo( nCurrLayerId, KEY_SURF_BORDER_SIZE_TOL, dSizeTol) for i, sLabel in ipairs( sLabels) do if sLabel == vsVal[1] then EgtSetInfo( nCurrLayerId, KEY_SELECTION_LAST_SELECTION_TYPE, i) break end end -- Recupero le tolleranze impostate alla creazione del Layer di Trimming corrente local dLinTol = EgtGetInfo( nCurrLayerId, KEY_LIN_TOL, 'd') local dAngTol = EgtGetInfo( nCurrLayerId, KEY_ANG_TOL, 'd') local dAngFaceTol = EgtGetInfo( nCurrLayerId, KEY_SURF_ANG_TOL, 'd') local bOk = ( dLinTol ~= nil and dAngTol ~= nil and dAngFaceTol ~= nil) if not bOk then EgtOutBox( ERROR_SELECTION .. 'Not a valid Trimming Layer', 'Error', 'ERROR', 'OK') return end -- Rendo invisibili gli altri Layer di Trimmatura e tutti i Layer Temporanei -- NB. Il focus va sul Layer corrente EgtSetStatus( nCurrLayerId, GDB_ST.ON) nId = EgtGetFirstInGroup( nCurrPartId) while nId do if nId ~= nCurrLayerId and ( GlobVar.IsTrimmingLayer( nId) or GlobVar.IsEditSyncCurvesLayer( nId)) then EgtSetStatus( nId, GDB_ST.OFF) end nId = EgtGetNext( nId) end -- Rimuovo Tutte le Entità ad Eccezione delle Superfici Selezionate ( nel caso Le rendo Visibili) -- [NB. La Selezione cancella tutti i risutati fatti fino ad adesso] local vIdToErase = {} local nId = EgtGetFirstInGroup( nCurrLayerId) while nId do local sName = EgtGetName( nId) if sName ~= SELECTION_SURF_NAME then table.insert( vIdToErase, nId) else EgtSetStatus( nId, GDB_ST.ON) end nId = EgtGetNext( nId) end EgtErase( vIdToErase) -- Se Esiste un Layer di Edit o di Stored, lo Rimuovo -- [NB. La Selezione cancella tutti i risultati fatti fino ad adesso] local nLayerSynEditId = GlobVar.GetCurrentEditSyncCurvesLayer( nCurrPartId, nCurrLayerId) if nLayerSynEditId ~= nil and nLayerSynEditId ~= GDB_ID.NULL then EgtErase( nLayerEditId) end --------------------------------- Inserimento della Superficie --------------------------------- -- [Nessuna Tolleranza] if vsVal[1] == 'Insert Surface' then -- Recupero tutti gli Oggetti selezionati che sono Superfici TriMesh o di Bezier local vObjId = {} local nCurrObjId = EgtGetFirstSelectedObj() while nCurrObjId do local nObjType = EgtGetType( nCurrObjId) if nObjType == GDB_TY.SRF_MESH or nObjType == GDB_TY.SRF_BEZ then table.insert( vObjId, nCurrObjId) end nCurrObjId = EgtGetNextSelectedObj() end -- Dalle superfici già presenti nel Layer di selezione, rimuovo gli Id dei riferimenti ( per non duplicare superfici) local vOldSelSurfId = {} local nId = EgtGetFirstNameInGroup( nCurrLayerId, 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, KEY_SELECTION_REF_SURF, 'i') if nRefSurfId then table.insert( vOldSelSurfId, nRefSurfId) end end nId = EgtGetNextName( nId, SELECTION_SURF_NAME) end local setToRemove = {} for _, v in ipairs( vOldSelSurfId) do setToRemove[v] = true end local vSurfIds = {} for _, v in ipairs( vObjId) do if not setToRemove[v] then table.insert( vSurfIds, v) end end -- Inserisco le superfici rimanenti nel Layer di Selezione local vNewSurfIds = {} for i = 1, #vSurfIds do local nInsertSurfId = EgtCopyGlob( vSurfIds[i], nCurrLayerId) if not nInsertSurfId or nInsertSurfId == GDB_ID.NULL then EgtOutBox( ERROR_SELECTION .. 'Copying Surface failed', 'Error', 'ERROR', 'OK') return end table.insert( vNewSurfIds, nInsertSurfId) -- Assegno Colore e Nome local bOk = EgtSetColor( nInsertSurfId, SELECTION_SURF_COLOR) and EgtSetInfo( nInsertSurfId, KEY_SELECTION_REF_SURF, vSurfIds[i]) and EgtSetName( nInsertSurfId, SELECTION_SURF_NAME) if not bOk then EgtOutBox( ERROR_SELECTION .. 'Copying Surface failed', 'Error', 'ERROR', 'OK') return end end EgtSetInfo( nCurrLayerId, KEY_SELECTION_LAST_IDS, vNewSurfIds) --------------------------------- Ricerca delle facce adiancenti -------------------------------- -- [Tolleranza angolare di superficie] elseif vsVal[1] == 'Face Search' then local bOk = false -- Se la superficie è una Bezier, allora la inserisco direttamente come Preview if nType == GDB_TY.SRF_BEZ then local nInsertSurfId = EgtCopyGlob( nSelObjId, nCurrLayerId) 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, KEY_SELECTION_REF_SURF, nSelObjId) and EgtSetName( nInsertSurfId, SELECTION_SURF_NAME) and EgtSetInfo( nCurrLayerId, KEY_SELECTION_LAST_IDS, { nInsertSurfId}) -- Se invece è una Trimesh, recupero il primo triangolo selezionato else local nId, nTria, ptInt = EgtGetLastSelInfo() if not nId or not nTria then EgtOutBox( ERROR_SELECTION .. 'Face not found', 'Error', 'ERROR', 'OK') return end -- *** Debug ( nel caso di errori, so che punto è stato selezionato ) *** local sStr = 'Pt = ' .. tostring( ptInt) .. ' - nTria = ' .. tostring( nTria) EgtOutLog( sStr) -- Recupero la superficie mediante adiancenza dei triangoli local nInsertSurfId = EgtTrimmingGetSurfTmFaceAdj( nCurrLayerId, nId, {nTria}, {ptInt}, 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, KEY_SELECTION_REF_SURF, nId) and EgtSetName( nInsertSurfId, SELECTION_SURF_NAME) and EgtSetInfo( nCurrLayerId, KEY_SELECTION_LAST_IDS, { nInsertSurfId}) end if not bOk then EgtOutBox( ERROR_SELECTION .. 'Copying Surface failed', 'Error', 'ERROR', 'OK') return end --------------------------------- Ricerca delle superfici Adiacenti --------------------------------- -- [Tolleranza lineare, Tolleranza angolare e Tolleranza angolare applicata alla Superficie] elseif vsVal[1] == 'Surf Search' then -- 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 table.insert( vAllSurfIds, nOtherId) 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, 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, KEY_SELECTION_REF_SURF, 'i') if nRefSurfId then table.insert( vOldSelSurfId, nRefSurfId) end end nId = EgtGetNextName( nId, 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, KEY_SELECTION_REF_SURF, vSurfIds[nSurf]) and EgtSetName( nInsertSurfId, SELECTION_SURF_NAME) if not bOk then EgtOutBox( ERROR_SELECTION .. 'Copying Surface failed', 'Error', 'ERROR', 'OK') return end end EgtSetInfo( nCurrLayerId, KEY_SELECTION_LAST_IDS, vNewInsertedSrfIds) end -- Deselelziono la superficie corrente e aggiorno la grafica EgtDeselectObj( nSelObjId) EgtDraw()