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
}
}
@@ -36,19 +36,17 @@ 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. /**
* Modified from the Battery Symbol component. * Component type for Voltage Source Symbol, options for both AC and DC.
* Author: JD * Modified from the Battery Symbol component. 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;
@@ -60,8 +58,6 @@ public class VoltageSourceSymbol extends AbstractSchematicLeadedSymbol<String> {
// 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());
@@ -85,21 +81,23 @@ public class VoltageSourceSymbol extends AbstractSchematicLeadedSymbol<String> {
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 @EditableProperty(name = "Source Type")
public SourceType getSourceType() { public SourceType getSourceType() {
if (source_Type == null) if (source_Type == null)
source_Type = SourceType.DC; source_Type = SourceType.DC;
return source_Type; return source_Type;
} }
/** Sets whether the source is AC or DC. /**
* Uses Enum "SourceType" * Sets 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)
*
* @param source_Type * @param source_Type
*/ */
public void setSourceType(SourceType source_Type) { public void setSourceType(SourceType source_Type) {
@@ -131,6 +129,7 @@ public class VoltageSourceSymbol extends AbstractSchematicLeadedSymbol<String> {
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 case AC: // AC Source, inside will be a sine wave
// Cubic curve format is: (x1, y1, cx1, cy1, cx2, cy2, x2, y2) // Cubic curve format is: (x1, y1, cx1, cy1, cx2, cy2, x2, y2)
g2d.draw(new CubicCurve2D.Double(0.2 * arbritaryLength, arbritaryLength / 2, // x1, y1 g2d.draw(new CubicCurve2D.Double(0.2 * arbritaryLength, arbritaryLength / 2, // x1, y1
@@ -138,6 +137,7 @@ public class VoltageSourceSymbol extends AbstractSchematicLeadedSymbol<String> {
0.5 * arbritaryLength, arbritaryLength + arbritaryLength * 0.3, // cx2, cy2 0.5 * arbritaryLength, arbritaryLength + arbritaryLength * 0.3, // cx2, cy2
0.8 * arbritaryLength, arbritaryLength / 2)); // x2, y2 0.8 * arbritaryLength, arbritaryLength / 2)); // x2, y2
break; break;
case DC: // DC Source, inside will have "+" & "-" signs case DC: // DC Source, inside will have "+" & "-" signs
double plusSize = arbritaryLength / 6; // Plus sign is 1/6 of overall size double plusSize = arbritaryLength / 6; // Plus sign is 1/6 of overall size
// Draw "+" sign // Draw "+" sign
@@ -178,10 +178,12 @@ public class VoltageSourceSymbol extends AbstractSchematicLeadedSymbol<String> {
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(
new Line2D.Double(startx + arbritaryLength * 0.12 + plusSize / 2, starty + arbritaryLength / 2 + plusSize / 2,
startx + arbritaryLength * 0.12 + plusSize / 2, starty + arbritaryLength / 2 - 1 * (plusSize / 2))); startx + arbritaryLength * 0.12 + plusSize / 2, starty + arbritaryLength / 2 - 1 * (plusSize / 2)));
// Draw "-" Sign // Draw "-" Sign
g2d.draw(new Line2D.Double(startx + arbritaryLength*0.72 + plusSize/2, starty + arbritaryLength/2 + 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))); startx + arbritaryLength * 0.72 + plusSize / 2, starty + arbritaryLength / 2 - 1 * (plusSize / 2)));
} }