DATAKIT SDK  V2026.4
WriteComponent.hpp File Reference

Go to the source code of this file.

Functions

Dtk_ErrorStatus WriteComponent (Dtk_ComponentPtr inComponent, const Dtk_transfo &inMatrix=Dtk_transfo())
 
Dtk_ErrorStatus WriteDocument (Dtk_MainDocPtr inDocument)
 
Dtk_ErrorStatus WriteInstance (Dtk_ComponentPtr inComponent)
 
void WritePreview (const Dtk_PreviewPtr &inPreview, const Dtk_string &inName)
 
void WritePrototype (Dtk_ComponentPtr inComponent, const Dtk_transfo &inMatrix)
 

Function Documentation

◆ WriteComponent()

Dtk_ErrorStatus WriteComponent ( Dtk_ComponentPtr  inComponent,
const Dtk_transfo &  inMatrix = Dtk_transfo() 
)
13 {
14  // Get the unit scale (e.g., 25.4 for inch) for the component.
15  double unitFactor;
16  auto error = inComponent->GetConceptionUnitScale(unitFactor);
17  DTK_UNUSED( error );
18 
19  // Get the component name.
20  Dtk_string componentName = inComponent->Name();
21 
22  // Get component attributes (activation, blanked status, color).
23  Dtk_InfoPtr componentAttributes = inComponent->GetInfos();
24  if (componentAttributes.IsNotNULL())
25  {
26  // Get activation flag (1 if activated, 0 otherwise).
27  int activationStatus = 1;
28  activationStatus = componentAttributes->GetActivationFlag();
29 
30  // Get blanked status (0: visible, 1: invisible, 2: construction geometry).
31  int blankedStatus = 0;
32  blankedStatus = componentAttributes->GetBlankedStatus();
33 
34  // Get color (RGB values).
35  Dtk_RGB color = componentAttributes->GetColor();
36  }
37 
38  // Get and write preview image if available.
39  Dtk_PreviewPtr preview = inComponent->GetPreview();
40  Dtk_string previewName = componentName + L"_";
41  previewName.add_int( inComponent->GetID() );
42  if( preview.IsNotNULL() )
43  {
44  WritePreview( preview, previewName );
45  }
46 
47 
48  if (IsXmlDumpActivated())
49  {
50  // initialize XML output for the component if XML export is enabled.
51  XmlInitComponent(inComponent);
52  }
53 
54  // Process the component based on its type.
55  Dtk_Component::ComponentTypeEnum type = inComponent->ComponentType();
56  switch (type)
57  {
58  // Instance: represents a prototype with a matrix placement.
60  {
61  // Write the component instance
62  WriteInstance(inComponent);
63  break;
64  }
65  // Prototype: check if already processed to avoid redundant work.
66  // Use inComponent->GetID() to obtain its unique ID.
68  {
69  // Write the prototype component with the given transformation matrix.
70  WritePrototype(inComponent, inMatrix);
71  break;
72  }
73  // Catalog: represents multiple possible configurations (scene, workspace, etc.).
75  {
76  // Get number of children and recursively write each child component.
77  Dtk_Size_t numChildren = inComponent->GetNumChildren();
78  if (numChildren > 0)
79  {
80  // Get default component index in the catalog
81  Dtk_Int32 defaultComponentIndex = inComponent->GetDefaultChildInCatalog();
82 
83  // Get activated component indices in the catalog
84  Dtk_tab<Dtk_Int32> activatedComponentIndices = inComponent->GetActivatedChildrenInCatalog();
85 
86  // Get number of activated components in the catalog
87  Dtk_Size_t numActivatedComponents = activatedComponentIndices.size();
88 
89  if (IsXmlDumpActivated())
90  {
91  // Write all non-activated and non-default children as XML dump entries (to provide a complete component tree)
92  for (Dtk_Size_t i = 0; i < numChildren; i++)
93  {
94  // Skip activated components
95  if( activatedComponentIndices.find( ( Dtk_Int32 )i ) >= 0 )
96  {
97  continue;
98  }
99 
100  // Skip default component
101  if( defaultComponentIndex == ( Dtk_Int32 )i )
102  {
103  continue;
104  }
105 
106  // Write XML entry for non-activated and non-default child component
107  Dtk_ComponentPtr childComponent = inComponent->GetChild(i);
108  XmlInitComponent(childComponent);
109  XmlEndComponent();
110  }
111  }
112 
113  // Write activated components or default component
114  if (numActivatedComponents > 0)
115  {
116  // Write all activated components
117  for ( Dtk_Size_t i = 0; i < numActivatedComponents; i++)
118  {
119  // Write the activated component
120  Dtk_ComponentPtr activatedComponent = inComponent->GetChild(activatedComponentIndices[i]);
121  WriteComponent(activatedComponent, inMatrix);
122  }
123  }
124  else
125  {
126  // Write the default component
127  Dtk_ComponentPtr defaultChildComponent = inComponent->GetChild(defaultComponentIndex);
128  WriteComponent(defaultChildComponent, inMatrix);
129  }
130  }
131 
132  // If necessary, iterate over all children and select the desired one(s) to process based on their name.
133  break;
134  }
135  // Virtual: component containing only children.
137  {
138  // Get number of child components and recursively write each child component.
139  Dtk_Size_t numChildComponents = inComponent->GetNumChildren();
140  for (Dtk_Size_t i = 0; i < numChildComponents; i++)
141  {
142  Dtk_ComponentPtr childComponent = inComponent->GetChild(i);
143  WriteComponent(childComponent, inMatrix);
144  }
145  break;
146  }
147  }
148 
149  if (IsXmlDumpActivated())
150  {
151  // Finalize XML output for the component if XML export is enabled.
152  XmlEndComponent();
153  }
154 
155  return dtkNoError;
156 }

◆ WriteDocument()

Dtk_ErrorStatus WriteDocument ( Dtk_MainDocPtr  inDocument)
16 {
17  if( IsXmlDumpActivated() )
18  {
19  XmlInitMainDoc( inDocument );
20  }
21  // Retrieve the root component from the document.
22  Dtk_ComponentPtr rootComponent = inDocument->RootComponent();
23 
24  // If the root component is valid, process it and its children.
25  if (rootComponent.IsNotNULL())
26  {
27  // Initialize PDF output for the root component if PDF export is enabled.
28  if (IsPdfDumpActivated())
29  {
30  PdfInitComponent(rootComponent);
31  }
32 
33  // Recursively process the root component and its children.
34  WriteComponent(rootComponent);
35 
36  // Finalize PDF output for the root component if PDF export is enabled.
37  if (IsPdfDumpActivated())
38  {
39  // Finalize PDF output for the root component if PDF export is enabled.
41  }
42  }
43  if( IsXmlDumpActivated() )
44  {
45  XmlEndMainDoc( inDocument );
46  }
47 
48 
49  // Return error if the document is invalid.
50  if( rootComponent.IsNULL() )
51  return dtkErrorNullPointer;
52  else
53  return dtkNoError;
54 }

◆ WriteInstance()

Dtk_ErrorStatus WriteInstance ( Dtk_ComponentPtr  inComponent)
17 {
18  // Get the prototype name.
19  Dtk_string prototypeName = inComponent->Name();
20 
21  // Get the instance name.
22  Dtk_string instanceName = inComponent->InstanceName();
23 
24  // Get the prototype component (first child of instance) and its transformation matrix.
25  Dtk_ComponentPtr prototype = inComponent->GetChild(0);
26  Dtk_transfo matrix = inComponent->TransformationMatrix();
27 
28  // Get the ID of the prototype component.
29  Dtk_ID childID = prototype->GetID();
30 
31  // Initialize PDF instance ID.
32  Dtk_ID pdfInstanceID = 0;
33 
34  // Initialize PDF output for the instance if PDF export is enabled.
35  if (IsPdfDumpActivated())
36  {
37  pdfInstanceID = PdfInitInstance(inComponent);
38  }
39 
40  // Write the prototype component.
41  // Note: The transformation matrix can be passed if needed for further processing.
42  WriteComponent(prototype, Dtk_transfo());
43 
44  // Finalize PDF output for the instance if PDF export is enabled.
45  if (IsPdfDumpActivated())
46  {
47  PdfEndInstance(pdfInstanceID, childID);
48  }
49 
50  // Track the processed component by its ID to avoid redundant processing.
51  if (ProcessedComponents.find(childID) < 0)
52  {
53  ProcessedComponents.push_back(childID);
54  }
55 
56  return dtkNoError;
57 }

◆ WritePreview()

void WritePreview ( const Dtk_PreviewPtr &  inPreview,
const Dtk_string &  inName 
)
5 {
6  if( inPreview.IsNULL() )
7  return;
8 
9  Dtk_Int32 size = inPreview->GetStreamSize();
10  char* imageData = inPreview->GetStream();
11 
12  if( size <= 0 || imageData == nullptr )
13  return;
14 
15  Dtk_string previewExt;
16 
17  switch( inPreview->GetType() )
18  {
20  previewExt = "jpg";
21  break;
23  previewExt = "bmp";
24  break;
26  previewExt = "png";
27  break;
29  previewExt = "cgm";
30  break;
32  previewExt = "gif";
33  break;
35  previewExt = "tiff";
36  break;
38  previewExt = "ico";
39  break;
41  previewExt = "emf";
42  break;
44  default:
45  previewExt = "jpg"; // Default to jpg if type is unknown
46  break;
47  }
48 
49  Dtk_string previewName = inName + "_Preview." + previewExt;
50  FILE* previewPtr = previewName.OpenFile( "wb" );
51  if( previewPtr )
52  {
53  fwrite( imageData, sizeof( char ), size, previewPtr );
54  fclose( previewPtr );
55  //std::cout << "Writing Preview " << previewName.c_str() << std::endl;
56  }
57 }

◆ WritePrototype()

void WritePrototype ( Dtk_ComponentPtr  inComponent,
const Dtk_transfo &  inMatrix 
)
20 {
21  // Get the component ID.
22  Dtk_ID componentID = inComponent->GetID();
23 
24  // Check if the component has already been processed.
25  // Only process the prototype if it hasn't been processed yet.
26  Dtk_Int32 componentIndex = ProcessedComponents.find(componentID);
27  if (componentIndex == -1)
28  {
29  Dtk_NodePtr rootNode;
30 
31  // Get the Datakit API instance.
32  Dtk_API *myAPI = Dtk_API::GetAPI();
33 
34  // Recursively process all child components (instances, sub-assemblies, etc.).
35  Dtk_Size_t numChildComponents = inComponent->GetNumChildren();
36  for ( Dtk_Size_t i = 0; i < numChildComponents; i++)
37  {
38  // Get current child component.
39  Dtk_ComponentPtr childComponent = inComponent->GetChild(i);
40 
41  // Recursively write the child component with the current transformation matrix.
42  WriteComponent(childComponent, inMatrix);
43  }
44 
45  // Read the construction tree for this prototype.
46  Dtk_ErrorStatus errorStatus;
47  if( inComponent->IsAvailabilityFlaggedAs(Dtk_Component::ComponentMissing) )
48  {
49  errorStatus = dtkErrorFileNotExist;
50  }
51  else
52  {
53  // Read the construction tree of the component; rootNode will point to the tree's root on success.
54  errorStatus = myAPI->ReadComponent( inComponent, rootNode );
55  }
56 
57  // If the construction tree is valid, process it.
58  // A rootNode == NULL with err == dtkNoError means the component is empty.
59  if (errorStatus == dtkNoError && rootNode.IsNotNULL())
60  {
61  // Set the current matrix for flattening assemblies.
62  CurrentMatrix = inMatrix;
63 
64  // Recursively process the root node and its children.
65  WriteNode(rootNode);
66 
67  // Write global data set and metadata to XML if XML export is enabled.
68  if (IsXmlDumpActivated())
69  {
70  XmlWriteGlobalDataSet(inComponent->GetGlobalDataSet());
71 
72  Dtk_Size_t numMetaData = inComponent->GetNumMetaData();
73  if (numMetaData)
74  {
75  for ( Dtk_Size_t i = 0; i < numMetaData; i++)
76  {
77  XmlWriteMetaData(inComponent->GetMetaData(i));
78  }
79  }
80  }
81  }
82 
83  // Write metadata to PDF if PDF export is enabled.
84  if (IsPdfDumpActivated())
85  {
86  PdfWriteMetaData(inComponent);
87  }
88 
89  // Finalize and free resources for the component.
90  errorStatus = myAPI->EndComponent(inComponent);
91  }
92  else
93  {
94  // If the prototype has already been processed, handle the PDF instance case.
95  // Use the unique ID returned by GetID() to map the component to your write ID.
96  if (IsPdfDumpActivated())
97  {
98  PdfInstanceExistingPrototype(componentIndex);
99  }
100  }
101 }
ProcessedComponents
Dtk_tab< Dtk_ID > ProcessedComponents
Definition: WritePrototype.cpp:16
Dtk_ID
uint32_t Dtk_ID
Definition: define.h:688
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:575
PREVIEW_TYPE_DETK_TIFF
@ PREVIEW_TYPE_DETK_TIFF
Definition: define.h:638
dtkErrorFileNotExist
@ dtkErrorFileNotExist
Definition: error_dtk.hpp:103
WriteComponent
Dtk_ErrorStatus WriteComponent(Dtk_ComponentPtr inComponent, const Dtk_transfo &inMatrix)
Definition: WriteComponent.cpp:12
Dtk_SmartPtr::IsNotNULL
Dtk_bool IsNotNULL() const
Definition: util_ptr_dtk.hpp:119
XmlInitComponent
void XmlInitComponent(Dtk_ComponentPtr inComponent)
Definition: XmlWrite.cpp:60
XmlWriteMetaData
void XmlWriteMetaData(const Dtk_MetaDataPtr &inMetaData)
Definition: XmlWrite.cpp:140
XmlEndComponent
void XmlEndComponent()
Definition: XmlWrite.cpp:67
PdfWriteMetaData
void PdfWriteMetaData(Dtk_ComponentPtr inComponent)
Definition: PdfWrite.cpp:120
XmlWriteGlobalDataSet
void XmlWriteGlobalDataSet(const Dtk_GlobalDataSetPtr &inSelectionSet)
Definition: XmlWrite.cpp:134
Dtk_string
This is a high level string class.
Definition: dtk_string.hpp:53
PdfEndInstance
void PdfEndInstance(Dtk_ID pdfInstID, Dtk_ID childID)
Definition: PdfWrite.cpp:111
Dtk_Size_t
size_t Dtk_Size_t
Definition: define.h:711
Dtk_Info::GetColor
Dtk_RGB GetColor() const
Retrieves the entity color as Dtk_RGBA values.
PREVIEW_TYPE_DETK_GIF
@ PREVIEW_TYPE_DETK_GIF
Definition: define.h:637
Dtk_Info::GetActivationFlag
int GetActivationFlag() const
XmlEndMainDoc
void XmlEndMainDoc(Dtk_MainDocPtr inMainDoc)
Definition: XmlWrite.cpp:55
PREVIEW_TYPE_DETK_JPG
@ PREVIEW_TYPE_DETK_JPG
Definition: define.h:633
Dtk_API::EndComponent
Dtk_ErrorStatus EndComponent(Dtk_ComponentPtr &inComponent)
EndComponent release data allocated by ReadComponent or ReadComponentGraphic. You have to call EndCom...
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
PREVIEW_TYPE_DETK_PNG
@ PREVIEW_TYPE_DETK_PNG
Definition: define.h:635
WritePrototype
void WritePrototype(Dtk_ComponentPtr inComponent, const Dtk_transfo &inMatrix)
Definition: WritePrototype.cpp:19
Dtk_Component::VirtualComponentType
@ VirtualComponentType
Definition: dtk_maindoc.hpp:576
Dtk_Component::InstanceComponentType
@ InstanceComponentType
Definition: dtk_maindoc.hpp:573
PREVIEW_TYPE_DETK_CGM
@ PREVIEW_TYPE_DETK_CGM
Definition: define.h:636
Dtk_string::add_int
void add_int(const int integer, int force_unsigned_int=0)
concat an int to the Dtk_string (convert the int to Dtk_string)
PREVIEW_TYPE_DETK_BMP
@ PREVIEW_TYPE_DETK_BMP
Definition: define.h:634
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:686
PREVIEW_TYPE_DETK_EMF
@ PREVIEW_TYPE_DETK_EMF
Definition: define.h:640
Dtk_Component::ComponentTypeEnum
ComponentTypeEnum
Definition: dtk_maindoc.hpp:572
PREVIEW_TYPE_DETK_ICO
@ PREVIEW_TYPE_DETK_ICO
Definition: define.h:639
PREVIEW_TYPE_DETK_UNKNOWN
@ PREVIEW_TYPE_DETK_UNKNOWN
Definition: define.h:632
ProcessedComponents
Dtk_tab< Dtk_ID > ProcessedComponents
Definition: WritePrototype.cpp:16
Dtk_ErrorStatus
Dtk_ErrorStatus
Definition: error_dtk.hpp:6
Dtk_SmartPtr::IsNULL
Dtk_bool IsNULL() const
Definition: util_ptr_dtk.hpp:118
CurrentMatrix
Dtk_transfo CurrentMatrix
Definition: WritePrototype.cpp:13
Dtk_SmartPtr< Dtk_Info >
PdfInitInstance
Dtk_ID PdfInitInstance(Dtk_ComponentPtr inComponent)
Definition: PdfWrite.cpp:93
WritePreview
void WritePreview(const Dtk_PreviewPtr &inPreview, const Dtk_string &inName)
Definition: WritePreview.cpp:4
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:574
IsXmlDumpActivated
Dtk_bool IsXmlDumpActivated()
Definition: XmlWrite.cpp:17
Dtk_Component::ComponentMissing
@ ComponentMissing
Definition: dtk_maindoc.hpp:562
XmlInitMainDoc
void XmlInitMainDoc(Dtk_MainDocPtr inMainDoc)
Definition: XmlWrite.cpp:49
dtkNoError
@ dtkNoError
Definition: error_dtk.hpp:151
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:8
DTK_UNUSED
#define DTK_UNUSED(a)
Definition: define.h:729
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