//
// (C) Copyright 2003-2019 by Autodesk, Inc.
//
// Permission to use, copy, modify, and distribute this software in
// object code form for any purpose and without fee is hereby granted,
// provided that the above copyright notice appears in all copies and
// that both that copyright notice and the limited warranty and
// restricted rights notice below appear in all supporting
// documentation.
//
// AUTODESK PROVIDES THIS PROGRAM "AS IS" AND WITH ALL FAULTS.
// AUTODESK SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTY OF
// MERCHANTABILITY OR FITNESS FOR A PARTICULAR USE. AUTODESK, INC.
// DOES NOT WARRANT THAT THE OPERATION OF THE PROGRAM WILL BE
// UNINTERRUPTED OR ERROR FREE.
//
// Use, duplication, or disclosure by the U.S. Government is subject to
// restrictions set forth in FAR 52.227-19 (Commercial Computer
// Software - Restricted Rights) and DFAR 252.227-7013(c)(1)(ii)
// (Rights in Technical Data and Computer Software), as applicable.
//
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using System.Reflection;
namespace Revit.SDK.Samples.WindowWizard.CS
{
///
/// The wizard form
///
public partial class WizardForm : System.Windows.Forms.Form
{
#region Class Memeber Variables
///
/// store the wizard parameter
///
WizardParameter m_para;
///
/// store the family types list
///
List m_types = new List();
///
/// store the bindSource
///
BindingSource bindSource = new BindingSource();
///
/// store the new type button tooltip
///
ToolTip m_newTip = new ToolTip();
///
/// store the copy type button tooltip
///
ToolTip m_copyTip = new ToolTip();
///
/// store the error tooltip
///
ToolTip m_errorTip = new ToolTip();
///
/// store the font
///
Font m_highFont, m_commonFont;
///
/// store DoubleHungWinPara list
///
BindingList paraList = new BindingList();
#endregion
///
/// constructor of WizardForm
///
/// the WizardParameter
public WizardForm(WizardParameter para)
{
m_para = para;
InitializeComponent();
InitializePara();
m_newTip.SetToolTip(button_newType, "Add new type");
m_copyTip.SetToolTip(button_duplicateType, "Duplicate the type");
m_errorTip.ShowAlways = false;
m_highFont = InputDimensionLabel.Font;
m_commonFont = WindowPropertyLabel.Font;
SetPanelVisibility(2);
}
///
/// The nextbutton click function
///
/// sender
/// EventArgs
private void Step1_NextButton_Click(object sender, EventArgs e)
{
if (this.panel1.Visible)
{
InitializePara();
SetPanelVisibility(2);
}
else if (this.panel2.Visible)
{
transforData();
SetPanelVisibility(3);
}
else if (this.panel3.Visible)
{
SetPanelVisibility(4);
SetGridData();
}
else if (this.panel4.Visible)
{
m_para.PathName = this.m_pathName.Text;
this.DialogResult = DialogResult.OK;
Close();
}
}
///
/// set panel visibility
///
/// panel number
private void SetPanelVisibility(int panelNum)
{
switch (panelNum)
{
case 1:
this.panel1.Visible = true;
this.panel2.Visible = false;
this.panel3.Visible = false;
this.panel4.Visible = false;
this.Step1_BackButton.Enabled = false;
break;
case 2:
this.panel1.Visible = false;
this.panel2.Visible = true;
this.panel3.Visible = false;
this.panel4.Visible = false;
this.Step1_BackButton.Enabled = false;
this.InputDimensionLabel.ForeColor = System.Drawing.Color.Black;
this.InputDimensionLabel.Font = m_highFont;
this.WindowPropertyLabel.ForeColor = System.Drawing.Color.Gray;
this.WindowPropertyLabel.Font = m_commonFont;
this.InputPathLabel.ForeColor = System.Drawing.Color.Gray;
this.InputPathLabel.Font = m_commonFont;
break;
case 3:
this.panel3.Visible = true;
this.panel1.Visible = false;
this.panel2.Visible = false;
this.panel4.Visible = false;
this.Step1_BackButton.Enabled = true;
this.Step1_NextButton.Text = "Next >";
this.InputPathLabel.ForeColor = System.Drawing.Color.Gray;
this.InputDimensionLabel.Font = m_commonFont;
this.WindowPropertyLabel.ForeColor = System.Drawing.Color.Black;
this.WindowPropertyLabel.Font = m_highFont;
this.InputPathLabel.ForeColor = System.Drawing.Color.Gray;
this.InputPathLabel.Font = m_commonFont;
break;
case 4:
this.panel1.Visible = false;
this.panel2.Visible = false;
this.panel3.Visible = false;
this.panel4.Visible = true;
this.Step1_BackButton.Enabled = true;
this.Step1_NextButton.Text = "Finish";
this.InputPathLabel.ForeColor = System.Drawing.Color.Gray;
this.InputDimensionLabel.Font = m_commonFont;
this.WindowPropertyLabel.ForeColor = System.Drawing.Color.Gray;
this.WindowPropertyLabel.Font = m_commonFont;
this.InputPathLabel.ForeColor = System.Drawing.Color.Black;
this.InputPathLabel.Font = m_highFont;
break;
}
}
///
/// the backbutton click function
///
/// sender
/// EventArgs
private void Step1_BackButton_Click(object sender, EventArgs e)
{
if (this.panel2.Visible)
{
SetPanelVisibility(1);
}
else if (this.panel3.Visible)
{
SetPanelVisibility(2);
}
else if (this.panel4.Visible)
{
SetPanelVisibility(3);
}
}
///
/// transfer data
///
private void transforData()
{
if (m_para.m_template == "DoubleHung")
{
DoubleHungWinPara dbhungPara = new DoubleHungWinPara(m_para.Validator.IsMetric);
dbhungPara.Height = Convert.ToDouble(m_height.Text);
dbhungPara.Width = Convert.ToDouble(m_width.Text);
dbhungPara.Inset = Convert.ToDouble(m_inset.Text);
dbhungPara.SillHeight = Convert.ToDouble(m_sillHeight.Text);
dbhungPara.Type = m_comboType.Text;
m_para.CurrentPara = dbhungPara;
if (!m_para.WinParaTab.Contains(dbhungPara.Type))
{
m_para.WinParaTab.Add(dbhungPara.Type, dbhungPara);
m_comboType.Items.Add(dbhungPara.Type);
}
else
{
m_para.WinParaTab[dbhungPara.Type] = dbhungPara;
}
}
Update();
}
///
/// Initialize data
///
private void InitializePara()
{
DoubleHungWinPara dbhungPara = new DoubleHungWinPara(m_para.Validator.IsMetric);
if (!m_para.WinParaTab.Contains(dbhungPara.Type))
{
m_para.WinParaTab.Add(dbhungPara.Type, dbhungPara);
m_types.Add(dbhungPara.Type);
}
else
{
m_para.WinParaTab[dbhungPara.Type] = dbhungPara;
}
bindSource.DataSource = m_types;
this.m_comboType.Items.Add(m_para.CurrentPara.Type);
this.m_comboType.SelectedIndex = 0;
this.m_glassMat.DataSource = m_para.GlassMaterials;
this.m_sashMat.DataSource = m_para.FrameMaterials;
this.m_pathName.Text = m_para.PathName;
SetParaText(dbhungPara);
}
///
/// The newtype button click function
///
/// sender
/// EventArgs
private void button_newType_Click(object sender, EventArgs e)
{
transforData();
DoubleHungWinPara newPara = new DoubleHungWinPara(m_para.Validator.IsMetric);
SetParaText(newPara);
this.m_comboType.Focus();
}
///
/// The duplicatebutton click function
///
/// sender
/// EventArgs
private void button_duplicateType_Click(object sender, EventArgs e)
{
transforData();
DoubleHungWinPara copyPara = new DoubleHungWinPara((DoubleHungWinPara)m_para.CurrentPara);
SetParaText(copyPara);
this.m_comboType.Focus();
}
///
/// set WindowParameter text
///
/// the WindowParameter
private void SetParaText(WindowParameter para)
{
DoubleHungWinPara dbhungPara = para as DoubleHungWinPara;
m_sillHeight.Text = dbhungPara.SillHeight.ToString();
m_width.Text = dbhungPara.Width.ToString();
m_height.Text = dbhungPara.Height.ToString();
m_inset.Text = dbhungPara.Inset.ToString();
m_comboType.Text = dbhungPara.Type;
}
///
/// set grid data
///
private void SetGridData()
{
paraList.Clear();
foreach (String key in m_para.WinParaTab.Keys)
{
DoubleHungWinPara para = m_para.WinParaTab[key] as DoubleHungWinPara;
if (null == para)
{
continue;
}
paraList.Add(para);
}
this.dataGridView1.DataSource = paraList;
}
///
/// m_height textbox's text changed event
///
/// sender
/// EventArgs
private void m_height_TextChanged(object sender, EventArgs e)
{
ValidateInput(m_height);
}
///
/// m_width textbox's text changed event
///
/// sender
/// EventArgs
private void m_width_TextChanged(object sender, EventArgs e)
{
ValidateInput(m_width);
}
///
/// m_inset textbox's text changed event
///
/// sender
/// EventArgs
private void m_inset_TextChanged(object sender, EventArgs e)
{
ValidateInput(m_inset);
}
///
/// m_sillHeight textbox's text changed event
///
/// sender
/// EventArgs
private void m_sillHeight_TextChanged(object sender, EventArgs e)
{
ValidateInput(m_sillHeight);
}
///
/// m_comboType SelectedIndexChanged event
///
/// sender
/// EventArgs
private void m_comboType_SelectedIndexChanged(object sender, EventArgs e)
{
m_para.CurrentPara = m_para.WinParaTab[m_comboType.SelectedItem.ToString()] as WindowParameter;
SetParaText(m_para.CurrentPara);
}
///
/// m_glassMat SelectedIndexChanged event
///
/// sender
/// EventArgs
private void m_glassMat_SelectedIndexChanged(object sender, EventArgs e)
{
m_para.GlassMat = m_glassMat.SelectedItem.ToString();
}
///
/// m_sashMat SelectedIndexChanged event
///
/// sender
/// EventArgs
private void m_sashMat_SelectedIndexChanged(object sender, EventArgs e)
{
m_para.SashMat = m_sashMat.SelectedItem.ToString();
}
///
/// validate control input value
///
/// the control
private bool ValidateInput(Control control)
{
if (null == control)
{
return true;
}
if (String.IsNullOrEmpty(control.Text))
{
this.Step1_NextButton.Enabled = false;
return false;
}
TextBox textbox = control as TextBox;
if (null == textbox)
{
return true;
}
double value = 0.0;
String result = m_para.Validator.IsDouble(textbox.Text, ref value);
if (!String.IsNullOrEmpty(result))
{
m_errorTip.SetToolTip(textbox, result);
textbox.Text = String.Empty;
this.Step1_NextButton.Enabled = false;
return false;
}
m_errorTip.RemoveAll();
switch (textbox.Name)
{
case "m_height":
result = m_para.Validator.IsHeightInRange(value);
break;
case "m_width":
result = m_para.Validator.IsWidthInRange(value);
break;
case "m_inset":
result = m_para.Validator.IsInsetInRange(value);
break;
case "m_sillHeight":
result = m_para.Validator.IsSillHeightInRange(value);
break;
default:
break;
}
if (!String.IsNullOrEmpty(result))
{
m_errorTip.SetToolTip(textbox, result);
this.Step1_NextButton.Enabled = false;
return false;
}
m_errorTip.RemoveAll();
this.Step1_NextButton.Enabled = true;
return true;
}
///
/// m_buttonBrowser click event
///
/// sender
/// EventArgs
private void m_buttonBrowser_Click(object sender, EventArgs e)
{
SaveFileDialog saveDialog = new SaveFileDialog();
saveDialog.CheckPathExists = true;
saveDialog.SupportMultiDottedExtensions = true;
saveDialog.OverwritePrompt = true;
saveDialog.ValidateNames = true;
saveDialog.Filter = "Family file(*.rfa)|*.rfa|All files(*.*)|*.*";
saveDialog.FilterIndex = 2;
if (DialogResult.OK == saveDialog.ShowDialog())
{
if (!String.IsNullOrEmpty(saveDialog.FileName))
{
m_pathName.Text = saveDialog.FileName;
}
}
}
///
/// m_height leave event
///
/// sender
/// EventArgs
private void m_height_Leave(object sender, EventArgs e)
{
CheckValue(m_height);
}
///
/// check input
///
/// the host control
private void CheckValue(Control control)
{
if (String.IsNullOrEmpty(control.Text))
{
control.Focus();
m_errorTip.SetToolTip(control, "Please input a valid value");
}
if (!ValidateInput(control))
{
control.Focus();
control.Text = String.Empty;
}
}
///
/// m_width leave event
///
/// sender
/// EventArgs
private void m_width_Leave(object sender, EventArgs e)
{
CheckValue(m_width);
}
///
/// m_inset leave event
///
/// sender
/// EventArgs
private void m_inset_Leave(object sender, EventArgs e)
{
CheckValue(m_inset);
}
///
/// m_sillHeight leave event
///
/// sender
/// EventArgs
private void m_sillHeight_Leave(object sender, EventArgs e)
{
CheckValue(m_sillHeight);
}
///
/// m_comboType leave event
///
/// sender
/// EventArgs
private void m_comboType_Leave(object sender, EventArgs e)
{
CheckValue(m_comboType);
}
///
/// Step1_HelpButton click event to open the help document
///
/// sender
/// EventArgs
private void Step1_HelpButton_Click(object sender, EventArgs e)
{
string path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
char sp = System.IO.Path.DirectorySeparatorChar;//{'\\'};
path = path.Substring(0, path.LastIndexOf(sp));
path = path.Substring(0, path.LastIndexOf(sp)) + sp + "ReadMe_WindowWizard.rtf";
System.Diagnostics.Process.Start(path);
}
}
}