Share

Script39_CoordinatesTriangle.lua

-- Lua example for Autodesk Netfabb 2021.1

-- Gets the coordinates of a triangle and colors the triangle based on the z coordinate

system:setloggingtooglwindow(true)

local insertMeshesIntoTable
insertMeshesIntoTable = function(meshgroup, Outtable)
    for mesh_index = 0, meshgroup.meshcount - 1 do
        local traymesh = meshgroup:getmesh(mesh_index)
        table.insert(Outtable, traymesh)
    end
    for group_index = 0, meshgroup.groupcount - 1 do
        subgroup = meshgroup:getsubgroup(group_index)
        insertMeshesIntoTable(subgroup, Outtable)
    end
end

function ColorBasedZHeightOfFace(mesh)
    node1 = system:createvector3()
    node2 = system:createvector3()
    node3 = system:createvector3()
    for j = 0, mesh.facecount - 1 do
        mesh:getnodepositionofface(j, node1, node2, node3)
        -- arbitrary example function to color a triangle red or blue depending on
        -- whether the arithmetic mean of the vertices' Z values rounds to an even or odd number of millimeters
        if ((math.floor((node1.z + node2.z + node3.z) / 3) % 2) == 0) then
            mesh:colortriangle(j, 255, 0, 0)
        else
            mesh:colortriangle(j, 0, 0, 255)
        end
    end
end

-- Get root meshgroup from tray
local root = tray.root

-- Collect meshes in the tray
local meshes = {}
insertMeshesIntoTable(root, meshes)

-- Iterate over meshes in tray
for i, traymesh in pairs(meshes) do
    local luamesh = traymesh.mesh
    luamesh:applymatrix(traymesh.matrix)
    ColorBasedZHeightOfFace(luamesh)
    root:addmesh(luamesh, traymesh.name .. " colorized")
end

Was this information helpful?