From 51611bb8fe30650ca9c44ab248d749e647d2063e Mon Sep 17 00:00:00 2001 From: stefan schippers Date: Fri, 20 Mar 2026 11:17:25 +0100 Subject: [PATCH] apply ev_precision to cursor, waverorm and node annotation display --- src/callback.c | 18 +++++++------- src/draw.c | 26 +++++++++++---------- src/eval_expr.y | 4 ++-- src/token.c | 14 +++++------ src/xinit.c | 1 + src/xschem.h | 2 +- src/xschem.tcl | 7 ++++-- xschem_library/examples/mos_power_ampli.sch | 13 ++++++----- 8 files changed, 46 insertions(+), 39 deletions(-) diff --git a/src/callback.c b/src/callback.c index b2bc5847..ba6a4a58 100644 --- a/src/callback.c +++ b/src/callback.c @@ -389,7 +389,7 @@ void backannotate_at_cursor_b_pos(xRect *r, Graph_ctx *gr) for(i = 0; i < raw->nvars; ++i) { char s[100]; raw->cursor_b_val[i] = interpolate_yval(i, p, cursor2, sweep_idx, (p < ofs_end)); - my_snprintf(s, S(s), "%.5g", raw->cursor_b_val[i]); + sprintf(s, "%.*g", xctx->ev_precision, raw->cursor_b_val[i]); /* tclvareval("array set ngspice::ngspice_data [list {", raw->names[i], "} ", s, "]", NULL); */ Tcl_SetVar2(interp, "ngspice::ngspice_data", raw->names[i], s, TCL_GLOBAL_ONLY); } @@ -480,14 +480,14 @@ static int waves_callback(int event, int mx, int my, KeySym key, int button, int if(gr->logx) xval = pow(10, xval); if(gr->logy) yval = pow(10, yval); if(gr->unitx != 1.0) - my_snprintf(sx, S(sx), "%.5g%c", gr->unitx * xval, gr->unitx_suffix); + sprintf(sx, "%.*g%c", xctx->ev_precision, gr->unitx * xval, gr->unitx_suffix); else - my_strncpy(sx, dtoa_eng(xval, 5), S(sx)); + my_strncpy(sx, dtoa_eng(xval, xctx->ev_precision), S(sx)); if(gr->unitx != 1.0) - my_snprintf(sy, S(sy), "%.4g%c", gr->unity * yval, gr->unity_suffix); + sprintf(sy, "%.*g%c", xctx->ev_precision, gr->unity * yval, gr->unity_suffix); else - my_strncpy(sy, dtoa_eng(yval, 5), S(sy)); + my_strncpy(sy, dtoa_eng(yval, xctx->ev_precision), S(sy)); tclvareval("set measure_text \"y=", sy, "\nx=", sx, "\"", NULL); tcleval("graph_show_measure"); @@ -650,7 +650,7 @@ static int waves_callback(int event, int mx, int my, KeySym key, int button, int logcursor = mylog10(cursor); } if(fabs(xctx->mousex - W_X(logcursor)) < 10) { - tclvareval("input_line {Pos:} {} ", dtoa_eng(cursor, 5), NULL); + tclvareval("input_line {Pos:} {} ", dtoa_eng(cursor, xctx->ev_precision), NULL); cursor = atof_eng(tclresult()); if(r->flags & 4) { my_strdup(_ALLOC_ID_, &r->prop_ptr, subst_token(r->prop_ptr, "cursor1_x", dtoa(cursor))); @@ -680,7 +680,7 @@ static int waves_callback(int event, int mx, int my, KeySym key, int button, int logcursor = mylog10(cursor); } if(fabs(xctx->mousex - W_X(logcursor)) < 10) { - tclvareval("input_line {Pos:} {} ", dtoa_eng(cursor, 5), NULL); + tclvareval("input_line {Pos:} {} ", dtoa_eng(cursor, xctx->ev_precision), NULL); cursor = atof_eng(tclresult()); if(r->flags & 4) { my_strdup(_ALLOC_ID_, &r->prop_ptr, subst_token(r->prop_ptr, "cursor2_x", dtoa(cursor))); @@ -705,7 +705,7 @@ static int waves_callback(int event, int mx, int my, KeySym key, int button, int logcursor = mylog10(cursor); } if(fabs(xctx->mousey - W_Y(logcursor)) < 10) { - tclvareval("input_line {Pos:} {} ", dtoa_eng(cursor, 5), NULL); + tclvareval("input_line {Pos:} {} ", dtoa_eng(cursor, xctx->ev_precision), NULL); cursor = atof_eng(tclresult()); my_strdup(_ALLOC_ID_, &r->prop_ptr, subst_token(r->prop_ptr, "hcursor1_y", dtoa(cursor))); event = 0; button = 0; /* avoid further processing ButtonPress that might set GRAPHPAN */ @@ -720,7 +720,7 @@ static int waves_callback(int event, int mx, int my, KeySym key, int button, int logcursor = mylog10(cursor); } if(fabs(xctx->mousey - W_Y(logcursor)) < 10) { - tclvareval("input_line {Pos:} {} ", dtoa_eng(cursor, 5), NULL); + tclvareval("input_line {Pos:} {} ", dtoa_eng(cursor, xctx->ev_precision), NULL); cursor = atof_eng(tclresult()); my_strdup(_ALLOC_ID_, &r->prop_ptr, subst_token(r->prop_ptr, "hcursor2_y", dtoa(cursor))); event = 0; button = 0; /* avoid further processing ButtonPress that might set GRAPHPAN */ diff --git a/src/draw.c b/src/draw.c index ac608a6c..f8068466 100644 --- a/src/draw.c +++ b/src/draw.c @@ -22,7 +22,6 @@ #include "xschem.h" - #define xDashType LineOnOffDash /* CapNotLast, CapButt, CapRound or CapProjecting */ #define xCap CapNotLast @@ -3404,7 +3403,7 @@ static void draw_cursor(double active_cursorx, double other_cursorx, int cursor_ if(xx >= gr->x1 && xx <= gr->x2) { drawline(cursor_color, NOW, xx, gr->ry1, xx, gr->ry2, 0.0, 1, NULL); if(gr->unitx != 1.0) - my_snprintf(tmpstr, S(tmpstr), "%.5g%c", gr->unitx * active_cursorx , gr->unitx_suffix); + sprintf(tmpstr, "%.*g%c", xctx->ev_precision, gr->unitx * active_cursorx , gr->unitx_suffix); else my_snprintf(tmpstr, S(tmpstr), "%s", dtoa_eng(active_cursorx, 5)); text_bbox(tmpstr, txtsize, txtsize, 2, flip, 0, 0, xx + xoffs, gr->ry2-1, &tx1, &ty1, &tx2, &ty2, &tmp, &dtmp); @@ -3436,7 +3435,7 @@ static void draw_cursor_difference(double c1, double c2, Graph_ctx *gr) diffw = fabs(c2 - c1); if(gr->unitx != 1.0) - my_snprintf(tmpstr, S(tmpstr), "%.4g%c", gr->unitx * diffw , gr->unitx_suffix); + sprintf(tmpstr, "%.*g%c", xctx->ev_precision, gr->unitx * diffw , gr->unitx_suffix); else my_snprintf(tmpstr, S(tmpstr), "%s", dtoa_eng(diffw, 5)); text_bbox(tmpstr, txtsize, txtsize, 2, 0, 1, 0, xx, yy, &tx1, &ty1, &tx2, &ty2, &tmp, &dtmp); @@ -3466,7 +3465,7 @@ static void draw_hcursor(double active_cursory, int cursor_color, Graph_ctx *gr) if(yy >= gr->y1 && yy <= gr->y2) { drawline(cursor_color, NOW, gr->rx1 + 10, yy, gr->rx2 - 10, yy, 0.0, 1, NULL); if(gr->unity != 1.0) - my_snprintf(tmpstr, S(tmpstr), " %.5g%c ", gr->unity * active_cursory , gr->unity_suffix); + sprintf(tmpstr, " %.*g%c ", xctx->ev_precision, gr->unity * active_cursory , gr->unity_suffix); else my_snprintf(tmpstr, S(tmpstr), " %s ", dtoa_eng(active_cursory, 5)); text_bbox(tmpstr, txtsize, txtsize, 0, 0, 0, 0, gr->rx1 + 5, yy, &tx1, &ty1, &tx2, &ty2, &tmp, &dtmp); @@ -3500,7 +3499,7 @@ static void draw_hcursor_difference(double c1, double c2, Graph_ctx *gr) if(gr->digital) return; diffh = fabs(c2 - c1); if(gr->unity != 1.0) - my_snprintf(tmpstr, S(tmpstr), " %.4g%c ", gr->unity * diffh , gr->unity_suffix); + sprintf(tmpstr, " %.*g%c ", xctx->ev_precision, gr->unity * diffh , gr->unity_suffix); else my_snprintf(tmpstr, S(tmpstr), " %s ", dtoa_eng(diffh, 5)); text_bbox(tmpstr, txtsize, txtsize, 0, 0, 0, 1, xx, yy, &tx1, &ty1, &tx2, &ty2, &tmp, &dtmp); @@ -3681,7 +3680,7 @@ static void show_node_measures(int measure_p, double measure_x, double measure_p double diffx; char *fmt1, *fmt2; double yy1; - + int prec = xctx->ev_precision; if( gr->logx) cursor1 = mylog10(cursor1); yy1 = xctx->raw->values[idx][measure_p-1]; @@ -3690,14 +3689,15 @@ static void show_node_measures(int measure_p, double measure_x, double measure_p yy = yy1 + diffy / diffx * (cursor1 - measure_prev_x); if(XSIGN0(gr->gy1) != XSIGN0(gr->gy2) && fabs(yy) < 1e-12 * fabs(gr->gh)) yy = 0.0; if(yy != 0.0 && fabs(yy * gr->unity) < 1.0e-3) { - fmt1="%.2e"; - fmt2="%.2e%c"; + prec = 2; + fmt1="%.*e"; + fmt2="%.*e%c"; } else { - fmt1="%.4g"; - fmt2="%.4g%c"; + fmt1="%.*g"; + fmt2="%.*g%c"; } - if(gr->unity != 1.0) my_snprintf(tmpstr, S(tmpstr), fmt2, yy * gr->unity, gr->unity_suffix); - else my_snprintf(tmpstr, S(tmpstr), fmt1, yy); + if(gr->unity != 1.0) sprintf(tmpstr, fmt2, prec, yy * gr->unity, gr->unity_suffix); + else sprintf(tmpstr, fmt1, prec, yy); } else { double vthl, vthh; int hex_digits = ((n_bits - 1) >> 2) + 1; @@ -4174,6 +4174,7 @@ void draw_graph(int i, int flags, Graph_ctx *gr, void *ct) int save_extra_idx = -1; double cursor1, cursor2; + xctx->ev_precision = tclgetintvar("ev_precision"); if(xctx->only_probes) return; if(RECT_OUTSIDE( gr->sx1, gr->sy1, gr->sx2, gr->sy2, xctx->areax1, xctx->areay1, xctx->areax2, xctx->areay2)) return; @@ -5259,6 +5260,7 @@ void draw(void) if(!xctx || xctx->no_draw) return; tk_scaling = atof(tcleval("tk scaling")); cs = tclgetdoublevar("cadsnap"); + xctx->ev_precision = tclgetintvar("ev_precision"); cairo_font_scale = tclgetdoublevar("cairo_font_scale"); xctx->cadhalfdotsize = CADHALFDOTSIZE * (cs < 20. ? cs : 20.) / 10.; xctx->crosshair_layer = tclgetintvar("crosshair_layer"); diff --git a/src/eval_expr.y b/src/eval_expr.y index 9e89304c..0686cb78 100644 --- a/src/eval_expr.y +++ b/src/eval_expr.y @@ -237,7 +237,7 @@ void eval_expr_clear_table(void) static int kklex() { int c; - int eng = tclgetintvar("ev_precision"); + xctx->ev_precision = tclgetintvar("ev_precision"); if(!str) { return 0; } if(strstr(str, "expr(") == str) { lex_state = 1; @@ -251,7 +251,7 @@ static int kklex() lex_state = 1; str += 9; dbg(dbglev, "lex(): EXPR_ENG\n"); - engineering = eng; + engineering = xctx->ev_precision; return EXPR; } diff --git a/src/token.c b/src/token.c index f9ee7738..883a35c7 100644 --- a/src/token.c +++ b/src/token.c @@ -4229,7 +4229,7 @@ static char *get_pin_attr(const char *token, int inst, int engineering) else if(idx < 0) { valstr = "-"; } else { - valstr = engineering? dtoa_eng(val, 5) : dtoa(val); + valstr = engineering? dtoa_eng(val, xctx->ev_precision) : dtoa(val); } my_strdup2(_ALLOC_ID_, &pin_attr_value, valstr); dbg(1, "inst %d, net=%s, fqnet=%s idx=%d valstr=%s\n", inst, net, fqnet, idx, valstr); @@ -4343,7 +4343,7 @@ const char *spice_get_node(const char *token) } else { /* always use engineering as these tokens are generated from single * @spice_get_node(...) patterns */ - valstr = dtoa_eng(val, 5); + valstr = dtoa_eng(val, xctx->ev_precision); } dbg(1, "valstr=%s\n", valstr); my_strdup2(_ALLOC_ID_, &token2, str_replace(token, "@spice_get_node ", "", 0, 1)); @@ -4722,7 +4722,7 @@ const char *translate(int inst, const char* s) xctx->tok_size = 5; len = 5; } else { - valstr = engineering ? dtoa_eng(val, 5) : dtoa(val); + valstr = engineering ? dtoa_eng(val, xctx->ev_precision) : dtoa(val); len = xctx->tok_size; } if(len) { @@ -4823,7 +4823,7 @@ const char *translate(int inst, const char* s) } else { /* always use engineering as these tokens are generated from single * @spice_get_voltage patterns */ - valstr = dtoa_eng(val, 5); + valstr = dtoa_eng(val, xctx->ev_precision); len = xctx->tok_size; } if(len) { @@ -4924,7 +4924,7 @@ const char *translate(int inst, const char* s) } else { /* always use engineering as these tokens are generated from single * @spice_get_voltage patterns */ - valstr = dtoa_eng(val, 5); + valstr = dtoa_eng(val, xctx->ev_precision); len = xctx->tok_size; } if(len) { @@ -4990,7 +4990,7 @@ const char *translate(int inst, const char* s) double val1 = gnd1 ? 0.0 : xctx->raw->cursor_b_val[idx1]; double val2 = gnd2 ? 0.0 : xctx->raw->cursor_b_val[idx2]; val = val1 - val2; - valstr = engineering ? dtoa_eng(val, 5) : dtoa(val); + valstr = engineering ? dtoa_eng(val, xctx->ev_precision) : dtoa(val); len = xctx->tok_size; } if(len) { @@ -5123,7 +5123,7 @@ const char *translate(int inst, const char* s) xctx->tok_size = 1; len = 1; } else { - valstr = engineering ? dtoa_eng(val, 5) : dtoa(val); + valstr = engineering ? dtoa_eng(val, xctx->ev_precision) : dtoa(val); len = xctx->tok_size; } if(len) { diff --git a/src/xinit.c b/src/xinit.c index f3b5ef40..71522a8a 100644 --- a/src/xinit.c +++ b/src/xinit.c @@ -524,6 +524,7 @@ static void alloc_xschem_data(const char *top_path, const char *win_path) xctx->version_string = NULL; xctx->header_text = NULL; xctx->rectcolor= 4; /* this is the current layer when xschem started. */ + xctx->ev_precision= 4; /* copied from TCL ev_precision var in draw() and draw_graph() */ xctx->currsch = 0; xctx->constr_mv = 0; /* constrained move (vertical (2) / horizontal (1) ) */ xctx->ui_state = 0; diff --git a/src/xschem.h b/src/xschem.h index 4343aafe..37745003 100644 --- a/src/xschem.h +++ b/src/xschem.h @@ -1119,7 +1119,7 @@ typedef struct { int extra_raw_n; /* number of elements in array */ int extra_raw_size; /* size of raw_arr (will be incremented if needed) */ - + int ev_precision; /* copied from TCL ev_precision var in draw() and draw_graph() */ /* */ /* data related to all graphs, so not stored in per-graph graph_struct */ double graph_cursor1_x, graph_cursor2_x; diff --git a/src/xschem.tcl b/src/xschem.tcl index 1efa279a..34018e0a 100644 --- a/src/xschem.tcl +++ b/src/xschem.tcl @@ -635,6 +635,9 @@ proc from_eng {i} { ## convert number to engineering form proc to_eng {args} { + global ev_precision + if {[info exists ev_precision]} {set pr $ev_precision} else {set pr 4} + set suffix {} if {[ catch {uplevel #0 expr [join $args]} i]} { @@ -657,9 +660,9 @@ proc to_eng {args} { } else { set mult 1e18 ; set suffix a} if {$suffix ne {}} { set i [expr {$i * $mult}] - set s [format {%.5g%s} $i $suffix] + set s [format %.${pr}g%s $i $suffix] } else { - set s [format {%.5g} $i] + set s [format %.${pr}g $i] } return $s } diff --git a/xschem_library/examples/mos_power_ampli.sch b/xschem_library/examples/mos_power_ampli.sch index 878098f9..e6c178dc 100644 --- a/xschem_library/examples/mos_power_ampli.sch +++ b/xschem_library/examples/mos_power_ampli.sch @@ -1,4 +1,4 @@ -v {xschem version=3.4.6 file_version=1.2 +v {xschem version=3.4.8RC file_version=1.3 * * This file is part of XSCHEM, * a schematic capture and Spice/Vhdl/Verilog netlisting tool for circuit @@ -27,6 +27,7 @@ template="name=x1" } V {} S {} +F {} E {} L 15 270 -540 340 -470 {} L 15 270 -410 340 -470 {} @@ -408,15 +409,15 @@ node="[format %.4g [expr ([ngspice::get_voltage e6] - [ngspice::get_voltage c6]) descr = power } C {ngspice_get_expr.sym} 130 -690 0 1 {name=r33 -node="[format %.4g [expr ([ngspice::get_voltage c7] - [ngspice::get_voltage c5]) * [ngspice::get_current \{q7[ic]\}]]]" +node="[to_eng ([ngspice::get_voltage c7] - [ngspice::get_voltage c5]) * [ngspice::get_current \{q7[ic]\}]]" descr = power } C {ngspice_get_expr.sym} 200 -590 0 0 {name=r34 -node="[format %.4g [expr ([ngspice::get_voltage c5] - [ngspice::get_voltage e9]) * [ngspice::get_current \{q5[ic]\}]]]" +node="[to_eng ([ngspice::get_voltage c5] - [ngspice::get_voltage e9]) * [ngspice::get_current \{q5[ic]\}]]" descr = power } C {ngspice_get_expr.sym} 320 -590 0 1 {name=r35 -node="[format %.4g [expr ([ngspice::get_voltage c9] - [ngspice::get_voltage e9]) * [ngspice::get_current \{q9[ic]\}]]]" +node="[to_eng ([ngspice::get_voltage c9] - [ngspice::get_voltage e9]) * [ngspice::get_current \{q9[ic]\}]]" descr = power } C {ngspice_get_expr.sym} 330 -850 0 1 {name=r36 @@ -477,8 +478,8 @@ C {ngspice_get_expr.sym} 1130 -860 0 0 {name=r22 node="[ngspice::get_current \{xm1.rd[i]\}]" descr = current } -C {ngspice_get_expr.sym} 610 -830 0 1 {name=r20 -node="[format %.4g [expr [ngspice::get_current \{q6[ic]\}] / [ngspice::get_current \{q6[ib]\}] ] ]" +C {ngspice_get_expr.sym} 550 -820 0 1 {name=r20 +node="[to_eng [ngspice::get_current \{q6[ic]\}] / [ngspice::get_current \{q6[ib]\}]] " descr = beta } C {ngspice_get_expr.sym} 130 -730 0 1 {name=r21