Class 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 DXFDocument
      DXFDocument​(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 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.
      DXFXrecord addXrecord()
      Add an Xrecord to the Objects section of the DXF file.
      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.
      void generatePoints​(boolean usePoints)
      When true, the DXF output will include points in place of zero-length lines and zero-radius circles.
      DXFGraphics getGraphics()
      Get the DXFGraphics associated with this document for use with standard Graphics drawing operations to generate a DXF text representation.
      void insertShapesAsBlocks​(boolean useBlocks)
      When true, Java Shapes will be incorporated as Blocks in the DXF output when draw(Shape) is called and included as Insert entities (when feasible - see below).
      void setLayer​(java.lang.String layerName)
      Set the current layer, creating a new layer if one with the supplied name doesn't already exist.
      void setPenByLayer​(boolean useLayerPen)
      When setPenByLayer(true) is called, all subsequent entities will have their color, line width and linetype set to ByLayer, so that they will use the values of their assigned layer rather than the current values set for the DXFGraphics object.
      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.
      void setViewportCenter​(double centerX, double centerY)
      Set the center of the default viewport display window when viewed in a CAD program.
      void setViewportScale​(double viewportScale)
      Set the scale for the active viewport; this will determine the default height and width of the display window when viewed in a CAD program.
      java.lang.String toDXFString()
      Return the DXF text associated with this DXF document.
      • Methods inherited from class java.lang.Object

        equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • DXFDocument

        public DXFDocument()
        Create a new DXFDocument
      • DXFDocument

        public DXFDocument​(java.lang.String documentComment)
        Create a new DXF document with the specified comment in its header
        Parameters:
        documentComment - Comment for the 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
      • generatePoints

        public void generatePoints​(boolean usePoints)
        When true, the DXF output will include points in place of zero-length lines and zero-radius circles. Note that Java AWT graphics class doesn't include specific point drawing commands; the standard approach to representing a point onscreen is to create a line with the same start and end point, which will be represented as a single pixel. Default is true; points will be generated.
        Parameters:
        usePoints - When true, will generate a DXF point in output when a zero-length line or zero-radius circle is drawn; when false, will generate a line or circle in the DXF output
      • insertShapesAsBlocks

        public void insertShapesAsBlocks​(boolean useBlocks)
        When true, Java Shapes will be incorporated as Blocks in the DXF output when draw(Shape) is called and included as Insert entities (when feasible - see below). The Insert entities will use the current Java transform to translate and rotate the corresponding Block to position it in the drawing. Note that this is feasible only when the Java transform consists only of scaling, rotation and translation, i.e., has no shear component, since the DXF Insert declaration provides only these operations. Thus if the Java transform includes shear, the Shape will be incorporated directly in the Entities section even if this flag is true. Default is false; Shapes will be drawn as entities inline rather than referenced as blocks. Note that the entities within Blocks are assigned to layer "0", the default layer, and the pen characteristics (color, linewidth, linetype) are assigned values of ByBlock. In this way, they will inherit the layers and pen characteristics of the corresponding Insert entities.
        Parameters:
        useBlocks - When true, will generate a DXF Block in response to a draw(Shape) operation and include a corresponding Insert entitiy in the DXF output (as long as the current Java transform doesn't include shear)
      • setLayer

        public void setLayer​(java.lang.String layerName)
        Set the current layer, creating a new layer if one with the supplied name doesn't already exist. All subsequent entities will be assigned to this layer until the next call to setLayer. When created, the layer is assigned the drawing charateristics - color, line width and linetype - that the corresponding DXFGraphics object has at the time of the call. The default is for all entities to be placed on the default layer "0". Calling setLayer("0") or setLayer(null) will reset the layer to the default layer.
        Parameters:
        layerName - The name of the layer, or null (or "0") to set to default layer
      • setPenByLayer

        public void setPenByLayer​(boolean useLayerPen)
        When setPenByLayer(true) is called, all subsequent entities will have their color, line width and linetype set to ByLayer, so that they will use the values of their assigned layer rather than the current values set for the DXFGraphics object.
        Parameters:
        useLayerPen -
      • setViewportScale

        public void setViewportScale​(double viewportScale)
        Set the scale for the active viewport; this will determine the default height and width of the display window when viewed in a CAD program.
        Parameters:
        viewportScale - The span in drawing units of the default CAD display window
      • setViewportCenter

        public void setViewportCenter​(double centerX,
                                      double centerY)
        Set the center of the default viewport display window when viewed in a CAD program.
        Parameters:
        centerX - X coordinate, in drawing units
        centerY - Y coordinate, in drawing units
      • addXrecord

        public DXFXrecord addXrecord()
        Add an Xrecord to the Objects section of the DXF file. The added DXFXrecord object is returned, allowing group-code/value associations to be added using the DXFXrecord method addAttribute.
        Returns:
        The DXFXrecord object added to the Objects section
      • 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 lines
        code - the group code, e.g., 70 for the above
        value - 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.