Refer to the Molding Feature Recognizer Example
feature_group.hxx
#ifndef _FeatureGroup_HeaderFile
#define _FeatureGroup_HeaderFile
#include <cadex/Geom/Direction.hxx>
#include <cadex/MTKBase_Feature.hxx>
#include <cadex/MTKBase_FeatureComparator.hxx>
#include <algorithm>
#include <array>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <sstream>
#include <vector>
using namespace std;
typedef std::pair<double, double> PairType;
typedef std::array<double, 3> ArrayType;
inline std::ostream& operator<< (std::ostream& theStream, const PairType& thePair)
{
return theStream << thePair.first << " x " << thePair.second;
}
inline std::ostream& operator<< (std::ostream& theStream, const ArrayType& theArray)
{
return theStream << theArray[0] << " x " << theArray[1] << " x " << theArray[2];
}
inline std::ostream& operator<< (std::ostream& theStream,
const Geom::Direction& theDir)
{
stringstream aStream;
aStream << setprecision(2) << fixed <<
"(" << theDir.
X() <<
", " << theDir.
Y() <<
", " << theDir.
Z() <<
")";
return theStream << aStream.str();
}
class FeatureGroupManager
{
public:
void AddFeature (const char* theGroupName,
const char* theSubgroupName,
bool theHasParameters,
{
auto aRes = std::find_if (myGroups.begin(), myGroups.end(),
[&] (const FeatureGroup& theGroup) { return theGroup.myName == theGroupName; });
if (aRes == myGroups.end()) {
aRes = myGroups.insert (aRes, FeatureGroup (theGroupName, theSubgroupName, theHasParameters));
}
auto& aGroup = *aRes;
++aGroup.myFeatureSubgroups[theFeature];
}
void Print (
const char* theFeatureType, function<
void (
MTKBase_Feature)> thePrintFeatureParameters)
{
sort (myGroups.begin(), myGroups.end(), FeatureGroupComparator());
cout << setprecision(6);
size_t aTotalCount = 0;
for (const auto& aFeatureGroup : myGroups) {
size_t aFeatureCount = aFeatureGroup.FeatureCount();
aTotalCount += aFeatureCount;
cout << " " << aFeatureGroup.myName << ": " << aFeatureCount << endl;
if (!aFeatureGroup.myHasParameters) {
continue;
}
const char* aSubgroupName = aFeatureGroup.mySubgroupName.c_str();
for (const auto& aFeatureSubgroup : aFeatureGroup.myFeatureSubgroups) {
cout << " " << aFeatureSubgroup.second << " " << aSubgroupName << " with" << endl;
thePrintFeatureParameters (aFeatureSubgroup.first);
}
}
cout << "\n Total " << theFeatureType << ": " << aTotalCount << "\n" << endl;
}
template <typename T>
static void PrintFeatureParameter (const char* theName, const T& theValue, const char* theUnits)
{
cout << " " << theName << ": " << theValue << " " << theUnits << endl;
}
private:
class FeatureGroup
{
public:
FeatureGroup (const std::string& theName, const std::string& theSubgroupName, bool theHasParameters) :
myName (theName), mySubgroupName (theSubgroupName), myHasParameters (theHasParameters)
{}
size_t FeatureCount() const
{
size_t aCount = 0;
for (const auto& aFeatureSubgroup : myFeatureSubgroups) {
aCount += aFeatureSubgroup.second;
}
return aCount;
}
string myName;
string mySubgroupName;
bool myHasParameters;
map<MTKBase_Feature, size_t, MTKBase_FeatureComparator> myFeatureSubgroups;
};
class FeatureGroupComparator
{
public:
bool operator() (const FeatureGroup& theA, const FeatureGroup& theB) const
{
const auto& anAName = theA.myName;
const auto& aBName = theB.myName;
if (anAName == aBName) {
return false;
}
const auto& anAFeatureSubgroups = theA.myFeatureSubgroups;
const auto& aBFeatureSubgroups = theB.myFeatureSubgroups;
if (anAFeatureSubgroups.empty() || aBFeatureSubgroups.empty()) {
return anAName < aBName;
}
aBFeatureSubgroups.begin()->first);
}
};
vector<FeatureGroup> myGroups;
};
#endif
Defines a 3D Direction.
Definition Direction.hxx:34
Provides possibility to compare MTK based features depending on their type and parameters.
Definition MTKBase_FeatureComparator.hxx:29
Describes a base class of MTK based features.
Definition MTKBase_Feature.hxx:33
Defines classes, namespaces, enums, types, and global functions related to Manufacturing Toolkit.
Definition LicenseManager_LicenseError.hxx:30
shape_processor.hxx
#ifndef _ShapeProcessor_HeaderFile
#define _ShapeProcessor_HeaderFile
#include <cadex/Base/UTF16String.hxx>
#include <cadex/ModelData/Body.hxx>
#include <cadex/ModelData/Model.hxx>
#include <cadex/ModelData/Part.hxx>
#include <cadex/ModelData/Shell.hxx>
#include <cadex/ModelData/Solid.hxx>
#include <cadex/WallThickness_Data.hxx>
#include <iostream>
using namespace std;
{
public:
{
size_t aBodyNumber = 0;
const auto& aBodies = thePart.
Bodies();
for (const auto& aBody : aBodies) {
while (aShapeIt.
HasNext()) {
const auto& aShape = aShapeIt.
Next();
if (aShape.Type() == ModelData::ShapeType::Solid) {
cout << "Part #" << myPartIndex << " [\"" << aPartName << "\"] - solid #" << std::to_string (aBodyNumber) << " has:" << endl;
ProcessSolid (ModelData::Solid::Cast (aShape));
} else if (aShape.Type() == ModelData::ShapeType::Shell) {
cout << "Part #" << myPartIndex << " [\"" << aPartName << "\"] - shell #" << std::to_string (aBodyNumber) << " has:" << endl;
ProcessShell (ModelData::Shell::Cast (aShape));
}
}
++aBodyNumber;
}
++myPartIndex;
}
private:
size_t myPartIndex = 0;
};
{
public:
{
size_t aBodyNumber = 0;
const auto& aBodies = thePart.Bodies();
for (const auto& aBody : aBodies) {
while (aShapeIt.HasNext()) {
const auto& aShape = aShapeIt.Next();
if (aShape.Type() == ModelData::ShapeType::Solid) {
cout << "Part #" << myPartIndex << " [\"" << aPartName << "\"] - solid #" << std::to_string (aBodyNumber++) << " has:" << endl;
ProcessSolid (ModelData::Solid::Cast (aShape));
}
}
}
++myPartIndex;
}
private:
size_t myPartIndex = 0;
};
{
public:
{
size_t aBodyNumber = 0;
const auto& aBodies = thePart.Bodies();
for (const auto& aBody : aBodies) {
while (aShapeIt.HasNext()) {
const auto& aShape = aShapeIt.Next();
if (aShape.Type() == ModelData::ShapeType::Solid) {
cout << "Part #" << myPartIndex << " [\"" << aPartName << "\"] - solid #" << std::to_string (aBodyNumber++) << " has:" << endl;
ProcessSolid (ModelData::Solid::Cast (aShape), aPartName, aBodyNumber);
}
}
}
++myPartIndex;
}
const UTF16String& thePartName,
size_t theShapeIndex) = 0;
protected:
size_t myPartIndex = 0;
};
#endif
UTF16String Name() const
Returns a name.
Definition ModelElement.cxx:55
Element visitor with empty implementation.
Definition ModelElementVisitor.hxx:64
Defines a leaf node in the scene graph hiearchy.
Definition Part.hxx:34
Iterates over subshapes in a shape.
Definition ShapeIterator.hxx:32
Defines a connected set of faces.
Definition Shell.hxx:32
Defines a topological solid.
Definition Solid.hxx:32
Defines a Unicode (UTF-16) string wrapping a standard string.
Definition UTF16String.hxx:30
bool IsEmpty() const
Returns true if the string is empty.
Definition UTF16String.cxx:337
main.cxx
#include <cadex/LicenseManager_Activate.h>
#include <cadex/Molding_FeatureRecognizer.hxx>
#include <cadex/Molding_FeatureRecognizerParameters.hxx>
#include <cadex/Molding_Rib.hxx>
#include <cadex/Molding_ScrewBoss.hxx>
#include <cadex/ModelData/Model.hxx>
#include <cadex/ModelData/ModelReader.hxx>
#include <cadex/MTKBase_Boss.hxx>
#include <cadex/MTKBase_Feature.hxx>
#include <cadex/MTKBase_FeatureList.hxx>
#define _USE_MATH_DEFINES
#include <math.h>
#include <unordered_map>
#include "../../helpers/feature_group.hxx"
#include "../../helpers/shape_processor.hxx"
#include "../../mtk_license.cxx"
using namespace std;
double ToDegrees (double theAngleRad)
{
return theAngleRad * 180. / M_PI;
}
{
FeatureGroupManager aManager;
for (
size_t i = 0; i < theFeatureList.
Size(); i++) {
const auto& aFeature = theFeatureList[i];
aManager.AddFeature ("Screw Boss(es)", "Screw Boss(es)", true, aScrewBoss);
const auto& aBoss =
static_cast<const MTKBase_Boss&
> (aFeature);
aManager.AddFeature ("Boss(es)", "Boss(es)", true, aBoss);
const auto& aRib =
static_cast<const Molding_Rib&
> (aFeature);
aManager.AddFeature ("Rib(s)", "Rib(s)", true, aRib);
}
}
{
FeatureGroupManager::PrintFeatureParameter ("outer radius", aScrewBoss.OuterRadius(), "mm");
FeatureGroupManager::PrintFeatureParameter ("inner radius", aScrewBoss.InnerRadius(), "mm");
FeatureGroupManager::PrintFeatureParameter ("draft angle", ToDegrees (aScrewBoss.DraftAngle()), "deg");
const auto& aBoss =
static_cast<const MTKBase_Boss&
> (theFeature);
FeatureGroupManager::PrintFeatureParameter ("length", aBoss.Length(), "mm");
FeatureGroupManager::PrintFeatureParameter ("height", aBoss.Height(), "mm");
FeatureGroupManager::PrintFeatureParameter ("width", aBoss.Width(), "mm");
const auto& aRib =
static_cast<const Molding_Rib&
> (theFeature);
FeatureGroupManager::PrintFeatureParameter ("length", aRib.Length(), "mm");
FeatureGroupManager::PrintFeatureParameter ("height", aRib.Height(), "mm");
FeatureGroupManager::PrintFeatureParameter ("thickness", aRib.Thickness(), "mm");
FeatureGroupManager::PrintFeatureParameter ("draft angle", ToDegrees (aRib.DraftAngle()), "deg");
}
};
aManager.Print ("features", PrintFeatureParameters);
}
class PartProcessor : public SolidProcessor
{
public:
PartProcessor ()
{}
{
Molding_FeatureRecognizerParameters aRecognizerParameters;
aRecognizerParameters.
SetMaxRibThickness (30.0);
aRecognizerParameters.
SetMaxRibDraftAngle (0.2);
aRecognizerParameters.
SetMaxRibTaperAngle (0.1);
auto aFeatureList = aRecognizer.Perform (theSolid);
PrintFeatures (aFeatureList);
}
};
int main (int argc, char* argv[])
{
auto aKey = MTKLicenseKey::Value();
if (!CADExLicense_Activate (aKey)) {
cerr << "Failed to activate Manufacturing Toolkit license." << endl;
return 1;
}
if (argc != 2) {
cerr << "Usage: " << argv[0] << " <input_file>, where:" << endl;
cerr << " <input_file> is a name of the file to be read" << endl;
return 1;
}
const char* aSource = argv[1];
if (!aReader.
Read (aSource, aModel)) {
cerr << "Failed to read the file " << aSource << endl;
return 1;
}
cout <<
"Model: " << aModel.
Name() <<
"\n" << endl;
PartProcessor aPartProcessor;
return 0;
}
Describes a boss. In CNC Machining a boss is a protrusion or raised area on a workpiece that is creat...
Definition MTKBase_Boss.hxx:31
Defines a list of features.
Definition MTKBase_FeatureList.hxx:36
size_t Size() const
Returns the number of elements in the list.
Definition MTKBase_FeatureList.cxx:88
Defines a visitor that visits each unique element only once.
Definition ModelElementVisitor.hxx:87
Provides MTK data model.
Definition Model.hxx:40
UTF16String Name() const
Returns a model name.
Definition Model.cxx:250
void Accept(ModelElementVisitor &theVisitor) const
Accepts a visitor.
Definition Model.cxx:270
Reads STEP and native format.
Definition ModelReader.hxx:29
bool Read(const UTF16String &theFilePath, ModelData::Model &theModel)
Reads the file at the specified path into the specified model.
Definition ModelReader.cxx:227
Provides an interface to recognizing molding features.
Definition Molding_FeatureRecognizer.hxx:37
Describes a rib.
Definition Molding_Rib.hxx:29
Describes a screw boss. In injection molding, the Screw Boss is essentially a cylindrical protrusion ...
Definition Molding_ScrewBoss.hxx:29