📄 dxf.java
字号:
/* -*- tab-width: 4 -*- * * Electric(tm) VLSI Design System * * File: DXF.java * Input/output tool: DXF input * Written by Steven M. Rubin, Sun Microsystems. * * Copyright (c) 2004 Sun Microsystems and Static Free Software * * Electric(tm) 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. * * Electric(tm) 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 Electric(tm); see the file COPYING. If not, write to * the Free Software Foundation, Inc., 59 Temple Place, Suite 330, * Boston, Mass 02111-1307, USA. */package com.sun.electric.tool.io.input;import com.sun.electric.database.geometry.EPoint;import com.sun.electric.database.geometry.Orientation;import com.sun.electric.database.hierarchy.Cell;import com.sun.electric.database.hierarchy.Library;import com.sun.electric.database.hierarchy.View;import com.sun.electric.database.prototype.NodeProto;import com.sun.electric.database.text.TextUtils;import com.sun.electric.database.topology.NodeInst;import com.sun.electric.database.variable.TextDescriptor;import com.sun.electric.database.variable.Variable;import com.sun.electric.technology.Layer;import com.sun.electric.technology.technologies.Artwork;import com.sun.electric.technology.technologies.Generic;import com.sun.electric.tool.Job;import com.sun.electric.tool.io.IOTool;import java.awt.geom.AffineTransform;import java.awt.geom.Point2D;import java.awt.geom.Rectangle2D;import java.io.IOException;import java.util.ArrayList;import java.util.HashSet;import java.util.Iterator;import java.util.List;/** * This class reads files in DEF files. */public class DXF extends Input{ private static class DXFLayer { private String layerName; private int layerColor; private double layerRed, layerGreen, layerBlue; private DXFLayer next; } private static class ForwardRef { private String refName; private Cell parent; private double x, y; private int rot; private double xSca, ySca; private ForwardRef nextForwardRef; } private static class PolyPoint { double x, y, z; double bulge; } private int lastGroupID; private String lastText; private boolean lastPairValid; private int ignoredPoints, ignoredAttributeDefs, ignoredAttributes; private int readPolyLines, readLines, readCircles, readSolids, read3DFaces, readArcs, readInserts, readTexts; private DXFLayer firstLayer; private ForwardRef firstForwardRef; private Cell mainCell; private Cell curCell; private List<String> headerText; private List<Integer> headerID; private int inputMode; /* 0: pairs not read, 1: normal pairs, 2: blank lines */ private HashSet<String> validLayerNames; private HashSet<String> ignoredLayerNames; private TextUtils.UnitScale dispUnit; private int groupID; private String text; /** key of Variable holding DXF layer name. */ public static final Variable.Key DXF_LAYER_KEY = Variable.newKey("IO_dxf_layer"); /** key of Variable holding DXF header text. */ public static final Variable.Key DXF_HEADER_TEXT_KEY = Variable.newKey("IO_dxf_header_text"); /** key of Variable holding DXF header information. */ public static final Variable.Key DXF_HEADER_ID_KEY = Variable.newKey("IO_dxf_header_ID"); /** * Method to import a library from disk. * @param lib the library to fill * @return the created library (null on error). */ protected Library importALibrary(Library lib) { try { if (readLibrary(lib)) return null; } catch (IOException e) {} return lib; } /** * Method to read the DXF file into library "lib". Returns true on error. */ private boolean readLibrary(Library lib) throws IOException { // set the scale setCurUnits(); // examine technology for acceptable DXF layer names getAcceptableLayers(); // make the only cell in this library mainCell = Cell.makeInstance(lib, lib.getName()); if (mainCell == null) return true; Job.getUserInterface().setCurrentCell(lib, mainCell); curCell = mainCell; headerID = new ArrayList<Integer>(); headerText = new ArrayList<String>(); ignoredLayerNames = new HashSet<String>(); // read the file lastPairValid = false; boolean err = false; firstLayer = null; firstForwardRef = null; ignoredPoints = ignoredAttributes = ignoredAttributeDefs = 0; readPolyLines = readLines = readCircles = readSolids = 0; read3DFaces = readArcs = readInserts = readTexts = 0; inputMode = 0; for(;;) { if (getNextPair()) { err = true; break; } // must have section change here if (groupID != 0) { System.out.println("Expected group 0 (start section) at line " + lineReader.getLineNumber()); err = true; break; } if (text.equals("EOF")) break; if (text.equals("SECTION")) { // see what section is coming if (getNextPair()) break; if (groupID != 2) { System.out.println("Expected group 2 (name) at line " + lineReader.getLineNumber()); err = true; break; } if (text.equals("HEADER")) { if (err = readHeaderSection()) break; continue; } if (text.equals("TABLES")) { if (err = readTablesSection()) break; continue; } if (text.equals("BLOCKS")) { if (err = readEntities(lib)) break; continue; } if (text.equals("ENTITIES")) { if (err = readEntities(lib)) break; continue; } if (text.equals("CLASSES")) { if (err = ignoreSection()) break; continue; } if (text.equals("OBJECTS")) { if (err = ignoreSection()) break; continue; } } System.out.println("Unknown section name (" + text + ") at line " + lineReader.getLineNumber()); err = true; break; } // insert forward references for(ForwardRef fr = firstForwardRef; fr != null; fr = fr.nextForwardRef) { // have to search by hand because of weird prototype names Cell found = null; for(Iterator<Cell> it = lib.getCells(); it.hasNext(); ) { Cell cell = it.next(); if (cell.getName().equals(fr.refName)) { found = cell; break; } } if (found == null) { System.out.println("Cannot find block '" + fr.refName + "'"); continue; } if (IOTool.isDXFInputFlattensHierarchy()) { if (extractInsert(found, fr.x, fr.y, fr.xSca, fr.ySca, fr.rot, fr.parent)) return true; } else { if (fr.xSca != 1.0 || fr.ySca != 1.0) { found = getScaledCell(found, fr.xSca, fr.ySca); if (found == null) return true; } Rectangle2D bounds = found.getBounds(); Orientation orient = Orientation.fromAngle(fr.rot*10); NodeInst ni = NodeInst.makeInstance(found, new Point2D.Double(fr.x, fr.y), bounds.getWidth(), bounds.getHeight(), fr.parent, orient, null, 0); if (ni == null) return true; ni.setExpanded(); } } // save header with library if (headerID.size() > 0) { int len = headerID.size(); Integer [] headerIDs = new Integer[len]; for(int i=0; i<len; i++) headerIDs[i] = headerID.get(i); lib.newVar(DXF_HEADER_ID_KEY, headerIDs); } if (headerText.size() > 0) { int len = headerText.size(); String [] headerTexts = new String[len]; for(int i=0; i<len; i++) headerTexts[i] = headerText.get(i); lib.newVar(DXF_HEADER_TEXT_KEY, headerTexts); } if (readPolyLines > 0 || readLines > 0 || readCircles > 0 || readSolids > 0 || read3DFaces > 0 || readArcs > 0 || readTexts > 0 || readInserts > 0) { String warning = "Read"; boolean first = true; if (readPolyLines > 0) { if (first) warning += ","; first = false; warning += " " + readPolyLines + " polylines"; } if (readLines > 0) { if (first) warning += ","; first = false; warning += " " + readLines + " lines"; } if (readCircles > 0) { if (first) warning += ","; first = false; warning += " " + readCircles + " circles"; } if (readSolids > 0) { if (first) warning += ","; first = false; warning += " " + readSolids + " solids"; } if (read3DFaces > 0) { if (first) warning += ","; first = false; warning += " " + read3DFaces + " 3d faces"; } if (readArcs > 0) { if (first) warning += ","; first = false; warning += " " + readArcs + " arcs"; } if (readTexts > 0) { if (first) warning += ","; first = false; warning += " " + readTexts + " texts"; } if (readInserts > 0) { if (first) warning += ","; first = false; warning += " " + readInserts + " inserts"; } System.out.println(warning); } if (ignoredPoints > 0 || ignoredAttributes > 0 || ignoredAttributeDefs > 0) { String warning = "Ignored"; boolean first = true; if (ignoredPoints > 0) { if (first) warning += ","; first = false; warning += " " + ignoredPoints + " points"; } if (ignoredAttributes > 0) { if (first) warning += ","; first = false; warning += " " + ignoredAttributes + " attributes"; } if (ignoredAttributeDefs > 0) { if (first) warning += ","; first = false; warning += " " + ignoredAttributeDefs + " attribute definitions"; } System.out.println(warning); } // say which layers were ignored if (ignoredLayerNames.size() > 0) { String warning = "Ignored layers "; boolean first = true; for(String name : ignoredLayerNames) { if (!first) warning += ", "; first = false; warning += "'" + name + "'"; } System.out.println(warning); } return err; } /** * Method to read the next group ID and content pair from the file. * Returns true on end-of-file. */ private boolean getNextPair() throws IOException { if (lastPairValid) { text = lastText; groupID = lastGroupID; lastPairValid = false; return false; } for(;;) { // read a line and get the group ID lastText = getNextLine(false); if (lastText == null) { System.out.println("Unexpected end-of-file at line " + lineReader.getLineNumber()); return true; } String groupLine = lastText.trim(); if (!TextUtils.isANumber(groupLine)) { System.out.println("Invalid group ID on line " + lineReader.getLineNumber() + " (" + lastText + ")"); return true; } groupID = TextUtils.atoi(groupLine); // ignore blank line if file is double-spaced if (inputMode == 2) getNextLine(true); // read a line and get the text lastText = getNextLine(true); if (lastText == null) { System.out.println("Unexpected end-of-file at line " + lineReader.getLineNumber()); return true; } text = lastText.trim(); // ignore blank line if file is double-spaced if (inputMode == 2) getNextLine(true); if (inputMode == 0) { // see if file is single or double spaced if (lastText.length() != 0) inputMode = 1; else { inputMode = 2; lastText = getNextLine(true); if (lastText == null) { System.out.println("Unexpected end-of-file at line " + lineReader.getLineNumber()); return true; } text = lastText; getNextLine(true); } } // continue reading if a comment, otherwise quit if (groupID != 999) break; } return false; } private String getNextLine(boolean canBeBlank) throws IOException { for(;;) { String text = lineReader.readLine(); if (canBeBlank || text.length() != 0) return text; } } /****************************************** READING SECTIONS ******************************************/ private boolean readHeaderSection() throws IOException { // just save everything until the end-of-section for(int line=0; ; line++) { if (getNextPair()) return true; if (groupID == 0 && text.equals("ENDSEC")) break; // save it headerID.add(new Integer(groupID)); headerText.add(text); } return false; } private boolean readTablesSection() throws IOException { // just ignore everything until the end-of-section for(;;) { if (getNextPair()) return true; // quit now if at the end of the table section if (groupID == 0 && text.equals("ENDSEC")) break; // must be a 'TABLE' declaration if (groupID != 0 || !text.equals("TABLE")) continue; // a table: see what kind it is if (getNextPair()) return true; if (groupID != 2 || !text.equals("LAYER")) continue; // a layer table: ignore the size information if (getNextPair()) return true; if (groupID != 70) continue; // read the layers DXFLayer layer = null; for(;;) { if (getNextPair()) return true; if (groupID == 0 && text.equals("ENDTAB")) break; if (groupID == 0 && text.equals("LAYER")) { // make a new layer layer = new DXFLayer(); layer.layerName = null; layer.layerColor = -1; layer.layerRed = 1.0; layer.layerGreen = 1.0; layer.layerBlue = 1.0; layer.next = firstLayer; firstLayer = layer; } if (groupID == 2 && layer != null) { layer.layerName = text; } if (groupID == 62 && layer != null) { layer.layerColor = TextUtils.atoi(text); DXFLayer found = null; for(DXFLayer l = firstLayer; l != null; l = l.next) { if (l == layer) continue; if (l.layerColor == layer.layerColor) { found = l; break; } } if (found != null) { layer.layerRed = found.layerRed; layer.layerGreen = found.layerGreen; layer.layerBlue = found.layerBlue; } else { switch (layer.layerColor) { case 1: // red layer.layerRed = 1.0; layer.layerGreen = 0.0; layer.layerBlue = 0.0; break; case 2: // yellow layer.layerRed = 1.0; layer.layerGreen = 1.0; layer.layerBlue = 0.0; break; case 3: // green layer.layerRed = 0.0; layer.layerGreen = 1.0; layer.layerBlue = 0.0;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -