private void LengthTextBox_TextChanged() { if (_lenghtTextBoxIsFocused && !string.IsNullOrEmpty(LengthModel.SelectedText)) { // run only if revit context injected if (_revitDocument != null && LengthModel.LengthRepresentation != null) { Units docUnits = _revitDocument.GetUnits(); ValueParsingOptions vpo = new ValueParsingOptions(); vpo.AllowedValues = AllowedValues.NonNegative; LengthModel.ParsingResult = UnitFormatUtils.TryParse(docUnits, UnitType.UT_Length, LengthModel.LengthRepresentation, vpo, out double output); if (LengthModel.ParsingResult == true) { LengthModel.LengthInDouble = output; output = output * 12 * 32; output = Math.Round(output, MidpointRounding.AwayFromZero); int outputInt = Convert.ToInt32(output); int count = LengthModel.ProperTextSelection.Length; if (outputInt.ToString().Length > count) { LengthModel.ParsingResult = false; LengthModel.UpdatedCodedLength = new string('9', count); } else { LengthModel.UpdatedCodedLength = PadNumberWithLeadingZeros(outputInt, count); } } else if (LengthModel.UpdatedCodedLength == null) { int count = LengthModel.ProperTextSelection.Length; LengthModel.UpdatedCodedLength = new string('9', count); } //TO DO: Change event to PropertyChanged event. This one is redundant. OnLengthParsed(EventArgs.Empty); } } }
public void UpdateLengthRepresentation() { //Check if in Revit context. if (_revitDocument != null) { Units docUnits = _revitDocument.GetUnits(); ValueParsingOptions vpo = new ValueParsingOptions(); vpo.AllowedValues = AllowedValues.NonNegative; bool parsRes = UnitFormatUtils.TryParse(docUnits, UnitType.UT_Length, LengthModel.LengthRepresentation, vpo, out double output); //If parsing succeded if (parsRes == true) { output = output * 12 * 32; output = Math.Round(output, 0, MidpointRounding.AwayFromZero); output = output / 12 / 32; LengthModel.LengthRepresentation = UnitFormatUtils.Format( docUnits, UnitType.UT_Length, output, true, true); } } }