DATAKIT SDK  V2026.4
WriteParasolid.hpp File Reference

Go to the source code of this file.

Functions

Dtk_string PswGetLastOutputFile ()
 
const Dtk_tab< char > & PswGetLastStream ()
 
void PswWriteDocument (Dtk_MainDocPtr inDocument)
 
Dtk_ErrorStatus PswWriteEnd ()
 
Dtk_ErrorStatus PswWriteInit (const Dtk_string &inInputFile, const Dtk_string &inOutputFolder)
 
Dtk_ErrorStatus PswWriteInitMemory ()
 
void WriteBodyForParasolid (Dtk_BodyPtr inBody, Dtk_tab< Dtk_ID > &outIds)
 

Function Documentation

◆ PswGetLastOutputFile()

Dtk_string PswGetLastOutputFile ( )
26 {
27  return g_lastOutputFile;
28 }

◆ PswGetLastStream()

const Dtk_tab<char>& PswGetLastStream ( )
33 {
34  return g_lastStream;
35 }

◆ PswWriteDocument()

void PswWriteDocument ( Dtk_MainDocPtr  inDocument)
350 {
351  g_prototypeCache.clear();
352 
353  Dtk_ComponentPtr rootComponent = inDocument->RootComponent();
354  if( rootComponent.IsNULL() )
355  {
356  cout << "Warning: document has no root component." << endl;
357  return;
358  }
359 
360  // Always create a top-level Parasolid assembly to hold the entire document.
361  Dtk_ID rootAssemblyId = 0;
362  psw_CreateEmptyAssembly( rootAssemblyId );
363 
364  // Set the document/root name on the assembly.
365  Dtk_string docName = rootComponent->Name();
366  if( docName.len() > 0 )
367  {
368  Dtk_InfoPtr info = Dtk_Info::Create();
369  info->SetName( docName );
370  psw_CreateAttribut( rootAssemblyId, info );
371  }
372 
373  // Recursively traverse the component tree and fill the root assembly.
374  PswWriteComponent( rootComponent, rootAssemblyId );
375 
376  // Finalise the root assembly (must be called once all instances are added).
377  psw_LinkAttribut( rootAssemblyId );
378 
379  g_prototypeCache.clear();
380 }

◆ PswWriteEnd()

Dtk_ErrorStatus PswWriteEnd ( )
443 {
444  Dtk_tab<Dtk_ID> emptyIds; // Root assembly is implicit when the tab is empty.
445  Dtk_ErrorStatus err = psw_EndFile( emptyIds );
446  if( err != dtkNoError )
447  cout << "Warning: psw_EndFile failed: " << dtkTypeError( err ).c_str() << endl;
448  return err;
449 }

◆ PswWriteInit()

Dtk_ErrorStatus PswWriteInit ( const Dtk_string &  inInputFile,
const Dtk_string &  inOutputFolder 
)
389 {
390  g_prototypeCache.clear();
392 
393  Dtk_string outputFile;
394  if( inOutputFolder.len() > 0 )
395  outputFile = inOutputFolder + inInputFile.filename() + inInputFile.extension() + L"_psw.x_t";
396  else
397  outputFile = inInputFile + L"_psw.x_t";
398 
399  // Persist the output path so callers can retrieve it via PswGetLastOutputFile().
400  g_lastOutputFile = outputFile;
401 
402  const Dtk_bool muteMode = DTK_TRUE; // Suppress the psw internal log file.
403  Dtk_ErrorStatus err = psw_InitFile( outputFile, muteMode );
404 
405  if( err == dtkNoError )
406  cout << "Creating Parasolid file: " << outputFile.c_str() << endl;
407  else
408  cout << "Warning: psw_InitFile failed: " << dtkTypeError( err ).c_str() << endl;
409 
410  return err;
411 }

◆ PswWriteInitMemory()

Dtk_ErrorStatus PswWriteInitMemory ( )
420 {
421  g_prototypeCache.clear();
423  g_lastOutputFile = Dtk_string(); // no output file in memory mode
424 
425  const Dtk_bool muteMode = DTK_TRUE;
426  Dtk_ErrorStatus err = psw_InitFile( g_lastStream, muteMode );
427 
428  if( err == dtkNoError )
429  cout << "Parasolid writer: using in-memory stream (no .x_t file)." << endl;
430  else
431  cout << "Warning: psw_InitFile (memory) failed: " << dtkTypeError( err ).c_str() << endl;
432 
433  return err;
434 }

◆ WriteBodyForParasolid()

void WriteBodyForParasolid ( Dtk_BodyPtr  inBody,
Dtk_tab< Dtk_ID > &  outIds 
)
13 {
14 
15  Dtk_tab<Dtk_ID> bodyIds;
16 
17  DtkErrorStatus errf, err;
18 
19  Dtk_InfoPtr infBody = inBody->info();
20  if( infBody.IsNULL() )
21  {
22  infBody = Dtk_Info::Create();
23  }
24  // Add custom attributes to the body and its faces, edges, and vertices
25  // Custom attributes are stored in the Parasolid file and can be retrieved later
26  // For example, you can use the following code to retrieve the custom attributes from a Parasolid file:
27  infBody->AddCustomerDefinedAttribute( L"my_body_id", inBody->get_info()->GetId() );
28  // You can also add custom attributes as string
29  infBody->AddCustomerDefinedAttribute( L"my_feature", Dtk_string( L"created_from_feature" ) );
30 
31  for( size_t i = 0; i < inBody->GetNumTopologicalEntities( DTK_TYPE_UNKNOWN ); i++ )
32  {
33  Dtk_FacePtr face = Dtk_FacePtr::DtkDynamicCast( inBody->GetPtr( i ) );
34  if( face.IsNotNULL() && face->get_info().IsNotNULL() )
35  {
36  face->get_info()->AddCustomerDefinedAttribute( L"my_face_id", face->get_info()->GetId() );
37  }
38  Dtk_EdgePtr edge = Dtk_EdgePtr::DtkDynamicCast( inBody->GetPtr( i ) );
39  if( edge.IsNotNULL() && edge->get_info().IsNotNULL() )
40  {
41  edge->get_info()->AddCustomerDefinedAttribute( L"my_edge_id", edge->get_info()->GetId() );
42  }
43  Dtk_VertexPtr vertex = Dtk_VertexPtr::DtkDynamicCast( inBody->GetPtr( i ) );
44  if( vertex.IsNotNULL() && vertex->get_info().IsNotNULL() )
45  {
46  vertex->get_info()->AddCustomerDefinedAttribute( L"my_vertex_id", vertex->get_info()->GetId() );
47  }
48  }
49 
50 
51 
52  // Initialize the Parasolid file in memory
53  Dtk_tab<char> outParasolidStream;
54  errf = psw_InitFile( outParasolidStream, 0 );
55 
56  // Initialize the Parasolid file on disk (uncomment the following lines to write to disk)
57  // The filename is constructed using the body name and ID to ensure uniqueness
58  //Dtk_string myFilename = infBody->GetName() +"_";
59  //myFilename.add_int( infBody->GetId() );
60  //myFilename += "_myxbfile.x_b";
61  //errf = psw_InitFile( myFilename ,1,270);
62 
63  // Write the body to the Parasolid file or memory stream
64  err = psw_WriteBody( inBody, bodyIds );
65 
66  // End the Parasolid file and write the body IDs to the output file or memory stream
67  errf = psw_EndFile( bodyIds );
68 
69  // The following code is commented out because it requires the Parasolid kernel headers and libraries to be linked
70  // It allows to read the Parasolid file from memory and transmit it to an existing Parasolid session
71 
72  //// #include "Include_pskernel\parasolid_kernel.h"
73  //// PK_PART_receive_o_t recvOpts;
74  //// PK_PART_receive_o_m( recvOpts );
75  //// int nParts = 0;
76  //// PK_PART_t* parts = nullptr;
77 
78  //// PK_MEMORY_block_t block;
79  //// block.next = nullptr;
80  //// block.n_bytes = static_cast< size_t >( outParasolidStream.size() );
81  //// block.bytes = &outParasolidStream.GetArray()[ 0 ];
82 
83  ////// Load stream into Parasolid memory and receive the parts (bodies) from it
84  //// PK_ERROR_code_t pkErr = PK_PART_receive_b( block, &recvOpts, &nParts, &parts);
85 
86  ////// Control by dumping a file
87  ////PK_PART_transmit_o_t transmit_option;
88  ////PK_PART_transmit_o_m( transmit_option );
89  ////pkErr = PK_PART_transmit( nParts, parts, "xmtControlFile", &transmit_option );
90 
91 
92  if( err == dtkNoError )
93  {
94  for( Dtk_Size_t i = 0; i < bodyIds.size(); ++i )
95  outIds.push_back( bodyIds[ i ] );
96  }
97  else
98  {
99  cout << "Warning: psw_WriteBody failed: " << dtkTypeError( err ).c_str() << endl;
100  }
101 }
Dtk_Info::Create
static Dtk_SmartPtr< Dtk_Info > Create()
Calls default constructor to allocate a new object.
Dtk_Entity::get_info
Dtk_InfoPtr get_info() const
Dtk_ID
uint32_t Dtk_ID
Definition: define.h:688
PswWriteComponent
static void PswWriteComponent(Dtk_ComponentPtr inComponent, Dtk_ID inParentAssemblyId)
Definition: WriteParasolid.cpp:250
DTK_TYPE_UNKNOWN
@ DTK_TYPE_UNKNOWN
Definition: define.h:26
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_SmartPtr::IsNotNULL
Dtk_bool IsNotNULL() const
Definition: util_ptr_dtk.hpp:119
Dtk_Info::AddCustomerDefinedAttribute
Dtk_ErrorStatus AddCustomerDefinedAttribute(Dtk_string name, Dtk_Val val)
DTK_TRUE
#define DTK_TRUE
Definition: define.h:726
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:711
Dtk_bool
char Dtk_bool
Definition: define.h:724
g_lastOutputFile
static Dtk_string g_lastOutputFile
Definition: WriteParasolid.cpp:18
psw_CreateAttribut
DtkErrorStatus psw_CreateAttribut(Dtk_ID inIdCompo, Dtk_InfoPtr &info)
Create all attribut attached to a component (body, assembly, instance)
Dtk_string::filename
Dtk_string filename() const
File Utility : Retrieves the filename in Dtk_string form.
Dtk_SmartPtr< Dtk_Face >::DtkDynamicCast
static Dtk_SmartPtr< Dtk_Face > DtkDynamicCast(const Dtk_SmartPtr< T2 > &p)
Definition: util_ptr_dtk.hpp:101
Dtk_ErrorStatus
Dtk_ErrorStatus
Definition: error_dtk.hpp:6
Dtk_SmartPtr::IsNULL
Dtk_bool IsNULL() const
Definition: util_ptr_dtk.hpp:118
Dtk_SmartPtr
Definition: util_ptr_dtk.hpp:37
dtkTypeError
Dtk_string dtkTypeError(Dtk_Int32 errNumero)
g_prototypeCache
static map< Dtk_ID, Dtk_ID > g_prototypeCache
Definition: WriteParasolid.cpp:14
Dtk_string::c_str
const char * c_str() const
Retrieve the ASCII conversion string.
Dtk_string::extension
Dtk_string extension() const
File Utility : Retrieves the extension in Dtk_string form.
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_tab
This is a high level array class.
Definition: util_stl_dtk.hpp:84
Dtk_tab::size
Dtk_Size_t size() const
Returns the size of the array.
Definition: util_stl_dtk.hpp:503
g_lastStream
static Dtk_tab< char > g_lastStream
Definition: WriteParasolid.cpp:22
psw_CreateEmptyAssembly
DtkErrorStatus psw_CreateEmptyAssembly(Dtk_ID &outAssemblyId)
Create the entry item of an assembly.
Dtk_string::len
int len() const
Retrieve the length of the Dtk_string.
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.
dtkNoError
@ dtkNoError
Definition: error_dtk.hpp:151
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
Dtk_tab::clear
void clear(int no_delete=0)
Resets the Dtk_tab content.
Definition: util_stl_dtk.hpp:352
Dtk_Info::GetId
int GetId() const