DATAKIT SDK  V2026.4
WriteBodyToOccBinaryObject.cpp File Reference

Functions

int GetTesselationStatus ()
 
static void PrintOccVersion ()
 
void WriteBody (Dtk_NodePtr inNode)
 
void WriteDtk_Mesh (const Dtk_MeshPtr &inMeshToWrite)
 

Variables

Dtk_transfo CurrentMatrix
 

Function Documentation

◆ GetTesselationStatus()

int GetTesselationStatus ( )
12 {
13  return TessStatus;
14 }

◆ PrintOccVersion()

static void PrintOccVersion ( )
static
30 {
31 #if defined(OCC_VERSION_COMPLETE)
32  std::cout << "OpenCascade version: " << OCC_VERSION_COMPLETE << std::endl;
33 #elif defined(OCC_VERSION_STRING)
34  std::cout << "OpenCascade version: " << OCC_VERSION_STRING << std::endl;
35 #elif defined(OCC_VERSION_MAJOR) && defined(OCC_VERSION_MINOR) && defined(OCC_VERSION_MAINTENANCE)
36  std::cout << "OpenCascade version: "
37  << OCC_VERSION_MAJOR << "."
38  << OCC_VERSION_MINOR << "."
39  << OCC_VERSION_MAINTENANCE << std::endl;
40 #else
41  std::cout << "OpenCascade version macro not available in this OCC headers set." << std::endl;
42 #endif
43 }

◆ WriteBody()

void WriteBody ( Dtk_NodePtr  inNode)
47 {
48  // Get if the node represent infinite geometry
49  // 1 = Infinite, 0 = Finite
50  int NodeInfiniteGeometry = inNode->GetInfos()->GetInfiniteGeometryFlag();
51  if (NodeInfiniteGeometry == 0)
52  {
53  //Calling both methods GetDtk_MeshPtr() and GetDtk_BodyPtr() on BodyNode will give you the same result
54  //Choose the one you need in order to avoid duplicated data
55  const Dtk_BodyPtr TmpBody = inNode->GetDtk_BodyPtr();
56  if (TmpBody.IsNotNULL())
57  {
58  // Used to write an XML File
59  if (IsXmlDumpActivated())
60  {
61  XmlWriteBody(TmpBody);
62  }
63 
64  static bool occVersionPrinted = false;
65  if( !occVersionPrinted )
66  {
68  occVersionPrinted = true;
69  }
70 
71  // TEST OCC
72  // For each body , write a BREP file (OCC format)
73  DKOC_Body2OCCBRep B2OC;
74  TopoDS_Shape res;
77  Dtk_ErrorStatus stat = B2OC.ConvertBody(TmpBody);
78  res = B2OC.GetMainShape();
79  if (res.IsNull())
80  {
81  std::cout << "WriteBodyOcc Error : " << dtkTypeError(stat).c_str() << std::endl;
82  return;
83  }
84 
85  // Name of BREP file : with a number
86  static int numbrep = 0;
87  numbrep++;
88 
89  Dtk_InfoPtr bodyinfo = TmpBody->get_info();
90  Dtk_string bodyname;
91  if (bodyinfo.IsNotNULL()) bodyname = bodyinfo->GetName();
92  if (bodyname.len() == 0)
93  {
94  bodyinfo = inNode->GetInfos();
95  if (bodyinfo.IsNotNULL()) bodyname = bodyinfo->GetName();
96  if (bodyname.len() == 0) bodyname = "???";
97  }
98 
99  std::cout << "Writing ToOpenCascade Body " << numbrep << " name: " << bodyname.c_str() << std::endl;
100 
101  Dtk_string filebrep = "body";
102  filebrep += "_";
103  filebrep.add_int(numbrep);
104  filebrep += ".brep";
105 
106  // Write the brep file
107  BRepTools::Write(res, filebrep.c_str());
108 
109  // How to get a TopoDS_Shape corresponding to a Dtk_Entity from its ID ( get_info()->GetId() )
110  // Useful to resolve Dtk_Connector
111  // This code gets the ID of the first face and then getting the corresponding TopoDS_Shape
113  Dtk_VolumePtr Vol;
115  Dtk_FacePtr Face;
116  Dtk_bool orient;
117  TopoDS_Shape facewithid;
118  TmpBody->GetLump(0, Lump);
119  if (Lump.IsNotNULL())
120  {
121  Lump->GetVolume(0, Vol);
122  if (Vol.IsNotNULL())
123  {
124  Vol->GetShell(0, Shell);
125  if (Shell.IsNotNULL())
126  {
127  Shell->GetFace(0, Face, orient);
128  if (Face.IsNotNULL())
129  {
130  Dtk_ID faceid = Face->get_info()->GetId();
131  Dtk_string SampleBrepFileForFace = "facewithid";
132  SampleBrepFileForFace.add_int(faceid);
133  SampleBrepFileForFace += ".brep";
134  facewithid = B2OC.ShapeFromDtkID(faceid);
135  BRepTools::Write(facewithid, SampleBrepFileForFace.c_str());
136  }
137  }
138  }
139  }
140 
141  // How to get the Dtk_Entity corresponding to a TopoDS_Shape
142  // This code show how to retrieve Dtk_Entity form a TopoDS_Shape
143  if( !facewithid.IsNull() )
144  {
145  Dtk_ID DatakitFaceID = B2OC.GetDtkID( B2OC.GetShapeID( facewithid ) );
146  Dtk_tab<Dtk_EntityPtr> CorrespondingFaces;
147  Dtk_ErrorStatus err = TmpBody->GetEntities( DatakitFaceID, CorrespondingFaces );
148 
149  //However, if you are looking for a Dtk_TopologialEntity, we recommand to use TmpBody->GetPtr which provides a direct access to the entity through its internal index in the Dtk_Body.
150  //To get this, you first have to construct a map from the Dtk_Body with the method FillTopologicalEntitiesIdMapping.
151  //It fills a map between Topological Entity identifiers (Dtk_ID from Dtk_Info::GetId) and the indices of corresponding entities within the Dtk_Body.
152  //While GetEntities is looking for the Dtk_Entity(s) corresponding to a Dtk_ID by browsing all the entities of the body, GetPtr is directly looking for the Dtk_TopologicalEntity pointer in the Dtk_Body using its index.
153  std::unordered_map<Dtk_ID, std::vector<Dtk_Size_t>> map;
154  TmpBody->FillTopologicalEntitiesIdMapping( map );
155 
156  Dtk_tab<Dtk_TopologicalEntityPtr> CorrespondingFacesWithGetPtr;
157  if( map.count( DatakitFaceID ) > 0 )
158  {
159  for( Dtk_Size_t i = 0; i < map.at( DatakitFaceID ).size(); ++i )
160  {
161  CorrespondingFacesWithGetPtr.push_back( TmpBody->GetPtr( map.at( DatakitFaceID )[ i ] ) );
162  }
163  }
164  }
165 
166  // END OCC conversion
167  }
168  }
169 }

◆ WriteDtk_Mesh()

void WriteDtk_Mesh ( const Dtk_MeshPtr &  inMeshToWrite)
9 {
10  // convert triangles-strip, fans, polygons into simple triangles
11  inMeshToWrite->explode();
12 
13  // Used to write a PDF File
14  if (IsPdfDumpActivated())
15  {
17  }
18 
19  // Used to write an XML File
20  if (IsXmlDumpActivated())
21  {
22  XmlWriteMesh(inMeshToWrite);
23  }
24 
25  Dtk_RGB rgb = inMeshToWrite->get_mesh_color(); // mesh global color.
26 
27  // vertices :
28  Dtk_Size_t nbvertices = inMeshToWrite->get_nb_vertices();
29  Dtk_Size_t i, j;
30  for (i = 0; i < nbvertices; i++)
31  {
32  Dtk_pnt pnt;
33  Dtk_dir dir;
34  Dtk_RGB color;
35  float u, v;
36  u = v = 0.0;
37  inMeshToWrite->get_vertex(i, &pnt); // coordinates of the vertices
38  if (inMeshToWrite->has_normals())
39  inMeshToWrite->get_normal(i, &dir); // vertex normal (if enabled)
40  if (inMeshToWrite->has_colors())
41  inMeshToWrite->get_color(i, &color); // vertex color (if enabled)
42  if (inMeshToWrite->has_texcoords())
43  {
44  u = inMeshToWrite->GetUTexture(i); // vertex u,v texture coordinate (if enabled)
45  v = inMeshToWrite->GetVTexture(i);
46  }
47  }
48  // mesh_faces
49  Dtk_Size_t k, m, nbmeshfaces = inMeshToWrite->get_nb_mesh_face();
50  for (i = 0; i < nbmeshfaces; i++)
51  {
52  Dtk_mesh_face* mf = inMeshToWrite->get_mesh_face(i);
53  Dtk_Size_t nbtriangles = mf->get_nbtriangles();
54  Dtk_Int32 trianglesindices[3];
55  for (j = 0; j < nbtriangles; j++)
56  {
57  const Dtk_UInt32* tri = mf->get_triangle_indices(j);
58  trianglesindices[0] = tri[0];
59  trianglesindices[1] = tri[1];
60  trianglesindices[2] = tri[2];
61  // id of vertex in vertices table you read in the first step of this function
62  }
63 
64  //face boundaries
65  Dtk_Size_t nbpolyline = mf->get_nbpolylines();
66  for (j = 0; j < nbpolyline; j++)
67  {
68  const Dtk_tab<Dtk_UInt32> *pnt;
69  pnt = mf->get_polyline_indices(j);
70  Dtk_Size_t nbvertex = pnt->size();
71  for (k = 0; k < nbvertex; k++)
72  {
73  m = (*pnt)[k]; // id of vertex in vertices table
74  Dtk_pnt pt = inMeshToWrite->get_vertex(m);
75  }
76  }
77  }
78 }

Variable Documentation

◆ CurrentMatrix

Dtk_transfo CurrentMatrix
extern
Dtk_Entity::get_info
Dtk_InfoPtr get_info() const
Dtk_ID
uint32_t Dtk_ID
Definition: define.h:688
PdfWriteEntity
void PdfWriteEntity(Dtk_EntityPtr inEntity)
Definition: PdfWrite.cpp:176
Dtk_mesh_face
This is a high level face class.
Definition: util_mesh_dtk.hpp:1076
Dtk_Info::GetName
Dtk_string GetName() const
Retrieves the entity name.
XmlWriteMesh
void XmlWriteMesh(const Dtk_MeshPtr &inMeshToWrite)
Definition: XmlWrite.cpp:73
Dtk_SmartPtr::IsNotNULL
Dtk_bool IsNotNULL() const
Definition: util_ptr_dtk.hpp:119
DKOC_Body2OCCBRep::ConvertBody
Dtk_ErrorStatus ConvertBody(const Dtk_BodyPtr &inBody)
Convert a Dtk_Body to set of TopoDS_Shape representing all topological entities.
sampleWriter::Shell
@ Shell
Definition: testcreatecube.cpp:20
DTK_TRUE
#define DTK_TRUE
Definition: define.h:726
PrintOccVersion
static void PrintOccVersion()
Definition: WriteBodyToOccBinaryObject.cpp:29
DKOC_Body2OCCBRep
Class DKOC_Body2OCCBRep This class allow the conversion from a Dtk_BodyPtr to a set of TopoDS_Shape b...
Definition: DKOC/DKOC_Body2OCCBRep.hpp:14
Dtk_UInt32
uint32_t Dtk_UInt32
Definition: define.h:687
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_mesh_face::get_polyline_indices
const Dtk_tab< Dtk_UInt32 > * get_polyline_indices(Dtk_Size_t inI) const
Get inI-th polygon.
Dtk_bool
char Dtk_bool
Definition: define.h:724
Dtk_mesh_face::get_nbpolylines
Dtk_Size_t get_nbpolylines() const
Get the number of polylines kept in the class instance.
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)
DKOC_Body2OCCBRep::GetDtkID
int GetDtkID(const int inShapeID) const
Get Dtk_ID from a ShapeID.
DKOC_Body2OCCBRep::SetConfigShapeFix
void SetConfigShapeFix(const Dtk_bool inMode)
Option to activate ShapeFix or not (activated by default)
Dtk_Int32
int32_t Dtk_Int32
Definition: define.h:686
Dtk_mesh_face::get_triangle_indices
const Dtk_UInt32 * get_triangle_indices(Dtk_Size_t inI) const
Get pointer of triangle indices of i-th triangle.
DKOC_Body2OCCBRep::ShapeFromDtkID
TopoDS_Shape ShapeFromDtkID(const int inDtkID) const
Get Shape with inDtkID stored in Dtk_Entity->getinfo()->GetID()
Dtk_SmartPtr< Dtk_Entity >::DtkDynamicCast
static Dtk_SmartPtr< Dtk_Entity > DtkDynamicCast(const Dtk_SmartPtr< T2 > &p)
Definition: util_ptr_dtk.hpp:101
DKOC_Body2OCCBRep::SetConfigKeepPcurves
void SetConfigKeepPcurves(const Dtk_bool inMode)
Option to keep existing pcurves or not (activated by default)
DKOC_Body2OCCBRep::GetMainShape
TopoDS_Shape GetMainShape() const
Get the highest level TopoDS_Shape shape.
Dtk_ErrorStatus
Dtk_ErrorStatus
Definition: error_dtk.hpp:6
Dtk_SmartPtr
Definition: util_ptr_dtk.hpp:37
dtkTypeError
Dtk_string dtkTypeError(Dtk_Int32 errNumero)
Dtk_string::c_str
const char * c_str() const
Retrieve the ASCII conversion string.
sampleWriter::Lump
@ Lump
Definition: testcreatecube.cpp:18
XmlWriteBody
void XmlWriteBody(const Dtk_BodyPtr &inBody)
Definition: XmlWrite.cpp:128
Dtk_pnt
This is a mathematical point class.
Definition: dtk_pnt.hpp:20
Dtk_mesh_face::get_nbtriangles
Dtk_Size_t get_nbtriangles() const
Get the number of simple triangles kept in the class instance.
DKOC_Body2OCCBRep::GetShapeID
int GetShapeID(const TopoDS_Shape &inShape) const
Get ID from Shape.
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
TessStatus
int TessStatus
Definition: TesselationEngine.cpp:8
IsXmlDumpActivated
Dtk_bool IsXmlDumpActivated()
Definition: XmlWrite.cpp:17
Dtk_string::len
int len() const
Retrieve the length of the Dtk_string.
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_Info::GetId
int GetId() const
Dtk_RGB
Definition: dtk_rgb.hpp:8
Dtk_dir
This is a mathematical direction class.
Definition: dtk_dir.hpp:14
IsPdfDumpActivated
Dtk_bool IsPdfDumpActivated()
Definition: PdfWrite.cpp:29