// Stress Branch - Ductile Material

// - This 'shortcut' script creates an structural branch under the Project object in
//   the DesignSpace tree, including results and tools appropriate for parts made
//   with ductile materials

// copied from DSConstants.js
//
var id_EquivalentStress                       = 1;
var id_MaximumShearStress                     = 5;
var id_TotalDisplacement                      = 25;
var id_StressSafetyMargin                     = 29;
var id_StressSafetyFactor                     = 30;
var id_MaximumEquivalentStressTheory          = 0;
var id_MaximumShearStressTheory               = 1;

// make sure there's an active tree object
//
if( DS.Tree )
{
    var MBBT_OK = 0;
    var MBBT_ICONWARNING = 48;
    var buttonType = MBBT_OK + MBBT_ICONWARNING;
    var caption = DS.Script.localString("ID_DesignSpaceWarning");
    var message;
    var branchObj = null;
    var resultObj;
    var toolObj;
    branchObj = DS.Tree.FirstActiveBranch;
    if( !branchObj )
    {
        message = DS.Script.localString("ID_CannotFindBranch");
        DS.Script.WBScript.Out( message, true, caption, buttonType, this );
    }

    if( branchObj )
    {
        // get the Answers object
        var answersObj = branchObj.AnswerSet;
        if( answersObj )
        {
            var activeId = DS.Tree.FirstActiveObjectID;
            var answersObjId = answersObj.ID;
            DS.Script.changeActiveObject( answersObjId );

            if( DS.GUIState.AnswerSetLayer.CanInsertStressTool() )
            {
                // make sure there's nothing selected
                //
                SM.Clear();
                // add the stress results to the branch
                //
                resultObj = answersObj.AddResult( SM, id_EquivalentStress );
                resultObj = answersObj.AddResult( SM, id_MaximumShearStress );
                resultObj = answersObj.AddResult( SM, id_TotalDisplacement );

                // add the stress safety tools to the branch
                //
                toolObj = answersObj.AddStressSafetyTool( id_MaximumEquivalentStressTheory );
                if( toolObj )
                {
                    resultObj = toolObj.AddResult( SM, id_StressSafetyFactor );
                    resultObj = toolObj.AddResult( SM, id_StressSafetyMargin );
                }

                toolObj = answersObj.AddStressSafetyTool( id_MaximumShearStressTheory );
                if( toolObj )
                {
                    resultObj = toolObj.AddResult( SM, id_StressSafetyFactor );
                    resultObj = toolObj.AddResult( SM, id_StressSafetyMargin );
                }

                DS.Script.fillTree();

                WizObj.ShowWizard( "StressWizard.xml" );
            }
            else
            {
                //if an error occurs, alert the user that the branch was not added
                var message = DS.Script.localString("ID_InsertOutlineBranchNotValid");
                DS.Script.WBScript.Out( message, true, caption, buttonType, this );
            }

            DS.Script.changeActiveObject( activeId );
        }
    }
}
