Add CurrentSourceSymbol + VoltageSymbol corrections (#723)

* Improved readability

Tabs and spaces changed for better readability. No functional changes.

* Correct labelling of "Source Type" property

More readability improvements as well as property change.

* Add CurrentSourceSymbol

A schematic symbol for current sources with two alternate drawing styles
This commit is contained in:
Jamie Drinkell
2022-08-28 08:54:36 +01:00
committed by GitHub
parent c6ae5af997
commit 9e3f1793fd
2 changed files with 359 additions and 146 deletions
@@ -0,0 +1,211 @@
/*
DIY Layout Creator (DIYLC).
Copyright (c) 2009-2018 held jointly by the individual authors.
This file is part of DIYLC.
DIYLC is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
DIYLC is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with DIYLC. If not, see <http://www.gnu.org/licenses/>.
*/
package org.diylc.components.misc;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.geom.GeneralPath;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import org.diylc.components.AbstractSchematicLeadedSymbol;
import org.diylc.core.CreationMethod;
import org.diylc.core.IDIYComponent;
import org.diylc.core.annotations.ComponentDescriptor;
import org.diylc.core.annotations.EditableProperty;
import org.diylc.core.measures.CurrentUnit;
import org.diylc.core.measures.Size;
import org.diylc.core.measures.SizeUnit;
import org.diylc.core.measures.VoltageUnit;
@ComponentDescriptor(name = "Current Source", author = "JD", category = "Schematic Symbols", creationMethod = CreationMethod.POINT_BY_POINT,
instanceNamePrefix = "I", description = "Current Source schematic symbol", zOrder = IDIYComponent.COMPONENT)
/**
* Component type for Current Source Symbol, options for 2 drawing styles.
* Modified from the Voltage Source Symbol component. Author: JD
*
* @author JD
*/
public class CurrentSourceSymbol extends AbstractSchematicLeadedSymbol<String> {
// FIELDS
private static final long serialVersionUID = 1L;
private String value = "Current Source";
private SourceStyle source_Style = SourceStyle.Standard;
// Overall size
public static Size DEFAULT_LENGTH = new Size(0.2, SizeUnit.in);
public static Size DEFAULT_WIDTH = new Size(0.2, SizeUnit.in);
// Sets current to 100mA by default
private org.diylc.core.measures.Current currentNew = new org.diylc.core.measures.Current(100d, CurrentUnit.mA);
// METHODS
@Override
public String getValueForDisplay() {
return getValue().toString() + (getCurrentNew() == null ? "" : " " + getCurrentNew().toString());
}
@EditableProperty
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
@EditableProperty(name = "Current")
public org.diylc.core.measures.Current getCurrentNew() {
return currentNew;
}
public void setVoltageNew(org.diylc.core.measures.Current currentNew) {
this.currentNew = currentNew;
}
/**
* Returns whether the source drawing style is Standard or Alternate. Uses Enum
* "SourceStyle" Author: JD (Modified Bancika's code, credit to Bancika)
* @return
*/
@EditableProperty(name = "Source Style")
public SourceStyle getSourceStyle() {
if (source_Style == null)
source_Style = SourceStyle.Standard;
return source_Style;
}
/**
* Sets whether the source is standard or alternate. Uses Enum "SourceStyle"
* Author: JD (Modified Bancika's code, credit to Bancika)
* @param sourceStyle
*/
public void setSourceStyle(SourceStyle source_Style) {
this.source_Style = source_Style;
}
@Override
protected Size getDefaultWidth() {
return DEFAULT_WIDTH;
}
@Override
protected Size getDefaultLength() {
return DEFAULT_LENGTH;
}
@Override
protected Shape getBodyShape() {
double width = getWidth().convertToPixels();
double length = getLength().convertToPixels();
GeneralPath body = new GeneralPath();
switch (getSourceStyle()) {
case Standard:
// Main body is a circle, drawn with an Ellipse
body.append(new Ellipse2D.Double(0d, 0d, width, length), true);
break;
case Alternate:
// Main body is two circles
body.append(new Ellipse2D.Double(0d, 0d, length, length), true);
body.moveTo(length * 1.5 , length / 2); // Prevents a closing line being drawn
body.append(new Ellipse2D.Double(length / 2, 0d, length, length), true);
break;
}
return (Shape) body;
}
@Override
protected void decorateComponentBody(Graphics2D g2d, boolean outlineMode) {
double width = getWidth().convertToPixels();
double height = getLength().convertToPixels();
g2d.setColor(getBorderColor());
switch (getSourceStyle()) {
case Standard: // Draw arrow inside main circle
g2d.draw(new Line2D.Double(0.25 * width, height / 2,
0.75 * width, height / 2));
g2d.draw(new Line2D.Double(0.75 * width, height / 2,
(0.75 * width) - 0.45 * (0.25 * width), (height / 2) + height / 5));
g2d.draw(new Line2D.Double(0.75 * width, height / 2,
(0.75 * width) - 0.45 * (0.25 * width), (height / 2) - height / 5));
break;
case Alternate: // Draw arrow below the two circles
g2d.draw(new Line2D.Double(0.25 * width * 1.5, height * 1.5,
0.75 * width * 1.5, height * 1.5));
g2d.draw(new Line2D.Double(0.75 * width * 1.5, height * 1.5,
(0.75 * width * 1.5) - 0.45 * (0.25 * width * 1.5), (height * 1.5) + height / 5));
g2d.draw(new Line2D.Double(0.75 * width * 1.5, height * 1.5,
(0.75 * width * 1.5) - 0.45 * (0.25 * width * 1.5), (height * 1.5) - height / 5));
break;
default:
}
}
public void drawIcon(Graphics2D g2d, int width, int height) {
g2d.rotate(-Math.PI / 4, width / 2, height / 2);
// g2d.rotate(-Math.PI, width / 2, height / 2);
g2d.setColor(LEAD_COLOR);
// Draw Leads
g2d.draw(new Line2D.Double(0, height / 2, width / 4, height / 2));
g2d.draw(new Line2D.Double(width, height / 2, width * 0.75, height / 2));
g2d.setColor(COLOR);
// Draw Ellipse
double startx = width / 4;
double starty = height / 4;
g2d.draw(new Ellipse2D.Double(startx, starty, 0.5 * width, 0.5 * height));
// Draw arrow
// g2d.setStroke();
g2d.draw(new Line2D.Double(startx + 0.25 * width / 2, starty + height / 4,
startx + 0.75 * width / 2, starty + height / 4));
g2d.draw(new Line2D.Double(startx + 0.25 * width / 2, starty + height / 4,
startx + (0.25 * width / 2) + 0.3 * (startx + (0.25 * width / 2)), (starty + height / 4) + height / 9));
g2d.draw(new Line2D.Double(startx + 0.25 * width / 2, starty + height / 4,
startx + (0.25 * width / 2) + 0.3 * (startx + (0.25 * width / 2)), (starty + height / 4) - height / 9));
}
@Override
public String getControlPointNodeName(int index) {
return getName() + "." + (index == 0 ? "+" : "-");
}
public enum SourceStyle {
Standard, Alternate
}
}
@@ -1,67 +1,63 @@
/* /*
DIY Layout Creator (DIYLC). DIY Layout Creator (DIYLC).
Copyright (c) 2009-2018 held jointly by the individual authors. Copyright (c) 2009-2018 held jointly by the individual authors.
This file is part of DIYLC. This file is part of DIYLC.
DIYLC is free software: you can redistribute it and/or modify DIYLC is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or the Free Software Foundation, either version 3 of the License, or
(at your option) any later version. (at your option) any later version.
DIYLC is distributed in the hope that it will be useful, DIYLC is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details. GNU General Public License for more details.
You should have received a copy of the GNU General Public License You should have received a copy of the GNU General Public License
along with DIYLC. If not, see <http://www.gnu.org/licenses/>. along with DIYLC. If not, see <http://www.gnu.org/licenses/>.
*/ */
package org.diylc.components.misc; package org.diylc.components.misc;
import java.awt.Graphics2D; import java.awt.Graphics2D;
import java.awt.Shape; import java.awt.Shape;
import java.awt.geom.CubicCurve2D; import java.awt.geom.CubicCurve2D;
import java.awt.geom.Ellipse2D; import java.awt.geom.Ellipse2D;
import java.awt.geom.GeneralPath; import java.awt.geom.GeneralPath;
import java.awt.geom.Line2D; import java.awt.geom.Line2D;
import org.diylc.components.AbstractSchematicLeadedSymbol; import org.diylc.components.AbstractSchematicLeadedSymbol;
import org.diylc.core.CreationMethod; import org.diylc.core.CreationMethod;
import org.diylc.core.IDIYComponent; import org.diylc.core.IDIYComponent;
import org.diylc.core.annotations.ComponentDescriptor; import org.diylc.core.annotations.ComponentDescriptor;
import org.diylc.core.annotations.EditableProperty; import org.diylc.core.annotations.EditableProperty;
import org.diylc.core.measures.Size; import org.diylc.core.measures.Size;
import org.diylc.core.measures.SizeUnit; import org.diylc.core.measures.SizeUnit;
import org.diylc.core.measures.VoltageUnit; import org.diylc.core.measures.VoltageUnit;
@ComponentDescriptor(name = "Voltage Source", author = "JD", category = "Schematic Symbols", @ComponentDescriptor(name = "Voltage Source", author = "JD", category = "Schematic Symbols", creationMethod = CreationMethod.POINT_BY_POINT,
creationMethod = CreationMethod.POINT_BY_POINT, instanceNamePrefix = "V", description = "Voltage Source schematic symbol", instanceNamePrefix = "V", description = "Voltage Source schematic symbol", zOrder = IDIYComponent.COMPONENT)
zOrder = IDIYComponent.COMPONENT)
/**
/** Component type for Voltage Source Symbol, options for both AC and DC. * Component type for Voltage Source Symbol, options for both AC and DC.
* Modified from the Battery Symbol component. * Modified from the Battery Symbol component. Author: JD
* Author: JD *
* * @author JD
* @author JD
*/ */
public class VoltageSourceSymbol extends AbstractSchematicLeadedSymbol<String> { public class VoltageSourceSymbol extends AbstractSchematicLeadedSymbol<String> {
// FIELDS
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
private String value = "Voltage Source"; private String value = "Voltage Source";
private SourceType source_Type = SourceType.DC; private SourceType source_Type = SourceType.DC;
// Overall size // Overall size
public static Size DEFAULT_LENGTH = new Size(0.2, SizeUnit.in); public static Size DEFAULT_LENGTH = new Size(0.2, SizeUnit.in);
public static Size DEFAULT_WIDTH = new Size(0.2, SizeUnit.in); public static Size DEFAULT_WIDTH = new Size(0.2, SizeUnit.in);
// Sets voltage to 5V by default // Sets voltage to 5V by default
private org.diylc.core.measures.Voltage voltageNew = new org.diylc.core.measures.Voltage(5d, VoltageUnit.V); private org.diylc.core.measures.Voltage voltageNew = new org.diylc.core.measures.Voltage(5d, VoltageUnit.V);
// METHODS
@Override @Override
public String getValueForDisplay() { public String getValueForDisplay() {
return getValue().toString() + (getVoltageNew() == null ? "" : " " + getVoltageNew().toString()); return getValue().toString() + (getVoltageNew() == null ? "" : " " + getVoltageNew().toString());
@@ -84,28 +80,30 @@ public class VoltageSourceSymbol extends AbstractSchematicLeadedSymbol<String> {
public void setVoltageNew(org.diylc.core.measures.Voltage voltageNew) { public void setVoltageNew(org.diylc.core.measures.Voltage voltageNew) {
this.voltageNew = voltageNew; this.voltageNew = voltageNew;
} }
/** Returns whether the source is AC or DC. /**
* Uses Enum "SourceType" * Returns whether the source is AC or DC. Uses Enum "SourceType" Author: JD
* Author: JD (Modified Bancika's code, credit to Bancika) * (Modified Bancika's code, credit to Bancika)
* @return *
*/ * @return
@EditableProperty */
public SourceType getSourceType() { @EditableProperty(name = "Source Type")
if (source_Type == null) public SourceType getSourceType() {
source_Type = SourceType.DC; if (source_Type == null)
return source_Type; source_Type = SourceType.DC;
} return source_Type;
}
/** Sets whether the source is AC or DC.
* Uses Enum "SourceType" /**
* Author: JD (Modified Bancika's code, credit to Bancika) * Sets whether the source is AC or DC. Uses Enum "SourceType" Author: JD
* @param source_Type * (Modified Bancika's code, credit to Bancika)
*/ *
public void setSourceType(SourceType source_Type) { * @param source_Type
this.source_Type = source_Type; */
} public void setSourceType(SourceType source_Type) {
this.source_Type = source_Type;
}
@Override @Override
protected Size getDefaultWidth() { protected Size getDefaultWidth() {
return DEFAULT_WIDTH; return DEFAULT_WIDTH;
@@ -117,80 +115,84 @@ public class VoltageSourceSymbol extends AbstractSchematicLeadedSymbol<String> {
} }
@Override @Override
protected Shape getBodyShape() { protected Shape getBodyShape() {
// Main body is a circle, drawn with an Ellipse // Main body is a circle, drawn with an Ellipse
double width = getWidth().convertToPixels(); double width = getWidth().convertToPixels();
double length = getLength().convertToPixels(); double length = getLength().convertToPixels();
GeneralPath body = new GeneralPath(); GeneralPath body = new GeneralPath();
body.append(new Ellipse2D.Double(0d, 0d, width, length), true); body.append(new Ellipse2D.Double(0d, 0d, width, length), true);
return (Shape)body; return (Shape) body;
} }
@Override @Override
protected void decorateComponentBody(Graphics2D g2d, boolean outlineMode) { protected void decorateComponentBody(Graphics2D g2d, boolean outlineMode) {
g2d.setColor(getBorderColor()); g2d.setColor(getBorderColor());
double arbritaryLength = getWidth().convertToPixels(); double arbritaryLength = getWidth().convertToPixels();
switch (getSourceType()){ switch (getSourceType()) {
case AC: // AC Source, inside will be a sine wave
// Cubic curve format is: (x1, y1, cx1, cy1, cx2, cy2, x2, y2) case AC: // AC Source, inside will be a sine wave
g2d.draw(new CubicCurve2D.Double(0.2 * arbritaryLength, arbritaryLength / 2, // x1, y1 // Cubic curve format is: (x1, y1, cx1, cy1, cx2, cy2, x2, y2)
0.5 * arbritaryLength, - arbritaryLength * 0.3, // cx1, cy1 g2d.draw(new CubicCurve2D.Double(0.2 * arbritaryLength, arbritaryLength / 2, // x1, y1
0.5 * arbritaryLength, arbritaryLength + arbritaryLength * 0.3, // cx2, cy2 0.5 * arbritaryLength, -arbritaryLength * 0.3, // cx1, cy1
0.8 * arbritaryLength, arbritaryLength / 2)); // x2, y2 0.5 * arbritaryLength, arbritaryLength + arbritaryLength * 0.3, // cx2, cy2
break; 0.8 * arbritaryLength, arbritaryLength / 2)); // x2, y2
case DC: // DC Source, inside will have "+" & "-" signs break;
double plusSize = arbritaryLength / 6; // Plus sign is 1/6 of overall size
// Draw "+" sign case DC: // DC Source, inside will have "+" & "-" signs
g2d.draw(new Line2D.Double(arbritaryLength*0.1, arbritaryLength/2, double plusSize = arbritaryLength / 6; // Plus sign is 1/6 of overall size
arbritaryLength*0.1 + plusSize, arbritaryLength/2)); // Draw "+" sign
g2d.draw(new Line2D.Double(arbritaryLength * 0.1, arbritaryLength / 2,
g2d.draw(new Line2D.Double(arbritaryLength*0.1 + plusSize/2, arbritaryLength/2 + plusSize/2, arbritaryLength * 0.1 + plusSize, arbritaryLength / 2));
arbritaryLength*0.1 + plusSize/2, arbritaryLength/2 - plusSize/2));
// Draw "-" Sign g2d.draw(new Line2D.Double(arbritaryLength * 0.1 + plusSize / 2, arbritaryLength / 2 + plusSize / 2,
g2d.draw(new Line2D.Double(arbritaryLength*0.76 + plusSize/2, arbritaryLength/2 + plusSize/2, arbritaryLength * 0.1 + plusSize / 2, arbritaryLength / 2 - plusSize / 2));
arbritaryLength*0.76 + plusSize/2, arbritaryLength/2 - plusSize/2)); // Draw "-" Sign
break; g2d.draw(new Line2D.Double(arbritaryLength * 0.76 + plusSize / 2, arbritaryLength / 2 + plusSize / 2,
default: arbritaryLength * 0.76 + plusSize / 2, arbritaryLength / 2 - plusSize / 2));
} break;
default:
}
} }
public void drawIcon(Graphics2D g2d, int width, int height) { public void drawIcon(Graphics2D g2d, int width, int height) {
g2d.rotate(-Math.PI / 4, width / 2, height / 2); g2d.rotate(-Math.PI / 4, width / 2, height / 2);
// g2d.rotate(-Math.PI, width / 2, height / 2); // g2d.rotate(-Math.PI, width / 2, height / 2);
g2d.setColor(LEAD_COLOR); g2d.setColor(LEAD_COLOR);
// Draw Leads // Draw Leads
g2d.draw(new Line2D.Double(0, height / 2, width / 4, height / 2)); g2d.draw(new Line2D.Double(0, height / 2, width / 4, height / 2));
g2d.draw(new Line2D.Double(width, height / 2, width * 0.75, height / 2)); g2d.draw(new Line2D.Double(width, height / 2, width * 0.75, height / 2));
g2d.setColor(COLOR); g2d.setColor(COLOR);
// Draw Ellipse // Draw Ellipse
double startx = width/4; double startx = width / 4;
double starty = height/4; double starty = height / 4;
g2d.draw(new Ellipse2D.Double(startx, starty, 0.5 * width, 0.5 * height)); g2d.draw(new Ellipse2D.Double(startx, starty, 0.5 * width, 0.5 * height));
// Draw signs // Draw signs
double arbritaryLength = width * 0.5; double arbritaryLength = width * 0.5;
double plusSize = arbritaryLength / 5; // Plus sign is 1/5 of overall size double plusSize = arbritaryLength / 5; // Plus sign is 1/5 of overall size
// Draw "+" sign // Draw "+" sign
g2d.draw(new Line2D.Double(startx + arbritaryLength*0.12, starty + arbritaryLength/2, g2d.draw(new Line2D.Double(startx + arbritaryLength * 0.12, starty + arbritaryLength / 2,
startx + arbritaryLength*0.12 + plusSize, starty + arbritaryLength/2)); startx + arbritaryLength * 0.12 + plusSize, starty + arbritaryLength / 2));
g2d.draw(new Line2D.Double(startx + arbritaryLength*0.12 + plusSize/2, starty + arbritaryLength/2 + plusSize/2, g2d.draw(
startx + arbritaryLength*0.12 + plusSize/2, starty + arbritaryLength/2 -1*(plusSize/2))); new Line2D.Double(startx + arbritaryLength * 0.12 + plusSize / 2, starty + arbritaryLength / 2 + plusSize / 2,
// Draw "-" Sign startx + arbritaryLength * 0.12 + plusSize / 2, starty + arbritaryLength / 2 - 1 * (plusSize / 2)));
g2d.draw(new Line2D.Double(startx + arbritaryLength*0.72 + plusSize/2, starty + arbritaryLength/2 + plusSize/2, // Draw "-" Sign
startx + arbritaryLength*0.72 + plusSize/2, starty + arbritaryLength/2 -1*(plusSize/2))); g2d.draw(
} new Line2D.Double(startx + arbritaryLength * 0.72 + plusSize / 2, starty + arbritaryLength / 2 + plusSize / 2,
startx + arbritaryLength * 0.72 + plusSize / 2, starty + arbritaryLength / 2 - 1 * (plusSize / 2)));
@Override
public String getControlPointNodeName(int index) {
return getName() + "." + (index == 0 ? "+" : "-");
}
public enum SourceType {
AC, DC
} }
}
@Override
public String getControlPointNodeName(int index) {
return getName() + "." + (index == 0 ? "+" : "-");
}
public enum SourceType {
AC, DC
}
}