using System;
using System.Collections.Generic;
using FeatureMapType = System.Collections.Generic.SortedDictionary<
cadex.MTKBase_Feature, uint>;
namespace feature_group
{
class FeatureComparer : IComparer<MTKBase_Feature>
{
public int Compare(MTKBase_Feature theA, MTKBase_Feature theB)
{
MTKBase_FeatureComparator aComparator = new MTKBase_FeatureComparator();
bool anALessThanB = aComparator.Apply(theA, theB);
if (anALessThanB)
{
return -1;
}
bool aBLessThanA = aComparator.Apply(theB, theA);
if (aBLessThanA)
{
return 1;
}
return 0;
}
}
public struct Pair
{
public Pair(double theFirst, double theSecond)
{
First = theFirst;
Second = theSecond;
}
public double First { get; }
public double Second { get; }
public override string ToString() => $"{First} x {Second}";
}
public struct Dimension
{
public Dimension(double theL, double theW, double theD)
{
L = theL;
W = theW;
D = theD;
}
public double L { get; }
public double W { get; }
public double D { get; }
public override string ToString() => $"{L} x {W} x {D}";
}
public struct Direction
{
public Direction(double theX, double theY, double theZ)
{
X = theX;
Y = theY;
Z = theZ;
}
public double X { get; }
public double Y { get; }
public double Z { get; }
public override string ToString() => $"({FormattedString(X)}, {FormattedString(Y)}, {FormattedString(Z)})";
private string FormattedString(double theValue)
{
System.Globalization.CultureInfo aCI = new System.Globalization.CultureInfo("en-US");
return string.Format(aCI, "{0:0.00}", theValue);
}
}
class FeatureGroupManager
{
public FeatureGroupManager()
{
myGroups = new List<FeatureGroup>();
}
private class FeatureGroup
{
public FeatureGroup(string theName, string theSubgroupName, bool theHasParameters)
{
myName = theName;
mySubgroupName = theSubgroupName;
myHasParameters = theHasParameters;
myFeatureSubgroups = new FeatureMapType(new FeatureComparer());
}
public uint FeatureCount()
{
uint aCount = 0;
foreach (var i in myFeatureSubgroups)
{
aCount += i.Value;
}
return aCount;
}
public string myName;
public string mySubgroupName;
public bool myHasParameters;
public FeatureMapType myFeatureSubgroups;
}
private class FeatureGroupComparer : IComparer<FeatureGroup>
{
public int Compare(FeatureGroup theA, FeatureGroup theB)
{
string anAName = theA.myName;
string aBName = theB.myName;
if (anAName == aBName)
{
return 0;
}
FeatureMapType anAFeatureSubgroups = theA.myFeatureSubgroups;
FeatureMapType aBFeatureSubgroups = theB.myFeatureSubgroups;
if (anAFeatureSubgroups.Count == 0 || aBFeatureSubgroups.Count == 0)
{
return anAName.CompareTo(aBName);
}
MTKBase_Feature anAFeature = new MTKBase_Feature();
MTKBase_Feature aBFeature = new MTKBase_Feature();
foreach (var i in anAFeatureSubgroups)
{
anAFeature = i.Key;
break;
}
foreach (var i in aBFeatureSubgroups)
{
aBFeature = i.Key;
break;
}
FeatureComparer aFeatureComparator = new FeatureComparer();
return aFeatureComparator.Compare(anAFeature, aBFeature);
}
}
private List<FeatureGroup> myGroups;
public void AddFeature(string theGroupName, string theSubgroupName, bool theHasParameters, MTKBase_Feature theFeature)
{
int aRes = myGroups.FindIndex(theGroup => theGroup.myName == theGroupName);
if (aRes == -1)
{
myGroups.Add(new FeatureGroup(theGroupName, theSubgroupName, theHasParameters));
aRes = myGroups.Count - 1;
}
FeatureGroup aGroup = myGroups[aRes];
FeatureMapType aSubgroups = aGroup.myFeatureSubgroups;
if (aSubgroups.ContainsKey(theFeature))
{
++aSubgroups[theFeature];
}
else
{
aSubgroups[theFeature] = 1;
}
}
public void Print(string theFeatureType, Action<MTKBase_Feature> thePrintFeatureParameters)
{
myGroups.Sort(new FeatureGroupComparer());
uint aTotalCount = 0;
foreach (var i in myGroups)
{
uint aFeatureCount = i.FeatureCount();
aTotalCount += aFeatureCount;
Console.WriteLine($" {i.myName}: {aFeatureCount}");
if (!i.myHasParameters)
{
continue;
}
string aSubgroupName = i.mySubgroupName;
foreach (var j in i.myFeatureSubgroups)
{
Console.WriteLine($" {j.Value} {aSubgroupName} with");
thePrintFeatureParameters(j.Key);
}
}
Console.WriteLine($"\n Total {theFeatureType}: {aTotalCount}\n");
}
public static void PrintFeatureParameter<T>(string theName, T theValue, string theUnits)
{
Console.WriteLine($" {theName}: {theValue} {theUnits}");
}
}
}
The mtk namespace is intended to become the primary namespace for MTK and replace direct use of the c...
Contains classes, namespaces, enums, types, and global functions related to Manufacturing Toolkit.
using System;
namespace shape_processor
{
abstract class ShapeProcessor : ModelElementVoidVisitor
{
public override void Apply(Part thePart)
{
var aPartName = thePart.Name().IsEmpty() ? new UTF16String("noname") : thePart.Name();
var aBodyVec = thePart.Bodies();
if (aBodyVec.Count != 0)
{
for (int i = 0; i < aBodyVec.Count; ++i)
{
Body aBody = aBodyVec[i];
var aShapeIt = new ShapeIterator(aBody);
foreach (var aShape in aShapeIt)
{
if (aShape.Type() == ShapeType.Solid)
{
Console.Write($"Part #{myPartIndex} [\"{aPartName}\"] - solid #{i} has:\n");
ProcessSolid(Solid.Cast(aShape));
}
else if (aShape.Type() == ShapeType.Shell)
{
Console.Write($"Part #{myPartIndex} [\"{aPartName}\"] - shell #{i} has:\n");
ProcessShell(Shell.Cast(aShape));
}
}
}
}
++myPartIndex;
}
public abstract void ProcessSolid(Solid theSolid);
public abstract void ProcessShell(Shell theShell);
private uint myPartIndex = 0;
}
abstract class SolidProcessor : ModelElementVoidVisitor
{
public override void Apply(Part thePart)
{
var aPartName = thePart.Name().IsEmpty() ? new UTF16String("noname") : thePart.Name();
var aBodyVec = thePart.Bodies();
if (aBodyVec.Count != 0)
{
for (int i = 0; i < aBodyVec.Count; ++i)
{
Body aBody = aBodyVec[i];
var aShapeIt = new ShapeIterator(aBody);
foreach (var aShape in aShapeIt)
{
if (aShape.Type() == ShapeType.Solid)
{
Console.Write($"Part #{myPartIndex} [\"{aPartName}\"] - solid #{i} has:\n");
ProcessSolid(Solid.Cast(aShape));
}
}
}
}
++myPartIndex;
}
public abstract void ProcessSolid(Solid theSolid);
private uint myPartIndex = 0;
}
abstract class SolidAndMeshProcessor : ModelElementVoidVisitor
{
public override void Apply(Part thePart)
{
string aPartName = thePart.Name().IsEmpty() ? "noname" : thePart.Name().ToString();
int aShapeIndex = 0;
var aBodyVec = thePart.Bodies();
foreach (var aBody in aBodyVec)
{
if(MeshBody.CompareType(aBody))
{
ProcessMeshBody(MeshBody.Cast(aBody), aPartName, ref aShapeIndex);
} else if(SolidBody.CompareType(aBody))
{
Console.Write($"Part #{myPartIndex} [\"{aPartName}\"] - solid #{aShapeIndex} has:\n");
ProcessSolid(SolidBody.Cast(aBody).Solid(), aPartName, aShapeIndex++);
}
}
++myPartIndex;
}
public abstract void ProcessSolid(Solid theSolid, string thePartName, int theShapeIndex);
public abstract void ProcessITS(IndexedTriangleSet theITS, string thePartName, int theShapeIndex);
private void ProcessMeshBody(MeshBody theMeshBody, string thePartName, ref int theShapeIndex)
{
var aMeshShapes = theMeshBody.Shapes();
foreach (var aShape in aMeshShapes)
{
if(IndexedTriangleSet.CompareType(aShape))
{
Console.Write($"Part #{myPartIndex} [\"{thePartName}\"] #{theShapeIndex}:\n");
ProcessITS(IndexedTriangleSet.Cast(aShape), thePartName, theShapeIndex++);
}
}
}
protected uint myPartIndex = 0;
}
}
Defines classes, types, enums, and functions related to topological entities and scene graph elements...
using feature_group;
using shape_processor;
using System;
using System.Collections.Generic;
namespace feature_recognizer
{
class Program
{
static int Main(string[] args)
{
if (args.Length != 2)
{
Console.WriteLine("Usage: " +
$"{System.Reflection.Assembly.GetExecutingAssembly().Location} <input_file> <operation>, where:");
Console.WriteLine($" <input_file> is a name of the file to be read");
Console.WriteLine($" <operation> is a name of desired machining operation");
Console.WriteLine($"");
PrintSupportedOperations();
return 1;
}
string aKey = MTKLicenseKey.Value();
if (!LicenseHelper.SetupRuntimeKey())
{
return 1;
}
try
{
LicenseManager.Activate(aKey);
}
catch (LicenseError theException)
{
LicenseManager.Deactivate();
Console.WriteLine("Failed to activate Manufacturing Toolkit license: " + theException.what());
return 1;
}
string aSource = args[0];
var aModel = new Model();
var aReader = new ModelReader();
if (!aReader.Read(new UTF16String(aSource), aModel))
{
LicenseManager.Deactivate();
Console.WriteLine($"Failed to read the file {aSource}");
return 1;
}
Console.WriteLine($"Model: {aModel.Name()}\n");
string anOperationStr = args[1];
Machining_OperationType anOperation = OperationType(anOperationStr);
if (anOperation == Machining_OperationType.Machining_OT_Undefined)
{
LicenseManager.Deactivate();
Console.WriteLine($"Unsupported operation - {anOperationStr}");
Console.WriteLine($"Please use one of the following.");
PrintSupportedOperations();
return 1;
}
var aPartProcessor = new PartProcessor(anOperation);
var aVisitor = new ModelElementUniqueVisitor(aPartProcessor);
aModel.Accept(aVisitor);
LicenseManager.Deactivate();
return 0;
}
class PartProcessor : SolidProcessor
{
public PartProcessor(Machining_OperationType theOperation)
{
myOperation = theOperation;
}
public override void ProcessSolid(Solid theSolid)
{
var aParam = new Machining_FeatureRecognizerParameters();
aParam.SetOperation(myOperation);
var aRecognizer = new Machining_FeatureRecognizer(aParam);
var aFeatureList = aRecognizer.Perform(theSolid);
PrintFeatures(aFeatureList);
}
private Machining_OperationType myOperation = Machining_OperationType.Machining_OT_Undefined;
}
static void PrintSupportedOperations()
{
Console.WriteLine($"Supported operations:");
Console.WriteLine($" milling:\t CNC Machining Milling feature recognition");
Console.WriteLine($" turning:\t CNC Machining Lathe+Milling feature recognition");
}
static Machining_OperationType OperationType(string theOperationStr)
{
var aProcessDictionary = new Dictionary<string, Machining_OperationType>()
{
{ "milling", Machining_OperationType.Machining_OT_Milling},
{ "turning", Machining_OperationType.Machining_OT_LatheMilling}
};
Machining_OperationType aProcess = Machining_OperationType.Machining_OT_Undefined;
bool aRes = aProcessDictionary.TryGetValue(theOperationStr, out aProcess);
if (aRes)
{
return aProcess;
}
return Machining_OperationType.Machining_OT_Undefined;
}
static void PrintFeatures(MTKBase_FeatureList theFeatureList)
{
FeatureGroupManager aManager = new FeatureGroupManager();
Action<MTKBase_FeatureList> GroupByParameters = null;
GroupByParameters = new Action<MTKBase_FeatureList>((theFeatures) =>
{
for (uint i = 0; i < theFeatures.Size(); ++i)
{
MTKBase_Feature aFeature = theFeatures.Feature(i);
if (Machining_TurningFace.CompareType(aFeature))
{
Machining_TurningFace aTurningFace = Machining_TurningFace.Cast(aFeature);
aManager.AddFeature(FaceTypeToString(aTurningFace.Type()), "Turning Face(s)", true, aFeature);
}
else if (Machining_Face.CompareType(aFeature))
{
Machining_Face aFace = Machining_Face.Cast(aFeature);
aManager.AddFeature(FaceTypeToString(aFace.Type()), "", false, aFeature);
}
else if (Machining_Countersink.CompareType(aFeature))
{
aManager.AddFeature("Countersink(s)", "Countersink(s)", true, aFeature);
}
else if (Machining_Thread.CompareType(aFeature))
{
aManager.AddFeature("Thread(s)", "Thread(s)", true, aFeature);
}
else if (Machining_ThreadedHole.CompareType(aFeature))
{
Machining_ThreadedHole aThreadedHole = Machining_ThreadedHole.Cast(aFeature);
string aGroupName = "Threaded " + HoleTypeToString(aThreadedHole.Type());
aManager.AddFeature(aGroupName, "Threaded Hole(s)", true, aFeature);
}
else if (Machining_Hole.CompareType(aFeature))
{
Machining_Hole aHole = Machining_Hole.Cast(aFeature);
aManager.AddFeature(HoleTypeToString(aHole.Type()), "Hole(s)", true, aFeature);
}
else if (Machining_SteppedHole.CompareType(aFeature))
{
aManager.AddFeature("Stepped Hole(s)", "Stepped Hole(s)", true, aFeature);
}
else if (Machining_Pocket.CompareType(aFeature))
{
Machining_Pocket aPocket = Machining_Pocket.Cast(aFeature);
aManager.AddFeature(PocketTypeToString(aPocket.Type()), "", true, aFeature);
}
else if (MTKBase_Boss.CompareType(aFeature))
{
aManager.AddFeature("Boss(es)", "Boss(es)", true, aFeature);
}
else if (Machining_TurningGroove.CompareType(aFeature))
{
Machining_TurningGroove aTurningGroove = Machining_TurningGroove.Cast(aFeature);
aManager.AddFeature(TurningGrooveTypeToString(aTurningGroove.Type()), "", true, aFeature);
}
else if (Machining_Bore.CompareType(aFeature))
{
aManager.AddFeature("Bore(s)", "", true, aFeature);
}
}
});
GroupByParameters(theFeatureList);
Action<MTKBase_Feature> PrintFeatureParameters = theFeature =>
{
if (Machining_TurningFace.CompareType(theFeature))
{
Machining_TurningFace aTurningFace = Machining_TurningFace.Cast(theFeature);
FeatureGroupManager.PrintFeatureParameter("radius", aTurningFace.Radius(), "mm");
}
else if (Machining_Face.CompareType(theFeature))
{
}
else if (Machining_Countersink.CompareType(theFeature))
{
Machining_Countersink aCountersink = Machining_Countersink.Cast(theFeature);
cadex.Geom.Direction anAxis = aCountersink.Axis().Axis();
feature_group.Direction aDir = new feature_group.Direction(anAxis.X(), anAxis.Y(), anAxis.Z());
FeatureGroupManager.PrintFeatureParameter("radius", aCountersink.Radius(), "mm");
FeatureGroupManager.PrintFeatureParameter("depth", aCountersink.Depth(), "mm");
FeatureGroupManager.PrintFeatureParameter("axis", aDir, "");
}
else if (Machining_Thread.CompareType(theFeature))
{
Machining_Thread aThread = Machining_Thread.Cast(theFeature);
cadex.Geom.Direction anAxis = aThread.Axis().Direction();
feature_group.Direction aDir = new feature_group.Direction(anAxis.X(), anAxis.Y(), anAxis.Z());
FeatureGroupManager.PrintFeatureParameter("minor diameter", aThread.MinorDiameter(), "mm");
FeatureGroupManager.PrintFeatureParameter("major diameter", aThread.MajorDiameter(), "mm");
FeatureGroupManager.PrintFeatureParameter("pitch", aThread.Pitch(), "mm");
FeatureGroupManager.PrintFeatureParameter("length", aThread.Length(), "mm");
FeatureGroupManager.PrintFeatureParameter("axis", aDir, "");
}
else if (Machining_ThreadedHole.CompareType(theFeature))
{
Machining_ThreadedHole aThreadedHole = Machining_ThreadedHole.Cast(theFeature);
cadex.Geom.Direction anAxis = aThreadedHole.Axis().Axis();
feature_group.Direction aDir = new feature_group.Direction(anAxis.X(), anAxis.Y(), anAxis.Z());
FeatureGroupManager.PrintFeatureParameter("minor radius", aThreadedHole.MinorRadius(), "mm");
FeatureGroupManager.PrintFeatureParameter("major radius", aThreadedHole.MajorRadius(), "mm");
FeatureGroupManager.PrintFeatureParameter("thread length", aThreadedHole.ThreadLength(), "mm");
FeatureGroupManager.PrintFeatureParameter("pitch", aThreadedHole.Pitch(), "mm");
FeatureGroupManager.PrintFeatureParameter("depth", aThreadedHole.Depth(), "mm");
FeatureGroupManager.PrintFeatureParameter("axis", aDir, "");
}
else if (Machining_Hole.CompareType(theFeature))
{
Machining_Hole aHole = Machining_Hole.Cast(theFeature);
cadex.Geom.Direction anAxis = aHole.Axis().Axis();
feature_group.Direction aDir = new feature_group.Direction(anAxis.X(), anAxis.Y(), anAxis.Z());
FeatureGroupManager.PrintFeatureParameter("radius", aHole.Radius(), "mm");
FeatureGroupManager.PrintFeatureParameter("depth", aHole.Depth(), "mm");
FeatureGroupManager.PrintFeatureParameter("axis", aDir, "");
}
else if (Machining_SteppedHole.CompareType(theFeature))
{
Machining_SteppedHole aHole = Machining_SteppedHole.Cast(theFeature);
FeatureGroupManager.PrintFeatureParameter("depth", aHole.Depth(), "mm");
}
else if (Machining_Pocket.CompareType(theFeature))
{
Machining_Pocket aPocket = Machining_Pocket.Cast(theFeature);
cadex.Geom.Direction anAxis = aPocket.Axis().Direction();
feature_group.Direction aDir = new feature_group.Direction(anAxis.X(), anAxis.Y(), anAxis.Z());
FeatureGroupManager.PrintFeatureParameter("length", aPocket.Length(), "mm");
FeatureGroupManager.PrintFeatureParameter("width", aPocket.Width(), "mm");
FeatureGroupManager.PrintFeatureParameter("depth", aPocket.Depth(), "mm");
FeatureGroupManager.PrintFeatureParameter("maximum cutter radius", aPocket.MaxCutterRadius(), "mm");
FeatureGroupManager.PrintFeatureParameter("axis", aDir, "");
}
else if (MTKBase_Boss.CompareType(theFeature))
{
MTKBase_Boss aBoss = MTKBase_Boss.Cast(theFeature);
FeatureGroupManager.PrintFeatureParameter("length", aBoss.Length(), "mm");
FeatureGroupManager.PrintFeatureParameter("width", aBoss.Width(), "mm");
FeatureGroupManager.PrintFeatureParameter("height", aBoss.Height(), "mm");
}
else if (Machining_TurningGroove.CompareType(theFeature))
{
Machining_TurningGroove aTurningGroove = Machining_TurningGroove.Cast(theFeature);
FeatureGroupManager.PrintFeatureParameter("radius", aTurningGroove.Radius(), "mm");
FeatureGroupManager.PrintFeatureParameter("depth", aTurningGroove.Depth(), "mm");
FeatureGroupManager.PrintFeatureParameter("width", aTurningGroove.Width(), "mm");
}
else if (Machining_Bore.CompareType(theFeature))
{
Machining_Bore aBore = Machining_Bore.Cast(theFeature);
FeatureGroupManager.PrintFeatureParameter("radius", aBore.Radius(), "mm");
FeatureGroupManager.PrintFeatureParameter("depth", aBore.Depth(), "mm");
}
};
aManager.Print("features", PrintFeatureParameters);
}
static string FaceTypeToString(Machining_FaceType theType)
{
switch (theType)
{
case Machining_FaceType.Machining_FT_FlatFaceMilled: return "Flat Face Milled Face(s)";
case Machining_FaceType.Machining_FT_FlatSideMilled: return "Flat Side Milled Face(s)";
case Machining_FaceType.Machining_FT_CurvedMilled: return "Curved Milled Face(s)";
case Machining_FaceType.Machining_FT_CircularMilled: return "Circular Milled Face(s)";
case Machining_FaceType.Machining_FT_ConvexProfileEdgeMilling: return "Convex Profile Edge Milling Face(s)";
case Machining_FaceType.Machining_FT_ConcaveFilletEdgeMilling: return "Concave Fillet Edge Milling Face(s)";
case Machining_FaceType.Machining_FT_FlatMilled: return "Flat Milled Face(s)";
case Machining_FaceType.Machining_FT_TurnDiameter: return "Turn Diameter Face(s)";
case Machining_FaceType.Machining_FT_TurnForm: return "Turn Form Face(s)";
case Machining_FaceType.Machining_FT_TurnFace: return "Turn Face Face(s)";
default:
break;
}
return "Face(s)";
}
static string PocketTypeToString(Machining_PocketType theType)
{
switch (theType)
{
case Machining_PocketType.Machining_PT_Closed: return "Closed Pocket(s)";
case Machining_PocketType.Machining_PT_Open: return "Open Pocket(s)";
case Machining_PocketType.Machining_PT_Through: return "Through Pocket(s)";
default:
break;
}
return "Pocket(s)";
}
static string HoleTypeToString(Machining_HoleType theType)
{
switch (theType)
{
case Machining_HoleType.Machining_HT_Through: return "Through Hole(s)";
case Machining_HoleType.Machining_HT_FlatBottom: return "Flat Bottom Hole(s)";
case Machining_HoleType.Machining_HT_Blind: return "Blind Hole(s)";
case Machining_HoleType.Machining_HT_Partial: return "Partial Hole(s)";
default:
break;
}
return "Hole(s)";
}
static string TurningGrooveTypeToString(Machining_TurningGrooveType theType)
{
switch (theType)
{
case Machining_TurningGrooveType.Machining_TGT_OuterDiameter: return "Outer Diameter Groove(s)";
case Machining_TurningGrooveType.Machining_TGT_InnerDiameter: return "Inner Diameter Groove(s)";
case Machining_TurningGrooveType.Machining_TGT_EndFace: return "End Face Groove(s)";
default:
break;
}
return "Turning Groove(s)";
}
}
}