// // (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.Text; using System.Windows.Forms; using System.Resources; using System.Collections; using Autodesk.Revit.UI; namespace Revit.SDK.Samples.GridCreation.CS { /// /// Class to validate input data before creating grids /// public static class Validation { // Get the resource contains strings static ResourceManager resManager = Properties.Resources.ResourceManager; /// /// Validate numbers in UI /// /// Control contains number information /// Control contains another number information /// Whether the numbers are validated public static bool ValidateNumbers(Control number1Ctrl, Control number2Ctrl) { if (!ValidateNumber(number1Ctrl) || !ValidateNumber(number2Ctrl)) { return false; } if (Convert.ToUInt32(number1Ctrl.Text) == 0 && Convert.ToUInt32(number2Ctrl.Text) == 0) { ShowWarningMessage(resManager.GetString("NumbersCannotBeBothZero"), Properties.Resources.ResourceManager.GetString("FailureCaptionInvalidValue")); number1Ctrl.Focus(); return false; } return true; } /// /// Validate number value /// /// Control contains number information /// Whether the number value is validated public static bool ValidateNumber(Control numberCtrl) { if (!ValidateNotNull(numberCtrl, "Number")) { return false; } try { uint number = Convert.ToUInt32(numberCtrl.Text); if (number > 200) { ShowWarningMessage(resManager.GetString("NumberBetween0And200"), Properties.Resources.ResourceManager.GetString("FailureCaptionInvalidValue")); numberCtrl.Focus(); return false; } } catch (OverflowException) { ShowWarningMessage(resManager.GetString("NumberBetween0And200"), Properties.Resources.ResourceManager.GetString("FailureCaptionInvalidValue")); numberCtrl.Focus(); return false; } catch (Exception) { ShowWarningMessage(resManager.GetString("NumberFormatWrong"), Properties.Resources.ResourceManager.GetString("FailureCaptionInvalidValue")); numberCtrl.Focus(); return false; } return true; } /// /// Validate length value /// /// Control contains length information /// Type of length /// Whether the length can be zero /// Whether the length value is validated public static bool ValidateLength(Control lengthCtrl, String typeName, bool canBeZero) { if (!ValidateNotNull(lengthCtrl, typeName)) { return false; } try { double length = Convert.ToDouble(lengthCtrl.Text); if (length <= 0 && !canBeZero) { ShowWarningMessage(resManager.GetString(typeName + "CannotBeNegativeOrZero"), Properties.Resources.ResourceManager.GetString("FailureCaptionInvalidValue")); lengthCtrl.Focus(); return false; } else if (length < 0 && canBeZero) { ShowWarningMessage(resManager.GetString(typeName + "CannotBeNegative"), Properties.Resources.ResourceManager.GetString("FailureCaptionInvalidValue")); lengthCtrl.Focus(); return false; } } catch (Exception) { ShowWarningMessage(resManager.GetString(typeName + "FormatWrong"), Properties.Resources.ResourceManager.GetString("FailureCaptionInvalidValue")); lengthCtrl.Focus(); return false; } return true; } /// /// Validate coordinate value /// /// Control contains coordinate information /// Whether the coordinate value is validated public static bool ValidateCoord(Control coordCtrl) { if (!ValidateNotNull(coordCtrl, "Coordinate")) { return false; } try { Convert.ToDouble(coordCtrl.Text); } catch (Exception) { ShowWarningMessage(resManager.GetString("CoordinateFormatWrong"), Properties.Resources.ResourceManager.GetString("FailureCaptionInvalidValue")); coordCtrl.Focus(); return false; } return true; } /// /// Validate start degree and end degree /// /// Control contains start degree information /// Control contains end degree information /// Whether the degree values are validated public static bool ValidateDegrees(Control startDegree, Control endDegree) { if (!ValidateDegree(startDegree) || !ValidateDegree(endDegree)) { return false; } if (Math.Abs(Convert.ToDouble(startDegree.Text) - Convert.ToDouble(endDegree.Text)) <= Double.Epsilon) { ShowWarningMessage(resManager.GetString("DegreesAreTooClose"), Properties.Resources.ResourceManager.GetString("FailureCaptionInvalidValue")); startDegree.Focus(); return false; } if (Convert.ToDouble(startDegree.Text) >= Convert.ToDouble(endDegree.Text)) { ShowWarningMessage(resManager.GetString("StartDegreeShouldBeLessThanEndDegree"), Properties.Resources.ResourceManager.GetString("FailureCaptionInvalidValue")); startDegree.Focus(); return false; } return true; } /// /// Validate degree value /// /// Control contains degree information /// Whether the degree value is validated public static bool ValidateDegree(Control degreeCtrl) { if (!ValidateNotNull(degreeCtrl, "Degree")) { return false; } try { double startDegree = Convert.ToDouble(degreeCtrl.Text); if (startDegree < 0 || startDegree > 360) { ShowWarningMessage(resManager.GetString("DegreeWithin0To360"), Properties.Resources.ResourceManager.GetString("FailureCaptionInvalidValue")); degreeCtrl.Focus(); return false; } } catch (Exception) { ShowWarningMessage(resManager.GetString("DegreeFormatWrong"), Properties.Resources.ResourceManager.GetString("FailureCaptionInvalidValue")); degreeCtrl.Focus(); return false; } return true; } /// /// Validate label /// /// Control contains label information /// List contains all labels in Revit document /// Whether the label value is validated public static bool ValidateLabel(Control labelCtrl, ArrayList allLabels) { if (!ValidateNotNull(labelCtrl, "Label")) { return false; } String labelToBeValidated = labelCtrl.Text; foreach (String label in allLabels) { if (label == labelToBeValidated) { ShowWarningMessage(resManager.GetString("LabelExisted"), Properties.Resources.ResourceManager.GetString("FailureCaptionInvalidValue")); labelCtrl.Focus(); return false; } } return true; } /// /// Assure value is not null /// /// Control contains information needs to be checked /// Type of information /// Whether the value is not null public static bool ValidateNotNull(Control control, String typeName) { if (String.IsNullOrEmpty(control.Text.TrimStart(' ').TrimEnd(' '))) { ShowWarningMessage(resManager.GetString(typeName + "CannotBeNull"), Properties.Resources.ResourceManager.GetString("FailureCaptionInvalidValue")); control.Focus(); return false; } return true; } /// /// Assure two labels are not same /// /// Control contains label information /// Control contains label information /// Whether the labels are same public static bool ValidateLabels(Control label1Ctrl, Control label2Ctrl) { if (label1Ctrl.Text.TrimStart(' ').TrimEnd(' ') == label2Ctrl.Text.TrimStart(' ').TrimEnd(' ')) { ShowWarningMessage(resManager.GetString("LabelsCannotBeSame"), Properties.Resources.ResourceManager.GetString("FailureCaptionInvalidValue")); label1Ctrl.Focus(); return false; } return true; } /// /// Show a warning message box /// /// Message /// title of message box public static void ShowWarningMessage(String message, String caption) { TaskDialog.Show(caption, message, TaskDialogCommonButtons.Ok); } } }