Compare commits

...

2 Commits

2 changed files with 173 additions and 154 deletions

View File

@ -80,7 +80,6 @@ function doSave(value, type, name) {
type: type type: type
}); });
} else { } else {
var BlobBuilder = window.BlobBuilder || window.MozBlobBuilder || window.WebKitBlobBuilder || window.MSBlobBuilder; var BlobBuilder = window.BlobBuilder || window.MozBlobBuilder || window.WebKitBlobBuilder || window.MSBlobBuilder;

View File

@ -59,20 +59,18 @@ class AltiumSchematicRenderer
this.graph.removeCells(); this.graph.removeCells();
} }
render() { render() {
this.graph = new mxGraph(document.getElementById('graphContainer')); this.graph = new mxGraph(document.getElementById('graphContainer'));
mxConnectionHandler.prototype.movePreviewAway = true; mxConnectionHandler.prototype.movePreviewAway = false;
mxConnectionHandler.prototype.waypointsEnabled = true; mxConnectionHandler.prototype.waypointsEnabled = true;
mxGraph.prototype.resetEdgesOnConnect = true; mxGraph.prototype.resetEdgesOnConnect = false;
mxConstants.SHADOWCOLOR = '#C0C0C0'; mxConstants.SHADOWCOLOR = '#C0C0C0';
var joinNodeSize = 4; var joinNodeSize = 4;
var strokeWidth = 2; var strokeWidth = 2;
// Enables guides // Enables guides
mxGraphHandler.prototype.guidesEnabled = false; mxGraphHandler.prototype.guidesEnabled = true; // 启用导航线
// Alt disables guides // Alt disables guides
mxGuide.prototype.isEnabledForEvent = function(evt) mxGuide.prototype.isEnabledForEvent = function(evt)
@ -81,13 +79,12 @@ class AltiumSchematicRenderer
}; };
// Enables snapping waypoints to terminals // Enables snapping waypoints to terminals
mxEdgeHandler.prototype.snapToTerminals = true; mxEdgeHandler.prototype.snapToTerminals = false; // 自动连接目标
mxEdgeStyle.WireConnector = function(state, source, target, hints, result) mxEdgeStyle.WireConnector = function(state, source, target, hints, result)
{ {
mxLog.debug('add waypoint'); console.log(hint)
mxLog.debug("WireConnector",state,source,target,hint)
// Creates array of all way- and terminalpoints // Creates array of all way- and terminalpoints
var pts = state.absolutePoints; var pts = state.absolutePoints;
var horizontal = true; var horizontal = true;
@ -98,15 +95,13 @@ class AltiumSchematicRenderer
{ {
horizontal = state.style['sourceConstraint'] == 'horizontal'; horizontal = state.style['sourceConstraint'] == 'horizontal';
} }
else if (source != null) else if (source != null) {
{
horizontal = source.style['portConstraint'] != 'vertical'; horizontal = source.style['portConstraint'] != 'vertical';
// Checks the direction of the shape and rotates // Checks the direction of the shape and rotates
var direction = source.style[mxConstants.STYLE_DIRECTION]; var direction = source.style[mxConstants.STYLE_DIRECTION];
if (direction == 'north' || direction == 'south') if (direction == 'north' || direction == 'south') {
{
horizontal = !horizontal; horizontal = !horizontal;
} }
} }
@ -125,12 +120,12 @@ class AltiumSchematicRenderer
} }
var first = pt; var first = pt;
console.log(hints)
// Adds the waypoints // Adds the waypoints
if (hints != null && hints.length > 0) if (hints != null && hints.length > 0)
{ {
// FIXME: First segment not movable // FIXME: First segment not movable
hint = state.view.transformControlPoint(state, hints[0]); /*hint = state.view.transformControlPoint(state, hints[0]);
mxLog.show(); mxLog.show();
mxLog.debug(hints.length,'hints0.y='+hint.y, pt.y) mxLog.debug(hints.length,'hints0.y='+hint.y, pt.y)
@ -138,64 +133,162 @@ class AltiumSchematicRenderer
{ {
mxLog.show(); mxLog.show();
mxLog.debug('add waypoint'); mxLog.debug('add waypoint');
pt = new mxPoint(pt.x, hint.y); pt = new mxPoint(pt.x, hint.y);
result.push(pt); result.push(pt);
pt = pt.clone(); pt = pt.clone();
//horizontal = !horizontal; //horizontal = !horizontal;
} }*/
for (var i = 0; i < hints.length; i++) for (var i = 0; i < hints.length; i++) {
{
horizontal = !horizontal; horizontal = !horizontal;
hint = state.view.transformControlPoint(state, hints[i]); hint = state.view.transformControlPoint(state, hints[i]);
if (horizontal) if (horizontal)
{ {
if (pt.y != hint.y) if (pt.y != hint.y) {
{
pt.y = hint.y; pt.y = hint.y;
result.push(pt.clone()); result.push(pt.clone());
} }
} }
else if (pt.x != hint.x) else if (pt.x != hint.x) {
{
pt.x = hint.x; pt.x = hint.x;
result.push(pt.clone()); result.push(pt.clone());
} }
} }
} }
else else {
{
hint = pt; hint = pt;
} }
// Adds the last point // Adds the last point
pt = pts[pts.length - 1]; pt = pts[pts.length - 1];
// TODO: Should move along connected segment // TODO: Should move along connected segment
if (pt == null && target != null) if (pt == null && target != null) {
{ pt = new mxPoint(state.view.getRoutingCenterX(target),
pt = new mxPoint(state.view.getRoutingCenterX(target), state.view.getRoutingCenterY(target)); state.view.getRoutingCenterY(target));
} }
if (horizontal) if (horizontal) {
{ if (pt.y != hint.y && first.x != pt.x) {
if (pt.y != hint.y && first.x != pt.x) console.log(hint.x,pt.y)
{ result.push(new mxPoint(pt.x, hint.y));
// result.push(new mxPoint(pt.x, hint.y));
} }
} }
else if (pt.x != hint.x && first.y != pt.y) else if (pt.x != hint.x && first.y != pt.y) {
{ result.push(new mxPoint(hint.x, pt.y));
// result.push(new mxPoint(hint.x, pt.y));
} }
}; };
mxStyleRegistry.putValue('wireEdgeStyle', mxEdgeStyle.WireConnector); mxStyleRegistry.putValue('wireEdgeStyle', mxEdgeStyle.WireConnector);
// This connector needs an mxEdgeSegmentHandler
let mxGraphCreateHandler = mxGraph.prototype.createHandler;
mxGraph.prototype.createHandler = function(state)
{
var result = null;
if (state != null)
{
if (this.model.isEdge(state.cell))
{
var style = this.view.getEdgeStyle(state);
if (style == mxEdgeStyle.WireConnector)
{
return new mxEdgeSegmentHandler(state);
}
}
}
return mxGraphCreateHandler.apply(this, arguments);
};
mxGraphView.prototype.updateFixedTerminalPoint = function(edge, terminal, source, constraint)
{
mxLog.show()
mxLog.debug("updateFixedTerminalPoint")
var pt = null;
if (constraint != null)
{
pt = this.graph.getConnectionPoint(terminal, constraint);
}
if (source)
{
edge.sourceSegment = null;
}
else
{
edge.targetSegment = null;
}
if (pt == null)
{
var s = this.scale;
var tr = this.translate;
var orig = edge.origin;
var geo = this.graph.getCellGeometry(edge.cell);
pt = geo.getTerminalPoint(source);
// Computes edge-to-edge connection point
if (pt != null)
{
pt = new mxPoint(s * (tr.x + pt.x + orig.x),
s * (tr.y + pt.y + orig.y));
// Finds nearest segment on edge and computes intersection
if (terminal != null && terminal.absolutePoints != null)
{
var seg = mxUtils.findNearestSegment(terminal, pt.x, pt.y);
// Finds orientation of the segment
var p0 = terminal.absolutePoints[seg];
var pe = terminal.absolutePoints[seg + 1];
var horizontal = (p0.x - pe.x == 0);
// Stores the segment in the edge state
var key = (source) ? 'sourceConstraint' : 'targetConstraint';
var value = (horizontal) ? 'horizontal' : 'vertical';
edge.style[key] = value;
// Keeps the coordinate within the segment bounds
if (horizontal)
{
pt.x = p0.x;
pt.y = Math.min(pt.y, Math.max(p0.y, pe.y));
pt.y = Math.max(pt.y, Math.min(p0.y, pe.y));
}
else
{
pt.y = p0.y;
pt.x = Math.min(pt.x, Math.max(p0.x, pe.x));
pt.x = Math.max(pt.x, Math.min(p0.x, pe.x));
}
}
}
// Computes constraint connection points on vertices and ports
else if (terminal != null && terminal.cell.geometry.relative)
{
pt = new mxPoint(this.getRoutingCenterX(terminal),
this.getRoutingCenterY(terminal));
}
// Snaps point to grid
/*if (pt != null)
{
var tr = this.graph.view.translate;
var s = this.graph.view.scale;
pt.x = (this.graph.snap(pt.x / s - tr.x) + tr.x) * s;
pt.y = (this.graph.snap(pt.y / s - tr.y) + tr.y) * s;
}*/
}
edge.setAbsoluteTerminalPoint(pt, source);
};
let mxGraphGetCellStyle = mxGraph.prototype.getCellStyle; let mxGraphGetCellStyle = mxGraph.prototype.getCellStyle;
mxGraph.prototype.getCellStyle = function(cell) mxGraph.prototype.getCellStyle = function(cell)
{ {
@ -282,7 +375,8 @@ class AltiumSchematicRenderer
mxConnectionHandler.prototype.createEdgeState = function(me) mxConnectionHandler.prototype.createEdgeState = function(me)
{ {
var edge = this.graph.createEdge(); var edge = this.graph.createEdge();
mxLog.debug("createEdgeState")
if (this.sourceConstraint != null && this.previous != null) if (this.sourceConstraint != null && this.previous != null)
{ {
edge.style = mxConstants.STYLE_EXIT_X+'='+this.sourceConstraint.point.x+';'+ edge.style = mxConstants.STYLE_EXIT_X+'='+this.sourceConstraint.point.x+';'+
@ -310,6 +404,7 @@ class AltiumSchematicRenderer
let mxConnectionHandlerUpdateCurrentState = mxConnectionHandler.prototype.updateCurrentState; let mxConnectionHandlerUpdateCurrentState = mxConnectionHandler.prototype.updateCurrentState;
mxConnectionHandler.prototype.updateCurrentState = function(me) mxConnectionHandler.prototype.updateCurrentState = function(me)
{ {
mxLog.debug('updateCurrentState')
mxConnectionHandlerUpdateCurrentState.apply(this, arguments); mxConnectionHandlerUpdateCurrentState.apply(this, arguments);
if (this.edgeState != null) if (this.edgeState != null)
@ -401,30 +496,30 @@ class AltiumSchematicRenderer
return result; return result;
}; };
// https://github.com/jgraph/mxgraph/blob/master/javascript/src/js/handler/mxGraphHandler.js#L1036 // https://github.com/jgraph/mxgraph/blob/master/javascript/src/js/handler/mxGraphHandler.js#L1036
mxGraphHandler.prototype.updatePreview = function(remote) { // mxGraphHandler.prototype.updatePreview = function(remote) {
if (this.livePreviewUsed && !remote) { // if (this.livePreviewUsed && !remote) {
if (this.cells != null) // if (this.cells != null)
{ // {
this.setHandlesVisibleForCells(this.graph.getSelectionCells(), false); // this.setHandlesVisibleForCells(this.graph.getSelectionCells(), false);
this.updateLivePreview(this.currentDx, this.currentDy); // this.updateLivePreview(this.currentDx, this.currentDy);
} // }
} // }
else // else
{ // {
this.updatePreviewShape(); // this.updatePreviewShape();
} // }
}; // };
let oldMove = mxGraphHandler.prototype.mouseMove // let oldMove = mxGraphHandler.prototype.mouseMove
mxGraphHandler.prototype.mouseMove = function(sender, me){ // mxGraphHandler.prototype.mouseMove = function(sender, me){
// mxLog.debug("mouse move",sender,me) // // mxLog.debug("mouse move",sender,me)
// https://github.com/jgraph/mxgraph/blob/master/javascript/src/js/handler/mxGraphHandler.js#L901 // // https://github.com/jgraph/mxgraph/blob/master/javascript/src/js/handler/mxGraphHandler.js#L901
if (!this.livePreviewUsed && this.shape == null) { // if (!this.livePreviewUsed && this.shape == null) {
this.shape = this.createPreviewShape(this.bounds); // this.shape = this.createPreviewShape(this.bounds);
}else if(this.shape != null){ // }else if(this.shape != null){
// mxLog.debug("mouseMove",this) // // mxLog.debug("mouseMove",this)
} // }
oldMove.call(this, sender, me); // oldMove.call(this, sender, me);
} // }
let oldClick = mxGraphHandler.prototype.mouseDown let oldClick = mxGraphHandler.prototype.mouseDown
@ -546,7 +641,9 @@ class AltiumSchematicRenderer
} }
if((geo != null) && (this.getModel().isEdge(terminal.cell))) { if((geo != null) && (this.getModel().isEdge(terminal.cell))) {
console.log(terminal.cell) console.log(terminal.cell)
return [new mxConnectionConstraint(new mxPoint(0, 0), false)]; if(terminal.cell.Wire == true)
return [new mxConnectionConstraint(new mxPoint(1, 0), false),
new mxConnectionConstraint(new mxPoint(0.75, 1), true)];
} }
return null; return null;
}; };
@ -567,13 +664,9 @@ class AltiumSchematicRenderer
var parent = this.graph.getDefaultParent(); var parent = this.graph.getDefaultParent();
mxVertexHandler.prototype.livePreview = true;
var labelBackground = (false) ? '#000000' : '#FFFFFF'; var labelBackground = (false) ? '#000000' : '#FFFFFF';
var fontColor = (false) ? '#FFFFFF' : '#000000'; var fontColor = (false) ? '#FFFFFF' : '#000000';
var strokeColor = (false) ? '#C0C0C0' : '#000000'; var strokeColor = (false) ? '#C0C0C0' : '#000000';
var fillColor = (false) ? 'none' : '#FFFFFF';
var style = this.graph.getStylesheet().getDefaultEdgeStyle(); var style = this.graph.getStylesheet().getDefaultEdgeStyle();
delete style['endArrow']; delete style['endArrow'];
@ -591,7 +684,7 @@ class AltiumSchematicRenderer
// Sets join node size // Sets join node size
style['startSize'] = joinNodeSize; style['startSize'] = joinNodeSize;
style['endSize'] = joinNodeSize; style['endSize'] = joinNodeSize;
style = this.graph.getStylesheet().getDefaultVertexStyle(); style = this.graph.getStylesheet().getDefaultVertexStyle();
//style['gradientColor'] = '#909090'; //style['gradientColor'] = '#909090';
style['strokeColor'] = strokeColor; style['strokeColor'] = strokeColor;
@ -606,77 +699,17 @@ class AltiumSchematicRenderer
let doc = this.document; let doc = this.document;
let sheetObject = doc.objects.find(o => o instanceof AltiumSheet); let sheetObject = doc.objects.find(o => o instanceof AltiumSheet);
// canvas.style.width = sheetObject.width + "px";
// canvas.style.height = sheetObject.height + "px";
// canvas.width = sheetObject.width * window.devicePixelRatio;
// canvas.height = sheetObject.height * window.devicePixelRatio;
let areaColourInt = Number.parseInt(sheetObject.attributes.areacolor, 10); let areaColourInt = Number.parseInt(sheetObject.attributes.areacolor, 10);
let areaColour = this.#altiumColourToHex(areaColourInt);
// canvas.style.backgroundColor = areaColour;
// let ctx = canvas.getContext('2d');
// ctx.scale(1, -1);
// ctx.translate(0.5, 0.5);
// ctx.translate(0, -canvas.height);
// ctx.font = "7pt sans-serif";
// ctx.textRendering = "optimizeLegibility";
// ctx.imageSmoothingQuality = "high";
// ctx.textBaseline = "bottom";
// ctx.fillStyle = areaColour;
// ctx.fillRect(0, 0, canvas.width, canvas.height);
let results = document.getElementById("results"); let results = document.getElementById("results");
let sheet = doc.objects.find((o) => o instanceof AltiumSheet); let sheet = doc.objects.find((o) => o instanceof AltiumSheet);
let gridLight = "#eeeeee"; let gridLight = "#eeeeee";
let gridDark = "#cccccc"; let gridDark = "#cccccc";
// ctx.lineWidth = 1;
// ctx.globalAlpha = 0.5;
if (sheet.show_grid)
{
let n = 0; if (sheet.show_grid) {
// for (let x = 0; x < canvas.width; x += sheet.grid_size)
// {
// ctx.strokeStyle = ((n % 10) == 0) ? gridDark : gridLight;
// ctx.beginPath();
// ctx.moveTo(x, 0);
// ctx.lineTo(x, canvas.height);
// ctx.stroke();
// n++;
// }
// n = 0;
// for (let y = 0; y < canvas.height; y += sheet.grid_size)
// {
// ctx.strokeStyle = ((n % 10) == 0) ? gridDark : gridLight;
// ctx.beginPath();
// ctx.moveTo(0, y);
// ctx.lineTo(canvas.width, y);
// ctx.stroke();
// n++;
// }
} }
// ctx.globalAlpha = 1;
/*
ctx.textAlign = "center";
ctx.font = "bold 100px serif";
ctx.fillStyle = "#333300";
ctx.globalAlpha = 0.03;
ctx.save();
ctx.rotate(Math.PI/4);
ctx.scale(1,-1);
for (let y = 0; y < canvas.height * 2; y += 400)
{
ctx.fillText("PREVIEW - BETA - PREVIEW - BETA - PREVIEW - BETA - PREVIEW - BETA - PREVIEW - BETA - PREVIEW - BETA - PREVIEW - BETA - PREVIEW - BETA - PREVIEW - BETA - PREVIEW - BETA - PREVIEW - BETA - PREVIEW - BETA - PREVIEW - BETA - PREVIEW - BETA - PREVIEW - BETA - PREVIEW - BETA - PREVIEW - BETA", canvas.width / 2, canvas.height - (y + 200));
ctx.fillText("BETA - PREVIEW - BETA - PREVIEW - BETA - PREVIEW - BETA - PREVIEW - BETA - PREVIEW - BETA - PREVIEW - BETA - PREVIEW - BETA - PREVIEW - BETA - PREVIEW - BETA - PREVIEW - BETA - PREVIEW - BETA - PREVIEW - BETA - PREVIEW - BETA - PREVIEW - BETA - PREVIEW - BETA - PREVIEW - BETA - PREVIEW", canvas.width / 2, canvas.height - y);
}
ctx.textAlign = "left";
*/
this.graph.getModel().beginUpdate(); this.graph.getModel().beginUpdate();
try{ try{
let bom = []; let bom = [];
@ -694,11 +727,8 @@ class AltiumSchematicRenderer
bom.push(bomLine); bom.push(bomLine);
} }
//bomLine += obj.description; //bomLine += obj.description;
} }
results.innerText = bom.join("\n"); results.innerText = bom.join("\n");
for (let obj of doc.objects.filter((o) => o instanceof AltiumBus)) for (let obj of doc.objects.filter((o) => o instanceof AltiumBus))
{ {
// if (!this.#shouldShow(obj)) continue; // if (!this.#shouldShow(obj)) continue;
@ -713,7 +743,6 @@ class AltiumSchematicRenderer
// } // }
// ctx.stroke(); // ctx.stroke();
} }
for (let obj of doc.objects.filter((o) => o instanceof AltiumSheetSymbol)) for (let obj of doc.objects.filter((o) => o instanceof AltiumSheetSymbol))
{ {
// if (!this.#shouldShow(obj)) continue; // if (!this.#shouldShow(obj)) continue;
@ -748,8 +777,7 @@ class AltiumSchematicRenderer
// ctx.stroke(); // ctx.stroke();
} }
let chips = {} let chips = {}
for (let obj of doc.objects.filter((o) => o instanceof AltiumRectangle)) for (let obj of doc.objects.filter((o) => o instanceof AltiumRectangle)) {
{
obj.top = 840 - obj.top obj.top = 840 - obj.top
obj.bottom = 840 - obj.bottom obj.bottom = 840 - obj.bottom
if(!obj.transparent){ if(!obj.transparent){
@ -784,8 +812,7 @@ class AltiumSchematicRenderer
} }
// todo undo // todo undo
for (let obj of doc.objects.filter((o) => o instanceof AltiumSheetEntry)) for (let obj of doc.objects.filter((o) => o instanceof AltiumSheetEntry)) {
{
if (!this.#shouldShow(obj)) continue; if (!this.#shouldShow(obj)) continue;
// ctx.strokeStyle = this.#altiumColourToHex(obj.colour); // ctx.strokeStyle = this.#altiumColourToHex(obj.colour);
@ -820,7 +847,7 @@ class AltiumSchematicRenderer
for (let obj of doc.objects.filter((o) => o instanceof AltiumPin)) for (let obj of doc.objects.filter((o) => o instanceof AltiumPin))
{ {
var style = 'shape=line;fontColor=#000000;strokeColor=#000000;' var style = 'shape=line;fontColor=#000000;strokeColor=#000000;routingCenterX=-0.5;portConstraint=horizontal;'
let name = '' let name = ''
if(obj.show_name){ if(obj.show_name){
name = obj.name name = obj.name
@ -1173,8 +1200,6 @@ class AltiumSchematicRenderer
// ctx.fillText(obj.text, obj.x, canvas.height - obj.y); // ctx.fillText(obj.text, obj.x, canvas.height - obj.y);
// } // }
} }
// ctx.textAlign = "left";
// ctx.textBaseline = "bottom";
for (let obj of doc.objects.filter((o) => o instanceof AltiumNetLabel)) { for (let obj of doc.objects.filter((o) => o instanceof AltiumNetLabel)) {
obj = obj obj = obj
@ -1206,12 +1231,6 @@ class AltiumSchematicRenderer
// ctx.fillText(obj.text, obj.x, canvas.height - obj.y); // ctx.fillText(obj.text, obj.x, canvas.height - obj.y);
// } // }
} }
// ctx.textAlign = "left";
// ctx.textBaseline = "bottom";
// ctx.textAlign = "left";
// ctx.textBaseline = "bottom";
for (let obj of doc.objects.filter((o) => o instanceof AltiumPowerPort)) { for (let obj of doc.objects.filter((o) => o instanceof AltiumPowerPort)) {
obj = obj obj = obj
@ -1325,12 +1344,13 @@ class AltiumSchematicRenderer
e6.geometry.setTerminalPoint(new mxPoint(obj.points[0].x, obj.points[0].y), true) e6.geometry.setTerminalPoint(new mxPoint(obj.points[0].x, obj.points[0].y), true)
e6.geometry.setTerminalPoint(new mxPoint(obj.points[obj.points.length - 1].x, e6.geometry.setTerminalPoint(new mxPoint(obj.points[obj.points.length - 1].x,
obj.points[obj.points.length - 1].y), false) obj.points[obj.points.length - 1].y), false)
e6.Wire = true
if(this.wires == undefined){ if(this.wires == undefined){
this.wires = [] this.wires = []
this.wires.push(e6) this.wires.push(e6)
}else{ }else{
this.wires.push(e6) this.wires.push(e6)
} }