DATAKIT SDK  V2026.2
WriteComponent.cpp File Reference

Functions

Dtk_ErrorStatus WriteComponent (Dtk_ComponentPtr inComponent, const Dtk_transfo &inMatrix)
 

Function Documentation

◆ WriteComponent()

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