Share

Script11_PricingDemo.lua

-- Lua example for Autodesk Netfabb 2019.0

-- Generates a table to write part information and read it back out, then drops the table

-- Requires an SQL server, a user for the SQL server, and an active ODBC connector

ODBC_DSNNAME = "test"
ODBC_USER = "demouser"
ODBC_PASSWORD = "LL10203040"

sql_connection = nil

function init_db()
    system:log("Connecting to database...")
    sql_connection = system:connecttoodbc(ODBC_DSNNAME, ODBC_USER, ODBC_PASSWORD)
end

function CreateDB()
    local query_string, uuid
    uuid = sql_connection:getuniquestring()
    query_string =
        string.format("CREATE TABLE netfabb_tray (meshname varchar(255), meshsize FLOAT, insert_uuid varchar(255) )")
    sql_connection:sendquery(query_string)
end

function DestroyDB()
    local query_string, uuid
    uuid = sql_connection:getuniquestring()
    query_string =
        string.format("DROP TABLE netfabb_tray")
    sql_connection:sendquery(query_string)
end

function DemoDB_InsertIntoDatabase(meshnameValue, meshsizeValue, imageb64)
    local query_string, uuid
    uuid = sql_connection:getuniquestring()
    query_string =
        string.format(
        "INSERT INTO netfabb_tray (meshname, meshsize, insert_uuid) VALUES ('%s', '%d', '%s' )",
        meshnameValue,
        meshsizeValue,
        uuid
    )
    --system:log(query_string)
    sql_connection:sendquery(query_string)
end

function DemoDB_GetMeshSizeFromMeshName(meshname)
    local query_string, uuid, insertid

    -- Since ODBC does not support InsertID-Calls, we simply select back our UUID
    query_string = string.format("SELECT meshsize FROM netfabb_tray WHERE meshname = '%s' ", meshname)
    sql_connection:sendquery(query_string)

    -- Extract and parse result
    result = sql_connection:getresult()
    if result:getfieldcount() > 0 then
        insertid = result:getfield(0)
    else
        insertid = -1 -- if there has been an error, we won't find any tray
    end
    result:release()

    return insertid
end

init_db()
CreateDB()
system:log("table created")

-- Iterate meshes in root group; not bothering with subgroups
local root = tray.root
for mesh_index = 0, root.meshcount - 1 do
    local mesh = root:getmesh(mesh_index)
    local meshobject = mesh.mesh
    DemoDB_InsertIntoDatabase(mesh.name, meshobject.volume)
end

--For demo purposes only verify
for mesh_index = 0, root.meshcount - 1 do
    local mesh = root:getmesh(mesh_index)
    local value = DemoDB_GetMeshSizeFromMeshName(mesh.name)
    system:log(value)
end
DestroyDB()
sql_connection:disconnect()

Was this information helpful?