////////////////////////////////////////////////////////////////////////////
//                                                                        //
//          Copyright (C) 2011 ANSYS Inc.  All rights reserved.           //
//     Unauthorized use, distribution or duplication is prohibited.      //
//                                                                        //
// This file contains proprietary software licensed from ANSYS Inc.       //
// This header must remain in any source code despite modifications or    //
// enhancements by any party.                                             //
//                                                                        //
////////////////////////////////////////////////////////////////////////////
//
// $Workfile: $
// $Revision: 503475 $
// $Date: 2011-09-08 01:31:15 -0400 (Thu, 08 Sep 2011) $
// $Author: mnarayan $
//
// Decription: JScript for the AG (ANSYS Geometry) Workbench GUI
//             Implements cross section scripts
//
// All declared identifiers start with 'ag'
////////////////////////////////////////////////////////////////////////////

function agCrossSection_RECT (B, H, Nb, Nh)
{
  // B  rectangle width
  // H  rectangle height
  // Nb number of cells along width
  // Nh number of cells along height
  
  // Tighten tolerances
  ag.m.BeginUserScript();

  //Create the Cross Section
  ag.b.CSRect(B, H);

  // Loosen tolerances
  ag.m.EndUserScript();
}

function agCrossSection_RECT_wrapper ()
{
  var i = ag.gui.getLengthUnit();
  switch (i)
  {
    case 1:
      agCrossSection_RECT(.01, .01, 2, 2);
      break;
    case 3:
      agCrossSection_RECT(1.0, 1.0, 2, 2);
      break;
    case 4:
      agCrossSection_RECT(10.0, 10.0, 2, 2);
      break;
    case 6:
      agCrossSection_RECT(1.0, 1.0, 2, 2);
      break;
    case 7:
      agCrossSection_RECT(.25, .25, 2, 2);
      break;
    case 9:
      agCrossSection_RECT(10.0, 10.0, 2, 2);
      break;
  }
}

//--------------------------------------------------------------------------

function agCrossSection_QUAD (yI, zI, yJ, zJ, yK, zK, yL, zL, Ng, Nh)
{
  // yI, zI  coordinates of point I
  // yJ, zJ  coordinates of point J
  // yK, zK  coordinates of point K
  // yL, zL  coordinates of point L
  // Ng number of cells along width
  // Nh number of cells along height
  
  // Tighten tolerances
  ag.m.BeginUserScript();
  
  // Create the cross section plane and sketch
  var csplane = ag.gui.CreateCrossSection(ag.m.constant("QUAD_CROSS_SECTION"));
  var cssketch = csplane.GetActiveSketch();
  
  // Draw the four edges
  var IJ_line = cssketch.Line (yI, zI, yJ, zJ);
  var JK_line = cssketch.Line (yJ, zJ, yK, zK);
  var KL_line = cssketch.Line (yK, zK, yL, zL);
  var LI_line = cssketch.Line (yL, zL, yI, zI);
  ag.gui.Regen();
  
  // Make coincident constraints
  csplane.CoincidentCon (IJ_line.End, yJ, zJ,
                         JK_line.Base, yJ, zJ);
  csplane.CoincidentCon (JK_line.End, yK, zK,
                         KL_line.Base, yK, zK);
  csplane.CoincidentCon (KL_line.End, yL, zL,
                         LI_line.Base, yL, zL);
  csplane.CoincidentCon (LI_line.End, yI, zI,
                         IJ_line.Base, yI, zI);
  
  //Set Permanent Lock on Plane
  csplane.SetPermPlaneLock();

  // Loosen tolerances
  ag.m.EndUserScript();
}

function agCrossSection_QUAD_wrapper ()
{
  var i = ag.gui.getLengthUnit();
  switch (i)
  {
    case 1:
      agCrossSection_QUAD (-0.01, -0.01, 0.01, -0.01, 0.01, 0.01, -0.01, 0.01, 2, 2);
      break;
    case 3:
      agCrossSection_QUAD (-1.0, -1.0, 1.0, -1.0, 1.0, 1.0, -1.0, 1.0, 2, 2);
      break;
    case 4:
      agCrossSection_QUAD (-5.0, -5.0, 5.0, -5.0, 5.0, 5.0, -5.0, 5.0, 2, 2);
      break;
    case 6:
      agCrossSection_QUAD (-1.0, -1.0, 1.0, -1.0, 1.0, 1.0, -1.0, 1.0, 2, 2);
      break;
    case 7:
      agCrossSection_QUAD (-.25, -.25, .25, -.25, .25, .25, -.25, .25, 2, 2);
      break;
    case 9:
      agCrossSection_QUAD (-5.0, -5.0, 5.0, -5.0, 5.0, 5.0, -5.0, 5.0, 2, 2);
      break;
  }
}

//--------------------------------------------------------------------------

function agCrossSection_CSOLID (R, N, T)
{
  // R  radius of circle
  // N  number of divisions around circumference
  // T  number of divisions through the radius
  
  // Tighten tolerances
  ag.m.BeginUserScript();
  
  //Create the Cross Section
  ag.b.CSCirc(R);

  // Loosen tolerances
  ag.m.EndUserScript();
}

function agCrossSection_CSOLID_wrapper ()
{
  var i = ag.gui.getLengthUnit();
  switch (i)
  {
    case 1:
      agCrossSection_CSOLID (0.01, 8, 2);
      break;
    case 3:
      agCrossSection_CSOLID (1.0, 8, 2);
      break;
    case 4:
      agCrossSection_CSOLID (10.0, 8, 2);
      break;
    case 6:
      agCrossSection_CSOLID (1.0, 8, 2);
      break;
    case 7:
      agCrossSection_CSOLID (0.25, 8, 2);
      break;
    case 9:
      agCrossSection_CSOLID (10.0, 8, 2);
      break;
  }
}

//--------------------------------------------------------------------------

function agCrossSection_CTUBE (Ri, Ro, N)
{
  // Ri  Inner radius of the tube
  // Ro  Outer radius of the tube
  // N   number of cells along the circumference
  
  // Tighten tolerances
  ag.m.BeginUserScript();

  // Create the cross section
  ag.b.CSCircTube(Ri, Ro);

  // Loosen tolerances
  ag.m.EndUserScript();
}

function agCrossSection_CTUBE_wrapper ()
{
  var i = ag.gui.getLengthUnit();
  switch (i)
  {
    case 1:
      agCrossSection_CTUBE (0.01, 0.02, 8);
      break;
    case 3:
      agCrossSection_CTUBE (1.0, 2.0, 8);
      break;
    case 4:
      agCrossSection_CTUBE (10.0, 20.0, 8);
      break;
    case 6:
      agCrossSection_CTUBE (1.0, 2.0, 8);
      break;
    case 7:
      agCrossSection_CTUBE (0.25, 0.5, 8);
      break;
    case 9:
      agCrossSection_CTUBE (10.0, 20.0, 8);
      break;
  }
}

//--------------------------------------------------------------------------

function agCrossSection_CHAN (W1, W2, W3, t1, t2, t3)
{
  // W1, W2   lengths of the flanges
  // W3       overall depth
  // t1, t2   flange thicknesses
  // t3       web thicknesses
  
  // Tighten tolerances
  ag.m.BeginUserScript();
  
  // Create the cross section
  
  //
  //     |<---W2--->|
  //     |          |
  //     |    2     |
  //  _  +----------+
  //  ^  |      t2  |3
  //  |  |  +-------+
  //  |  |  |   4
  //  |  |  |
  // W3 1|  |5
  //  |  |  |
  //  |  |t3|
  //  |  |  |   6
  //  |  |  +-------+
  //  v  |      t1  |7
  //  _  X----------+
  //     |    8     |
  //     |          |
  //     |<---W1--->|
 
  ag.b.CSCSection(W1, W2, W3, t1, t2, t3);

  // Loosen tolerances
  ag.m.EndUserScript();
}

function agCrossSection_CHAN_wrapper ()
{
  var i = ag.gui.getLengthUnit();
  switch (i)
  {
    case 1:
      agCrossSection_CHAN (0.1, 0.1, 0.2, 0.01, 0.01, 0.01);
      break;
    case 3:
      agCrossSection_CHAN (10.0, 10.0, 20.0, 1.0, 1.0, 1.0);
      break;
    case 4:
      agCrossSection_CHAN (10.0, 10.0, 20.0, 1.0, 1.0, 1.0);
      break;
    case 6:
      agCrossSection_CHAN (3.0, 3.0, 6.0, 1.0, 1.0, 1.0);
      break;
    case 7:
      agCrossSection_CHAN (0.25, 0.25, 0.5, 0.1, 0.1, 0.1);
      break;
    case 9:
      agCrossSection_CHAN (10.0, 10.0, 20.0, 1.0, 1.0, 1.0);
      break;
  }
}

//--------------------------------------------------------------------------

function agCrossSection_I (W1, W2, W3, t1, t2, t3)
{
  // W1, W2   width of the top and bottom flanges
  // W3       overall depth
  // t1, t2   flange thicknesses
  // t3       web thicknesses
  
  // Tighten tolerances
  ag.m.BeginUserScript();
  
  // Create the cross section plane and sketch
  //
  //  |<------W2------->|
  //  |                 |
  //  |        6        |
  //  +-----------------+-----
  // 5|  t2             |7  ^
  //  +------+   +------+   |
  //     4   |   |   8      |
  //         |   |          |
  //        3|   |9         W3
  //         | t3|          |
  //         |   |          |
  //     2   |   |  10      |
  //  +------+   +------+   |
  // 1|  t1             |11 V
  //  +--------X--------+-----
  //  |       12        |
  //  |                 |
  //  |<------W1------->|

  ag.b.CSISection(W1, W2, W3, t1, t2, t3);
  
  // Loosen tolerances
  ag.m.EndUserScript();
}

function agCrossSection_I_wrapper ()
{
  var i = ag.gui.getLengthUnit();
  switch (i)
  {
    case 1:
      agCrossSection_I (0.1, 0.1, 0.2, 0.01, 0.01, 0.01);
      break;
    case 3:
      agCrossSection_I (10.0, 10.0, 20.0, 1.0, 1.0, 1.0);
      break;
    case 4:
      agCrossSection_I (10.0, 10.0, 20.0, 1.0, 1.0, 1.0);
      break;
    case 6:
      agCrossSection_I (3.0, 3.0, 6.0, 1.0, 1.0, 1.0);
      break;
    case 7:
      agCrossSection_I (0.25, 0.25, 0.5, 0.1, 0.1, 0.1);
      break;
    case 9:
      agCrossSection_I (10.0, 10.0, 20.0, 1.0, 1.0, 1.0);
      break;
  }
}

//--------------------------------------------------------------------------

function agCrossSection_Z (W1, W2, W3, t1, t2, t3)
{
  // W1, W2   flange lengths
  // W3       overall depth
  // t1, t2   flange thicknesses
  // t3       stem thicknesses
  
  // Tighten tolerances
  ag.m.BeginUserScript();

  // Create the cross section plane and sketch
  //
  //     |<---W2--->|
  //     |          |
  //     |    4     |
  //  ---+----------+
  //  ^ 3|          |
  //  |  +------+   |
  //  |     2   |   |
  //  |         |   |
  //  |        1|   |5
  //  W3        |   |
  //  |         |   |
  //  |         |   |  6
  //  |         |   +------+
  //  V         | X        |7
  //  ----------+----------+
  //            |     8    |
  //            |          |
  //            |<---W1--->|

  ag.b.CSZSection(W1, W2, W3, t1, t2, t3);
  
  // Loosen tolerances
  ag.m.EndUserScript();
}

function agCrossSection_Z_wrapper ()
{
  var i = ag.gui.getLengthUnit();
  switch (i)
  {
    case 1:
      agCrossSection_Z (0.1, 0.1, 0.2, 0.01, 0.01, 0.01);
      break;
    case 3:
      agCrossSection_Z (10.0, 10.0, 20.0, 1.0, 1.0, 1.0);
      break;
    case 4:
      agCrossSection_Z (10.0, 10.0, 20.0, 1.0, 1.0, 1.0);
      break;
    case 6:
      agCrossSection_Z (3.0, 3.0, 6.0, 1.0, 1.0, 1.0);
      break;
    case 7:
      agCrossSection_Z (0.25, 0.25, 0.5, 0.1, 0.1, 0.1);
      break;
    case 9:
      agCrossSection_Z (10.0, 10.0, 20.0, 1.0, 1.0, 1.0);
      break;
  }
}

//--------------------------------------------------------------------------

function agCrossSection_L (W1, W2, t1, t2)
{
  // W1, W2   leg lengths
  // t1, t2   leg thicknesses
  
  // Tighten tolerances
  ag.m.BeginUserScript();
  
  // Create the cross section plane and sketch
  //       2
  //  ---+---+
  //  ^  |   |
  //  |  |   |
  //  |  |t2 |
  //  | 1|   |3
  //  W2 |   |
  //  |  |   |  4
  //  |  |   +------+
  //  v  | X     t1 |5
  //  ---+----------+
  //       6
  //     |          |
  //     |<---W1--->|
  
  ag.b.CSLSection(W1, W2, t1, t2);

  // Loosen tolerances
  ag.m.EndUserScript();
}

function agCrossSection_L_wrapper ()
{
  var i = ag.gui.getLengthUnit();
  switch (i)
  {
    case 1:
      agCrossSection_L (0.1, 0.1, 0.01, 0.01);
      break;
    case 3:
      agCrossSection_L (10.0, 10.0, 1.0, 1.0);
      break;
    case 4:
      agCrossSection_L (10.0, 10.0, 1.0, 1.0);
      break;
    case 6:
      agCrossSection_L (3.0, 3.0, 1.0, 1.0);
      break;
    case 7:
      agCrossSection_L (.25, .25, .1, 0.1);
      break;
    case 9:
      agCrossSection_L (10.0, 10.0, 1.0, 1.0);
      break;
  }
}

//--------------------------------------------------------------------------

function agCrossSection_T (W1, W2, t1, t2)
{
  // W1   flange width
  // W2   overall depth
  // t1   flange thickness
  // t2   stem thickness
  
  // Tighten tolerances
  ag.m.BeginUserScript();
  
  // Create the cross section plane and sketch
  //           4
  //         +---+----------
  //         |   |         ^
  //         |t2 |         |
  //         |   |         |
  //        3|   |5       W2
  //         |   |         |
  //     2   |   |   6     |
  //  +------+   +------+  |
  // 1|   t1   X        |7 v
  //  +-----------------+----
  //  |        8        |
  //  |                 |
  //  |<-------W1------>|
  
  ag.b.CSTSection(W1, W2, t1, t2);

  // Loosen tolerances
  ag.m.EndUserScript();
}

function agCrossSection_T_wrapper ()
{
  var i = ag.gui.getLengthUnit();
  switch (i)
  {
    case 1:
      agCrossSection_T (0.1, 0.1, 0.01, 0.01);
      break;
    case 3:
      agCrossSection_T (10.0, 10.0, 1.0, 1.0);
      break;
    case 4:
      agCrossSection_T (10.0, 10.0, 1.0, 1.0);
      break;
    case 6:
      agCrossSection_T (3.0, 3.0, 1.0, 1.0);
      break;
    case 7:
      agCrossSection_T (.25, .25, 0.1, 0.1);
      break;
    case 9:
      agCrossSection_T (10.0, 10.0, 1.0, 1.0);
      break;
  }
}

//--------------------------------------------------------------------------

function agCrossSection_HAT (W1, W2, W3, W4, t1, t2, t3, t4, t5)
{
  // W1, W2   width of the brim
  // W3       width of the top of the hat
  // W4       overall depth
  // t1, t2   thicknesses of the brim
  // t3       thickness of the top of the hat
  // t4, t5   web thicknesses
  
  // Tighten tolerances
  ag.m.BeginUserScript();
  
  // Create the cross section plane and sketch
  //           |<---W3---->|
  //           |           |
  //           |     4     |
  //  ---------+-----------+
  //  ^        |     t3    |
  //  |        |  +-----+  |
  //  |        |  |  10 |  |
  //  W4      3|  |     |  |5
  //  |        |t4|     |t5|
  //  |        |  |11  9|  |
  //  |        |  |     |  |
  //  |     2  |  |     |  |  6
  //  |  +-----+  |     |  +-----+
  //  v 1| t1     |     |     t2 |7
  //  ---X--------+     +--------+
  //     |   12   |     |   8    |
  //     |        |     |        |
  //     |<--W1-->|     |<--W2-->|

  ag.b.CSHatSection(W1, W2, W3, W4, t1, t2, t3, t4, t5);

  // Loosen tolerances
  ag.m.EndUserScript();
}

function agCrossSection_HAT_wrapper ()
{
  var i = ag.gui.getLengthUnit();
  switch (i)
  {
    case 1:
      agCrossSection_HAT (0.05, 0.05, 0.1, 0.2, 0.01, 0.01, 0.01, 0.01, 0.01);
      break;
    case 3:
      agCrossSection_HAT (5.0, 5.0, 10.0, 20.0, 1.0, 1.0, 1.0, 1.0, 1.0);
      break;
    case 4:
      agCrossSection_HAT (5.0, 5.0, 10.0, 20.0, 1.0, 1.0, 1.0, 1.0, 1.0);
      break;
    case 6:
      agCrossSection_HAT (3.0, 3.0, 6.0, 12.0, 1.0, 1.0, 1.0, 1.0, 1.0);
      break;
    case 7:
      agCrossSection_HAT (0.25, 0.25, 0.5, 1.0, 0.1, 0.1, 0.1, 0.1, 0.1);
      break;
    case 9:
      agCrossSection_HAT (5.0, 5.0, 10.0, 20.0, 1.0, 1.0, 1.0, 1.0, 1.0);
      break;
  }
}

//--------------------------------------------------------------------------

function agCrossSection_HREC (W1, W2, t1, t2, t3, t4)
{
  // W1              outer width of the box
  // W2              outer height of the box
  // t1, t2, t3, t4  wall thicknesses
  
  // Tighten tolerances
  ag.m.BeginUserScript();
  
  // Create the cross section plane and sketch
  //              2
  //  ---+-----------------+
  //  ^  |          t4     |
  //  |  |  +-----------+  |
  //  |  |  |     6     |  |
  //  | 1|  |5         7|  |3
  //  W2 |t1|     8     |t2|
  //  |  |  +-----------+  |
  //  v  |          t3     |
  //  ---X-----------------+
  //     |        4        |
  //     |                 |
  //     |<-------W1------>|

  ag.b.CSRectTube(W1, W2, t1, t2, t3, t4);
  
  // Loosen tolerances
  ag.m.EndUserScript();
}

function agCrossSection_HREC_wrapper ()
{
  var i = ag.gui.getLengthUnit();
  switch (i)
  {
    case 1:
      agCrossSection_HREC (0.1, 0.1, 0.01, 0.01, 0.01, 0.01);
      break;
    case 3:
      agCrossSection_HREC (10.0, 10.0, 1.0, 1.0, 1.0, 1.0);
      break;
    case 4:
      agCrossSection_HREC (10.0, 10.0, 1.0, 1.0, 1.0, 1.0);
      break;
    case 6:
      agCrossSection_HREC (3.0, 3.0, 1.0, 1.0, 1.0, 1.0);
      break;
    case 7:
      agCrossSection_HREC (0.25, 0.25, 0.1, 0.1, 0.1, 0.1);
      break;
    case 9:
      agCrossSection_HREC (10.0, 10.0, 1.0, 1.0, 1.0, 1.0);
      break;
  }
}

//--------------------------------------------------------------------------

function agCrossSection_ASEC (A, Ixx, Ixy, Iyy, Iw, J, CGx, CGy, SHx, SHy)
{
  // A     area of cross section
  // Ixx   moment of inertia about y-axis
  // Ixy   product of inertia
  // Iyy   moment of inertia about z-axis
  // Iw    warping constant
  // J     torsional constant
  // CGx   Y coordinate of centroid
  // CGy   Z coordinate of centroid
  // SHx   Y coordinate of shear center
  // SHy   Z coordinate of shear center

  // Tighten tolerances
  ag.m.BeginUserScript();

  // Create the cross section plane and sketch
  ag.b.CSUserInt(A, Ixx, Ixy, Iyy, Iw, J, CGx, CGy, SHx, SHy);

  // Loosen tolerances
  ag.m.EndUserScript();
}

function agCSUser_Sketch ()
{
  ag.b.CSUserSketch();
}


//--------------------------------------------------------------------------

function agCrossSectionSelect ()
{
  // This helper selects the last cross section in the tree.
  // It's used to display a cross section right after it is created.
  // Then it deselects it to remove the yellow highlight.
  var crossRoot = agTreeGetCrossSectionRoot();
  if (crossRoot)
    {
      // Find the last cross section in the tree
      var n_children = crossRoot.Children;
      var csNode = crossRoot.Child;
      if (csNode && (n_children > 1))
        csNode = csNode.LastSibling;

      // Select the last cross section
      if (csNode)
        {
          agTree_SelectNode (csNode); // Selct it to force the Look-At
          agUtilityHandler ("", 902); // Turn the yellow highlight off
        }
    }
}
