DATAKIT API  V2025.3
Sample to convert Dtk_Body to OpenCascade TopoDS_Shape

Using Datakit SDK for OpenCascade needs to go through assembly and construction tree like standard integration .
Then for Dtk_Node with type BodyType you can access to the Dtk_Body and convert it into TopoDS_Shape object. The following sample show you how to convert a Dtk_Body to an Opencascade binary object : TopoDS_Shape

#include "datakit.h"
#include "tess/tess.h"
#include "../LibReadersSample/WritingSample/PdfWrite/PdfWrite.hpp"
#include "../LibReadersSample/WritingSample/XmlWrite/XmlWrite.hpp"
// for TEST OCC
// include files of the target version of OpenCascade
#include <TopoDS_Shape.hxx>
#include <BRepTools.hxx>
// include files of the gateway SDK-OCC
void WriteDtk_Mesh(const Dtk_MeshPtr& inMeshToWrite);
//extern Dtk_ErrorStatus DKOC_Body_File(const Dtk_BodyPtr& inBody, const Dtk_string& inOutputFile, const int inShapeFixMode);
void WriteBody(Dtk_NodePtr inNode)
{
// Get if the node represent infinite geometry
// 1 = Infinite, 0 = Finite
int NodeInfiniteGeometry = inNode->GetInfos()->GetInfiniteGeometryFlag();
if (NodeInfiniteGeometry == 0)
{
//Calling both methods GetDtk_MeshPtr() and GetDtk_BodyPtr() on BodyNode will give you the same result
//Choose the one you need in order to avoid duplicated data
const Dtk_BodyPtr TmpBody = inNode->GetDtk_BodyPtr();
if (TmpBody.IsNotNULL())
{
// Used to write an XML File
{
XmlWriteBody(TmpBody);
}
// TEST OCC
// For each body , write a BREP file (OCC format)
TopoDS_Shape res;
Dtk_ErrorStatus stat = B2OC.ConvertBody(TmpBody);
res = B2OC.GetMainShape();
if (res.IsNull())
{
std::cout << "WriteBodyOcc Error : " << dtkTypeError(stat).c_str() << std::endl;
return;
}
// Name of BREP file : with a number
static int numbrep = 0;
numbrep++;
Dtk_InfoPtr bodyinfo = TmpBody->get_info();
Dtk_string bodyname;
if (bodyinfo.IsNotNULL()) bodyname = bodyinfo->GetName();
if (bodyname.len() == 0)
{
bodyinfo = inNode->GetInfos();
if (bodyinfo.IsNotNULL()) bodyname = bodyinfo->GetName();
if (bodyname.len() == 0) bodyname = "???";
}
std::cout << "Writing ToOpenCascade Body " << numbrep << " name: " << bodyname.c_str() << std::endl;
Dtk_string filebrep = bodyname;
filebrep += "_";
filebrep.add_int(numbrep);
filebrep += ".brep";
// Write the brep file
BRepTools::Write(res, filebrep.c_str());
// How to get a TopoDS_Shape corresponding to a Dtk_Entity from its ID ( get_info()->GetId() )
// Useful to resolve Dtk_Connector
// This code gets the ID of the first face and then getting the corresponding TopoDS_Shape
Dtk_bool orient;
TopoDS_Shape facewithid;
TmpBody->GetLump(0, Lump);
if (Lump.IsNotNULL())
{
Lump->GetVolume(0, Vol);
if (Vol.IsNotNULL())
{
Vol->GetShell(0, Shell);
if (Shell.IsNotNULL())
{
Shell->GetFace(0, Face, orient);
if (Face.IsNotNULL())
{
Dtk_ID faceid = Face->get_info()->GetId();
Dtk_string SampleBrepFileForFace = "facewithid";
SampleBrepFileForFace.add_int(faceid);
SampleBrepFileForFace += ".brep";
facewithid = B2OC.ShapeFromDtkID(faceid);
BRepTools::Write(facewithid, SampleBrepFileForFace.c_str());
}
}
}
}
// How to get the Dtk_Entity corresponding to a TopoDS_Shape
// This code show how to retrieve Dtk_Entity form a TopoDS_Shape
if (!facewithid.IsNull())
{
Dtk_ID DatakitFaceID = B2OC.GetDtkID(B2OC.GetShapeID(facewithid));
Dtk_EntityPtr CorrespondingFace = TmpBody->GetEntity(DatakitFaceID);
}
// END OCC conversion
}
// Some CAD formats store also faceted data besides B-Rep.
// So you can get faceted data corresponding to the body using the following method
const Dtk_MeshPtr TmpFacetedBody = inNode->GetDtk_MeshPtr();
if (TmpFacetedBody.IsNotNULL())
{
TmpFacetedBody->Transform(CurrentMatrix);
WriteDtk_Mesh(TmpFacetedBody);
}
// If there is no mesh data associated to the current body, you can tessellate (and if init of tessellation engine is successful)
if (TmpFacetedBody.IsNULL() && TmpBody.IsNotNULL() && GetTesselationStatus() == dtkNoError)
{
int err_tess = tess_BodyToMeshes(TmpBody, meshes, isclosed);
if( inNode->Name().len() > 0 )
std::cout << "NodeName = " << inNode->Name().c_str() << ";";
std::cout << "err_tess = " << dtkTypeError( err_tess ).c_str() << std::endl ;
if (err_tess == 0)
{
Dtk_Size_t i, nbmeshes = meshes.size();
for (i = 0; i < nbmeshes; i++)
{
meshes[i]->Transform(CurrentMatrix);
WriteDtk_Mesh(meshes[i]);
}
}
if (IsPdfDumpActivated() && TmpBody->HasWire())
{// handling wire / points
}
}
}
}
Dtk_ID
uint32_t Dtk_ID
Definition: define.h:689
Dtk_transfo
This is the Transformation dedicated class.
Definition: dtk_transfo.hpp:19
PdfWriteEntity
void PdfWriteEntity(Dtk_EntityPtr inEntity)
Definition: PdfWrite.cpp:173
Dtk_Info::GetName
Dtk_string GetName() const
Retrieves the entity name.
Dtk_Info::GetInfiniteGeometryFlag
int GetInfiniteGeometryFlag() const
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
WriteDtk_Mesh
void WriteDtk_Mesh(const Dtk_MeshPtr &inMeshToWrite)
Definition: WriteMesh.cpp:8
DTK_TRUE
#define DTK_TRUE
Definition: define.h:727
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_string
This is a high level string class.
Definition: dtk_string.hpp:58
Dtk_Size_t
size_t Dtk_Size_t
Definition: define.h:712
Dtk_bool
char Dtk_bool
Definition: define.h:725
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)
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
CurrentMatrix
Dtk_transfo CurrentMatrix
Definition: WritePrototype.cpp:11
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
tess.h
datakit.h
GetTesselationStatus
int GetTesselationStatus()
Definition: TesselationEngine.cpp:8
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
IsXmlDumpActivated
Dtk_bool IsXmlDumpActivated()
Definition: XmlWrite.cpp:17
Dtk_string::len
int len() const
Retrieve the length of the Dtk_string.
Dtk_SdkOcc.hpp
dtkNoError
@ dtkNoError
Definition: error_dtk.hpp:144
WriteBody
void WriteBody(Dtk_NodePtr inNode)
Definition: WriteBody.cpp:13
IsPdfDumpActivated
Dtk_bool IsPdfDumpActivated()
Definition: PdfWrite.cpp:26
Dtk_Node::GetDtk_BodyPtr
Dtk_BodyPtr GetDtk_BodyPtr()
Retrieves the Dtk_Node as a Dtk_BodyPtr - if exists -.