# Script Editing - Tutorial 5 - Hole Recognition

This tutorial is an example on how you can speed up the program creation from existing 2D geometry with different machined holes.

Sample part :

In this sample part you can see different taped holes, counterbore and simple hole. We can recognize and classificate some of these diameter and associate related machining strategies.

For example , a inner circle of diameter 5mm , with an concentric arc of diameter 6mm, it represents a M6x1 tapped hole.

Or a 6.5mm inner diameter with a 10.5 outer circle, most probably it's a counterbore hole.

This time the script is composed by several functions , the necessary step are :

• Recognize a certain feature ( tapped hole, counterbore , etc )
• Associate a machining strategy to extracted recognized geometry
• Process the machining strategy.

### Holes Recognition script file

Now we create a new script file and call it "HolesRecognition".

``````#load "CommonFunctions.csx"

/// <summary>
/// This is the function you need to call from command line.
/// All the necessary sub-step are called here
/// </summary>
public void HoleRecognitionMain()
{
//Recognize and associate strategies to geometries holes
DefineHolesStrategies();

//It run all the created strategy with method above
RunStrategies();
}

/// <summary>
/// With this function, we recognize geometries as tapped holes
/// or counterbored holes or just a simple drilled holes,
/// and we associate extracted geometries to a machining strategy
/// </summary>
public void DefineHolesStrategies()
{
//
// METRIC -
if (Unit == MeasureUnit.Metric)
{
//TAPPED HOLES - (mm)
AddStrategy(Extract2Diameter(5, 4.2, true), "TapStrategy");
AddStrategy(Extract2Diameter(6, 5, true), "TapStrategy");
AddStrategy(Extract2Diameter(8, 6.8, true), "TapStrategy");
AddStrategy(Extract2Diameter(10, 8.5, true), "TapStrategy");
AddStrategy(Extract2Diameter(12, 10.2, true), "TapStrategy");
AddStrategy(Extract2Diameter(14, 12, true), "TapStrategy");
AddStrategy(Extract2Diameter(16, 14, true), "TapStrategy");
AddStrategy(Extract2Diameter(18, 15.5, true), "TapStrategy");
AddStrategy(Extract2Diameter(20, 17.5, true), "TapStrategy");

//Add here more case

//COUNTERBORED HOLES - (mm)
AddStrategy(Extract2Diameter(6.5, 10.5, false), "CounterboreStrategy");
AddStrategy(Extract2Diameter(10.5, 17, false), "CounterboreStrategy");

//SIMPLE HOLE - (mm)
AddStrategy(ExtractRangeDiameterValues(5, 22), "SimpleDrillStrategy");

}

//
// IMPERIAL
else if (Unit == MeasureUnit.Imperial)
{

// Add data in similar way as above but the dimension expressed in inch

}
}

/// <summary>
/// This strategy is called to create a TAPPED holes
/// machining from recognized geometries
/// </summary>
public void TapStrategy()
{
// These is extracted geometry or selected geometries in viewport
var geometries = GetSelectedGeometries();

// I get thread data compatible with current geometry selection
var threadData = GetThreadDataFromGeometry(geometries.FirstOrDefault());

if (threadData == null) return;// No compatible thread data is found, exit from function.

var tapDia = threadData.DiaMax;
var predrillDia = threadData.PreDrillDia;
var pitch = threadData.Pitch;

var work = AddMachineWork(WorkType.Mill_Drilling);

//Initialize machining work parameters
work = InitWithCustomParameters(work);

//Link geometry to work
LinkGeometryToWork(work, geometries);

//Define parameters
work.MillingParams.ThreadDescription = threadData.Description;
work.MillingParams.ThreadPitch = pitch;
work.MillingParams.DrillHoleDiameter = predrillDia;
work.MillingParams.DepthDrill = tapDia * 3.5;
work.MillingParams.DepthThread = tapDia * 2.5;

//Create sub operation
var centerDrillOp = work.AddSubOperation(SubOperationType.CenterDrilling);
var drillOp = work.AddSubOperation(SubOperationType.Drilling);
var tapOp = work.AddSubOperation(SubOperationType.Tapping);
}

/// <summary>
/// This strategy is called to create a COUNTERBORED holes
/// machining from recognized geometries.
/// </summary>
public void CounterboreStrategy()
{
// These is extracted geometry or selected geometries in viewport
var geometries = GetSelectedGeometries();

// The geometries should be list of a couples of circles, rappresenting
// the outer and the inner diameter of the counterbore machining.

var firstCoupleOfDiameter = geometries.FirstOrDefault();

if (firstCoupleOfDiameter.Count() != 2)
return;

//By convention the first circle is the bigger.
var circleMax = firstCoupleOfDiameter.OfType<Circle>().OrderBy(c => c.Radius).First();
var circleMin = firstCoupleOfDiameter.OfType<Circle>().OrderByDescending(c => c.Radius).First();

var work = AddMachineWork(WorkType.Mill_Drilling);

//Initialize machining work parameters
work = InitWithCustomParameters(work);

//Link geometry to work
LinkGeometryToWork(work, geometries);

var innerDia = circleMin.Radius * 2;
var outerDia = circleMax.Radius * 2;

//Define parameters
work.MillingParams.DrillHoleDiameter = innerDia;
work.MillingParams.DepthDrill = innerDia * 5;
work.MillingParams.DepthCounterbore = outerDia;

//Create sub operation
var centerDrillOp = work.AddSubOperation(SubOperationType.CenterDrilling);
var drillOp = work.AddSubOperation(SubOperationType.Drilling);

//
// If the counterbore diameter is big, i use a roughing cycle with endmill
if (Unit == MeasureUnit.Metric && outerDia > 16)
{
var endMillCounterboreOp = work.AddSubOperation(SubOperationType.Roughing);
endMillCounterboreOp.Tool = FindRotaryTool(ToolType.MillType_EndMill, 10);

}
else if (Unit == MeasureUnit.Imperial && outerDia > 0.6)
{
var endMillCounterboreOp = work.AddSubOperation(SubOperationType.Roughing);
endMillCounterboreOp.Tool = FindRotaryTool(ToolType.MillType_EndMill, 0.4);

}
else
{
//If the counterbore outerdia is less than 16mm or 0.6inch , i use a counterbore tool
var counterBoreOp = work.AddSubOperation(SubOperationType.Counterbore);
}

}

/// <summary>
/// This strategy is called to create a SIMPLE DRILLED holes
/// machining from recognized geometries.
/// </summary>
public void SimpleDrillStrategy()
{
var geometries = GetSelectedGeometries();

//From geometry recognizer function, i get all circles grouped by diameter.
//For every different diameter i create a different machining work

foreach (var groupedByDiameter in geometries)
{
// I get the first circle of the group to obtain the diameter value.
var firstCircle = groupedByDiameter.FirstOrDefault() as Circle;

if (firstCircle == null) continue;

var diameterValue = firstCircle.Radius * 2;

var work = AddMachineWork(WorkType.Mill_Drilling);

LinkGeometryToWork(work, groupedByDiameter);

work.MillingParams.DrillHoleDiameter = diameterValue;
work.MillingParams.DepthDrill = diameterValue * 5;

var centerDrillOp = work.AddSubOperation(SubOperationType.CenterDrilling);
var drillOp = work.AddSubOperation(SubOperationType.Drilling);

drillOp.Tool = FindRotaryTool(ToolType.DrillType_Drill, diameterValue);
}
}
``````

Once the script is launched , this will be the project status, 15 operations are created automatically.

Note :

The effective holes depths must be adjusted manually , since there is no way to extract this information from a 2D drawing.