static int GetFormattedTimeHelper(int localeId, int sec, int min, int hour, int day, int month, int year, IntPtr pStringHolderFormat, IntPtr pResultString) { int rc; try { string dateformat = StringHolder.GetString(pStringHolderFormat); System.Globalization.CultureInfo ci = new System.Globalization.CultureInfo(localeId); DateTime dt = new DateTime(year, month, day, hour, min, sec); dt = dt.ToLocalTime(); string s = string.IsNullOrEmpty(dateformat) ? dt.ToString(ci) : dt.ToString(dateformat, ci); UnsafeNativeMethods.ON_wString_Set(pResultString, s); rc = 1; } catch (Exception ex) { UnsafeNativeMethods.ON_wString_Set(pResultString, ex.Message); rc = 0; } return rc; }
static int GetNowHelper(int localeId, IntPtr pStringHolderFormat, IntPtr pResultString) { int rc; try { string dateformat = StringHolder.GetString(pStringHolderFormat); if (string.IsNullOrEmpty(dateformat)) return 0; // surround apostrophe with quotes in order to keep the formatter happy dateformat = dateformat.Replace("'", "\"'\""); System.Globalization.CultureInfo ci = new System.Globalization.CultureInfo(localeId); DateTime now = System.DateTime.Now; string s = string.IsNullOrEmpty(dateformat) ? now.ToString(ci) : now.ToString(dateformat, ci); UnsafeNativeMethods.ON_wString_Set(pResultString, s); rc = 1; } catch (Exception ex) { UnsafeNativeMethods.ON_wString_Set(pResultString, ex.Message); rc = 0; } return rc; }
static int EvaluateExpressionHelper(IntPtr statementsAsStringHolder, IntPtr expressionAsStringHolder, int rhinoDocId, IntPtr pResultString) { int rc = 0; #if RHINO_SDK try { string state = StringHolder.GetString(statementsAsStringHolder); string expr = StringHolder.GetString(expressionAsStringHolder); PythonScript py = PythonScript.Create(); object eval_result = py.EvaluateExpression(state, expr); if (null != eval_result) { string s = null; RhinoDoc doc = RhinoDoc.FromId(rhinoDocId); if (eval_result is double || eval_result is float) { if (doc != null) { int display_precision = doc.DistanceDisplayPrecision; string format = "{0:0."; format = format.PadRight(display_precision + format.Length, '0') + "}"; s = string.Format(format, eval_result); } else s = eval_result.ToString(); } else if (eval_result is string) { s = eval_result.ToString(); } System.Collections.IEnumerable enumerable = eval_result as System.Collections.IEnumerable; if (string.IsNullOrEmpty(s) && enumerable != null) { string format = null; if (doc != null) { int display_precision = doc.DistanceDisplayPrecision; format = "{0:0."; format = format.PadRight(display_precision + format.Length, '0') + "}"; } System.Text.StringBuilder sb = new System.Text.StringBuilder(); foreach (object obj in enumerable) { if (sb.Length > 0) sb.Append(", "); if ( (obj is double || obj is float) && !string.IsNullOrEmpty(format) ) { sb.AppendFormat(format, obj); } else { sb.Append(obj); } } s = sb.ToString(); } if (string.IsNullOrEmpty(s)) s = eval_result.ToString(); UnsafeNativeMethods.ON_wString_Set(pResultString, s); } rc = 1; } catch (Exception ex) { UnsafeNativeMethods.ON_wString_Set(pResultString, ex.Message); rc = 0; } #endif return rc; }