首先需要引入kabeja这个包
个人理解:DXF图纸是由一张张图层(DXFLayer)组成的,一张张图层又是由一个个实体类(DXFEntity)组成的,至于块(DXFBlock)呢,我理解的他就是一个模板,也是由一个个的实体类组成,而图层中的实体类有可能就是通过模型插入的,而通过块所插入的实体类的类型就是DXFInsert(该类是DXFEntity的子类),具体可看上面链接的api。
直接上代码:
package parse;
import java.awt.Polygon;
import java.io.File;
import java.io.FileInputStream;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.ArrayList;
import java.util.Set;
import java.util.Iterator;
import java.util.Properties;
import org.kabeja.dxf.DXFConstants;
import org.kabeja.dxf.DXFDocument;
import org.kabeja.dxf.DXFEntity;
import org.kabeja.dxf.DXFLWPolyline;
import org.kabeja.dxf.DXFLayer;
import org.kabeja.dxf.DXFLine;
import org.kabeja.dxf.DXFMText;
import org.kabeja.dxf.DXFVertex;
import org.kabeja.dxf.helpers.Point;
import org.kabeja.parser.DXFParser;
import org.kabeja.parser.Parser;
import org.kabeja.parser.ParserBuilder;
import org.kabeja.dxf.DXFBlock;
import org.kabeja.dxf.DXFLineType;
import org.kabeja.dxf.DXFDimensionStyle;
import org.kabeja.dxf.objects.DXFObject;
import org.kabeja.dxf.DXFStyle;
import org.kabeja.dxf.DXFView;
import org.kabeja.dxf.DXFViewport;
import org.kabeja.dxf.DXFVariable;
import org.kabeja.dxf.objects.DXFDictionary;
import org.kabeja.dxf.DXFHatchPattern;
import org.kabeja.dxf.DXFInsert;
import org.kabeja.dxf.DXFLeader;
import org.kabeja.dxf.Bounds;
public class DXFUtil {
public static void main(String[] args) throws Exception{
Parser dxfParser = ParserBuilder.createDefaultParser();
dxfParser.parse(new FileInputStream("C:/Users/Administrator/Desktop/CAD_/28F平面图-0819.dxf"), "UTF-8");
DXFDocument doc = dxfParser.getDocument();
// getDXFViewports(doc);
// while(doc.getDXFHeader().getVarialbeIterator().hasNext()){
// DXFVariable dv = (DXFVariable)doc.getDXFHeader().getVarialbeIterator().next();
// System.out.println(dv.getName());
// }
// getDXFBlocks(doc);
// getDXFLineTypes(doc);
// getDXFStyles(doc);
// getDXFViews(doc);
getDXFLayers(doc);
// getRootDXFDictionarys(doc);
// getDXFHatchPatternIterators(doc);
// for(DXFBlock d : dxfBlocks){
// System.out.println(d.getName());
// System.out.println(d.getLayerID());
// }
}
//块列表 获取所有的的块(block)
public static List<DXFBlock> getDXFBlocks(DXFDocument dxfdoc){
List<DXFBlock> dxfBlocks = new ArrayList<DXFBlock>();
Iterator iter = dxfdoc.getDXFBlockIterator();
while(iter.hasNext()){
DXFBlock dXFBlock = (DXFBlock)iter.next();
// System.out.println("LayerID: "+dXFBlock.getLayerID()+" Name: "+dXFBlock.getName()+" description: "+dXFBlock.getDescription()+" document: "+dXFBlock.getDXFDocument()+" Ponit: "+dXFBlock.getReferencePoint());
// System.out.println("LayerID: "+dXFBlock.getLayerID()+" Name: "+dXFBlock.getName()+" description: "+dXFBlock.getDescription()+" Ponit: "+dXFBlock.getReferencePoint()+" Buonds: "+dXFBlock.getBounds());
Bounds b = dXFBlock.getBounds();
System.out.println