Package com.jsevy.jdxf
Class DXFDocument
- java.lang.Object
-
- com.jsevy.jdxf.DXFDocument
-
public class DXFDocument extends java.lang.Object
Class representing a DXF document, which owns a DXFGraphics on which drawing commands can be made. The document's toDXFString method can then be called to generate the DXF text corresponding to the drawing done on the Graphics object. The typical workflow is as follows:// Create a DXF document and get its associated DXFGraphics instance DXFDocument dxfDocument = new DXFDocument("Example"); DXFGraphics dxfGraphics = dxfDocument.getGraphics(); // Do drawing commands as on any other Graphics. If you have a paint(Graphics) method, // you can just use it with the DXFGraphics instance since it's a subclass of Graphics. graphics.setColor(Color.RED); graphics.setStroke(new BasicStroke(3)); graphics.drawLine(0, 0, 1000, 500); graphics.drawRect(1000, 500, 150, 150); graphics.drawRoundRect(20, 200, 130, 100, 20, 10); // Get the DXF output as a string - it's just text - and save in a file for use with a CAD package String stringOutput = dxfDocument.toDXFString(); String filePath = "path/to/file.dxf"; FileWriter fileWriter = new FileWriter(filePath); fileWriter.write(dxfText); fileWriter.flush(); fileWriter.close();
- Author:
- jsevy
-
-
Constructor Summary
Constructors Constructor Description DXFDocument()
Create a new DXFDocumentDXFDocument(java.lang.String documentComment)
Create a new DXF document with the specified comment in its header
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addEntity(DXFEntity entity)
Utility method used by the associated DXFGraphics object.void
addHeaderVariable(java.lang.String name, int code, java.lang.String value)
Used to add variable value to the header that isn't automatically included, to handle something like the desired format of units or the like.DXFStyle
addStyle(DXFStyle style)
Utility method used by the associated DXFGraphics object.void
addTable(DXFTable table)
Utility method used by the associated DXFGraphics object.void
generateCircularArcs(boolean useCircles)
When true, the DXF output will include circles and circular arcs when appropriate rather than always using the more general elliptical arcs.DXFGraphics
getGraphics()
Get the DXFGraphics associated with this document for use with standard Graphics drawing operations to generate a DXF text representation.void
setPrecisionDigits(int decimalDigits)
Set the number of digits of precision to output into the DXF file for measurement quantities (locations, angles, etc.) All calculations are done internally as double-precision quantities; however this allows the specification of how many decimal digits should be output in the DXF file.void
setUnits(int unitsCode)
Set the units for interpreting the dimension values in the DXF file.java.lang.String
toDXFString()
Return the DXF text associated with this DXF document.
-
-
-
Method Detail
-
setUnits
public void setUnits(int unitsCode)
Set the units for interpreting the dimension values in the DXF file.- Parameters:
unitsCode
- DXF code indicating units: 0 = Unitless 1 = Inches 2 = Feet 3 = Miles 4 = Millimeter 5 = Centimeters 6 = Meters 7 = Kilometers 8 = Microinche 9 = Mils 10 = Yards 11 = Angstroms 12 = Nanometer 13 = Microns 14 = Decimeters 15 = Decameter 16 = Hectometers 17 = Gigameters 18 = Astronomical units 19 = Light years 20 = Parsecs (no kidding)
-
setPrecisionDigits
public void setPrecisionDigits(int decimalDigits)
Set the number of digits of precision to output into the DXF file for measurement quantities (locations, angles, etc.) All calculations are done internally as double-precision quantities; however this allows the specification of how many decimal digits should be output in the DXF file. This avoids the annoyance of rounding error that may otherwise result in an angle expected to be 45 degrees represented as 45.0000000347 degrees. By setting the precision to something like 5 digits, this would be output as 45.0 degrees. The number of digits must be between 0 and 16, since 16 is the maximum precision of an IEEE double quantity. Default is 10 digits.- Parameters:
decimalDigits
- The number of digits to be output following the decimal point in double values.
-
generateCircularArcs
public void generateCircularArcs(boolean useCircles)
When true, the DXF output will include circles and circular arcs when appropriate rather than always using the more general elliptical arcs. Note that Java AWT graphics class doesn't include specific circle and circular arc drawing commands, providing just drawOval and drawArc for all such shapes. However, DXF can represent true circles and circular arcs, and setting optimizeArcs to true will cause these to be used in addition to elliptical arcs. Default is true; circular arcs will be generated.- Parameters:
useCircles
- When true, will include DXF circles and circular arcs in output; when false, will use DXF elliptical arcs for all circular/elliptical shapes
-
addHeaderVariable
public void addHeaderVariable(java.lang.String name, int code, java.lang.String value)
Used to add variable value to the header that isn't automatically included, to handle something like the desired format of units or the like. The JDXF library automatically sets the AutoCAD version value and the handle limit; there's also a utility routine, setUnits(), for indicating the units to be used for interpreting dimensions in the drawing. The full set of variable values that can be added can be found in the official DXF documentation from AutoCAD: https://www.autodesk.com/techpubs/autocad/acad2000/dxf/header_section_group_codes_dxf_02.htm This is an advanced method; you need to understand DXF header variables to use this effectively.- Parameters:
name
- the variable name of the element, e.g., $DIMLWD for the line weight to use for dimension linescode
- the group code, e.g., 70 for the abovevalue
- the value to be assigned; e.g., for the above example, a value from 0 to 211, supplied as a string, giving the dimension line thickness in 100ths of mm,
-
getGraphics
public DXFGraphics getGraphics()
Get the DXFGraphics associated with this document for use with standard Graphics drawing operations to generate a DXF text representation.- Returns:
- The DXFGraphics associated with this document, on which standard Java Graphics drawing calls can be made
-
toDXFString
public java.lang.String toDXFString()
Return the DXF text associated with this DXF document. This includes the header, classes, tables, blocks, entities and objects sections, populated with content generated by graphics calls on the associated DXFGraphics.- Returns:
- The DXF text associated with this document.
-
addTable
public void addTable(DXFTable table)
Utility method used by the associated DXFGraphics object.- Parameters:
table
- A DXFTable instance
-
addEntity
public void addEntity(DXFEntity entity)
Utility method used by the associated DXFGraphics object.- Parameters:
entity
- A DXFEntity instance
-
-