DATAKIT API  V2025.4
WriteBodyToOccBinaryObject.cpp File Reference

Functions

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

Variables

Dtk_transfo CurrentMatrix
 

Function Documentation

◆ GetTesselationStatus()

int GetTesselationStatus ( )
9 {
10  return TessStatus;
11 }

◆ WriteBody()

void WriteBody ( Dtk_NodePtr  inNode)
27 {
28  // Get if the node represent infinite geometry
29  // 1 = Infinite, 0 = Finite
30  int NodeInfiniteGeometry = inNode->GetInfos()->GetInfiniteGeometryFlag();
31  if (NodeInfiniteGeometry == 0)
32  {
33  //Calling both methods GetDtk_MeshPtr() and GetDtk_BodyPtr() on BodyNode will give you the same result
34  //Choose the one you need in order to avoid duplicated data
35  const Dtk_BodyPtr TmpBody = inNode->GetDtk_BodyPtr();
36  if (TmpBody.IsNotNULL())
37  {
38  // Used to write an XML File
39  if (IsXmlDumpActivated())
40  {
41  XmlWriteBody(TmpBody);
42  }
43 
44  // TEST OCC
45  // For each body , write a BREP file (OCC format)
46  DKOC_Body2OCCBRep B2OC;
47  TopoDS_Shape res;
50  Dtk_ErrorStatus stat = B2OC.ConvertBody(TmpBody);
51  res = B2OC.GetMainShape();
52  if (res.IsNull())
53  {
54  std::cout << "WriteBodyOcc Error : " << dtkTypeError(stat).c_str() << std::endl;
55  return;
56  }
57 
58  // Name of BREP file : with a number
59  static int numbrep = 0;
60  numbrep++;
61 
62  Dtk_InfoPtr bodyinfo = TmpBody->get_info();
63  Dtk_string bodyname;
64  if (bodyinfo.IsNotNULL()) bodyname = bodyinfo->GetName();
65  if (bodyname.len() == 0)
66  {
67  bodyinfo = inNode->GetInfos();
68  if (bodyinfo.IsNotNULL()) bodyname = bodyinfo->GetName();
69  if (bodyname.len() == 0) bodyname = "???";
70  }
71 
72  std::cout << "Writing ToOpenCascade Body " << numbrep << " name: " << bodyname.c_str() << std::endl;
73 
74  Dtk_string filebrep = bodyname;
75  filebrep += "_";
76  filebrep.add_int(numbrep);
77  filebrep += ".brep";
78 
79  // Write the brep file
80  BRepTools::Write(res, filebrep.c_str());
81 
82  // How to get a TopoDS_Shape corresponding to a Dtk_Entity from its ID ( get_info()->GetId() )
83  // Useful to resolve Dtk_Connector
84  // This code gets the ID of the first face and then getting the corresponding TopoDS_Shape
86  Dtk_VolumePtr Vol;
88  Dtk_FacePtr Face;
89  Dtk_bool orient;
90  TopoDS_Shape facewithid;
91  TmpBody->GetLump(0, Lump);
92  if (Lump.IsNotNULL())
93  {
94  Lump->GetVolume(0, Vol);
95  if (Vol.IsNotNULL())
96  {
97  Vol->GetShell(0, Shell);
98  if (Shell.IsNotNULL())
99  {
100  Shell->GetFace(0, Face, orient);
101  if (Face.IsNotNULL())
102  {
103  Dtk_ID faceid = Face->get_info()->GetId();
104  Dtk_string SampleBrepFileForFace = "facewithid";
105  SampleBrepFileForFace.add_int(faceid);
106  SampleBrepFileForFace += ".brep";
107  facewithid = B2OC.ShapeFromDtkID(faceid);
108  BRepTools::Write(facewithid, SampleBrepFileForFace.c_str());
109  }
110  }
111  }
112  }
113 
114  // How to get the Dtk_Entity corresponding to a TopoDS_Shape
115  // This code show how to retrieve Dtk_Entity form a TopoDS_Shape
116  if (!facewithid.IsNull())
117  {
118  Dtk_ID DatakitFaceID = B2OC.GetDtkID(B2OC.GetShapeID(facewithid));
119  Dtk_EntityPtr CorrespondingFace = TmpBody->GetEntity(DatakitFaceID);
120  }
121 
122  // END OCC conversion
123  }
124 
125  // Some CAD formats store also faceted data besides B-Rep.
126  // So you can get faceted data corresponding to the body using the following method
127  const Dtk_MeshPtr TmpFacetedBody = inNode->GetDtk_MeshPtr();
128  if (TmpFacetedBody.IsNotNULL())
129  {
130  TmpFacetedBody->Transform(CurrentMatrix);
131  WriteDtk_Mesh(TmpFacetedBody);
132  }
133 
134 
135  // If there is no mesh data associated to the current body, you can tessellate (and if init of tessellation engine is successful)
136  if (TmpFacetedBody.IsNULL() && TmpBody.IsNotNULL() && GetTesselationStatus() == dtkNoError)
137  {
138  Dtk_tab<Dtk_MeshPtr> meshes;
139  Dtk_tab<Dtk_Int32> isclosed;
140  int err_tess = tess_BodyToMeshes(TmpBody, meshes, isclosed);
141  if( inNode->Name().len() > 0 )
142  std::cout << "NodeName = " << inNode->Name().c_str() << ";";
143  std::cout << "err_tess = " << dtkTypeError( err_tess ).c_str() << std::endl ;
144  if (err_tess == 0)
145  {
146  Dtk_Size_t i, nbmeshes = meshes.size();
147  for (i = 0; i < nbmeshes; i++)
148  {
149  meshes[i]->Transform(CurrentMatrix);
150  WriteDtk_Mesh(meshes[i]);
151  }
152  }
153 
154  if (IsPdfDumpActivated() && TmpBody->HasWire())
155  {// handling wire / points
157  }
158  }
159  }
160 }

◆ 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_ID
uint32_t Dtk_ID
Definition: define.h:691
PdfWriteEntity
void PdfWriteEntity(Dtk_EntityPtr inEntity)
Definition: PdfWrite.cpp:177
Dtk_mesh_face
This is a high level face class.
Definition: util_mesh_dtk.hpp:865
Dtk_Info::GetName
Dtk_string GetName() const
Retrieves the entity name.
Dtk_Info::GetInfiniteGeometryFlag
int GetInfiniteGeometryFlag() const
XmlWriteMesh
void XmlWriteMesh(const Dtk_MeshPtr &inMeshToWrite)
Definition: XmlWrite.cpp:63
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:18
DTK_TRUE
#define DTK_TRUE
Definition: define.h:729
DKOC_Body2OCCBRep
Class DKOC_Body2OCCBRep This class allow the conversion from a Dtk_BodyPtr to a set of TopoDS_Shape b...
Definition: DKOC_Body2OCCBRep.hpp:14
Dtk_UInt32
uint32_t Dtk_UInt32
Definition: define.h:690
Dtk_string
This is a high level string class.
Definition: dtk_string.hpp:58
Dtk_Size_t
size_t Dtk_Size_t
Definition: define.h:714
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:727
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.
Dtk_DocElement::Name
const Dtk_string & Name() const
Retrieves the Dtk_DocElement Name - read only -.
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:689
GetTesselationStatus
int GetTesselationStatus()
Definition: TesselationEngine.cpp:8
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_DocElement::GetInfos
Dtk_InfoPtr GetInfos() const
Retrieves the Dtk_DocElement Dtk_InfoPtr - read only -.
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::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)
Dtk_string::c_str
const char * c_str() const
Retrieve the ASCII conversion string.
sampleWriter::Lump
@ Lump
Definition: testcreatecube.cpp:16
XmlWriteBody
void XmlWriteBody(const Dtk_BodyPtr &inBody)
Definition: XmlWrite.cpp:118
Dtk_pnt
This is a mathematical point class.
Definition: dtk_pnt.hpp:22
WriteDtk_Mesh
void WriteDtk_Mesh(const Dtk_MeshPtr &inMeshToWrite)
Definition: WriteMesh.cpp:8
Dtk_mesh_face::get_nbtriangles
Dtk_Size_t get_nbtriangles() const
Get the number of simple triangles kept in the class instance.
Dtk_Node::GetDtk_MeshPtr
Dtk_MeshPtr GetDtk_MeshPtr()
Retrieves the Dtk_Node as a Dtk_meshPtr - if exists -.
tess_BodyToMeshes
Dtk_ErrorStatus tess_BodyToMeshes(const Dtk_BodyPtr &inBodyToWrite, Dtk_tab< Dtk_MeshPtr > &outMeshes, Dtk_tab< Dtk_Int32 > &outIsSolid, Dtk_bool inTessWireframe=DTK_FALSE, Dtk_bool inApplyRenderInfos=DTK_FALSE)
: Make Tesselation from a Dtk_body and create a Set of Dtk_mesh if available
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:85
Dtk_tab::size
Dtk_Size_t size() const
Returns the size of the array.
Definition: util_stl_dtk.hpp:504
TessStatus
int TessStatus
Definition: TesselationEngine.cpp:6
IsXmlDumpActivated
Dtk_bool IsXmlDumpActivated()
Definition: XmlWrite.cpp:17
Dtk_string::len
int len() const
Retrieve the length of the Dtk_string.
dtkNoError
@ dtkNoError
Definition: error_dtk.hpp:144
Dtk_RGB
Definition: dtk_rgb.hpp:7
CurrentMatrix
Dtk_transfo CurrentMatrix
Definition: WritePrototype.cpp:11
Dtk_dir
This is a mathematical direction class.
Definition: dtk_dir.hpp:15
IsPdfDumpActivated
Dtk_bool IsPdfDumpActivated()
Definition: PdfWrite.cpp:30
Dtk_Node::GetDtk_BodyPtr
Dtk_BodyPtr GetDtk_BodyPtr()
Retrieves the Dtk_Node as a Dtk_BodyPtr - if exists -.