// Shape Finder Branch

// - This 'shortcut' script creates a weight reduction branch under the Project object in
//   the DesignSpace tree, including a typical weight reduction result

// copied from DSConstants.js
//
//var id_ShapeFinder                    = 47;
var id_ShapeFinder                      = 185;

// 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;
    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.CanInsertShapeFinder() )
            {
                // make sure there's nothing selected
                //
                SM.Clear();
                // add the shape result to the branch
                //
                resultObj = answersObj.AddResult( SM, id_ShapeFinder );
                // set the default target weight reduction
                //
                resultObj.TargetReduction = 20;

                DS.Script.fillTree();

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

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