How to create dxf file on swift

There is lot of examples how to create a pdf file on swift.

I search a lot for tutorials or idea how to create a dxf file on swift but I did not succeed.


Any help would be appreciated.


Regards

Can you please tell me what is dxf file ?

dxf is Drawing Exchange File (American Standard Code for Information Exhange AutoCAD file format)

A File with extension dxf can be opened by autocad.

Thanks for clarifying. In fact, when I searched with "swift dxf file", I could not understand what the titles of the search results mean...


But, unfortunately, as far as I know, there are no functionalities in Apple's frameworks to handle dxf files.

Neither open source libiraries.


I'm afraid you may need to make it all by yourself.

I am not that expert to make it by myself man 😉 .

Anyway, thanks a lot for your replies

You'll find high level information for file structure on wikipedia


The basic organization of a DXF file is as follows

  • HEADER section – General information about the drawing. Each parameter has a variable name and an associated value.
  • CLASSES section – Holds the information for application-defined classes whose instances appear in the BLOCKS, ENTITIES, and OBJECTS sections of the database. Generally does not provide sufficient information to allow interoperability with other programs.
  • TABLES section – This section contains definitions of named items.
Application ID (APPID) table Block Record (BLOCK_RECORD) table Dimension Style (DIMSTYLE) table Layer (LAYER) table Linetype (LTYPE) table Text style (STYLE) table User Coordinate System (UCS) table View (VIEW) table Viewport configuration (VPORT) table
  • BLOCKS section – This section contains Block Definition entities describing the entities comprising each Block in the drawing.
  • ENTITIES section – This section contains the drawing entities, including any Block References.
  • OBJECTS section – Contains the data that apply to nongraphical objects, used by AutoLISP and ObjectARX applications.
  • THUMBNAILIMAGE section – Contains the preview image for the DXF file.
  • END OF FILE


For instance, following file is dxf for a circle : centre (0.0,0.0,0.0) radius 1.0

0

SECTION

2

ENTITIES

0

CIRCLE

10

0.0

20

0.0

30

0.0

40

1.0

0

ENDSEC

0

EOF



Detailed spec has been published by Autocad (It is a 250 pages document)

h ttp://images.autodesk.com/adsk/files/autocad_2012_pdf_dxf-reference_enu.pdf


How complex is the model you need to write as dxf ?


Maybe this could help with code

h ttps://github.com/codelibs/libdxfrw

@Claude31: thanks man for your explanation.

By the way, the library provided is writen on C++, and I am not sure to do the same but in swift.


in majority of languages, there are ways to write on dxf file except swift I think.

That's not necessarily a question of language but of existing libraries in this language. I doubt there are language primitive to write dxf.


What trype of drawing do you need to export ? Creating dxf seems pretty simple if the drawing is simple enough.


You could also post your question on autodesk forum

With the help of kind people like you, I would like to ask how I can create a valid dxf file which can be opened by autocad, and then how to create a function lets say "func drawline(p1: CGPoint, p2: CGPoint)" which can draw a line between 2 points on dxf file

Once I have the above, I will be able to draw any drawing doesnt matter it is complexity.

Regards

You have to create a text file ; as an example, create it for a circle ;


0

SECTION

2

ENTITIES

0

CIRCLE

10

0.0

20

0.0

30

0.0

40

1.0

0

ENDSEC

0

EOF


look at specification I have osted to see how to describe a line


So write something of this kind:


func createDXF() -> Bool  {

    var content : String
     content = """
     0
     SECTION
       2
     ENTITIES
       0
     CIRCLE
     10
     0.0
     20
     0.0
     30
     0.0
     40
     1.0
       0
     ENDSEC
       0
     EOF
     """

    do {
        let documentDirUrl = try FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
        let fileName = "output.dxf"
        let fileUrl = documentDirUrl.appendingPathComponent(fileName)
        do {
            try content.write(to: fileUrl, atomically: true, encoding: .utf8)
        } catch {   // content.write
            print(" ---> NO WRITE dxf", error)
            return false
        }
    } catch {   // FileManager.default.url
        print(" ---> NO default.url", error)
        return false
    }
   
    return true
}
How to create dxf file on swift
 
 
Q