mirror of
https://github.com/bancika/diy-layout-creator
synced 2026-11-11 01:32:08 +10:00
add flexible leads to some components through the menu
This commit is contained in:
Binary file not shown.
@@ -69,6 +69,7 @@ public abstract class AbstractLeadedComponent<T> extends AbstractTransparentComp
|
||||
public static Color LEAD_COLOR_ICON = LEAD_COLOR.darker().darker();
|
||||
public static Size LEAD_THICKNESS = new Size(0.6d, SizeUnit.mm);
|
||||
public static Size DEFAULT_SIZE = new Size(1d, SizeUnit.in);
|
||||
private static final int LEAD_RENDER_THRESHOLD = 2;
|
||||
|
||||
protected Size length;
|
||||
protected Size width;
|
||||
@@ -203,32 +204,35 @@ public abstract class AbstractLeadedComponent<T> extends AbstractTransparentComp
|
||||
float leadThickness = getLeadThickness();
|
||||
double leadLength = (distance - calculatePinSpacing(shapeRect)) / 2;
|
||||
|
||||
if (shouldShadeLeads()) {
|
||||
Stroke leadStroke = ObjectCache.getInstance().fetchBasicStroke(leadThickness - 1);
|
||||
Color leadColor = getLeadColorForPainting(componentState);
|
||||
|
||||
Area leadArea = new Area();
|
||||
|
||||
int endX = (int) (getPoints()[0].x + Math.cos(theta) * leadLength);
|
||||
int endY = (int) (getPoints()[0].y + Math.sin(theta) * leadLength);
|
||||
Line2D line = new Line2D.Double(getPoints()[0].x, getPoints()[0].y, endX, endY);
|
||||
leadArea.add(new Area(leadStroke.createStrokedShape(line)));
|
||||
|
||||
endX = (int) (getPoints()[1].x + Math.cos(theta - Math.PI) * leadLength);
|
||||
endY = (int) (getPoints()[1].y + Math.sin(theta - Math.PI) * leadLength);
|
||||
line = new Line2D.Double(getPoints()[1].x, getPoints()[1].y, endX, endY);
|
||||
leadArea.add(new Area(leadStroke.createStrokedShape(line)));
|
||||
|
||||
g2d.setStroke(ObjectCache.getInstance().fetchBasicStroke(1f));
|
||||
g2d.setColor(leadColor);
|
||||
g2d.fill(leadArea);
|
||||
g2d.setColor(leadColor.darker());
|
||||
g2d.draw(leadArea);
|
||||
} else {
|
||||
g2d.setStroke(ObjectCache.getInstance().fetchBasicStroke(leadThickness));
|
||||
Color leadColor = getLeadColorForPainting(componentState);
|
||||
g2d.setColor(leadColor);
|
||||
drawLeads(g2d, theta, leadLength);
|
||||
// only draw leads longer than threshold
|
||||
if (leadLength > LEAD_RENDER_THRESHOLD) {
|
||||
if (shouldShadeLeads()) {
|
||||
Stroke leadStroke = ObjectCache.getInstance().fetchBasicStroke(leadThickness - 1);
|
||||
Color leadColor = getLeadColorForPainting(componentState);
|
||||
|
||||
Area leadArea = new Area();
|
||||
|
||||
int endX = (int) (getPoints()[0].x + Math.cos(theta) * leadLength);
|
||||
int endY = (int) (getPoints()[0].y + Math.sin(theta) * leadLength);
|
||||
Line2D line = new Line2D.Double(getPoints()[0].x, getPoints()[0].y, endX, endY);
|
||||
leadArea.add(new Area(leadStroke.createStrokedShape(line)));
|
||||
|
||||
endX = (int) (getPoints()[1].x + Math.cos(theta - Math.PI) * leadLength);
|
||||
endY = (int) (getPoints()[1].y + Math.sin(theta - Math.PI) * leadLength);
|
||||
line = new Line2D.Double(getPoints()[1].x, getPoints()[1].y, endX, endY);
|
||||
leadArea.add(new Area(leadStroke.createStrokedShape(line)));
|
||||
|
||||
g2d.setStroke(ObjectCache.getInstance().fetchBasicStroke(1f));
|
||||
g2d.setColor(leadColor);
|
||||
g2d.fill(leadArea);
|
||||
g2d.setColor(leadColor.darker());
|
||||
g2d.draw(leadArea);
|
||||
} else {
|
||||
g2d.setStroke(ObjectCache.getInstance().fetchBasicStroke(leadThickness));
|
||||
Color leadColor = getLeadColorForPainting(componentState);
|
||||
g2d.setColor(leadColor);
|
||||
drawLeads(g2d, theta, leadLength);
|
||||
}
|
||||
}
|
||||
// g2d.setTransform(textTransform);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,103 @@
|
||||
package org.diylc.editor;
|
||||
|
||||
import java.awt.Point;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import org.diylc.common.IProjectEditor;
|
||||
import org.diylc.components.AbstractCurvedComponent.PointCount;
|
||||
import org.diylc.components.AbstractLeadedComponent;
|
||||
import org.diylc.components.connectivity.AWG;
|
||||
import org.diylc.components.connectivity.HookupWire;
|
||||
import org.diylc.components.semiconductors.AbstractTransistorPackage;
|
||||
import org.diylc.core.IDIYComponent;
|
||||
import org.diylc.core.Project;
|
||||
import org.diylc.core.measures.Size;
|
||||
import org.diylc.presenter.InstantiationManager;
|
||||
|
||||
public class FlexibleLeadsEditor implements IProjectEditor {
|
||||
|
||||
private InstantiationManager instantiationManager;
|
||||
|
||||
@Override
|
||||
public Set<IDIYComponent<?>> edit(Project project, Set<IDIYComponent<?>> selection) {
|
||||
Set<IDIYComponent<?>> newSelection = new HashSet<IDIYComponent<?>>(selection);
|
||||
for (IDIYComponent<?> c : selection) {
|
||||
if (c instanceof AbstractLeadedComponent<?>)
|
||||
addLeads((AbstractLeadedComponent<?>) c, project, newSelection);
|
||||
}
|
||||
|
||||
return newSelection;
|
||||
}
|
||||
|
||||
private void addLeads(AbstractLeadedComponent<?> c, Project project, Set<IDIYComponent<?>> newSelection) {
|
||||
AbstractLeadedComponent<?> leaded = (AbstractLeadedComponent<?>)c;
|
||||
Size l = leaded.getLength();
|
||||
// do not apply to jumpers, etc
|
||||
if (l == null)
|
||||
return;
|
||||
|
||||
Point p1 = new Point(leaded.getControlPoint(0));
|
||||
Point p2 = new Point(leaded.getControlPoint(1));
|
||||
double d = p1.distance(p2);
|
||||
double len = l.convertToPixels() + 2;
|
||||
|
||||
double diff = (d - len) / 2;
|
||||
|
||||
if (diff < 20)
|
||||
return;
|
||||
|
||||
// find the center and angle
|
||||
double centerX = (p1.x + p2.x) / 2.0;
|
||||
double centerY = (p1.y + p2.y) / 2.0;
|
||||
double theta = Math.atan2(p2.y - p1.y, p2.x - p1.x);
|
||||
// shorten the component to match the size of the body
|
||||
Point newP1 = new Point((int)Math.round(centerX - Math.cos(theta) * len / 2), (int)Math.round(centerY - Math.sin(theta) * len / 2));
|
||||
Point newP2 = new Point((int)Math.round(centerX + Math.cos(theta) * len / 2), (int)Math.round(centerY + Math.sin(theta) * len / 2));
|
||||
leaded.setControlPoint(newP1, 0);
|
||||
leaded.setControlPoint(newP2, 1);
|
||||
|
||||
// create leads
|
||||
HookupWire w1 = new HookupWire();
|
||||
w1.setGauge(AWG._26);
|
||||
w1.setLeadColor(leaded.getLeadColor());
|
||||
w1.setPointCount(PointCount.THREE);
|
||||
w1.setControlPoint(p1, 0);
|
||||
w1.setControlPoint(new Point((int)Math.round(centerX - Math.cos(theta) * (len / 2 + diff / 2)), (int)Math.round(centerY - Math.sin(theta) * (len / 2 + diff / 2))), 1);
|
||||
w1.setControlPoint(newP1, 2);
|
||||
newSelection.add(w1);
|
||||
|
||||
HookupWire w2 = new HookupWire();
|
||||
w2.setGauge(AWG._26);
|
||||
w2.setLeadColor(leaded.getLeadColor());
|
||||
w2.setPointCount(PointCount.THREE);
|
||||
w2.setControlPoint(p2, 0);
|
||||
w2.setControlPoint(new Point((int)Math.round(centerX + Math.cos(theta) * (len / 2 + diff / 2)), (int)Math.round(centerY + Math.sin(theta) * (len / 2 + diff / 2))), 1);
|
||||
w2.setControlPoint(newP2, 2);
|
||||
newSelection.add(w2);
|
||||
|
||||
// inject leads right before the component
|
||||
int index = project.getComponents().indexOf(c);
|
||||
project.getComponents().add(index, w1);
|
||||
project.getComponents().add(index, w2);
|
||||
}
|
||||
|
||||
private void addLeads(AbstractTransistorPackage c, Project project, Set<IDIYComponent<?>> newSelection) {
|
||||
if (c.getFolded()) {
|
||||
|
||||
} else {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getEditAction() {
|
||||
return "Add Flexible Leads";
|
||||
}
|
||||
|
||||
public InstantiationManager getInstantiationManager() {
|
||||
if (instantiationManager == null)
|
||||
instantiationManager = new InstantiationManager();
|
||||
return instantiationManager;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user