This sample demonstrates how to use the API to extract the Bill of Materials (BOM) from a CAD document.
It shows the sequence of API calls required to access the document structure, traverse the assembly hierarchy, retrieve component availability, and generate a structured BOM.
#ifdef Linux
#include <unistd.h>
#endif
{
std::cout <<
"Processing: " << inFilePath.
c_str() << std::endl;
{
std::cout <<
"File version: " << fileVersion.
c_str() << std::endl;
}
{
std::cout <<
"Unable to get file version (error code: " <<
dtkTypeError( err ).
c_str() <<
")" << std::endl;
return err;
}
{
std::cout << "Error: unable to open document (code: "
}
}
{
#ifndef Linux
wchar_t workingDirectory[1024];
_wgetcwd(workingDirectory, 1024);
#else
char workingDirectory[1024];
getcwd(workingDirectory, 1024);
#endif
std::cout << "Starting DATAKIT API..." << std::endl;
if (myApi == NULL)
{
std::cout << "Error: cannot start DATAKIT API (code: " << errorStatus << ")" << std::endl;
return errorStatus;
}
std::cout << "DATAKIT API started successfully." << std::endl;
int returnCode = 0;
for (int i = 1; i < argc; ++i)
{
returnCode = static_cast<int>(fileErr);
}
std::cout << std::endl << "DATAKIT API stopped." << std::endl;
return returnCode;
}
#include <iostream>
#include <map>
#include <string>
{
{
std::cout << "Warning: document has no root component." << std::endl;
}
else
{
}
}
{
return;
{
Dtk_string instanceName = inComponent->InstanceName();
if (inComponent->GetNumChildren() > 0)
{
{
}
}
}
{
Dtk_Size_t numChildren = inComponent->GetNumChildren();
{
TraverseBom(inComponent->GetChild(i), outBomEntries, inDepth);
}
}
{
Dtk_Size_t numChildren = inComponent->GetNumChildren();
if (numChildren > 0)
{
if (activatedIndices.
size() > 0)
{
{
TraverseBom(inComponent->GetChild(activatedIndices[i]), outBomEntries, inDepth);
}
}
else
{
Dtk_Int32 defaultIndex = inComponent->GetDefaultChildInCatalog();
if (defaultIndex >= 0 &&
static_cast<Dtk_Size_t>(defaultIndex) < numChildren)
{
TraverseBom(inComponent->GetChild(defaultIndex), outBomEntries, inDepth);
}
}
}
}
{
Dtk_Size_t numChildren = inComponent->GetNumChildren();
{
TraverseBom(inComponent->GetChild(i), outBomEntries, inDepth);
}
}
}
{
std::cout << std::endl;
std::cout << "Bill of Materials" << std::endl;
std::cout << "-----------------" << std::endl;
if (inBomEntries.
size() == 0)
{
std::cout << " (no components found)" << std::endl;
return;
}
{
const BomEntry &entry = inBomEntries[i];
std::string prefix;
prefix += "| ";
if (depth > 0)
prefix += "|-- ";
std::cout << prefix;
std::cout << " [MISSING]";
std::cout << std::endl;
}
}
{
std::cout << std::endl;
std::cout << "Summary (flattened occurrence count)" << std::endl;
std::cout << "------------------------------------" << std::endl;
if (inBomEntries.
size() == 0)
{
std::cout << " (no components found)" << std::endl;
return;
}
std::map<std::string, int> countMap;
std::map<std::string, bool> missingMap;
{
const BomEntry &entry = inBomEntries[i];
countMap[key]++;
missingMap[key] = true;
}
std::cout << " Part Name | Count | Status" << std::endl;
std::cout << " ---------------------------------- | ----- | --------------------" << std::endl;
for (std::map<std::string, int>::const_iterator it = countMap.begin();
it != countMap.end(); ++it)
{
const std::string &key = it->first;
std::string name = key;
if (name.size() > 34)
name = name.substr(0, 31) + "...";
std::cout << " ";
std::cout << name;
for (size_t pad = name.size(); pad < 34; ++pad)
std::cout << ' ';
std::cout << " | ";
std::string countStr = std::to_string(it->second);
for (size_t pad = countStr.size(); pad < 5; ++pad)
std::cout << ' ';
std::cout << countStr;
std::cout << " | ";
bool hasMissing = missingMap.count(key) > 0;
if (hasMissing )
std::cout << " [MISSING]";
std::cout << std::endl;
}
std::cout << std::endl;
}