DATAKIT SDK  V2026.2
Through Assemblies

Sample to browse a Dtk_MainDoc
Sample to browse a Dtk_Component
Sample to browse a Dtk_Component type Instance
Sample to browse a Dtk_Component type Prototype
Selection Set sample

Sample to browse a Dtk_MainDoc

//**********************************************************************************************
// This file provides a sample implementation for writing the contents of a CAD document
// using the Datakit API.
//**********************************************************************************************
#include "datakit.h"
#include "../../WritingSample/PdfWrite/PdfWrite.hpp"
// Function declarations
extern Dtk_ErrorStatus WriteComponent(Dtk_ComponentPtr inComponent, const Dtk_transfo &inMatrix = Dtk_transfo());
// Writes the contents of a CAD document.
// Returns a Dtk_ErrorStatus indicating success (dtkNoError) or failure of the operation.
{
// Retrieve the root component from the document.
Dtk_ComponentPtr rootComponent = inDocument->RootComponent();
// If the root component is valid, process it and its children.
if (rootComponent.IsNotNULL())
{
// Initialize PDF output for the root component if PDF export is enabled.
{
PdfInitComponent(rootComponent);
}
// Recursively process the root component and its children.
WriteComponent(rootComponent);
// Finalize PDF output for the root component if PDF export is enabled.
{
// Finalize PDF output for the root component if PDF export is enabled.
}
return dtkNoError;
}
// Return error if the document is invalid.
}

Sample to browse a Dtk_Component

//**********************************************************************************************
// This file provides a sample implementation for writing the contents of a CAD component
// using the Datakit API.
//**********************************************************************************************
#include "datakit.h"
#include "../../WritingSample/XmlWrite/XmlWrite.hpp"
// Writes a CAD component and its children to output formats (XML and PDF).
// Returns a Dtk_ErrorStatus indicating success (dtkNoError) or failure of the operation.
{
// Get the unit scale (e.g., 25.4 for inch) for the component.
double unitFactor;
inComponent->GetConceptionUnitScale(unitFactor);
// Get the component name.
Dtk_string componentName = inComponent->Name();
// Get component attributes (activation, blanked status, color).
Dtk_InfoPtr componentAttributes = inComponent->GetInfos();
if (componentAttributes.IsNotNULL())
{
// Get activation flag (1 if activated, 0 otherwise).
int activationStatus = 1;
activationStatus = componentAttributes->GetActivationFlag();
// Get blanked status (0: visible, 1: invisible, 2: construction geometry).
int blankedStatus = 0;
blankedStatus = componentAttributes->GetBlankedStatus();
// Get color (RGB values).
Dtk_RGB color = componentAttributes->GetColor();
}
// Get and save preview image if available.
Dtk_PreviewPtr preview = inComponent->GetPreview();
if (preview.IsNotNULL())
{
Dtk_Int32 size = preview->GetStreamSize();
char *jpgImage = preview->GetStream();
Dtk_string previewName = "ComponentPreview.jpg";
FILE *jpg = previewName.OpenFile("wb");
if (jpg)
{
// Write JPEG image data to file.
fwrite(jpgImage, sizeof(char), size, jpg);
fclose(jpg);
}
}
{
// initialize XML output for the component if XML export is enabled.
XmlInitComponent(inComponent);
}
// Process the component based on its type.
Dtk_Component::ComponentTypeEnum type = inComponent->ComponentType();
switch (type)
{
// Instance: represents a prototype with a matrix placement.
{
// Write the component instance
WriteInstance(inComponent);
break;
}
// Prototype: check if already processed to avoid redundant work.
// Use inComponent->GetID() to obtain its unique ID.
{
// Write the prototype component with the given transformation matrix.
WritePrototype(inComponent, inMatrix);
break;
}
// Catalog: represents multiple possible configurations (scene, workspace, etc.).
{
// Get number of children and recursively write each child component.
Dtk_Size_t numChildren = inComponent->GetNumChildren();
if (numChildren > 0)
{
// Get default component index in the catalog
Dtk_Int32 defaultComponentIndex = inComponent->GetDefaultChildInCatalog();
// Get activated component indices in the catalog
Dtk_tab<Dtk_Int32> activatedComponentIndices = inComponent->GetActivatedChildrenInCatalog();
// Get number of activated components in the catalog
Dtk_Size_t numActivatedComponents = activatedComponentIndices.size();
{
// Write all non-activated and non-default children as XML dump entries (to provide a complete component tree)
for (Dtk_Size_t i = 0; i < numChildren; i++)
{
// Skip activated components
if( activatedComponentIndices.find( ( Dtk_Int32 )i ) >= 0 )
{
continue;
}
// Skip default component
if( defaultComponentIndex == ( Dtk_Int32 )i )
{
continue;
}
// Write XML entry for non-activated and non-default child component
Dtk_ComponentPtr childComponent = inComponent->GetChild(i);
XmlInitComponent(childComponent);
}
}
// Write activated components or default component
if (numActivatedComponents > 0)
{
// Write all activated components
for ( Dtk_Size_t i = 0; i < numActivatedComponents; i++)
{
// Write the activated component
Dtk_ComponentPtr activatedComponent = inComponent->GetChild(activatedComponentIndices[i]);
WriteComponent(activatedComponent, inMatrix);
}
}
else
{
// Write the default component
Dtk_ComponentPtr defaultChildComponent = inComponent->GetChild(defaultComponentIndex);
WriteComponent(defaultChildComponent, inMatrix);
}
}
// If necessary, iterate over all children and select the desired one(s) to process based on their name.
break;
}
// Virtual: component containing only children.
{
// Get number of child components and recursively write each child component.
Dtk_Size_t numChildComponents = inComponent->GetNumChildren();
for (Dtk_Size_t i = 0; i < numChildComponents; i++)
{
Dtk_ComponentPtr childComponent = inComponent->GetChild(i);
WriteComponent(childComponent, inMatrix);
}
break;
}
}
{
// Finalize XML output for the component if XML export is enabled.
}
return dtkNoError;
}

Sample to browse a Dtk_Component type Instance

//****************************************************************************************************
// This file provides a sample implementation for writing the contents of a CAD component instance
// using the Datakit API.
//****************************************************************************************************
#include "datakit.h"
#include "../../WritingSample/PdfWrite/PdfWrite.hpp"
#include "../../WritingSample/XmlWrite/XmlWrite.hpp"
// Global list to track processed components (to avoid redundant processing)
// Writes a CAD component instance
// Returns a Dtk_ErrorStatus indicating success (dtkNoError) or failure of the operation.
{
// Get the prototype name.
Dtk_string prototypeName = inComponent->Name();
// Get the instance name.
Dtk_string instanceName = inComponent->InstanceName();
// Get the prototype component (first child of instance) and its transformation matrix.
Dtk_ComponentPtr prototype = inComponent->GetChild(0);
Dtk_transfo matrix = inComponent->TransformationMatrix();
// Get the ID of the prototype component.
Dtk_ID childID = prototype->GetID();
// Initialize PDF instance ID.
Dtk_ID pdfInstanceID = 0;
// Initialize PDF output for the instance if PDF export is enabled.
{
pdfInstanceID = PdfInitInstance(inComponent);
}
// Write the prototype component.
// Note: The transformation matrix can be passed if needed for further processing.
WriteComponent(prototype, Dtk_transfo());
// Finalize PDF output for the instance if PDF export is enabled.
{
PdfEndInstance(pdfInstanceID, childID);
}
// Track the processed component by its ID to avoid redundant processing.
if (ProcessedComponents.find(childID) < 0)
{
ProcessedComponents.push_back(childID);
}
return dtkNoError;
}

Sample to browse a Dtk_Component type Prototype

//****************************************************************************************************
// This file provides a sample implementation for writing the contents of a CAD prototype component
// using the Datakit API.
//****************************************************************************************************
#include "datakit.h"
#include "../../WritingSample/PdfWrite/PdfWrite.hpp"
#include "../../WritingSample/XmlWrite/XmlWrite.hpp"
#include "../ThroughData/WriteNode.hpp"
// Global variable for flattening assemblies (stores the current transformation matrix)
// Global list to track processed components and avoid redundant processing
// Writes a prototype component, its children, and its construction tree.
void WritePrototype(Dtk_ComponentPtr inComponent, const Dtk_transfo &inMatrix)
{
// Get the component ID.
Dtk_ID componentID = inComponent->GetID();
// Check if the component has already been processed.
// Only process the prototype if it hasn't been processed yet.
Dtk_Int32 componentIndex = ProcessedComponents.find(componentID);
if (componentIndex == -1)
{
Dtk_NodePtr rootNode;
// Get the Datakit API instance.
// Recursively process all child components (instances, sub-assemblies, etc.).
Dtk_Size_t numChildComponents = inComponent->GetNumChildren();
for ( Dtk_Size_t i = 0; i < numChildComponents; i++)
{
// Get current child component.
Dtk_ComponentPtr childComponent = inComponent->GetChild(i);
// Recursively write the child component with the current transformation matrix.
WriteComponent(childComponent, inMatrix);
}
// Read the construction tree for this prototype.
Dtk_ErrorStatus errorStatus;
if( inComponent->ComponentAvailability() == Dtk_Component::ComponentMissing )
{
errorStatus = dtkErrorFileNotExist;
}
else
{
errorStatus = myAPI->ReadComponent( inComponent, rootNode );
}
// If the construction tree is valid, process it.
// A rootNode == NULL with err == dtkNoError means the component is empty.
if (errorStatus == dtkNoError && rootNode.IsNotNULL())
{
// Set the current matrix for flattening assemblies.
CurrentMatrix = inMatrix;
// Recursively process the root node and its children.
WriteNode(rootNode);
// Write global data set and metadata to XML if XML export is enabled.
{
XmlWriteGlobalDataSet(inComponent->GetGlobalDataSet());
Dtk_Size_t numMetaData = inComponent->GetNumMetaData();
if (numMetaData)
{
for ( Dtk_Size_t i = 0; i < numMetaData; i++)
{
XmlWriteMetaData(inComponent->GetMetaData(i));
}
}
}
}
// Write metadata to PDF if PDF export is enabled.
{
PdfWriteMetaData(inComponent);
}
// Finalize and free resources for the component.
errorStatus = myAPI->EndComponent(inComponent);
}
else
{
// If the prototype has already been processed, handle the PDF instance case.
// Use the unique ID returned by GetID() to map the component to your write ID.
{
}
}
}
Dtk_ID
uint32_t Dtk_ID
Definition: define.h:681
Dtk_transfo
This is the Transformation dedicated class.
Definition: dtk_transfo.hpp:19
Dtk_Info::GetBlankedStatus
int GetBlankedStatus() const
Retrieves the entity Blanked Status.
Dtk_Component::CatalogComponentType
@ CatalogComponentType
Definition: dtk_maindoc.hpp:571
dtkErrorFileNotExist
@ dtkErrorFileNotExist
Definition: error_dtk.hpp:101
Dtk_SmartPtr::IsNotNULL
Dtk_bool IsNotNULL() const
Definition: util_ptr_dtk.hpp:119
XmlInitComponent
void XmlInitComponent(Dtk_ComponentPtr inComponent)
Definition: XmlWrite.cpp:50
XmlWriteMetaData
void XmlWriteMetaData(const Dtk_MetaDataPtr &inMetaData)
Definition: XmlWrite.cpp:130
XmlEndComponent
void XmlEndComponent()
Definition: XmlWrite.cpp:57
PdfWriteMetaData
void PdfWriteMetaData(Dtk_ComponentPtr inComponent)
Definition: PdfWrite.cpp:120
XmlWriteGlobalDataSet
void XmlWriteGlobalDataSet(const Dtk_GlobalDataSetPtr &inSelectionSet)
Definition: XmlWrite.cpp:124
Dtk_string
This is a high level string class.
Definition: dtk_string.hpp:53
WriteDocument
Dtk_ErrorStatus WriteDocument(Dtk_MainDocPtr inDocument)
Definition: WriteDocument.cpp:14
PdfEndInstance
void PdfEndInstance(Dtk_ID pdfInstID, Dtk_ID childID)
Definition: PdfWrite.cpp:111
Dtk_Size_t
size_t Dtk_Size_t
Definition: define.h:704
Dtk_Info::GetColor
Dtk_RGB GetColor() const
Retrieves the entity color as Dtk_RGBA values.
Dtk_Info::GetActivationFlag
int GetActivationFlag() const
Dtk_API::EndComponent
Dtk_ErrorStatus EndComponent(Dtk_ComponentPtr &inComponent)
EndComponent.
Dtk_string::OpenFile
FILE * OpenFile(const Dtk_string &inRights) const
File Utility : Open a file with the given rights.
WriteComponent
Dtk_ErrorStatus WriteComponent(Dtk_ComponentPtr inComponent, const Dtk_transfo &inMatrix=Dtk_transfo())
Definition: WriteComponent.cpp:12
WritePrototype
void WritePrototype(Dtk_ComponentPtr inComponent, const Dtk_transfo &inMatrix)
Definition: WritePrototype.cpp:19
Dtk_Component::VirtualComponentType
@ VirtualComponentType
Definition: dtk_maindoc.hpp:572
WriteComponent.hpp
Dtk_Component::InstanceComponentType
@ InstanceComponentType
Definition: dtk_maindoc.hpp:569
Dtk_API::GetAPI
static Dtk_API * GetAPI()
Get DATAKIT API.
WriteInstance
Dtk_ErrorStatus WriteInstance(Dtk_ComponentPtr inComponent)
Definition: WriteInstance.cpp:16
Dtk_tab::find
int find(const T &e) const
Definition: util_stl_dtk.hpp:746
PdfInstanceExistingPrototype
void PdfInstanceExistingPrototype(Dtk_ID ComponentIndex)
Definition: PdfWrite.cpp:141
Dtk_Int32
int32_t Dtk_Int32
Definition: define.h:679
Dtk_Component::ComponentTypeEnum
ComponentTypeEnum
Definition: dtk_maindoc.hpp:568
Dtk_ErrorStatus
Dtk_ErrorStatus
Definition: error_dtk.hpp:6
CurrentMatrix
Dtk_transfo CurrentMatrix
Definition: WritePrototype.cpp:13
Dtk_SmartPtr
Definition: util_ptr_dtk.hpp:37
PdfInitInstance
Dtk_ID PdfInitInstance(Dtk_ComponentPtr inComponent)
Definition: PdfWrite.cpp:93
datakit.h
ProcessedComponents
Dtk_tab< Dtk_ID > ProcessedComponents
Definition: WritePrototype.cpp:16
Dtk_tab< Dtk_Int32 >
Dtk_tab::size
Dtk_Size_t size() const
Returns the size of the array.
Definition: util_stl_dtk.hpp:503
Dtk_Component::PrototypeComponentType
@ PrototypeComponentType
Definition: dtk_maindoc.hpp:570
IsXmlDumpActivated
Dtk_bool IsXmlDumpActivated()
Definition: XmlWrite.cpp:17
Dtk_Component::ComponentMissing
@ ComponentMissing
Definition: dtk_maindoc.hpp:559
dtkNoError
@ dtkNoError
Definition: error_dtk.hpp:149
Dtk_API::ReadComponent
Dtk_ErrorStatus ReadComponent(const Dtk_ComponentPtr &inComponent, Dtk_NodePtr &outRootNode)
Read Component from Assembly Tree (Call EndComponent to free data allocated)
WriteNode
Dtk_ErrorStatus WriteNode(Dtk_NodePtr inNode)
Definition: WriteNode.cpp:16
Dtk_RGB
Definition: dtk_rgb.hpp:7
IsPdfDumpActivated
Dtk_bool IsPdfDumpActivated()
Definition: PdfWrite.cpp:29
dtkErrorNullPointer
@ dtkErrorNullPointer
Definition: error_dtk.hpp:23
Dtk_API
Definition: dtk_api.hpp:75
PdfEndComponent
void PdfEndComponent()
Definition: PdfWrite.cpp:154
PdfInitComponent
Dtk_ID PdfInitComponent(Dtk_ComponentPtr inComponent)
Definition: PdfWrite.cpp:146