DATAKIT SDK  V2026.2
testlibparasolidwrite.cpp File Reference

Functions

static Dtk_ErrorStatus AddAttributesToParasolidEntity (const Dtk_ID parasolidEntityId, Dtk_string name, const Dtk_Int32 id, const std::map< Dtk_string, Dtk_Val > &customAttributes)
 
static Dtk_ErrorStatus CreateAssemblyWithFollowingEntities (const Dtk_tab< Dtk_ID > &entityIds, Dtk_ID &outAssemblyId)
 
static Dtk_tab< Dtk_BodyPtrCreateThreeDtkBodies ()
 
int ParasolidWriteSample (const Dtk_string &inResultDirectory)
 Sample to start using Parasolid Writer Library. More...
 
static Dtk_ErrorStatus SampleWriteAnArrayOfParts (const Dtk_string &outputFile)
 
static Dtk_ErrorStatus SampleWriteAnAssembly (const Dtk_string &outputFile)
 

Function Documentation

◆ AddAttributesToParasolidEntity()

static Dtk_ErrorStatus AddAttributesToParasolidEntity ( const Dtk_ID  parasolidEntityId,
Dtk_string  name,
const Dtk_Int32  id,
const std::map< Dtk_string, Dtk_Val > &  customAttributes 
)
static
104 {
105  // Fill the information in a Dtk_Info.
106  Dtk_InfoPtr attributesDtkInfo = Dtk_Info::create();
107 
108  // In this example, we set both name and id. Obviously, it can only be one of them, or none.
109  attributesDtkInfo->SetName( std::move( name ) );
110  attributesDtkInfo->SetId( id );
111 
112  // You can define as many custom attributes as you want in the same Dtk_Info, just call AddCustomerDefinedAttribute for each of them.
113  for( const auto& customAttribute : customAttributes )
114  {
115  attributesDtkInfo->AddCustomerDefinedAttribute( customAttribute.first, customAttribute.second );
116  }
117 
118  // Then call psw_CreateAttribut on the entity.
119  return psw_CreateAttribut( parasolidEntityId, attributesDtkInfo );
120 }

◆ CreateAssemblyWithFollowingEntities()

static Dtk_ErrorStatus CreateAssemblyWithFollowingEntities ( const Dtk_tab< Dtk_ID > &  entityIds,
Dtk_ID outAssemblyId 
)
static
71 {
72  Dtk_ErrorStatus outStatus = dtkNoError;
73 
74  // We create an empty assembly.
75  outAssemblyId = 0;
76  outStatus = psw_CreateEmptyAssembly( outAssemblyId );
77 
78  // Creating an instance requires a transformation matrix.
79  Dtk_transfoPtr identityMatrix = Dtk_transfo::create(); // Identity matrix by default.
80  // We create an instance for each entity.
81  for( Dtk_Size_t i_entity = 0; i_entity < entityIds.size(); ++i_entity )
82  {
83  // The parameters are :
84  // - the assembly the instance will be added to
85  // - the entity we want to instantiate
86  // - a transformation matrix applied to the instance
87  // It will return as "out" parameter the instance id. You can use the instance to add attributes to it, for example.
88  Dtk_ID unusedInstanceId = 0;
89  outStatus = psw_CreateInstance( outAssemblyId, entityIds[ i_entity ], identityMatrix, unusedInstanceId );
90  }
91 
92  // psw_LinkAttribut needs to be called when all instances have been added to the assembly.
93  outStatus = psw_LinkAttribut( outAssemblyId );
94 
95  return outStatus;
96 }

◆ CreateThreeDtkBodies()

static Dtk_tab<Dtk_BodyPtr> CreateThreeDtkBodies ( )
static
17 {
18  Dtk_tab<Dtk_BodyPtr> dtkBodies( 3 );
19  dtkBodies.push_back( sampleWriter::CreateCube() );
20  dtkBodies.push_back( sampleWriter::CreateOpenShell() );
21  dtkBodies.push_back( sampleWriter::CreateCurves() );
22  return dtkBodies;
23 }

◆ ParasolidWriteSample()

int ParasolidWriteSample ( const Dtk_string inResultDirectory)

Sample to start using Parasolid Writer Library.

Parameters
[in]inResultDirectory: Directory where the resulting parasolid files will be stored.
Returns
dtkNoError if OK
226 {
227  Dtk_ErrorStatus outWriteStatus = dtkNoError;
228 
229  // Logs : Print in output that we are currently using Parasolid Write.
230  std::cout << std::endl << "----------------------------------------------" << std::endl;
231  std::cout << "Parasolid Write start" << std::endl;
232 
233  // Create output directory for parasolid files.
234  Dtk_string outputDirectory = inResultDirectory + L"Parasolid/";
235  outputDirectory.FixPathSeparator();
236  outputDirectory.mkdir();
237 
238  // Test different ways to write a parasolid file.
239  // 1) Write an array of parts.
240  outWriteStatus = SampleWriteAnArrayOfParts( outputDirectory + L"sample_listofbody.x_t" );
241  // 2) Write an assembly.
242  if( outWriteStatus == dtkNoError ) // Checks should not fail in the untouched sample.
243  outWriteStatus = SampleWriteAnAssembly( outputDirectory + L"sample_assemblyofbody.x_t" );
244 
245  // Logs : Print in output that we finished using Parasolid Write.
246  std::cout << "Parasolid Write end" << std::endl;
247 
248  return outWriteStatus;
249 }

◆ SampleWriteAnArrayOfParts()

static Dtk_ErrorStatus SampleWriteAnArrayOfParts ( const Dtk_string outputFile)
static
27 {
28  Dtk_ErrorStatus outStatus = dtkNoError;
29 
30  // Create an array of 3 Dtk_Body that we will write to a parasolid file.
32 
33  // First, we initialize the writer, by calling psw_InitFile with the path of the file that will be written.
34  const Dtk_bool muteMode = DTK_TRUE; // No log will be written.
35  outStatus = psw_InitFile( outputFile, muteMode );
36 
37  // Check if initialization went wrong.
38  if( outStatus != dtkNoError )
39  {
40  std::cout << "Error returned by psw_InitFile : " << dtkTypeError( outStatus ).c_str() << std::endl;
41  }
42  else
43  {
44  // Then, you just have to call psw_WriteBody for each Dtk_Body.
45  // You need to store the IDs of the parts created by the function for later use.
46  Dtk_tab<Dtk_ID> partIDs;
47  for( Dtk_Size_t i_body = 0; i_body < dtkBodies.size(); ++i_body )
48  {
49  outStatus = psw_WriteBody( dtkBodies[ i_body ], partIDs );
50  if( outStatus != dtkNoError )
51  std::cout << "Error returned by psw_WriteBody on body n°" << std::to_string( i_body ) << " : " << dtkTypeError( outStatus ).c_str() << std::endl;
52  }
53 
54  // In the end, to finish writing the file, call psw_EndFile.
55  // If the parasolid file is an array of parts, then all part IDs need to be given to psw_EndFile.
56  outStatus = psw_EndFile( partIDs );
57 
58  // Log success or error.
59  if( outStatus == dtkNoError )
60  std::cout << "=> " << outputFile.c_str() << std::endl;
61  else
62  std::cout << "Error returned by psw_EndFile : " << dtkTypeError( outStatus ).c_str() << std::endl;
63  }
64 
65  return outStatus;
66 }

◆ SampleWriteAnAssembly()

static Dtk_ErrorStatus SampleWriteAnAssembly ( const Dtk_string outputFile)
static
124 {
125  Dtk_ErrorStatus outStatus = dtkNoError;
126 
127  // Create an array of 3 Dtk_Body that we will write to a parasolid file.
128  // We will write an assembly with those 3 parts, each instantiated 2 times.
130  const Dtk_Size_t nbBodies = dtkBodies.size();
131 
132  // First, we initialize the writer, by calling psw_InitFile with the path of the file that will be written.
133  const Dtk_bool muteMode = DTK_TRUE; // No log will be written.
134  outStatus = psw_InitFile( outputFile, muteMode );
135 
136  // We create a part or a sub-assembly for each Dtk_Body.
137  // We keep the IDs of the entities we created to link them to the assembly root node later.
138  Dtk_tab<Dtk_ID> partIDs( nbBodies );
139  for( Dtk_Size_t i_body = 0; i_body < nbBodies; ++i_body )
140  {
141  // We call psw_WriteBody on the current Dtk_Body.
142  Dtk_tab<Dtk_ID> returnedBodyIDs;
143  outStatus = psw_WriteBody( dtkBodies[ i_body ], returnedBodyIDs );
144 
145  // An array is returned because one Dtk_Body may be written as several parasolid entities.
146  // If more than one entity is created, we make a sub-assembly to have them together.
147  if( returnedBodyIDs.size() == 1 )
148  {
149  partIDs[ i_body ] = returnedBodyIDs.front();
150  }
151  else if( returnedBodyIDs.size() > 1 )
152  {
153  Dtk_ID subAssemblyId = 0;
154  outStatus = CreateAssemblyWithFollowingEntities( returnedBodyIDs, subAssemblyId );
155 
156  partIDs[ i_body ] = subAssemblyId;
157  }
158  else
159  {
160  // No parasolid entity has been created, we have an error.
161  partIDs[ i_body ] = 0; // 0 is an invalid ID
162  std::cout << "Error returned by psw_WriteBody : " << dtkTypeError( outStatus ).c_str() << std::endl;
163  }
164 
165  // Optional : Add attributes to the entity.
166  Dtk_string partName = L"DtkPart";
167  partName.add_int( ( int )i_body );
168  // Also add a custom attribute.
169  std::map<Dtk_string, Dtk_Val> customAttributesForPart;
170  customAttributesForPart.insert( std::make_pair( L"MyAttribute", Dtk_Val( L"MyValue" ) ) );
171  AddAttributesToParasolidEntity( partIDs[ i_body ], std::move( partName ), Dtk_Int32( i_body + 1 ), customAttributesForPart );
172  }
173 
174  // Now, we create the root assembly of our sample.
175  Dtk_ID rootAssemblyID = 0;
176  outStatus = psw_CreateEmptyAssembly( rootAssemblyID );
177  // Optional : Add attributes to the assembly.
178  outStatus = AddAttributesToParasolidEntity( rootAssemblyID, L"DtkAssembly", 5, std::map<Dtk_string, Dtk_Val>() );
179 
180  // We will create 2 instances of each parasolid entity that was a Dtk_Body (part or sub-assembly).
181  for( Dtk_Size_t i_bodyEntity = 0; i_bodyEntity < nbBodies; ++i_bodyEntity )
182  {
183  for( Dtk_Size_t i_instance = 0; i_instance < 2; ++i_instance )
184  {
185  // We need an matrix for the instance.
187  if( i_instance == 0 )
188  matrix->setOrigin( Dtk_pnt( 100.0, 0.0, 0.0 ) );
189  else
190  matrix->setOrigin( Dtk_pnt( 100.0, 100.0, 100.0 ) );
191 
192  // We create the parasolid instance in the root assembly.
193  Dtk_ID instanceId = 0;
194  outStatus = psw_CreateInstance( rootAssemblyID, partIDs[ i_bodyEntity ], matrix, instanceId );
195 
196  // Optional :Add attributes to the instance.
197  const Dtk_Size_t uniqueNumberForAttributesSample = i_bodyEntity + nbBodies * i_instance;
198  Dtk_string instanceName = L"DtkInstance";
199  instanceName.add_int( ( int )( uniqueNumberForAttributesSample ) );
200  const Dtk_Int32 instanceID = Dtk_Int32( 10 + uniqueNumberForAttributesSample );
201  outStatus = AddAttributesToParasolidEntity( instanceId, std::move( instanceName ), instanceID, std::map<Dtk_string, Dtk_Val>() );
202  }
203  }
204 
205  // We end the root assembly by calling psw_LinkAttribut on it.
206  outStatus = psw_LinkAttribut( rootAssemblyID );
207 
208  // In the end, to finish writing the file, call psw_EndFile.
209  // The array of IDs passed to the function is empty if we write an assembly.
210  Dtk_tab<Dtk_ID> emptyArray;
211  outStatus = psw_EndFile( emptyArray );
212 
213  // Log success or error.
214  if( outStatus == dtkNoError )
215  std::cout << "=> " << outputFile.c_str() << std::endl;
216  else
217  std::cout << "Error returned by psw_EndFile : " << dtkTypeError( outStatus ).c_str() << std::endl;
218 
219  return outStatus;
220 }
Dtk_ID
uint32_t Dtk_ID
Definition: define.h:681
Dtk_Info::SetName
Dtk_ErrorStatus SetName(Dtk_string inName)
psw_InitFile
DtkErrorStatus psw_InitFile(const Dtk_string &inFileOut, Dtk_bool muteMode, Dtk_Int32 inVersion=180)
Initialise a file to be written.
Dtk_Info::AddCustomerDefinedAttribute
Dtk_ErrorStatus AddCustomerDefinedAttribute(Dtk_string name, Dtk_Val val)
Dtk_transfo::create
static Dtk_SmartPtr< Dtk_transfo > create()
DTK_TRUE
#define DTK_TRUE
Definition: define.h:719
psw_LinkAttribut
DtkErrorStatus psw_LinkAttribut(Dtk_ID inAssemblyId)
link all attributes attached to an assembly
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
SampleWriteAnArrayOfParts
static Dtk_ErrorStatus SampleWriteAnArrayOfParts(const Dtk_string &outputFile)
Definition: testlibparasolidwrite.cpp:26
Dtk_bool
char Dtk_bool
Definition: define.h:717
CreateThreeDtkBodies
static Dtk_tab< Dtk_BodyPtr > CreateThreeDtkBodies()
Definition: testlibparasolidwrite.cpp:16
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)
psw_CreateAttribut
DtkErrorStatus psw_CreateAttribut(Dtk_ID inIdCompo, Dtk_InfoPtr &info)
Create all attribut attached to a component (body, assembly, instance)
Dtk_Val
Definition: dtk_val.hpp:67
Dtk_Int32
int32_t Dtk_Int32
Definition: define.h:679
Dtk_tab::front
T & front()
Return the elements at the beginning of the array.
Definition: util_stl_dtk.hpp:568
Dtk_ErrorStatus
Dtk_ErrorStatus
Definition: error_dtk.hpp:6
Dtk_Info::SetId
Dtk_ErrorStatus SetId(const Dtk_Int32 &inId)
Dtk_SmartPtr< Dtk_Info >
dtkTypeError
Dtk_string dtkTypeError(Dtk_Int32 errNumero)
psw_CreateInstance
DtkErrorStatus psw_CreateInstance(Dtk_ID inAssemblyId, Dtk_ID inIdPart, Dtk_transfoPtr &Matrix, Dtk_ID &outIdInst)
Create an Instance item of a part referenced by inIdPart.
Dtk_string::c_str
const char * c_str() const
Retrieve the ASCII conversion string.
Dtk_string::mkdir
int mkdir() const
File Utility : Create a Directory.
Dtk_pnt
This is a mathematical point class.
Definition: dtk_pnt.hpp:20
psw_WriteBody
Dtk_ErrorStatus psw_WriteBody(Dtk_BodyPtr &inBody)
Writes a Body of a 3D part (3D content of a product), of any kind (solid, shell / faces,...
Dtk_string::FixPathSeparator
void FixPathSeparator()
File Utility : Fixes path separator consistency. It lets you replace the '\' or '/' by the OS needed ...
Dtk_tab
This is a high level array class.
Definition: util_stl_dtk.hpp:84
sampleWriter::CreateCube
Dtk_BodyPtr CreateCube()
Definition: testcreatecube.cpp:1316
Dtk_tab::size
Dtk_Size_t size() const
Returns the size of the array.
Definition: util_stl_dtk.hpp:503
psw_CreateEmptyAssembly
DtkErrorStatus psw_CreateEmptyAssembly(Dtk_ID &outAssemblyId)
Create the entry item of an assembly.
psw_EndFile
DtkErrorStatus psw_EndFile(Dtk_tab< Dtk_ID > &inTabIds, Dtk_bool binForceListOfBody=DTK_FALSE)
Ends the writing of the Parasolid (x_t or x_b) file.
sampleWriter::CreateCurves
Dtk_BodyPtr CreateCurves()
Definition: testcreatecube.cpp:1357
AddAttributesToParasolidEntity
static Dtk_ErrorStatus AddAttributesToParasolidEntity(const Dtk_ID parasolidEntityId, Dtk_string name, const Dtk_Int32 id, const std::map< Dtk_string, Dtk_Val > &customAttributes)
Definition: testlibparasolidwrite.cpp:100
dtkNoError
@ dtkNoError
Definition: error_dtk.hpp:149
CreateAssemblyWithFollowingEntities
static Dtk_ErrorStatus CreateAssemblyWithFollowingEntities(const Dtk_tab< Dtk_ID > &entityIds, Dtk_ID &outAssemblyId)
Definition: testlibparasolidwrite.cpp:69
SampleWriteAnAssembly
static Dtk_ErrorStatus SampleWriteAnAssembly(const Dtk_string &outputFile)
Definition: testlibparasolidwrite.cpp:123
Dtk_Info::create
static Dtk_SmartPtr< Dtk_Info > create()
Calls default constructor to allocate a new object.
sampleWriter::CreateOpenShell
Dtk_BodyPtr CreateOpenShell()
Definition: testcreatecube.cpp:1343