Hide menu
Loading...
Searching...
No Matches
machining/feature_recognizer/feature_recognizer.java

Refer to the CNC Machining Feature Recognizer Example

feature_group.java

// ****************************************************************************
// $Id$
//
// Copyright (C) 2008-2014, Roman Lygin. All rights reserved.
// Copyright (C) 2014-2025, CADEX. All rights reserved.
//
// This file is part of the Manufacturing Toolkit 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.
//
// ****************************************************************************
import cadex.*;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Map;
import java.util.TreeMap;
import java.util.function.Consumer;
class FeatureComparator implements Comparator<MTKBase_Feature>
{
@Override
public int compare(MTKBase_Feature theA, MTKBase_Feature theB)
{
MTKBase_FeatureComparator aComparator = new MTKBase_FeatureComparator();
boolean anALessThanB = aComparator.Apply(theA, theB);
if (anALessThanB)
{
return -1;
}
boolean aBLessThanA = aComparator.Apply(theB, theA);
if (aBLessThanA)
{
return 1;
}
return 0;
}
}
class Pair
{
public Pair(double theFirst, double theSecond)
{
this.First = theFirst;
this.Second = theSecond;
}
public String toString()
{
return String.format("%f x %f", First, Second);
}
public double First;
public double Second;
}
class Dimension
{
public Dimension(double theL, double theW, double theD)
{
this.L = theL;
this.W = theW;
this.D = theD;
}
public String toString()
{
return String.format("%f x %f x %f", L, W, D);
}
public double L;
public double W;
public double D;
}
class Direction
{
public Direction(double theX, double theY, double theZ)
{
this.X = theX;
this.Y = theY;
this.Z = theZ;
}
public String toString()
{
return "(" + FormattedString(X) + ", " + FormattedString(Y) + ", " + FormattedString(Z) + ")";
}
private String FormattedString(double theValue)
{
DecimalFormat aDF = new DecimalFormat("0.00");
return aDF.format(theValue);
}
public double X;
public double Y;
public double Z;
}
class FeatureMap extends TreeMap<MTKBase_Feature, Long>
{
public FeatureMap()
{
super (new FeatureComparator());
}
}
class FeatureGroupManager
{
public FeatureGroupManager()
{
myGroups = new ArrayList<FeatureGroup>();
}
public void AddFeature(String theGroupName, String theSubgroupName, boolean theHasParameters, MTKBase_Feature theFeature)
{
//find or create
int aRes = -1;
for (int i = 0; i < myGroups.size(); ++i)
{
FeatureGroup aGroup = myGroups.get(i);
if (aGroup.myName == theGroupName)
{
aRes = i;
break;
}
}
if (aRes == -1)
{
myGroups.add(new FeatureGroup(theGroupName, theSubgroupName, theHasParameters));
aRes = myGroups.size() - 1;
}
//update
FeatureGroup aGroup = myGroups.get(aRes);
FeatureMap aSubgroups = aGroup.myFeatureSubgroups;
if (aSubgroups.containsKey(theFeature))
{
aSubgroups.put(theFeature, aSubgroups.get (theFeature) + 1);
}
else
{
aSubgroups.put(theFeature, 1L);
}
}
public void Print(String theFeatureType, Consumer<MTKBase_Feature> thePrintFeatureParameters)
{
Collections.sort(myGroups, new FeatureGroupComparator());
long aTotalCount = 0;
for (FeatureGroup aGroup : myGroups)
{
long aFeatureCount = aGroup.FeatureCount();
aTotalCount += aFeatureCount;
System.out.format(" %s: %d\n", aGroup.myName, aFeatureCount);
if (!aGroup.myHasParameters)
{
continue;
}
String aSubgroupName = aGroup.mySubgroupName;
for (Map.Entry<MTKBase_Feature, Long> aFeatureSubgroup : aGroup.myFeatureSubgroups.entrySet())
{
System.out.format(" %d %s with\n", aFeatureSubgroup.getValue(), aSubgroupName);
thePrintFeatureParameters.accept(aFeatureSubgroup.getKey());
}
}
System.out.format("\n Total %s: %d\n", theFeatureType, aTotalCount);
}
public static <T> void PrintFeatureParameter(String theName, T theValue, String theUnits)
{
System.out.format(" %s: %s %s\n", theName, theValue, theUnits);
}
private class FeatureGroup
{
public FeatureGroup(String theName, String theSubgroupName, boolean theHasParameters)
{
myName = theName;
mySubgroupName = theSubgroupName;
myHasParameters = theHasParameters;
myFeatureSubgroups = new FeatureMap();
}
public long FeatureCount()
{
long aCount = 0;
for (Map.Entry<MTKBase_Feature, Long> aFeatureSubgroup : myFeatureSubgroups.entrySet())
{
aCount += aFeatureSubgroup.getValue();
}
return aCount;
}
public String myName;
public String mySubgroupName;
public boolean myHasParameters;
public FeatureMap myFeatureSubgroups;
}
private class FeatureGroupComparator implements Comparator<FeatureGroup>
{
@Override
public int compare(FeatureGroup theA, FeatureGroup theB)
{
String anAName = theA.myName;
String aBName = theB.myName;
if (anAName == aBName)
{
return 0;
}
FeatureMap anAFeatureSubgroups = theA.myFeatureSubgroups;
FeatureMap aBFeatureSubgroups = theB.myFeatureSubgroups;
if (anAFeatureSubgroups.isEmpty() || aBFeatureSubgroups.isEmpty())
{
return anAName.compareTo(aBName);
}
MTKBase_Feature anAFeature = anAFeatureSubgroups.firstKey();
MTKBase_Feature aBFeature = aBFeatureSubgroups.firstKey();
FeatureComparator aFeatureComparator = new FeatureComparator();
return aFeatureComparator.compare(anAFeature, aBFeature);
}
}
private ArrayList<FeatureGroup> myGroups;
}
Defines classes, namespaces, enums, types, and global functions related to Manufacturing Toolkit.
Definition LicenseManager_LicenseError.hxx:30

shape_processor.java

// ****************************************************************************
// $Id$
//
// Copyright (C) 2008-2014, Roman Lygin. All rights reserved.
// Copyright (C) 2014-2025, CADEX. All rights reserved.
//
// This file is part of the Manufacturing Toolkit 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.
//
// ****************************************************************************
import cadex.*;
import cadex.ModelData.*;
import cadex.Collections.*;
abstract class ShapeProcessor extends ModelElementVoidVisitor
{
public void Apply(Part thePart) {
String aPartName = thePart.Name().IsEmpty() ? "noname" : thePart.Name().Data();
BodyList aBodyList = thePart.Bodies();
for (int i = 0; i < aBodyList.size(); ++i) {
Body aBody = aBodyList.get(i);
ShapeIterator aShapeIt = new ShapeIterator(aBody);
while (aShapeIt.HasNext()) {
Shape aShape = aShapeIt.Next();
if (aShape.Type() == ShapeType.Solid) {
System.out.format("Part #%d [\"%s\"] - solid #%d has:\n", myPartIndex, aPartName, i);
ProcessSolid(Solid.Cast(aShape));
} else if (aShape.Type() == ShapeType.Shell) {
System.out.format("Part #%d [\"%s\"] - shell #%d has:\n", myPartIndex, aPartName, i);
ProcessShell(Shell.Cast(aShape));
}
}
}
++myPartIndex;
}
public abstract void ProcessSolid(Solid theSolid);
public abstract void ProcessShell(Shell theShell);
private long myPartIndex = 0;
}
abstract class SolidProcessor extends ModelElementVoidVisitor
{
public void Apply(Part thePart) {
String aPartName = thePart.Name().IsEmpty() ? "noname" : thePart.Name().Data();
// Looking for a suitable body
BodyList aBodyList = thePart.Bodies();
for (int i = 0; i < aBodyList.size(); ++i) {
Body aBody = aBodyList.get(i);
ShapeIterator aShapeIt = new ShapeIterator(aBody);
while (aShapeIt.HasNext()) {
Shape aShape = aShapeIt.Next();
if (aShape.Type() == ShapeType.Solid) {
System.out.format("Part #%d [\"%s\"] - solid #%d has:\n", myPartIndex, aPartName, i);
ProcessSolid(Solid.Cast(aShape));
}
}
}
++myPartIndex;
}
public abstract void ProcessSolid(Solid theSolid);
private long myPartIndex = 0;
}
abstract class SolidAndMeshProcessor extends ModelElementVoidVisitor
{
public void Apply(Part thePart)
{
String aPartName = thePart.Name().IsEmpty() ? "noname" : thePart.Name().Data();
BodyList aBodyList = thePart.Bodies();
for (int i = 0; i < aBodyList.size(); ++i)
{
Body aBody = aBodyList.get(i);
ShapeIterator aShapeIt = new ShapeIterator(aBody);
while (aShapeIt.HasNext())
{
Shape aShape = aShapeIt.Next();
if (aShape.Type() == ShapeType.Solid)
{
System.out.format("Part #%d [\"%s\"] - solid #%d has:\n", myPartIndex, aPartName, i);
ProcessSolid(Solid.Cast(aShape), aPartName, i);
}
}
}
++myPartIndex;
}
public abstract void ProcessSolid(Solid theSolid, String thePartName, long theShapeIndex);
protected long myPartIndex = 0;
}
Defines classes, types, enums, and functions related to topological entities and scene graph elements...
ShapeType
Defines shape type.
Definition ShapeType.hxx:27

feature_recognizer.java

// ****************************************************************************
// $Id$
//
// Copyright (C) 2008-2014, Roman Lygin. All rights reserved.
// Copyright (C) 2014-2025, CADEX. All rights reserved.
//
// This file is part of the Manufacturing Toolkit 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.
//
// ****************************************************************************
import cadex.*;
import cadex.ModelData.*;
import java.util.HashMap;
import java.util.function.Consumer;
public class feature_recognizer {
static {
try {
System.loadLibrary("CadExMTK");
} catch (UnsatisfiedLinkError e) {
System.err.println("Native code library failed to load.\n" + e);
System.exit(1);
}
}
public static void main(String[] args) {
String aKey = MTKLicenseKey.Value();
// Activate the license (the key must be defined in MTKLicenseKey.java)
if (!LicenseManager.Activate(aKey)) {
System.out.println("Failed to activate Manufacturing Toolkit license.");
System.exit(1);
}
if (args.length != 2) {
System.out.println("Usage: " + " <input_file> <operation>, where:");
System.out.println(" <input_file> is a name of the file to be read");
System.out.println(" <operation> is a name of desired machining operation");
System.out.println("");
PrintSupportedOperations();
System.exit(1);
}
String aSource = args[0];
Model aModel = new Model();
ModelReader aReader = new ModelReader();
// Reading the file
if (!aReader.Read(new UTF16String(aSource), aModel)) {
System.out.println("Failed to read the file " + aSource);
System.exit(1);
}
System.out.println("Model: " + aModel.Name() + "\n");
String anOperationStr = args[1];
Machining_OperationType anOperation = OperationType(anOperationStr);
if (anOperation == Machining_OperationType.Machining_OT_Undefined) {
System.out.println("Unsupported operation - " + anOperationStr);
System.out.println("Please use one of the following.");
PrintSupportedOperations();
System.exit(1);
}
PartProcessor aPartProcessor = new PartProcessor(anOperation);
ModelElementUniqueVisitor aVisitor = new ModelElementUniqueVisitor(aPartProcessor);
aModel.Accept(aVisitor);
}
private static void PrintSupportedOperations()
{
System.out.println("Supported operations:");
System.out.println(" milling:\t CNC Machining Milling feature recognition");
System.out.println(" turning:\t CNC Machining Lathe+Milling feature recognition");
}
private static Machining_OperationType OperationType(String theOperationStr)
{
HashMap<String, Machining_OperationType> aProcessMap = new HashMap<>();
aProcessMap.put("milling", Machining_OperationType.Machining_OT_Milling);
aProcessMap.put("turning", Machining_OperationType.Machining_OT_LatheMilling);
Machining_OperationType aProcess = aProcessMap.getOrDefault(theOperationStr,
Machining_OperationType.Machining_OT_Undefined);
return aProcess;
}
static class PartProcessor extends SolidProcessor {
public PartProcessor(Machining_OperationType theOperation) {
myOperation = theOperation;
}
public void ProcessSolid(Solid theSolid) {
Machining_FeatureRecognizerParameters aParam = new Machining_FeatureRecognizerParameters();
aParam.SetOperation(myOperation);
Machining_FeatureRecognizer aRecognizer = new Machining_FeatureRecognizer(aParam);
MTKBase_FeatureList aFeatureList = aRecognizer.Perform(theSolid);
Report.PrintFeatures(aFeatureList);
}
private Machining_OperationType myOperation = Machining_OperationType.Machining_OT_Undefined;
}
static class Report {
public static void PrintFeatures(MTKBase_FeatureList theFeatureList) {
GroupByParameters(theFeatureList);
Consumer<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)) {
//no parameters
}
else if (Machining_Countersink.CompareType(theFeature)) {
Machining_Countersink aCountersink = Machining_Countersink.Cast(theFeature);
cadex.Geom.Direction anAxis = aCountersink.Axis().Axis();
Direction aDir = new 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_Hole.CompareType(theFeature)) {
Machining_Hole aHole = Machining_Hole.Cast(theFeature);
cadex.Geom.Direction anAxis = aHole.Axis().Axis();
Direction aDir = new 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 aSteppedHole = Machining_SteppedHole.Cast(theFeature);
GroupByParameters(aSteppedHole.FeatureList());
}
else if (Machining_Pocket.CompareType(theFeature)) {
Machining_Pocket aPocket = Machining_Pocket.Cast(theFeature);
cadex.Geom.Direction anAxis = aPocket.Axis().Direction();
Direction aDir = new 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("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");
}
};
myManager.Print("features", PrintFeatureParameters);
}
private static void GroupByParameters(MTKBase_FeatureList theFeatures) {
//group by parameters to provide more compact information about features
for (long i = 0; i < theFeatures.Size(); ++i) {
MTKBase_Feature aFeature = theFeatures.Feature(i);
if (Machining_TurningFace.CompareType(aFeature)) {
Machining_TurningFace aTurningFace = Machining_TurningFace.Cast(aFeature);
myManager.AddFeature(FaceTypeToString(aTurningFace.Type()), "Turning Face(s)", true, aFeature);
}
else if (Machining_Face.CompareType(aFeature)) {
Machining_Face aFace = Machining_Face.Cast(aFeature);
myManager.AddFeature(FaceTypeToString(aFace.Type()), "", false, aFeature);
}
else if (Machining_Countersink.CompareType(aFeature)) {
myManager.AddFeature("Countersink(s)", "Countersink(s)", true, aFeature);
}
else if (Machining_Hole.CompareType(aFeature)) {
Machining_Hole aHole = Machining_Hole.Cast(aFeature);
myManager.AddFeature(HoleTypeToString(aHole.Type()), "Hole(s)", true, aFeature);
}
else if (Machining_SteppedHole.CompareType(aFeature)) {
Machining_SteppedHole aSteppedHole = Machining_SteppedHole.Cast(aFeature);
GroupByParameters(aSteppedHole.FeatureList());
}
else if (Machining_Pocket.CompareType(aFeature)) {
Machining_Pocket aPocket = Machining_Pocket.Cast(aFeature);
myManager.AddFeature(PocketTypeToString(aPocket.Type()), "", true, aFeature);
}
else if (MTKBase_Boss.CompareType(aFeature)) {
myManager.AddFeature("Boss(es)", "Boss(es)", true, aFeature);
}
}
}
private static String FaceTypeToString (Machining_FaceType theType) {
switch (theType) {
case Machining_FT_FlatFaceMilled: return "Flat Face Milled Face(s)";
case Machining_FT_FlatSideMilled: return "Flat Side Milled Face(s)";
case Machining_FT_CurvedMilled: return "Curved Milled Face(s)";
case Machining_FT_CircularMilled: return "Circular Milled Face(s)";
case Machining_FT_Deburr: return "Deburr Face(s)";
case Machining_FT_ConvexProfileEdgeMilling: return "Convex Profile Edge Milling Face(s)";
case Machining_FT_ConcaveFilletEdgeMilling: return "Concave Fillet Edge Milling Face(s)";
case Machining_FT_FlatMilled: return "Flat Milled Face(s)";
case Machining_FT_TurnDiameter: return "Turn Diameter Face(s)";
case Machining_FT_TurnForm: return "Turn Form Face(s)";
case Machining_FT_TurnFace: return "Turn Face Face(s)";
case Machining_FT_Bore: return "Bore Face(s)";
default:
break;
}
return "Face(s)";
}
private static String PocketTypeToString (Machining_PocketType theType) {
switch (theType) {
case Machining_PT_Closed: return "Closed Pocket(s)";
case Machining_PT_Open: return "Open Pocket(s)";
case Machining_PT_Through: return "Through Pocket(s)";
default:
break;
}
return "Pocket(s)";
}
private static String HoleTypeToString (Machining_HoleType theType) {
switch (theType) {
case Machining_HT_Through: return "Through Hole(s)";
case Machining_HT_FlatBottom: return "Flat Bottom Hole(s)";
case Machining_HT_Blind: return "Blind Hole(s)";
case Machining_HT_Partial: return "Partial Hole(s)";
default:
break;
}
return "Hole(s)";
}
private static FeatureGroupManager myManager = new FeatureGroupManager();
}
}
Defines a 3D Direction.
Definition Direction.hxx:34
Direction()
Constructor.
Definition Direction.cxx:38