Script29_ExternalFabbprojectExample.lua
-- Lua example for Autodesk Netfabb 2021.1
-- Loads all meshfile parts from a directory,
-- repairs if any parts detected to have obvious faults,
-- adds them to a fabbproject,
-- and saves this to a FABBPROJECT file
inputdir = "d:\\temp\\" -- input path with trailing slash
outputdir = "d:\\temp\\" -- output path with trailing slash
outputname = "script29project" -- file name without suffix
system:setloggingtooglwindow(true)
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 loadfile(filename)
path, file, ext = string.match(filename, "(.-)([^\\/]-%.?([^%.\\/]*))$")
ext = ext:lower()
if ext == "stl" then
return system:loadstl(filename)
elseif ext == "3ds" then
return system:load3ds(filename)
elseif ext == "3mf" then
return system:load3mf(filename)
elseif ext == "amf" then
return system:loadamf(filename)
elseif ext == "gts" then
return system:loadgts(filename)
elseif ext == "ncm" then
return system:loadncm(filename)
elseif ext == "obj" then
return system:loadobj(filename)
elseif ext == "ply" then
return system:loadply(filename)
elseif ext == "svx" then
return system:loadvoxel(filename)
elseif ext == "vrml" then
return system:loadvrml(filename)
elseif ext == "wrl" then
return system:loadvrml(filename)
elseif ext == "x3d" then
return system:loadx3d(filename)
elseif ext == "x3db" then
return system:loadx3d(filename)
elseif ext == "zpr" then
return system:loadzpr(filename)
else
return nil
end
end
fabbproject = system:newfabbproject()
system:log(fabbproject.traycount)
xmlfilelist = system:getallfilesindirectory(inputdir) --Insert here your directory
system:log(xmlfilelist.childcount)
numberoffiles = xmlfilelist.childcount
ltray = fabbproject:gettray(0)
root = ltray.root
for i = 0, numberoffiles - 1 do
xmlChild = xmlfilelist:getchildindexed(i)
filename = xmlChild:getchildvalue("filename")
path, file, ext = string.match(filename, "(.-)([^\\/]-)%.([^%.\\/]*)$")
-- c:\foo\bar.suf captures into
-- c:\foo\, bar, suf
-- c:\foo\bar captures into nothing at all
-- not a problem, because:
meshfile = loadfile(filename) -- returns nil if there was no extension, loading nothing for this file
if meshfile ~= nil then
local traymesh = root:addmesh(meshfile, file)
system:log(string.format("$s.$s", file, ext))
end
end
local meshes = {}
insertMeshesIntoTable(root, meshes)
-- Check for faults, repair if necessary, add repaired mesh to tray, delete faulty original
for i, traymesh in pairs(meshes) do
local luamesh = traymesh.mesh
if not luamesh.isok then
newMesh:repairsimple()
newMesh:applymatrix(traymesh.matrix)
newTrayMesh = root:addmesh(newMesh, traymesh.name, tonumber(traymesh.color, 16))
root:removemesh(traymesh)
end
end
-- save project
fabbproject:savetofile(outputdir .. outputname .. ".fabbproject")