use <threads.scad>

// Resolution of 3D model
$fn=90;
// Thickness of the walls [mm]
wall=10;

/*[ Internal threaded section ]*/
// Length of the G 1-1/2" threaded part [mm]
threadLen=50;
// Outer diameter of the thread [mm]
threadDia=47.803;
// Pitch of the thread (=turns/mm)
threadPitch=2.309;

/*[ Hose connector section ]*/
// Hose connector length
hoseCnLen=70;
// Outer diameter of hose connector [mm]
hoseCnOuterDia=75;
// Inner diameter of hose connector [mm]
hoseCnInnerDia=67;
// Size of the lands on hose connector [mm]
hoseCnLandSize=7;


/*[ Hidden ]*/
grooveLen=(hoseCnLen-3*hoseCnLandSize)/2;

// Distance between threaded part and hose connector part
//a^2 + b^2 = c^2
//c=r=hoseCnOuterDia/2
//a=(threadDia+2*wall)/2
//b=?
//b^2 = c^2 - a^2
//    _____________________________________________
//b= V ((hoseCnOuterDia/2)^2-((threadDia+2*wall)/2)^2)
b = sqrt((hoseCnOuterDia/2)^2-(threadDia/2+wall)^2);
transitionDistance= b+threadLen;
echo("DIST = ", b);
echo("transitionDistance = ", transitionDistance);


///////////////////////////////////////////////////////////////////////////////////
// main
///////////////////////////////////////////////////////////////////////////////////

difference()
{
    makeOuterForm();
    !makeInnerForm();
}

///////////////////////////////////////////////////////////////////////////////////
// make the outer hull of the hose adapter
///////////////////////////////////////////////////////////////////////////////////
module makeOuterForm()
{
    makeLands()
    {
        union()
        {
            // make threaded section
            cylinder(h=threadLen, d=threadDia+2*wall);
            // make transition between threaded section and hose connector
            translate([0, 0, transitionDistance]) sphere(d=hoseCnOuterDia);
            // make hose connector part
            translate([0, 0, transitionDistance]) cylinder(h=hoseCnLen, d=hoseCnOuterDia);
        }
    }
}
///////////////////////////////////////////////////////////////////////////////////
// make the inner form that is going to be cut out of the outer form
///////////////////////////////////////////////////////////////////////////////////
module makeInnerForm()
{
    union()
    {
        // make threaded section
        translate([0, 0, -0.1])
        metric_thread (diameter=threadDia, pitch=threadPitch, length=threadLen+0.2, 
        internal=true, n_starts=1, thread_size=-1, groove=false, square=false,
        rectangle=0, angle=27.5, taper=0, leadin=0, leadfac=1.0, test=false);
        // make transition between threaded section and hose connector
        translate([0, 0, transitionDistance-2]) sphere(d=hoseCnOuterDia-2*wall);
        // make hose connector part
        translate([0, 0, transitionDistance-2]) cylinder(h=hoseCnLen+3, d=hoseCnOuterDia-2*wall);
    }
}
///////////////////////////////////////////////////////////////////////////////////
// make the lands on the outer hull of the hose adapter
///////////////////////////////////////////////////////////////////////////////////
module makeLands()
{
    difference()
    {
        children(0);
        translate([0, 0, transitionDistance+hoseCnLandSize]) makeLandRing();
        translate([0, 0, transitionDistance+grooveLen+hoseCnLandSize*2]) makeLandRing();
    }
}
///////////////////////////////////////////////////////////////////////////////////
// make a ring that is substracted from the hose connector forming a "land"
///////////////////////////////////////////////////////////////////////////////////
module makeLandRing()
{
    difference()
    {
        cylinder(h=grooveLen, d=hoseCnOuterDia+0.1);
        cylinder(h=grooveLen, d=hoseCnInnerDia);
    }
}