From 2f8b882e88fe0fa0670b20b65fe674cc5738b4fe Mon Sep 17 00:00:00 2001
From: Lucio Zambon <lucio.zambon@elettra.eu>
Date: Wed, 15 Jan 2025 02:50:29 +0000
Subject: [PATCH] Update simulator2d.js

---
 simulator2d.js | 41 ++++++++++++++++++++++++++++++++---------
 1 file changed, 32 insertions(+), 9 deletions(-)

diff --git a/simulator2d.js b/simulator2d.js
index aaa526d..d7b7922 100644
--- a/simulator2d.js
+++ b/simulator2d.js
@@ -193,6 +193,7 @@
 						// console.log("path", {id:facility+"_simulatorhor", name:"simulatorhor", d: dver.join(' ')+' Z'});
 					}
 				}
+				rulerScale();
 			});
 		}
 		if (document.location.search.indexOf('simulator')>-1) {/*params.simulator=true;*/ setTimeout(simulatorSwitch, 500, compData, params);}
@@ -212,13 +213,6 @@
 		for (let i in lattice) {if (i!='conf') simfacilities.push(i);}
 		simulatorMenu(lattice, simfacilities, params);
 	}
-	if (document.location.search.indexOf('ruler')>-1) {
-		const main = document.getElementById('main');
-		main.style.cursor = 'crosshair';
-		// main.addEventListener(measurementEvent, measurementListener, false);
-		main.addEventListener('mousemove', function(e) {getRulerPosition(main, e);});
-		rulerSwitch(0, 0);
-	}
 	function getRulerPosition(main, event) {
 		const rect = main.getBoundingClientRect();
 		const x = event.clientX - rect.left;
@@ -239,18 +233,47 @@
 		const i = dindex;
 		const j = (i+1) % lattice.sr.sections.length;
 		const alpha = 180/Math.PI*Math.atan2(lattice.sr.sections[j].start.z-lattice.sr.sections[i].start.z, lattice.sr.sections[j].start.x-lattice.sr.sections[i].start.x);
-		console.log(dindex, p.dist, p.x, p.y, alpha);
+		// console.log(dindex, p.dist, p.x, p.y, alpha);
 		// $('#ruler1').attr('transform', 'translate('+ ((p.x - panZoomPanther.getPan().x)/panZoomPanther.getSizes().realZoom)+' '+((p.y - panZoomPanther.getPan().y)/panZoomPanther.getSizes().realZoom)+')');
 		$('#ruler').attr('transform', 'translate('+ (p.x)+' '+(p.y)+'),rotate('+alpha+')');
 	}
+	function rulerScale() {
+		const scaleh = $('#scaleh').children().eq(1).children()[1].value-0;
+		const scalehunit = Math.ceil(Math.log10(scaleh)/3);
+		const scalehfactor = scaleh/Math.pow(1000, scalehunit);
+		const units = ['m', 'mm', '&mu;', 'nm'];
+		console.log('scalehfactor', scaleh, scalehunit, scalehfactor);
+		$('#redruler1').attr('y2', -10000*scalehfactor);
+		for (let i=0; i<=10; i++) {
+			const yt = (-i*1000*scalehfactor);
+			$('#redtick1'+(100-i)).attr('y1', yt);
+			$('#redtick1'+(100-i)).attr('y2', yt);
+			$('#redlabel'+(100-i)).attr('y', yt);
+			$('#redlabel'+(100-i)).html(i+' '+units[scalehunit]);
+			// appendSvg("text", {id: 'tickLabel'+i, x: cx, y: cy, fill:"white", stroke:"black","stroke-width":1, "font-family":"Arial", "font-size":20, "font-weight":"bold", "text-anchor": "start"});
+		}
+	}
+	if (document.location.search.indexOf('ruler')>-1) {
+		const main = document.getElementById('main');
+		main.style.cursor = 'crosshair';
+		main.addEventListener('mousemove', function(e) {getRulerPosition(main, e);});
+		rulerSwitch(0, 0);
+	}
 	function rulerSwitch(cx, cy) {
 		appendSvg("g", {id: 'ruler'}, false, false, 'rulerclass');
 		appendSvg("line", {id: 'ruler1', x1: cx-10000, y1: cy, x2: cx+10000, y2: cy, style:"display: block", stroke:"yellow", "stroke-width":100}, false, false, 'rulerclass', '#ruler');
 		for (let i=-10; i<=10; i++) {
 			const xt = (cx+i*1000);
-			appendSvg("line", {id: 'tick1'+(i+11), x1: xt, y1: cy, x2: xt, y2: cy+300, style:"display: block", stroke:"yellow","stroke-width":100}, false, false, 'rulerclass', '#ruler');
+			appendSvg("line", {id: 'tick1'+(100-i), x1: xt, y1: cy, x2: xt, y2: cy+300, style:"display: block", stroke:"yellow","stroke-width":100}, false, false, 'rulerclass', '#ruler');
 			// appendSvg("text", {id: 'tickLabel'+i, x: cx, y: cy, fill:"white", stroke:"black","stroke-width":1, "font-family":"Arial", "font-size":20, "font-weight":"bold", "text-anchor": "start"});
 		}
+		appendSvg("g", {id: 'redruler'}, false, false, 'rulerclass', '#ruler');
+		appendSvg("line", {id: 'redruler1', x1: cx, y1: cy, x2: cx, y2: cy+10000, style:"display: block", stroke:"red", "stroke-width":100}, false, false, 'rulerclass', '#ruler');
+		for (let i=0; i<=10; i++) {
+			const yt = (cy+i*1000);
+			appendSvg("line", {id: 'redtick1'+(100-i), x1: cx, y1: yt, x2: cx+300, y2: yt, style:"display: block", stroke:"red","stroke-width":100}, false, false, 'rulerclass', '#ruler');
+			appendSvg("text", {id: 'redlabel'+(100-i), x: cx+400, y: yt, fill:"red", stroke:"black","stroke-width":1, "font-family":"Arial", "font-size":300, "font-weight":"bold", "text-anchor": "start"}, false, false, 'rulerclass', '#ruler');
+		}
 	}
 	function simulatorSwitch(simulatorData, params) {
 		simulator = params.simulator;
-- 
GitLab