// Halback_array_rotor
//console.log("------ start script ------");
// Copyright 2018-2024, Vladimir Kuptsov
include('..\\sDraw.js');

function Rotor()
{
	// Rotor inner radius
	this.Rrinner = motor.rotorInnerRadius;
	// Rotor outer radius
	this.Rrouter = motor.rotorOuterRadius;
	// Number of pole pairs
	this.nPolePairs = motor.rotorNumberPolePairs;
	//Air gap length
	var lag = motor.airGap;
	
	// Default rotor dimensions:
	// Magnet depth:
	this.Dpm = 5*lag;
	// Magnetization: "Radial" | "Parallel"
	this.Magnetization = "Radial";
	// Magnet shape: "Curved" | "Flat"
	this.MagnetShape = "Curved";	
	// Rotor core type: "Ironless" | "Iron"
	this.RotorCoreType = "Ironless";
	// Retaining sleeve type: "Non-conductive or no retaining sleeve" | "Conductive retaining sleeve"
	this.RetainingSleeveType = "Non-conductive or no retaining sleeve";
	// Retaining sleeve thickness
	this.Trs = 0;
};

Rotor.prototype.resetGeometry = function (){
	if(this.t<10){
		this.t = this.t + 1;
		return this.calcGeometry(); 
	}
	return;
}

// generate rotor geometry 
// call from с++ code
Rotor.prototype.getGeometry = function (){
	this.Dpm_real = this.Dpm;
	this.k_1 = false;
	this.t = 0;
	return this.calcGeometry();
}

Rotor.prototype.calcGeometry = function (){
	// Machine type: InnerRotor | OuterRotor
	var machineType = motor.machineType;
	// Rotor inner radius
	this.Rrinner = motor.rotorInnerRadius;
	// Rotor outer radius
	this.Rrouter = motor.rotorOuterRadius; 
	// Number of pole pairs
	this.nPolePairs = motor.rotorNumberPolePairs;
	
	// rotor geometry
	this.geomr = new Array;
	
	this.point_0 = new QPointF(0, 0);
	
	this.alfa = 360 / this.nPolePairs / 4;
	
	this.line_0_5 = sDraw.createLineByPointAngle(0, 0, 90 - this.alfa);
	this.line_0_3 = sDraw.createLineByPointAngle(0, 0, 90 - this.alfa / 2);
	
	this.circle_Rrouter = sDraw.createCircle(0, 0, this.Rrouter);
	this.circle_Rrinner = sDraw.createCircle(0, 0, this.Rrinner);
		
	if (this.RetainingSleeveType == "Conductive retaining sleeve") {
		this.Trs_real = this.Trs; }
	else {
		this.Trs_real = 0; }
	
	if (motor.isInnerRotor()) {  // inner rotor
		if(this.Rrouter - this.Dpm_real - this.Trs_real < this.Rrinner){
			this.Trs_real = this.Rrouter - this.Rrinner - this.Dpm_real;
			if (this.Trs_real < 0) {
				this.Trs_real = 0; }
			if (this.RetainingSleeveType == "Conductive retaining sleeve" && this.Trs > 0) {
				console.warn('The retaining sleeve thickness has been corrected to fit the available space: Trs = ' + this.Trs_real); }
		}
		if(this.Rrouter - this.Dpm_real < this.Rrinner){
			this.Dpm_real = this.Rrouter - this.Rrinner;
			console.warn('Magnet depth is greater than the difference between the outer and inner radius. Magnet depth has been corrected: Dpm = ' + this.Dpm_real);
		}
		this.circle_Rsleeve = sDraw.createCircle(0, 0, this.Rrouter - this.Trs_real);   // sleeve inner radius circle
		this.Rsleeve_inner = this.circle_Rsleeve.radius;                                // sleeve inner radius
		this.point_1 = new QPointF(0, this.Rrouter - this.Trs_real);
		this.point_2 = new QPointF(0, this.Rrouter - this.Dpm_real - this.Trs_real);
		this.arc_5_1 = new Arc(90 - this.alfa, 90, this.Rrouter - this.Trs_real, 0, 0);
		this.point_3 = sDraw.intersection(this.line_0_3, this.circle_Rsleeve, 'higher');   
		
		if(this.MagnetShape == 'Curved'){
			this.circle_Rpm = sDraw.createCircle(0, 0, this.Rrouter - this.Trs_real - this.Dpm_real);     // PM inner radius circle
			this.arc_6_2 = new Arc(90 - this.alfa, 90, this.Rrouter - this.Trs_real - this.Dpm_real, 0, 0);
			this.point_4 = sDraw.intersection(this.line_0_3, this.circle_Rpm, 'higher');  
			this.segment_4_3 = new Segment(this.point_4, this.point_3);
			
			this.Rcore = this.circle_Rpm.radius;
			
			this.geomr.push(this.arc_6_2);
		}else{    // this.MagnetShape == 'Flat'
			this.line_2_4 = sDraw.createLineByPointAngle(this.point_2.x(), this.point_2.y(), 0);
			this.point_4 = sDraw.intersection(this.line_0_3, this.line_2_4);  
			var length_0_4 = calcLengthBy2Points(0, 0, this.point_4.x(), this.point_4.y());
			if(length_0_4 > this.Rsleeve_inner){
				//this.point_4 = this.point_3; 
				//this.Dpm_real = this.Rrouter - this.point_3.y();
				this.Dpm_real = this.Rsleeve_inner - this.point_3.y();
				console.warn('Magnet depth has been corrected: Dpm = ' + this.Dpm_real);
				return this.resetGeometry();
			}
			//var angle_4x_4_6 = alfa;
			this.line_6_4 = sDraw.createLineByPointAngle(this.point_4.x(), this.point_4.y(), 180 - this.alfa);
			this.point_6 = sDraw.intersection(this.line_0_5, this.line_6_4);  
			
			this.segment_2_4 = new Segment(this.point_2, this.point_4);
			this.segment_4_6 = new Segment(this.point_4, this.point_6);
			this.segment_4_3 = new Segment(this.point_4, this.point_3);
			
			this.Rcore = length_0_4;
			
			this.geomr.push(this.segment_2_4);
			this.geomr.push(this.segment_4_6);
		}
		
		this.circle_R_magnet_point = sDraw.createCircle(0, 0, (this.Rsleeve_inner + this.Rsleeve_inner - this.Dpm_real)/2);  // for magnet material assignment
		this.circle_R_core_point = sDraw.createCircle(0, 0, (this.Rrinner + this.Rcore)/2);  // for core material assignment
		
		this.geomr.push(this.arc_5_1);
		this.geomr.push(this.segment_4_3);
		var arc_Rrinner = new Arc(90 - this.alfa, 90, this.Rrinner, 0, 0);
		this.geomr.push(arc_Rrinner);
		
		if (this.Trs_real>0) {
			var arc_5rs_1rs = new Arc(Math.radToDeg(this.line_0_5.angleRad), 90, this.Rrouter, 0, 0);
			this.geomr.push(arc_5rs_1rs);    // arc 5rs-1rs of retaining sleeve
		}
		
	}else {      // outer rotor	
		if(this.Rrinner + this.Dpm_real > this.Rrouter){
			this.Dpm_real = this.Rrouter - this.Rrinner;
			console.warn('Magnet depth is greater than the difference between the outer and inner radius. Magnet depth has corrected: Dpm = ' + this.Dpm_real);
		}
		
		this.point_1 = new QPointF(0, this.Rrinner);
		this.point_2 = new QPointF(0, this.Rrinner + this.Dpm_real);
		this.arc_5_1 = new Arc(90 - this.alfa, 90, this.Rrinner, 0, 0);
		this.point_3 = sDraw.intersection(this.line_0_3, this.circle_Rrinner, 'higher'); 
		
		if(this.MagnetShape == 'Curved'){
			this.circle_Rpm = sDraw.createCircle(0, 0, this.Rrinner + this.Dpm_real);
			this.arc_6_2 = new Arc(90 - this.alfa, 90, this.Rrinner + this.Dpm_real, 0, 0);
			this.point_4 = sDraw.intersection(this.line_0_3, this.circle_Rpm, 'higher');  
				
			this.segment_4_3 = new Segment(this.point_4, this.point_3);
			
			this.Rcore = this.circle_Rpm.radius;
			
			this.geomr.push(this.arc_6_2);
		}else{    // this.MagnetShape == 'Flat'
			this.line_2_4 = sDraw.createLineByPointAngle(this.point_2.x(), this.point_2.y(), 0);
			this.point_4 = sDraw.intersection(this.line_0_3, this.line_2_4);  
			var length_0_4 = calcLengthBy2Points(0, 0, this.point_4.x(), this.point_4.y());
			this.point_4r = sDraw.intersection(this.line_0_3, this.circle_Rrouter, 'higher'); 
			//console.log(this.point_4r);
			if(length_0_4 > this.Rrouter){
				this.Dpm_real = this.point_4r.y() - this.Rrinner;
				console.warn('Magnet depth has been corrected: Dpm = ' + this.Dpm_real);
				return this.resetGeometry();
			}
			this.line_6_4 = sDraw.createLineByPointAngle(this.point_4.x(), this.point_4.y(), 180 - this.alfa);
			this.point_6 = sDraw.intersection(this.line_0_5, this.line_6_4);  
			
			this.segment_2_4 = new Segment(this.point_2, this.point_4);
			this.segment_4_6 = new Segment(this.point_4, this.point_6);
			this.segment_4_3 = new Segment(this.point_4, this.point_3);
			
			this.Rcore = length_0_4;
			
			this.geomr.push(this.segment_2_4);
			this.geomr.push(this.segment_4_6);
		}
		
		this.circle_R_magnet_point = sDraw.createCircle(0, 0, (this.Rrinner + this.Rcore)/2);
		this.circle_R_core_point = sDraw.createCircle(0, 0, (this.Rrouter + this.Rrinner + this.Dpm_real)/2);
		
		this.geomr.push(this.arc_5_1);
		this.geomr.push(this.segment_4_3);
		
		var arc_Rrouter = new Arc(90 - this.alfa, 90, this.Rrouter, 0, 0);
		this.geomr.push(arc_Rrouter);
	}
		
	return this.geomr;
};

// call from с++ code
Rotor.prototype.getMaterials = function() { // assign material types

	var materials = new Array();
	var delta = 0.01;
	
	// rotor core
	if (Math.abs(motor.rotorOuterRadius - motor.rotorInnerRadius - this.Trs_real) > this.Dpm_real || this.MagnetShape == 'Flat') {   
		if (motor.isInnerRotor()) {  // inner rotor
			this.point_iron = sDraw.intersection(this.line_0_3, this.circle_R_core_point, 'higher'); 
			if (this.RotorCoreType == "Ironless") {
				materials.push( new GeneralMaterial(this.point_iron.x(), this.point_iron.y())); }
			else {
				materials.push( new IronMaterial(this.point_iron.x(), this.point_iron.y())); }
		}
		else {   // outer rotor		
			if (this.RotorCoreType == "Ironless") {
				materials.push( new GeneralMaterial(delta, this.circle_R_core_point.radius)); }
			else {
				materials.push( new IronMaterial(delta, this.circle_R_core_point.radius)); }
			if(this.Rcore >= this.Rrouter - 0.000001){
				this.point_iron_2 = sDraw.intersection(this.line_0_5, this.circle_R_core_point, 'right'); 
				if (this.RotorCoreType == "Ironless") {
					materials.push( new GeneralMaterial(this.point_iron_2.x() - delta, this.point_iron_2.y() + delta)); }
				else {
					materials.push( new IronMaterial(this.point_iron_2.x() - delta, this.point_iron_2.y() + delta)); }
			}
		}
	}
	
	// magnet 1
	if (this.Dpm_real > 0 || this.MagnetShape == 'Flat') {  
		if (this.Magnetization == "Radial") {
			var materialMagnet = new MagnetRadialMaterial; }
		else if (this.Magnetization == "Parallel") {
			var materialMagnet = new MagnetParallelMaterial; }
		else { 
			console.error('Undefined magnetization type'); }
		materialMagnet.center = new QPointF(0, 0);
		if (motor.isInnerRotor()) {  // inner rotor
			materialMagnet.pos = new QPointF(delta, this.circle_R_magnet_point.radius);
			materialMagnet.direction = Direction.From;
			materialMagnet.angle = 90;
		}
		else {   // outer rotor	
			this.point_magnet = sDraw.intersection(this.line_0_3, this.circle_R_magnet_point, 'right'); 
			materialMagnet.pos = new QPointF(this.point_magnet.x() - delta, this.point_magnet.y() + delta);
			materialMagnet.direction = Direction.Toward;
			materialMagnet.angle = 270;
		}
		
		materials.push(materialMagnet);
	}
	// magnet 2
	if (this.Dpm_real > 0 || this.MagnetShape == 'Flat') {   
		this.point_magnet_2 = sDraw.intersection(this.line_0_5, this.circle_R_magnet_point, 'right'); 
		if (this.Magnetization == "Radial") {
			var materialMagnet = new MagnetRadialMaterial; }
		else if (this.Magnetization == "Parallel") {
			var materialMagnet = new MagnetParallelMaterial; }
		else { 
			console.error('Undefined magnetization type'); }
		materialMagnet.center = new QPointF(0, 0);
		if (motor.isInnerRotor()) {  // inner rotor
			materialMagnet.pos = new QPointF(this.point_magnet_2.x() - delta, this.point_magnet_2.y() + delta);
		}
		else {   // outer rotor	
			this.point_magnet_2 = sDraw.intersection(this.line_0_3, this.circle_R_magnet_point, 'right'); 
			materialMagnet.pos = new QPointF(this.point_magnet.x() + delta, this.point_magnet.y() - delta);
		}
		materialMagnet.direction = Direction.CCW;
		//materialMagnet.angle = 360 - 90 / this.nPolePairs;
		materialMagnet.angle = 180 - 90 / this.nPolePairs;
		materials.push(materialMagnet);
	}
	
	if (this.Trs_real > 0) { // retaining sleeve (conductor)
		materials.push( new ConductorMaterial(delta, motor.rotorOuterRadius - this.Trs_real/2)); }   // change to 'Conductor'
	return materials;
};
	
var rotor = new Rotor();

// rotor widgets constuctor
function RotorUI(ui)
{
	this.ui = ui;

	this.labelDpm = new QLabel("Magnet depth (Dpm):");
	this.labelDpm.toolTip = "Use the ‘? Guide’ button above for explanation on this parameter";
	this.editDpm = new QDoubleSpinBox;
	this.editDpm.objectName = "Dpm";
	this.editDpm.setRange(0.0, motor.rotorOuterRadius);
	this.editDpm.decimals = 2;
	this.editDpm.singleStep = 1;
	this.editDpm.keyboardTracking = false;
	this.editDpm.value = rotor.Dpm;
	//this.editDpm['valueChanged(double)'].connect(this, function(value){
	//	rotor.Dpm = value;
	//});
	this.unitsDpm = new QLabel("mm");
	
	this.labelMagnetShape = new QLabel("Magnet shape:");
	this.comboboxMagnetShape = new QComboBox();
	this.comboboxMagnetShape.objectName = "MagnetShape";
	this.comboboxMagnetShape.addItem("Curved");
	this.comboboxMagnetShape.addItem("Flat");
	this.comboboxMagnetShape.currentText = rotor.MagnetShape;
	//this.comboboxMagnetShape['currentIndexChanged(int)'].connect(this, function(value){
	//	rotor.MagnetShape = this.comboboxMagnetShape.currentText;
	//});
	
	this.labelMagnetization = new QLabel("Magnetization:");
	this.comboboxMagnetization = new QComboBox();
	this.comboboxMagnetization.objectName = "Magnetization";
	this.comboboxMagnetization.addItem("Radial");
	this.comboboxMagnetization.addItem("Parallel");
	this.comboboxMagnetization.currentText = rotor.Magnetization;
	//this.comboboxMagnetization['currentIndexChanged(int)'].connect(this, function(value){
	//	rotor.Magnetization = this.comboboxMagnetization.currentText;
	//});
	
	this.labelRotorCoreType = new QLabel("Rotor core type:");
	this.comboboxRotorCoreType = new QComboBox();
	this.comboboxRotorCoreType.objectName = "RotorCoreType";
	this.comboboxRotorCoreType.addItem("Ironless");
	this.comboboxRotorCoreType.addItem("Iron");
	this.comboboxRotorCoreType.currentText = rotor.RotorCoreType;
	//this.comboboxRotorCoreType['currentIndexChanged(int)'].connect(this, function(value){
	//	rotor.RotorCoreType = this.comboboxRotorCoreType.currentText;
	//});
	
	this.labelRetainingSleeveType = new QLabel("Retaining sleeve type:");
	this.labelRetainingSleeveType.toolTip = "Use 'Conductive retaining sleeve' item to include eddy current loss in retaining sleeve into analysis." + "\n" +
	"Retaining sleeve will use material assigned for 'Conductor' in the 'Materials' panel on the right." + "\n" +
	"Retaining sleeve loss will appear in the 'Other eddy current loss' parameter of 'Magnetostatic Analysis'";
	this.comboboxRetainingSleeveType = new QComboBox();
	this.comboboxRetainingSleeveType.objectName = "RetainingSleeveType";
	this.comboboxRetainingSleeveType.addItem("Non-conductive or no retaining sleeve");
	this.comboboxRetainingSleeveType.addItem("Conductive retaining sleeve");
	this.comboboxRetainingSleeveType.currentText = rotor.RetainingSleeveType;
	this.comboboxRetainingSleeveType.toolTip = "Use 'Conductive retaining sleeve' item to include eddy current loss in retaining sleeve into analysis." + "\n" +
	"Retaining sleeve will use material assigned for 'Conductor' in the 'Materials' panel on the right." + "\n" +
	"Retaining sleeve loss will appear in the 'Other eddy current loss' parameter of 'Magnetostatic Analysis'";
	//this.comboboxRetainingSleeveType['currentIndexChanged(int)'].connect(this, function(value){
	//	rotor.RetainingSleeveType = this.comboboxRetainingSleeveType.currentText;
	//});
	
	this.labelTrs = new QLabel("Retaining sleeve thickness (Trs):");
	this.labelTrs.toolTip = "Use the ‘? Guide’ button above for explanation on this parameter";
	this.editTrs = new QDoubleSpinBox;
	this.editTrs.objectName = "Trs";
	this.editTrs.setRange(0, Math.abs(motor.rotorOuterRadius - motor.rotorInnerRadius));
	this.editTrs.decimals = 1;
	this.editTrs.singleStep = 1;
	this.editTrs.keyboardTracking = false;
	this.editTrs.value = rotor.Trs;
	//this.editTrs['valueChanged(double)'].connect(this, function(value){
	//	rotor.Trs = value;
	//});
	this.unitsTrs = new QLabel("mm");
	
	
	this.layout = new QGridLayout(ui);
	
	this.layout.addWidget(this.labelDpm, 0, 0);
	this.layout.addWidget(this.editDpm, 0, 1);
	this.layout.addWidget(this.unitsDpm, 0, 2);

	this.layout.addWidget(this.labelMagnetShape, 1, 0);
	this.layout.addWidget(this.comboboxMagnetShape, 1, 1, 1, 2);
	
	this.layout.addWidget(this.labelMagnetization, 2, 0);
	this.layout.addWidget(this.comboboxMagnetization, 2, 1, 1, 2);
	
	this.layout.addWidget(this.labelRotorCoreType, 3, 0);
	this.layout.addWidget(this.comboboxRotorCoreType, 3, 1, 1, 2);
	
	// create a group for retaining sleeve widgets:
	this.groupRetainingSleeve = new QGroupBox("Retaining sleeve"); 
	// Arranging group of widgets:
	var layoutGroup_RetainingSleeve = new QGridLayout(this.groupRetainingSleeve); 
	
	layoutGroup_RetainingSleeve.addWidget(this.labelRetainingSleeveType, 0, 0);
	layoutGroup_RetainingSleeve.addWidget(this.comboboxRetainingSleeveType, 0, 1, 1, 2);
	
	layoutGroup_RetainingSleeve.addWidget(this.labelTrs, 1, 0);
	layoutGroup_RetainingSleeve.addWidget(this.editTrs, 1, 1);
	layoutGroup_RetainingSleeve.addWidget(this.unitsTrs, 1, 2);
	
	this.layout.addWidget(this.groupRetainingSleeve, 4, 0, 1, 3);   // insert group
};

// update rotor widgets
RotorUI.prototype.updateUI = function()
{
	//console.log("rotorUI.updateUI");
	
	// Visibility of retaining sleeve widgets
	if (motor.isInnerRotor()) {  // inner rotor
		var visibleRetainingSleeveType = true;
		var visibleTrs = rotor.RetainingSleeveType == "Conductive retaining sleeve";
	}
	else {                       // outer rotor
		var visibleRetainingSleeveType = false;
		var visibleTrs = false;
	}
	this.labelRetainingSleeveType.setVisible(visibleRetainingSleeveType);
	this.comboboxRetainingSleeveType.setVisible(visibleRetainingSleeveType);
	this.groupRetainingSleeve.setVisible(visibleRetainingSleeveType);
	this.labelTrs.setVisible(visibleTrs);
	this.editTrs.setVisible(visibleTrs);
	this.unitsTrs.setVisible(visibleTrs);
}

// call this function from c++ code
createRotorUI = function(ui)
{
	rotorUI = new RotorUI(ui);
	rotorUI.updateUI();
}

	

	
	
	
