Hide menu
Loading...
Searching...
No Matches
wall_thickness/visualization/main.cxx

Refer to the Wall Thickness Visualization Example

shape_processor.hxx

// ****************************************************************************
// $Id$
//
// Copyright (C) 2008-2014, Roman Lygin. All rights reserved.
// Copyright (C) 2014-2023, CADEX. All rights reserved.
//
// This file is part of the CAD Exchanger software.
//
// You may use this file under the terms of the BSD license as follows:
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
// * Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
//
// ****************************************************************************
#ifndef _ShapeProcessor_HeaderFile
#define _ShapeProcessor_HeaderFile
#include <cadex/Base_UTF16String.hxx>
#include <cadex/ModelData_Body.hxx>
#include <cadex/ModelData_BodyList.hxx>
#include <cadex/ModelData_BRepRepresentation.hxx>
#include <cadex/ModelData_IndexedTriangleSet.hxx>
#include <cadex/ModelData_Model.hxx>
#include <cadex/ModelData_Part.hxx>
#include <cadex/ModelData_PolyRepresentation.hxx>
#include <cadex/ModelData_PolyShapeList.hxx>
#include <cadex/ModelData_Shell.hxx>
#include <cadex/ModelData_Solid.hxx>
#include <cadex/WallThickness_Data.hxx>
#include <iostream>
using namespace cadex;
using namespace std;
class ShapeProcessor : public ModelData_Model::VoidElementVisitor
{
public:
void operator() (const ModelData_Part& thePart) override
{
auto aPartName = thePart.Name().IsEmpty() ? "noname" : thePart.Name();
auto aBRep = thePart.BRepRepresentation();
if (aBRep) {
const auto& aBodyList = aBRep.Get();
for (size_t i = 0, n = aBodyList.Size(); i < n; ++i) {
const auto& aBody = aBodyList[i];
ModelData_Shape::Iterator aShapeIt (aBody);
while (aShapeIt.HasNext()) {
const auto& aShape = aShapeIt.Next();
if (aShape.Type() == ModelData_ST_Solid) {
cout << "Part #" << myPartIndex << " [\"" << aPartName << "\"] - solid #" << std::to_string (i) << " has:" << endl;
ProcessSolid (ModelData_Solid::Cast (aShape));
} else if (aShape.Type() == ModelData_ST_Shell) {
cout << "Part #" << myPartIndex << " [\"" << aPartName << "\"] - shell #" << std::to_string (i) << " has:" << endl;
ProcessShell (ModelData_Shell::Cast (aShape));
}
}
}
}
++myPartIndex;
}
virtual void ProcessSolid (const ModelData_Solid& theSolid) = 0;
virtual void ProcessShell (const ModelData_Shell& theShell) = 0;
private:
size_t myPartIndex = 0;
};
class SolidProcessor : public ModelData_Model::VoidElementVisitor
{
public:
void operator() (const ModelData_Part& thePart) override
{
auto aPartName = thePart.Name().IsEmpty() ? "noname" : thePart.Name();
auto aBRep = thePart.BRepRepresentation();
if (aBRep) {
const auto& aBodyList = aBRep.Get();
for (size_t i = 0, n = aBodyList.Size(); i < n; ++i) {
const auto& aBody = aBodyList[i];
ModelData_Shape::Iterator aShapeIt (aBody);
while (aShapeIt.HasNext()) {
const auto& aShape = aShapeIt.Next();
if (aShape.Type() == ModelData_ST_Solid) {
cout << "Part #" << myPartIndex << " [\"" << aPartName << "\"] - solid #" << std::to_string (i) << " has:" << endl;
ProcessSolid (ModelData_Solid::Cast (aShape));
}
}
}
}
++myPartIndex;
}
virtual void ProcessSolid (const ModelData_Solid& theSolid) = 0;
private:
size_t myPartIndex = 0;
};
class SolidAndMeshProcessor : public ModelData_Model::VoidElementVisitor
{
public:
void operator() (const ModelData_Part& thePart) override
{
auto aPartName = thePart.Name().IsEmpty() ? "noname" : thePart.Name();
if (auto aBRep = thePart.BRepRepresentation()) {
const auto& aBodyList = aBRep.Get();
for (size_t i = 0, n = aBodyList.Size(); i < n; ++i) {
const auto& aBody = aBodyList[i];
ModelData_Shape::Iterator aShapeIt (aBody);
while (aShapeIt.HasNext()) {
const auto& aShape = aShapeIt.Next();
if (aShape.Type() == ModelData_ST_Solid) {
cout << "Part #" << myPartIndex << " [\"" << aPartName << "\"] - solid #" << std::to_string (i) << " has:" << endl;
ProcessSolid (ModelData_Solid::Cast (aShape), aPartName, i);
}
}
}
}
else if (auto aPolyRep = thePart.PolyRepresentation (ModelData_RM_Poly)) {
const auto& aPolyList = aPolyRep.Get();
for (size_t i = 0; i < aPolyList.Size(); ++i) {
const auto& aPVS = aPolyList[i];
if (aPVS.IsOfType<ModelData_IndexedTriangleSet>()) {
cout << "Part #" << myPartIndex << " [\"" << aPartName << "\"] - mesh #" << std::to_string (i) << " has:" << endl;
ProcessMesh (static_cast<const ModelData_IndexedTriangleSet&> (aPVS), aPartName, i);
}
}
}
++myPartIndex;
}
virtual void ProcessSolid (const ModelData_Solid& theSolid,
const Base_UTF16String& thePartName, size_t theShapeIndex) = 0;
virtual void ProcessMesh (const ModelData_IndexedTriangleSet& theMesh,
const Base_UTF16String& thePartName, size_t theShapeIndex) = 0;
protected:
size_t myPartIndex = 0;
};
#endif
Defines a Unicode (UTF-16) string wrapping a standard string.
Definition: Base_UTF16String.hxx:34
bool IsEmpty() const
Returns true if the string is empty.
Definition: Base_UTF16String.cxx:233
const ModelData_BodyList & Get() const
Returns an associated topological object.
Definition: ModelData_BRepRepresentation.cxx:626
Base_UTF16String Name() const
Definition: ModelData_BaseObject.cxx:218
Defines a polygonal shape consisting of triangles.
Definition: ModelData_IndexedTriangleSet.hxx:35
Element visitor with empty implementation.
Definition: ModelData_Model.hxx:113
Defines a leaf node in the scene graph hiearchy.
Definition: ModelData_Part.hxx:35
ModelData_BRepRepresentation BRepRepresentation() const
Definition: ModelData_Part.cxx:360
ModelData_PolyRepresentation PolyRepresentation(ModelData_RepresentationMask theRepresentationMask) const
Definition: ModelData_Part.cxx:371
Iterates over subshapes in a shape.
Definition: ModelData_Shape.hxx:41
Defines a connected set of faces.
Definition: ModelData_Shell.hxx:31
Defines a topological solid.
Definition: ModelData_Solid.hxx:31
Defines classes, types, and global functions related to CAD Exchanger.
Definition: A3DSTestLib.hxx:22

main.cxx

// ****************************************************************************
// $Id$
//
// Copyright (C) 2008-2014, Roman Lygin. All rights reserved.
// Copyright (C) 2014-2023, CADEX. All rights reserved.
//
// This file is part of the CAD Exchanger software.
//
// You may use this file under the terms of the BSD license as follows:
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
// * Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
//
// ****************************************************************************
#include <cadex/LicenseManager_Activate.h>
#include <cadex/MTKBase_DoubleList.hxx>
#include <cadex/ModelAlgo_BRepMesher.hxx>
#include <cadex/ModelData_Body.hxx>
#include <cadex/ModelData_BodyList.hxx>
#include <cadex/ModelData_BRepRepresentation.hxx>
#include <cadex/ModelData_IndexedTriangleSet.hxx>
#include <cadex/ModelData_ModelReader.hxx>
#include <cadex/ModelData_ModelWriter.hxx>
#include <cadex/ModelData_PolyRepresentation.hxx>
#include <cadex/ModelData_PolyShapeList.hxx>
#include <cadex/ModelData_PolyVertexSet.hxx>
#include <cadex/ModelData_SceneGraphElementUniqueVisitor.hxx>
#include <cadex/ModelData_Solid.hxx>
#include <cadex/WallThickness_Data.hxx>
#include <cadex/WallThickness_Analyzer.hxx>
#include <iostream>
#include "../../helpers/shape_processor.hxx"
#include "../../../cadex_license.cxx"
#include "../../../mtk_license.cxx"
using namespace cadex;
using namespace std;
class PartProcessor : public SolidAndMeshProcessor
{
public:
PartProcessor (const Base_UTF16String& theMeshFolderPath) : SolidAndMeshProcessor(),
myMeshFolderPath (theMeshFolderPath)
{}
void ProcessSolid (const ModelData_Solid& theSolid,
const Base_UTF16String& thePartName, size_t theShapeIndex) override
{
auto aWTData = myAnalyzer.Perform (theSolid, myResolution);
auto aColorizedMesh = CreateMesh (theSolid, aWTData.MinThickness());
ColorizeMesh (aColorizedMesh, aWTData);
Base_UTF16String aFileName = MeshFileName (thePartName, theShapeIndex, "solid");
PrintWTData (aWTData);
WriteMeshToFile (aColorizedMesh, aFileName);
}
void ProcessMesh (const ModelData_IndexedTriangleSet& theMesh,
const Base_UTF16String& thePartName, size_t theShapeIndex) override
{
auto aWTData = myAnalyzer.Perform (theMesh, myResolution);
ModelData_IndexedTriangleSet aColorizedMesh (theMesh);
ColorizeMesh (aColorizedMesh, aWTData);
Base_UTF16String aFileName = MeshFileName (thePartName, theShapeIndex, "mesh");
PrintWTData (aWTData);
WriteMeshToFile (aColorizedMesh, aFileName);
}
void PrintWTData (const WallThickness_Data& theData)
{
if (!theData.IsEmpty()) {
cout << " Min thickness = " << theData.MinThickness() << " mm" << endl;
cout << " Max thickness = " << theData.MaxThickness() << " mm\n" << endl;
} else {
cerr << " Failed to analyze the wall thickness of this entity.\n";
}
}
void WriteMeshToFile (const ModelData_IndexedTriangleSet& theMesh, const Base_UTF16String& theFileName)
{
if (!theMesh.IsEmpty()) {
cout << " Number of triangles = " << theMesh.NumberOfFaces() << endl;
cout << " Number of triangles with thickness less than threshold = " << myNumberOfTresholdTriangles << "\n" << endl;
} else {
cerr << " Failed to create colorized mesh of this entity.\n";
return;
}
ModelData_PolyRepresentation aPolyWithITS (theMesh);
aPart.AddRepresentation (aPolyWithITS);
aModel.AddRoot (aPart);
bool aRes = aWriter.Write (aModel, theFileName);
if (aRes) {
cout << " A colorized mesh of the part has been saved to " << theFileName << endl;
cout << " The mesh is shaded in a red-to-green gradient, "
<< " with red corresponding to the minimum thickness and green indicating the maximum thickness. " << theFileName << endl;
} else {
cerr << " Failed to save colorized mesh of the part to " << theFileName << endl;
}
}
ModelData_IndexedTriangleSet CreateMesh (const ModelData_Solid& theSolid, const double theMaxEdgeLength)
{
ModelAlgo_BRepMesherParameters aParams (ModelAlgo_BRepMesherParameters::Fine, true);
aParams.SetMaxEdgeLength (theMaxEdgeLength);
ModelAlgo_BRepMesher aMesher (aParams);
auto aShape = ModelData_Body::Create (theSolid);
auto aPSList = aMesher.Compute (aShape);
auto aPSListSize = aPSList.Size();
for (size_t i = 0; i < aPSListSize; ++i) {
auto& aPVS = aPSList[i];
if (aPVS.IsOfType<ModelData_IndexedTriangleSet>()) {
return static_cast<const ModelData_IndexedTriangleSet&> (aPVS);
}
}
}
void ColorizeMesh (ModelData_IndexedTriangleSet& theMesh, const WallThickness_Data& theData)
{
auto aMaxThickness = theData.MaxThickness();
auto aThicknessRange = aMaxThickness - myThicknessTreshold;
std::vector<ModelData_Color> aColors;
std::vector<int> aColorsIndices;
const auto& aThicknessList = theData.ThicknessList();
for (size_t i = 0; i < aThicknessList.Size(); i++) {
auto aPercent = (aThicknessList[i] - myThicknessTreshold) / aThicknessRange;
int aRColor = 255;
int aGColor = 255;
if (aPercent < 0.) {
aRColor = 255;
aGColor = 0;
} else if (aPercent < 0.5) {
aGColor = static_cast<int> (255 * (aPercent * 2));
} else {
aRColor = static_cast<int> (255 * (1. - aPercent));
}
ModelData_Color aColor (aRColor, aGColor, 0);
aColors.push_back (aColor);
}
for (int i = 0; i < theMesh.NumberOfFaces(); i++) {
double aThickness = theData.GetThickness (theMesh, i);
auto aPercent = (aThickness - myThicknessTreshold) / aThicknessRange;
int aRColor = 255;
int aGColor = 255;
if (aPercent < 0.) {
aRColor = 255;
aGColor = 0;
myNumberOfTresholdTriangles++;
} else if (aPercent < 0.5) {
aGColor = static_cast<int> (255 * (aPercent * 2));
} else {
aRColor = static_cast<int> (255 * (1. - aPercent));
}
ModelData_Color aColor (aRColor, aGColor, 0);
aColors.push_back (aColor);
aColorsIndices.push_back (static_cast<int> (aColors.size() - 1));
aColorsIndices.push_back (static_cast<int> (aColors.size() - 1));
aColorsIndices.push_back (static_cast<int> (aColors.size() - 1));
}
theMesh.AddColors (aColors.data(), aColors.size(), aColorsIndices.data(), aColorsIndices.size());
}
__CADEX_DEFINE_INITIALIZED_PRIMITIVE_PROPERTY (size_t, Resolution, 1000)
__CADEX_DEFINE_PRIMITIVE_PROPERTY (double, ThicknessTreshold)
private:
Base_UTF16String MeshFileName (const Base_UTF16String& thePartName,
size_t theShapeIndex, const Base_UTF16String& theShapeName)
{
Base_UTF16String aPartName = Base_UTF16String ("Part ") + std::to_string (myPartIndex).c_str() + " [" + thePartName + "]";
Base_UTF16String aShapeName = theShapeName + " " + std::to_string (theShapeIndex).c_str();
Base_UTF16String aFileName = myMeshFolderPath + "/" + aPartName + " - " + aShapeName + " - colorized.cdx";
return aFileName;
}
size_t myNumberOfTresholdTriangles = 0;
Base_UTF16String myMeshFolderPath;
};
int main (int argc, char* argv[])
{
auto aKey = LicenseKey::Value();
auto anMTKKey = MTKLicenseKey::Value();
// Activate the license (aKey must be defined in cadex_license.cxx
// and anMTKKey must be defined in mtk_license.cxx)
if (!CADExLicense_Activate (aKey)) {
cerr << "Failed to activate CAD Exchanger license." << endl;
return 1;
}
if (!CADExLicense_Activate (anMTKKey)) {
cerr << "Failed to activate Manufacturing Toolkit license." << endl;
return 1;
}
if (argc != 4) {
cerr << "Usage: " << argv[0] << " <input_file> <output_folder> <input_thickness_treshold>, where:" << endl;
cerr << " <input_file> is a name of the file to be read" << endl;
cerr << " <output_folder> is a name of the folder where CDX files with colorized mesh to be written" << endl;
cerr << " <input_thickness_treshold> is a value of the thickness at which the color of the triangles turns red." << endl;
return 1;
}
const char* aSource = argv[1];
const char* aColorizedMeshPath = argv[2];
double aThicknessTreshold = std::atof (argv[3]);
size_t aResolution = 1000;
// Reading the file
if (!aReader.Read (aSource, aModel)) {
cerr << "Failed to read the file " << aSource << endl;
return 1;
}
cout << "Model: " << aModel.Name() << "\n" << endl;
//processing
PartProcessor aPartProcessor (aColorizedMeshPath);
aPartProcessor.Resolution() = aResolution;
aPartProcessor.ThicknessTreshold() = aThicknessTreshold;
ModelData_SceneGraphElementUniqueVisitor aVisitor (aPartProcessor);
aModel.Accept (aVisitor);
return 0;
}
Computes a polygonal representation from a B-Rep one.
Definition: ModelAlgo_BRepMesher.hxx:48
void Compute(const ModelData_Model &theModel, bool theEnforceAddition=false) const
Computes polygonal representations for all parts in the model.
Definition: ModelAlgo_BRepMesher.cxx:478
Defines parameters used by the B-Rep mesher.
Definition: ModelAlgo_BRepMesherParameters.hxx:33
void SetSaveBRepToPolyAssociations(bool theValue)
Sets whether associations between ModelData_Shapes and meshes should be generated.
Definition: ModelAlgo_BRepMesherParameters.cxx:480
void SetMaxEdgeLength(double theValue)
Sets maximum length of triangle's edges.
Definition: ModelAlgo_BRepMesherParameters.cxx:241
Defines an RGBA color (with alpha channel).
Definition: ModelData_Color.hxx:34
void AddColors(const ColorType theColors[], size_t theColorNb, const IndexType theIndices[], size_t theIndexNb)
Adds vertex colors.
Definition: ModelData_IndexedTriangleSet.cxx:382
IndexType NumberOfFaces() const
Returns a number of faces (triangles).
Definition: ModelData_IndexedTriangleSet.cxx:179
Provides CAD Exchanger data model.
Definition: ModelData_Model.hxx:43
const Base_UTF16String & Name() const
Returns a model name.
Definition: ModelData_Model.cxx:358
const ModelData_SceneGraphElement & AddRoot(const ModelData_SceneGraphElement &theElement)
Adds new root element into the scene graph.
Definition: ModelData_Model.cxx:830
void Accept(ElementVisitor &theVisitor) const
Accepts a visitor.
Definition: ModelData_Model.cxx:882
Reads any format that CAD Exchanger can import.
Definition: ModelData_ModelReader.hxx:33
bool Read(const Base_UTF16String &theFilePath, ModelData_Model &theModel)
Reads the file at the specified path into the specified model.
Definition: ModelData_ModelReader.cxx:182
Writes any format that CAD Exchanger can export.
Definition: ModelData_ModelWriter.hxx:33
bool Write(const ModelData_Model &theModel, const Base_UTF16String &theFilePath)
Writes the specified model to the file at the specified path.
Definition: ModelData_ModelWriter.cxx:143
void AddRepresentation(const ModelData_Representation &theRepresentation)
Adds a representation.
Definition: ModelData_Part.cxx:341
Defines polygonal (faceted or tessellated) representation of part.
Definition: ModelData_PolyRepresentation.hxx:39
Defines a visitor that visits each unique element only once.
Definition: ModelData_SceneGraphElementUniqueVisitor.hxx:33
The wall thickness analyzing tool.
Definition: WallThickness_Analyzer.hxx:42
Contains information about minimum and maximum wall thicknesses.
Definition: WallThickness_Data.hxx:42
double GetThickness(const ModelData_Face &theFace, double theU, double theV) const
Returns the thickness at a specific point on a surface.
Definition: WallThickness_Data.cxx:178
double MaxThickness() const
Returns the maximum wall thickness in mm.
Definition: WallThickness_Data.cxx:141
bool IsEmpty() const
Returns true if WallThickness_Data is empty.
Definition: WallThickness_Data.cxx:190
MTKBase_DoubleList ThicknessList() const
Definition: WallThickness_Data.cxx:172
double MinThickness() const
Returns the minimum wall thickness in mm.
Definition: WallThickness_Data.cxx:135