DATAKIT SDK  V2026.2
Sample
//start_of_mainxmtwrite_dtk
/////////////////////////////////////////////////////
#include "testwriters.h"
// Required DATAKIT headers
#include "datakit.h"
#include "psw/psw.hpp" // Parasolid write specific functions
// Include set of functions that provides Dtk objects to test writers.
#include <map>
// Create an array of 3 different Dtk_Body.
{
Dtk_tab<Dtk_BodyPtr> dtkBodies( 3 );
return dtkBodies;
}
// Sample function to show how to write a parasolid file that is an array of parts.
{
// Create an array of 3 Dtk_Body that we will write to a parasolid file.
// First, we initialize the writer, by calling psw_InitFile with the path of the file that will be written.
const Dtk_bool muteMode = DTK_TRUE; // No log will be written.
outStatus = psw_InitFile( outputFile, muteMode );
// Check if initialization went wrong.
if( outStatus != dtkNoError )
{
std::cout << "Error returned by psw_InitFile : " << dtkTypeError( outStatus ).c_str() << std::endl;
}
else
{
// Then, you just have to call psw_WriteBody for each Dtk_Body.
// You need to store the IDs of the parts created by the function for later use.
Dtk_tab<Dtk_ID> partIDs;
for( Dtk_Size_t i_body = 0; i_body < dtkBodies.size(); ++i_body )
{
outStatus = psw_WriteBody( dtkBodies[ i_body ], partIDs );
if( outStatus != dtkNoError )
std::cout << "Error returned by psw_WriteBody on body n°" << std::to_string( i_body ) << " : " << dtkTypeError( outStatus ).c_str() << std::endl;
}
// In the end, to finish writing the file, call psw_EndFile.
// If the parasolid file is an array of parts, then all part IDs need to be given to psw_EndFile.
outStatus = psw_EndFile( partIDs );
// Log success or error.
if( outStatus == dtkNoError )
std::cout << "=> " << outputFile.c_str() << std::endl;
else
std::cout << "Error returned by psw_EndFile : " << dtkTypeError( outStatus ).c_str() << std::endl;
}
return outStatus;
}
// Create an assembly containing instances of the parasolid entities passed in parameters.
Dtk_ID& outAssemblyId )
{
// We create an empty assembly.
outAssemblyId = 0;
outStatus = psw_CreateEmptyAssembly( outAssemblyId );
// Creating an instance requires a transformation matrix.
Dtk_transfoPtr identityMatrix = Dtk_transfo::create(); // Identity matrix by default.
// We create an instance for each entity.
for( Dtk_Size_t i_entity = 0; i_entity < entityIds.size(); ++i_entity )
{
// The parameters are :
// - the assembly the instance will be added to
// - the entity we want to instantiate
// - a transformation matrix applied to the instance
// It will return as "out" parameter the instance id. You can use the instance to add attributes to it, for example.
Dtk_ID unusedInstanceId = 0;
outStatus = psw_CreateInstance( outAssemblyId, entityIds[ i_entity ], identityMatrix, unusedInstanceId );
}
// psw_LinkAttribut needs to be called when all instances have been added to the assembly.
outStatus = psw_LinkAttribut( outAssemblyId );
return outStatus;
}
// We can add attributes to a parasolid entity.
// Standard attributes such as name and id can be set, but also custom attributes defined by the user.
static Dtk_ErrorStatus AddAttributesToParasolidEntity( const Dtk_ID parasolidEntityId,
Dtk_string name,
const Dtk_Int32 id,
const std::map<Dtk_string, Dtk_Val>& customAttributes )
{
// Fill the information in a Dtk_Info.
Dtk_InfoPtr attributesDtkInfo = Dtk_Info::create();
// In this example, we set both name and id. Obviously, it can only be one of them, or none.
attributesDtkInfo->SetName( std::move( name ) );
attributesDtkInfo->SetId( id );
// You can define as many custom attributes as you want in the same Dtk_Info, just call AddCustomerDefinedAttribute for each of them.
for( const auto& customAttribute : customAttributes )
{
attributesDtkInfo->AddCustomerDefinedAttribute( customAttribute.first, customAttribute.second );
}
// Then call psw_CreateAttribut on the entity.
return psw_CreateAttribut( parasolidEntityId, attributesDtkInfo );
}
// Sample function to show how to write a parasolid file that is an assembly.
{
// Create an array of 3 Dtk_Body that we will write to a parasolid file.
// We will write an assembly with those 3 parts, each instantiated 2 times.
const Dtk_Size_t nbBodies = dtkBodies.size();
// First, we initialize the writer, by calling psw_InitFile with the path of the file that will be written.
const Dtk_bool muteMode = DTK_TRUE; // No log will be written.
outStatus = psw_InitFile( outputFile, muteMode );
// We create a part or a sub-assembly for each Dtk_Body.
// We keep the IDs of the entities we created to link them to the assembly root node later.
Dtk_tab<Dtk_ID> partIDs( nbBodies );
for( Dtk_Size_t i_body = 0; i_body < nbBodies; ++i_body )
{
// We call psw_WriteBody on the current Dtk_Body.
Dtk_tab<Dtk_ID> returnedBodyIDs;
outStatus = psw_WriteBody( dtkBodies[ i_body ], returnedBodyIDs );
// An array is returned because one Dtk_Body may be written as several parasolid entities.
// If more than one entity is created, we make a sub-assembly to have them together.
if( returnedBodyIDs.size() == 1 )
{
partIDs[ i_body ] = returnedBodyIDs.front();
}
else if( returnedBodyIDs.size() > 1 )
{
Dtk_ID subAssemblyId = 0;
outStatus = CreateAssemblyWithFollowingEntities( returnedBodyIDs, subAssemblyId );
partIDs[ i_body ] = subAssemblyId;
}
else
{
// No parasolid entity has been created, we have an error.
partIDs[ i_body ] = 0; // 0 is an invalid ID
std::cout << "Error returned by psw_WriteBody : " << dtkTypeError( outStatus ).c_str() << std::endl;
}
// Optional : Add attributes to the entity.
Dtk_string partName = L"DtkPart";
partName.add_int( ( int )i_body );
// Also add a custom attribute.
std::map<Dtk_string, Dtk_Val> customAttributesForPart;
customAttributesForPart.insert( std::make_pair( L"MyAttribute", Dtk_Val( L"MyValue" ) ) );
AddAttributesToParasolidEntity( partIDs[ i_body ], std::move( partName ), Dtk_Int32( i_body + 1 ), customAttributesForPart );
}
// Now, we create the root assembly of our sample.
Dtk_ID rootAssemblyID = 0;
outStatus = psw_CreateEmptyAssembly( rootAssemblyID );
// Optional : Add attributes to the assembly.
outStatus = AddAttributesToParasolidEntity( rootAssemblyID, L"DtkAssembly", 5, std::map<Dtk_string, Dtk_Val>() );
// We will create 2 instances of each parasolid entity that was a Dtk_Body (part or sub-assembly).
for( Dtk_Size_t i_bodyEntity = 0; i_bodyEntity < nbBodies; ++i_bodyEntity )
{
for( Dtk_Size_t i_instance = 0; i_instance < 2; ++i_instance )
{
// We need an matrix for the instance.
if( i_instance == 0 )
matrix->setOrigin( Dtk_pnt( 100.0, 0.0, 0.0 ) );
else
matrix->setOrigin( Dtk_pnt( 100.0, 100.0, 100.0 ) );
// We create the parasolid instance in the root assembly.
Dtk_ID instanceId = 0;
outStatus = psw_CreateInstance( rootAssemblyID, partIDs[ i_bodyEntity ], matrix, instanceId );
// Optional :Add attributes to the instance.
const Dtk_Size_t uniqueNumberForAttributesSample = i_bodyEntity + nbBodies * i_instance;
Dtk_string instanceName = L"DtkInstance";
instanceName.add_int( ( int )( uniqueNumberForAttributesSample ) );
const Dtk_Int32 instanceID = Dtk_Int32( 10 + uniqueNumberForAttributesSample );
outStatus = AddAttributesToParasolidEntity( instanceId, std::move( instanceName ), instanceID, std::map<Dtk_string, Dtk_Val>() );
}
}
// We end the root assembly by calling psw_LinkAttribut on it.
outStatus = psw_LinkAttribut( rootAssemblyID );
// In the end, to finish writing the file, call psw_EndFile.
// The array of IDs passed to the function is empty if we write an assembly.
Dtk_tab<Dtk_ID> emptyArray;
outStatus = psw_EndFile( emptyArray );
// Log success or error.
if( outStatus == dtkNoError )
std::cout << "=> " << outputFile.c_str() << std::endl;
else
std::cout << "Error returned by psw_EndFile : " << dtkTypeError( outStatus ).c_str() << std::endl;
return outStatus;
}
//! \brief Sample to start using Parasolid Writer Library
//! \param [in] inResultDirectory : Directory where the resulting parasolid files will be stored.
//! \return dtkNoError if OK
int ParasolidWriteSample( const Dtk_string& inResultDirectory )
{
Dtk_ErrorStatus outWriteStatus = dtkNoError;
// Logs : Print in output that we are currently using Parasolid Write.
std::cout << std::endl << "----------------------------------------------" << std::endl;
std::cout << "Parasolid Write start" << std::endl;
// Create output directory for parasolid files.
Dtk_string outputDirectory = inResultDirectory + L"Parasolid/";
outputDirectory.FixPathSeparator();
outputDirectory.mkdir();
// Test different ways to write a parasolid file.
// 1) Write an array of parts.
outWriteStatus = SampleWriteAnArrayOfParts( outputDirectory + L"sample_listofbody.x_t" );
// 2) Write an assembly.
if( outWriteStatus == dtkNoError ) // Checks should not fail in the untouched sample.
outWriteStatus = SampleWriteAnAssembly( outputDirectory + L"sample_assemblyofbody.x_t" );
// Logs : Print in output that we finished using Parasolid Write.
std::cout << "Parasolid Write end" << std::endl;
return outWriteStatus;
}
//end_of_mainxmtwrite_dtk
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
psw.hpp
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
ParasolidWriteSample
int ParasolidWriteSample(const Dtk_string &inResultDirectory)
Sample to start using Parasolid Writer Library.
Definition: testlibparasolidwrite.cpp:225
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
Definition: util_ptr_dtk.hpp:37
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 ...
datakit.h
testcreatecube.hpp
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
Dtk_tab::push_back
void push_back(const T &x)
Inserts an element at the end of the array.
Definition: util_stl_dtk.hpp:416
testwriters.h
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