// Simple example of stator geometry script
// Copyright 2018-2024, Vladimir Kuptsov
include('..\\sDraw.js');
function Stator()
{
	var Rsinner = motor.statorInnerRadius;   // Stator inner radius
	var Rsouter = motor.statorOuterRadius;   // Stator outer radius
	var Ns = motor.statorNumberSlots;        // Number of stator slots
	// Default stator dimensions:
	this.Sds = 0.5*Math.abs(Rsouter - Rsinner);  // Slot depth
	this.Ws = Math.PI*(Rsinner+Rsouter)/Ns/4;    // Slot width
};

// generate stator geometry 
Stator.prototype.getGeometry = function (){
	
	var Rsinner = motor.statorInnerRadius;   // Stator inner radius
	var Rsouter = motor.statorOuterRadius;   // Stator outer radius
	var Ns = motor.statorNumberSlots;        // Number of stator slots
	if (motor.isInnerRotor()) {   //Inner rotor
		var point_1 = new QPointF(0,  Rsinner+0.05*this.Sds);
		var point_2 = new QPointF(this.Ws, Rsinner+0.05*this.Sds);
		var point_3 = new QPointF(this.Ws, Rsinner+this.Sds);
		var point_4 = new QPointF(0,  Rsinner+this.Sds);
		//var line_1_4 = sDraw.createLineByPointAngle(point_1.x(),point_1.y(),90);
		//var line_3_4 = sDraw.createLineByPointAngle(point_3.x(),point_3.y(),135);
		//var point_4 = sDraw.intersection(line_1_4, line_3_4); 
		
		var geoms = new Array;   // stator geometry array
		geoms.push(new Segment(point_1, point_2));    // line 1-2
		geoms.push(new Segment(point_2, point_3));    // line 2-3
		geoms.push(new Segment(point_3, point_4));    // line 3-4
		geoms.push(new Arc(90-360/Ns/2,90,Rsinner,0,0));  // inner radius arc
		geoms.push(new Arc(90-360/Ns/2,90,Rsouter,0,0));  // outer radius arc	
	}
	else {     //Outer rotor
		console.error("This script does not support outer rotor geometry");
	}
	return geoms;
};
	
// assign materials
Stator.prototype.getMaterials = function() {
	var delta = 0.1;
	var materials = new Array();   // array of materials
	// stator iron material
	materials.push(new IronMaterial(0, motor.statorOuterRadius - delta) );
	// stator winding material
	var matWinding = new WindingMaterial(0, motor.statorInnerRadius + this.Sds/2);
	matWinding.layer = 1;
	materials.push(matWinding);   
	return materials;
};
	
var stator = new Stator();

// stator widgets constuctor
function StatorUI(ui)
{
	this.ui = ui;
	// Slot depth (Sds) controls:
	this.labelSds = new QLabel("Slot depth (Sds):");  // display widget name
	this.editSds = new QDoubleSpinBox;  // create a spin box widget to enter Sds value
	this.editSds.objectName = "Sds";    // name of this object
	var Sds_max = Math.abs(motor.statorOuterRadius-motor.statorInnerRadius);
	this.editSds.setRange(0.0, Sds_max); // set min and max values of Sds
	this.editSds.decimals = 2;           // precision of the Sds value in decimals
	this.editSds.singleStep = 1;         // set the step value
	this.editSds.keyboardTracking = false;
	this.editSds.value = stator.Sds;     // link the widget with variable
	this.unitsSds = new QLabel("mm");    // display units
	
	// Slot width (Ws) controls:
	this.labelWs = new QLabel("Slot width (Ws):");
	this.editWs = new QDoubleSpinBox;
	this.editWs.objectName = "Ws";
	var Ws_max = Math.PI*motor.statorInnerRadius/motor.statorNumberSlots;
	this.editWs.setRange(0.0, Ws_max);
	this.editWs.decimals = 2;
	this.editWs.singleStep = 1;
	this.editWs.keyboardTracking = false;
	this.editWs.value = stator.Ws;
	this.unitsWs = new QLabel("mm");
		
	this.layout = new QGridLayout(ui);
	// Add slot depth controls to UI:
	this.layout.addWidget(this.labelSds, 0, 0);
	this.layout.addWidget(this.editSds, 0, 1);
	this.layout.addWidget(this.unitsSds, 0, 2);
	// Add slot width controls to UI:
	this.layout.addWidget(this.labelWs, 1, 0);
	this.layout.addWidget(this.editWs, 1, 1);
	this.layout.addWidget(this.unitsWs, 1, 2);
};

StatorUI.prototype.statorDiameterChanged = function() {
	
}; 
 
// called if statorOuterDiameter changed
motor.statorOuterDiameterChanged.connect(this, function(d) {
	statorUI.statorDiameterChanged();
});

// called if statorInnerDiameter changed
motor.statorInnerDiameterChanged.connect(this, function(d) {
	statorUI.statorDiameterChanged() 
});
 
// update widgets
StatorUI.prototype.updateUI = function()
{

}

// call this function from c++ code
createStatorUI = function(ui)
{
	statorUI = new StatorUI(ui);
	statorUI.updateUI();
}
