Share

Script17_LabelMeshes.lua

-- Lua example for Autodesk Netfabb 2019.1

-- Impresses a text label on all parts present

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

GPosX = 0
GPosY = 0
GPosZ = 0

function SimpleEstimatePos(boundingbox, dir)
    -- This is a helper function for estimating the position from the bounding box
    -- it supports the 6 planes of the cube and follows the ViewCube naming

    if dir == "left" then
        GPosX = boundingbox.minx
        GPosY = (boundingbox.miny + boundingbox.maxy) / 2
        GPosZ = (boundingbox.minz + boundingbox.maxz) / 2
        result = true
    end
    if dir == "right" then
        GPosX = boundingbox.maxx
        GPosY = (boundingbox.miny + boundingbox.maxy) / 2
        GPosZ = (boundingbox.minz + boundingbox.maxz) / 2
        result = true
    end
    if dir == "top" then
        GPosX = (boundingbox.minx + boundingbox.maxx) / 2.0
        GPosY = (boundingbox.miny + boundingbox.maxy) / 2.0
        GPosZ = boundingbox.maxz
        result = true
    end
    if dir == "bottom" then
        GPosX = (boundingbox.minx + boundingbox.maxx) / 2.0
        GPosY = (boundingbox.miny + boundingbox.maxy) / 2.0
        GPosZ = boundingbox.minz
        result = true
    end
    if dir == "front" then
        GPosX = (boundingbox.minx + boundingbox.maxx) / 2.0
        GPosY = boundingbox.miny
        GPosZ = (boundingbox.minz + boundingbox.maxz) / 2
        result = true
    end
    if dir == "back" then
        GPosX = (boundingbox.minx + boundingbox.maxx) / 2.0
        GPosY = boundingbox.maxy
        GPosZ = (boundingbox.minz + boundingbox.maxz) / 2
        result = true
    end
end

function SetSimpleStamperSettings(stamperpar, PosX, PosY, PosZ, dir)
    -- This is a helper function for settings the label settings
    -- it supports the 6 planes of the cube and follows the ViewCube naming

    local result = false
    stamperpar:setpos(PosX, PosY, PosZ)
    if dir == "left" then
        stamperpar:setnormal(-1, 0, 0)
        stamperpar:setupvector(0, 0, -1)
        result = true
    end
    if dir == "right" then
        stamperpar:setnormal(1, 0, 0)
        stamperpar:setupvector(0, 0, -1)
        result = true
    end
    if dir == "bottom" then
        stamperpar:setnormal(0, 0, -1)
        stamperpar:setupvector(0, 1, 0)
        result = true
    end
    if dir == "top" then
        stamperpar:setnormal(0, 0, 1)
        stamperpar:setupvector(0, 1, 0)
        result = true
    end
    if dir == "front" then
        stamperpar:setnormal(0, -1, 0)
        stamperpar:setupvector(0, 0, -1)
        result = true
    end
    if dir == "back" then
        stamperpar:setnormal(0, 1, 0)
        stamperpar:setupvector(0, 0, -1)
        result = true
    end
    return result
end

system:setloggingtooglwindow(true)

if tray == nil then
    system:log("  tray is nil!")
else
    local root = tray.root
    -- Collect meshes in the tray
    local meshes = {}
    insertMeshesIntoTable(root, meshes)

    -- Iterate meshes in group
    local stamper = system:createstamper()
    stamper.depth = 2
    stamper.height = 20
    stamper.issubtracted = true

    for i, traymesh in pairs(meshes) do
        local luamesh = traymesh.mesh
        luamesh:applymatrix(traymesh.matrix)
        local boundingbox = luamesh:calcoutbox()
        SimpleEstimatePos(boundingbox, "front")
        SetSimpleStamperSettings(stamper, GPosX, GPosY, GPosZ, "front")

        local newmesh = stamper:stamp(luamesh, "Mylabel" .. tostring(i))
        root:addmesh(newmesh)
    end
end

Was this information helpful?