Problems importing DXF with OpenSCAD

Post here for help on using FreeCAD's graphical user interface (GUI).
Forum rules
and Helpful information
IMPORTANT: Please click here and read this first, before asking for help

Also, be nice to others! Read the FreeCAD code of conduct!
keithsloan52
Veteran
Posts: 2756
Joined: Mon Feb 27, 2012 5:31 pm

Problems importing DXF with OpenSCAD

Post by keithsloan52 »

@chennes Please could you take look at https://github.com/KeithSloan/OpenSCAD_ ... /issues/10 as I get the same problem with the standard importer.


Image 17-03-2023 at 20.21.jpg
Image 17-03-2023 at 20.21.jpg (55.12 KiB) Viewed 968 times

Code: Select all

opening /var/folders/cj/z259fmwd41dgzl8mq8nppwd40000gn/T/../../../../../../Users/keithsloan/Downloads/testbend.dxf...
20:17:54  ----------------
20:17:54  Layer
20:17:54  name: 0
20:17:54  line_color: (0.0, 0.0, 0.0)
20:17:54  line_width: 2.0
20:17:54  draw_style: Solid
20:17:54  transparency: 0
20:17:54  drawing 106 lines...
20:17:54  ----------------
20:17:54  Layer
20:17:54  name: Layer 1
20:17:54  line_width: 2.0
20:17:54  draw_style: None
20:17:54  typecheck[make_layer]: 'None' is not <class 'str'>
20:17:54  Wrong input: must be 'Solid', 'Dashed', 'Dotted', or 'Dashdot'.
20:17:54  Traceback (most recent call last):
20:17:54    File "/Applications/FreeCAD_0.20.2.app/Contents/Resources/Mod/OpenSCAD/OpenSCADCommands.py", line 404, in addelement
20:17:54      importCSG.insert(tmpfilename,doc.Name)
20:17:54    File "/Applications/FreeCAD_0.20.2.app/Contents/Resources/Mod/OpenSCAD/importCSG.py", line 167, in insert
20:17:54      processcsg(filename)
20:17:54    File "/Applications/FreeCAD_0.20.2.app/Contents/Resources/Mod/OpenSCAD/importCSG.py", line 192, in processcsg
20:17:54      result = parser.parse(f.read())
20:17:54    File "/Applications/FreeCAD_0.20.2.app/Contents/Resources/lib/python3.10/site-packages/ply/yacc.py", line 333, in parse
20:17:54      return self.parseopt_notrack(input, lexer, debug, tracking, tokenfunc)
20:17:54    File "/Applications/FreeCAD_0.20.2.app/Contents/Resources/lib/python3.10/site-packages/ply/yacc.py", line 1120, in parseopt_notrack
20:17:54      p.callable(pslice)
20:17:54    File "/Applications/FreeCAD_0.20.2.app/Contents/Resources/Mod/OpenSCAD/importCSG.py", line 846, in p_import_file1
20:17:54      p[0] = [process_import_file(filen,ext,p[3]['layer'])]
20:17:54    File "/Applications/FreeCAD_0.20.2.app/Contents/Resources/Mod/OpenSCAD/importCSG.py", line 864, in process_import_file
20:17:54      obj=processDXF(fname,layer)
20:17:54    File "/Applications/FreeCAD_0.20.2.app/Contents/Resources/Mod/OpenSCAD/importCSG.py", line 927, in processDXF
20:17:54      face = importDXFface(filename,layer,doc)
20:17:54    File "/Applications/FreeCAD_0.20.2.app/Contents/Resources/Mod/OpenSCAD/OpenSCAD2Dgeom.py", line 484, in importDXFface
20:17:54      layers = importDXF.processdxf(doc,filename,False,False) or importDXF.layers
20:17:54    File "/Applications/FreeCAD_0.20.2.app/Contents/Resources/Mod/Draft/importDXF.py", line 2255, in processdxf
20:17:54      newob = addObject(shape, "Line", line.layer)
20:17:54    File "/Applications/FreeCAD_0.20.2.app/Contents/Resources/Mod/Draft/importDXF.py", line 1978, in addObject
20:17:54      lay = locateLayer(layer)
20:17:54    File "/Applications/FreeCAD_0.20.2.app/Contents/Resources/Mod/Draft/importDXF.py", line 349, in locateLayer
20:17:54      newLayer.Label = wantedLayerName
20:17:54  AttributeError: 'NoneType' object
has no attribute 'Label'
testbend.dxf
(7.03 KiB) Downloaded 11 times
testbend2.dxf
(7.04 KiB) Downloaded 10 times
imm
Posts: 250
Joined: Wed Nov 03, 2021 1:00 pm

Re: Problems importing DXF with OpenSCAD

Post by imm »

Testbend.dxf imported fine in FreeCAD v0.21

Please remember to click on the 'Draft' workbench to ensure that module is loaded before you do your import....or go to preferences and ensure it is loaded at the time you start FreeCAD.
dfx_import_working.PNG
dfx_import_working.PNG (94.83 KiB) Viewed 934 times
keithsloan52
Veteran
Posts: 2756
Joined: Mon Feb 27, 2012 5:31 pm

Re: Problems importing DXF with OpenSCAD

Post by keithsloan52 »

imm wrote: Fri Mar 17, 2023 11:13 pm Testbend.dxf imported fine in FreeCAD v0.21

Please remember to click on the 'Draft' workbench to ensure that module is loaded before you do your import....or go to preferences and ensure it is loaded at the time you start FreeCAD.

dfx_import_working.PNG
Sorry but I don't think you picked up on the issue. The problem is that the file does not import when using the OpenSCAD function import and gives the error. This would be a problem if the dxf file was being used as part of a more complicated scad script.

The OpenSCAD importer code has

Code: Select all

def process_import_file(fname, ext, layer):
    from OpenSCADUtils import reverseimporttypes
    if printverbose: print("Importing : "+fname+"."+ext+" Layer : "+layer)
    if ext.lower() in reverseimporttypes()['Mesh']:
        obj=process_mesh_file(fname, ext)
    elif ext.lower() == 'dxf':
        obj=processDXF(fname, layer)
    elif ext.lower() == 'svg':
        obj=processSVG(fname, ext)
    else:
        raise ValueError("Unsupported file extension %s" % ext)
    return(obj)
So assumes the file has a layer
imm
Posts: 250
Joined: Wed Nov 03, 2021 1:00 pm

Re: Problems importing DXF with OpenSCAD

Post by imm »

The last few lines of your error posting indicate that the OpenSCAD module called on the Draft module functions to process the code.

You will need the Draft module library initialized before you run your OpenSCAD script.

Code: Select all

20:17:54    File "/Applications/FreeCAD_0.20.2.app/Contents/Resources/Mod/OpenSCAD/importCSG.py", line 927, in processDXF
20:17:54      face = importDXFface(filename,layer,doc)
20:17:54    File "/Applications/FreeCAD_0.20.2.app/Contents/Resources/Mod/OpenSCAD/OpenSCAD2Dgeom.py", line 484, in importDXFface
20:17:54      layers = importDXF.processdxf(doc,filename,False,False) or importDXF.layers
20:17:54    File "/Applications/FreeCAD_0.20.2.app/Contents/Resources/Mod/Draft/importDXF.py", line 2255, in processdxf
20:17:54      newob = addObject(shape, "Line", line.layer)
20:17:54    File "/Applications/FreeCAD_0.20.2.app/Contents/Resources/Mod/Draft/importDXF.py", line 1978, in addObject
20:17:54      lay = locateLayer(layer)
20:17:54    File "/Applications/FreeCAD_0.20.2.app/Contents/Resources/Mod/Draft/importDXF.py", line 349, in locateLayer
20:17:54      newLayer.Label = wantedLayerName
20:17:54  AttributeError: 'NoneType' object
has no attribute 'Label'
keithsloan52
Veteran
Posts: 2756
Joined: Mon Feb 27, 2012 5:31 pm

Re: Problems importing DXF with OpenSCAD

Post by keithsloan52 »

imm wrote: Sat Mar 18, 2023 10:14 am The last few lines of your error posting indicate that the OpenSCAD module called on the Draft module functions to process the code.

You will need the Draft module library initialized before you run your OpenSCAD script.

Code: Select all

20:17:54    File "/Applications/FreeCAD_0.20.2.app/Contents/Resources/Mod/OpenSCAD/importCSG.py", line 927, in processDXF
20:17:54      face = importDXFface(filename,layer,doc)
20:17:54    File "/Applications/FreeCAD_0.20.2.app/Contents/Resources/Mod/OpenSCAD/OpenSCAD2Dgeom.py", line 484, in importDXFface
20:17:54      layers = importDXF.processdxf(doc,filename,False,False) or importDXF.layers
20:17:54    File "/Applications/FreeCAD_0.20.2.app/Contents/Resources/Mod/Draft/importDXF.py", line 2255, in processdxf
20:17:54      newob = addObject(shape, "Line", line.layer)
20:17:54    File "/Applications/FreeCAD_0.20.2.app/Contents/Resources/Mod/Draft/importDXF.py", line 1978, in addObject
20:17:54      lay = locateLayer(layer)
20:17:54    File "/Applications/FreeCAD_0.20.2.app/Contents/Resources/Mod/Draft/importDXF.py", line 349, in locateLayer
20:17:54      newLayer.Label = wantedLayerName
20:17:54  AttributeError: 'NoneType' object
has no attribute 'Label'
The problem is NOT a failure of initializing the Draft module.

If I add a few diag prints I get

Code: Select all

11:05:16  Processing : /Users/keithsloan/Downloads/testImport.csg
11:05:16  Using Alternate OpenSCAD Importer
11:05:16  Doc testImport useMaxFn 16
11:05:16  Import File {'file': 'testBend.dxf', 'layer': 'Layer1', 'origin': [0.0, 0.0], 'scale': '1', 'convexity': '1', '$fn': '0', '$fa': '12', '$fs': '2', 'timestamp': '1679136770'}
11:05:16  Process DXF file
11:05:16  File Name : testBend
11:05:16  Layer : Layer1
11:05:16  PathName : /Users/keithsloan/Downloads
11:05:16  DXF Full path : /Users/keithsloan/Downloads/testBend.dxf
11:05:16  opening /Users/keithsloan/Downloads/testBend.dxf...
11:05:16  ----------------
11:05:16  Layer
11:05:16  name: 0
11:05:16  line_color: (0.0, 0.0, 0.0)
11:05:16  line_width: 2.0
11:05:16  draw_style: Solid
11:05:16  transparency: 0
11:05:16  drawing 106 lines...
11:05:16  ----------------
11:05:16  Layer
11:05:16  name: Layer1
11:05:16  line_width: 2.0
11:05:16  draw_style: None
11:05:16  typecheck[make_layer]: 'None' is not <class 'str'>
11:05:16  Wrong input: must be 'Solid', 'Dashed', 'Dotted', or 'Dashdot'.
11:05:16  Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/Users/keithsloan/Library/Application Support/FreeCAD/Mod/Alternate_OpenSCAD/./importCSG.py", line 108, in open
    processCSG(doc, filename)
  File "/Users/keithsloan/Library/Application Support/FreeCAD/Mod/Alternate_OpenSCAD/./importCSG.py", line 176, in processCSG
    result = parser.parse(f.read())
  File "/Applications/FreeCAD_0.20.2.app/Contents/Resources/lib/python3.10/site-packages/ply/yacc.py", line 333, in parse
    return self.parseopt_notrack(input, lexer, debug, tracking, tokenfunc)
  File "/Applications/FreeCAD_0.20.2.app/Contents/Resources/lib/python3.10/site-packages/ply/yacc.py", line 1120, in parseopt_notrack
    p.callable(pslice)
  File "/Users/keithsloan/Library/Application Support/FreeCAD/Mod/Alternate_OpenSCAD/./importCSG.py", line 901, in p_import_file1
    p[0] = [process_import_file(filen,ext,p[3]['layer'])]
  File "/Users/keithsloan/Library/Application Support/FreeCAD/Mod/Alternate_OpenSCAD/./importCSG.py", line 921, in process_import_file
    obj=processDXF(fname, layer)
  File "/Users/keithsloan/Library/Application Support/FreeCAD/Mod/Alternate_OpenSCAD/./importCSG.py", line 1020, in processDXF
    face = importDXFface(filename,layer,doc)
  File "/Users/keithsloan/Library/Application Support/FreeCAD/Mod/Alternate_OpenSCAD/./OpenSCAD2Dgeom.py", line 485, in importDXFface
    layers = importDXF.processdxf(doc,filename,False,False) or importDXF.layers
  File "/Applications/FreeCAD_0.20.2.app/Contents/Resources/Mod/Draft/importDXF.py", line 2255, in processdxf
    newob = addObject(shape, "Line", line.layer)
  File "/Applications/FreeCAD_0.20.2.app/Contents/Resources/Mod/Draft/importDXF.py", line 1978, in addObject
    lay = locateLayer(layer)
  File "/Applications/FreeCAD_0.20.2.app/Contents/Resources/Mod/Draft/importDXF.py", line 349, in locateLayer
    newLayer.Label = wantedLayerName
<class 'AttributeError'>: 'NoneType' object has no attribute 'Label'
If one looks at the significant part

Code: Select all

11:05:16  Layer
11:05:16  name: Layer1
11:05:16  line_width: 2.0
11:05:16  draw_style: None
11:05:16  typecheck[make_layer]: 'None' is not <class 'str'>
11:05:16  Wrong input: must be 'Solid', 'Dashed', 'Dotted', or 'Dashdot'.
11:05:16  Traceback (most recent call last):
What is causing the error is for Layer1 it finding a draw_style of None

It processes the first line ( not in layer1 ) so Draft has been initialized.
keithsloan52
Veteran
Posts: 2756
Joined: Mon Feb 27, 2012 5:31 pm

Re: Problems importing DXF with OpenSCAD

Post by keithsloan52 »

I think there maybe an error in Draft importDXF that is not setting a default for the line type in a given Layer
mconsidine
Posts: 125
Joined: Wed Jan 18, 2023 10:41 pm
Location: Randolph, VT USA

Re: Problems importing DXF with OpenSCAD

Post by mconsidine »

Hi,

I'm the original poster on this problem that Keith references.

Firstly:

Code: Select all

OS: Linux Mint 21 (X-Cinnamon/cinnamon)
Word size of FreeCAD: 64-bit
Version: 0.21.0.32346 (Git)
Build type: Release
Branch: master
Hash: db65bb751d347d81128658f5de352a9d4b4d0481
Python 3.10.6, Qt 5.15.3, Coin 4.0.1, Vtk 9.1.0, OCC 7.6.3
Locale: English/United States (en_US)
Installed mods: 
  * ProDarkThemePreferencePack 1.0.0
  * toSketch 1.0.1
  * Alternate_OpenSCAD 1.0.0
  * Render 2023.2.2
  * Manipulator 1.5.0
  * Help 1.0.3
  * POV-Ray-Rendering
  * MeshRemodel 1.8919.0
  * freecad.gears 1.0.0
  * fasteners 0.4.55
  * Silk 1.0.0
  * Dracula 0.0.2
  * Curves 0.6.8
  * ThreadProfile 1.84.0
Also, I am using a compile of the current "master" version of openSCAD

Attached files from GIMP (GIMP 2.10.34) and Inkscape 1.3-dev (b66badc, 2023-02-03) AppImage:
- testbend_filled.jpg comes from GIMP
- testbend_R12.dxf, testbend_plain.svg, testbend_inkscape.svg come from Inkscape
via Open, Trace Bitmap and Save As operations
- testbend_exportascsg.csg is Export As CSG output from OpenSCAD, after this
line in OpenSCAD
import("testbend_R12.dxf");
- testbend_exportfromOpenSCAD.dxf, testbend_exportfromOpenSCAD.svg are similar outputs

In FreeCAD, using OpenSCAD WB:
These don't work selecting "Add OpenSCAD Element":

//Version A:
//[...]importCSG.py", line 900, in p_import_file1
// p[0] = [process_import_file(filen,ext,p[3]['layer'])]

Code: Select all

import("/home/matt/Downloads/3D_design_files/testbend_R12.dxf");
//Version B:
//draws one line then fails, ending with
//File "[...]/Mod/Draft/importDXF.py", line 346, in locateLayer
// newLayer.Label = wantedLayerName
// AttributeError: 'NoneType' object has no attribute 'Label'

Code: Select all

import("/home/matt/Downloads/3D_design_files/testbend_R12.dxf",layer="");
//Version C:
//fails with same error from Version A

Code: Select all

import("/home/matt/Downloads/3D_design_files/testbend_inkscape.svg");
------------
These succeed, but only with an optional(?) OpenSCAD parameter whose value can be any string it seems. Otherwise they fail with Error A...

//Saved as Inkscape SVG format:

Code: Select all

import("/home/matt/Downloads/3D_design_files/testbend_inkscape.svg",layer="");
//Saved as Plain SVG format:

Code: Select all

import("/home/matt/Downloads/3D_design_files/testbend_plain.svg", layer="blah");
If I save this

Code: Select all

  import(file = "./testbend_inkscape.svg", origin = [0, 0], scale = 1, convexity = 1, $fn = 0, $fa = 12, $fs = 2, timestamp = 1679080119);
as testbend_exportascsg.csg and select File -> Open then the effort fails regardless of the importCSG option chosen. Note that this does not have "layer" specified, as this is how it comes from OpenSCAD

So, if I edit that file and save it with

Code: Select all

  layer=""
added, then *both* import options via File -> Open succeed.

Complicating matters even more, the following line in Add OpenSCAD Element works

Code: Select all

  import("/home/matt/Downloads/3D_design_files/testbend_exportfromOpenSCAD.dxf", layer="");
even though importing a DXF generated from Inkscape in the same manner (as above) does not. Ie the DXF generated by OpenSCAD was read in.

I don't know where to begin to figure out where the problem is. Simply put, I expected that if I could directly open a DXF file then I would be able to import same, as I can in OpenSCAD. The workaround (for me) appears to be to use .SVG files as both OpenSCAD and FC seem to be able to import them, even though in FC I need to use a keyword (layer) that is not included in .CSG output from OpenSCAD. For context, the genesis of the problem is an effort to port a large OpenSCAD script to FC and some shapes - which are drawn on blueprints - are more easily taken as bitmaps that are then extruded.

HTH and thanks in advance for any constructive suggestions.

mconsidine
Attachments
testbend_files.zip
(53.66 KiB) Downloaded 9 times
imm
Posts: 250
Joined: Wed Nov 03, 2021 1:00 pm

Re: Problems importing DXF with OpenSCAD

Post by imm »

from what I can see there is a problem with the importDXF.py in the draft module that is arbitrarily being used for import.

if you insert the following at line 336 it should resolve your issue.

Code: Select all

    # layers is a global variable.
    # It should probably be passed as an argument.
    # ---> snip below is to be inserted here  .....
    if isNone(wantedLayer):
        wantedLayer='0'
    # ---> snip end of inserted text .....
    wantedLayerName = decodeName(wantedLayer)
code claims to have a default value assigned, but if there was such it has been removed.
keithsloan52
Veteran
Posts: 2756
Joined: Mon Feb 27, 2012 5:31 pm

Re: Problems importing DXF with OpenSCAD

Post by keithsloan52 »

imm wrote: Thu Mar 23, 2023 12:26 am from what I can see there is a problem with the importDXF.py in the draft module that is arbitrarily being used for import.

if you insert the following at line 336 it should resolve your issue.

Code: Select all

    # layers is a global variable.
    # It should probably be passed as an argument.
    # ---> snip below is to be inserted here  .....
    if isNone(wantedLayer):
        wantedLayer='0'
    # ---> snip end of inserted text .....
    wantedLayerName = decodeName(wantedLayer)
code claims to have a default value assigned, but if there was such it has been removed.
"from what I can see there is a problem with the importDXF.py in the draft module that is arbitrarily being used for import."

Draft is being used because the OpenSCAD importer needs to import to Face, where as the standard FC importer is coded to import to a Document.

Thanks for the fix, hopefully @chennes can test and merge into Draft.
User avatar
chennes
Veteran
Posts: 3881
Joined: Fri Dec 23, 2016 3:38 pm
Location: Norman, OK, USA
Contact:

Re: Problems importing DXF with OpenSCAD

Post by chennes »

I've submitted a PR with the suggested fix (is there anything special about using "0" as the default layer name?). https://github.com/FreeCAD/FreeCAD/pull/9021
Chris Hennes
Pioneer Library System
GitHub profile, LinkedIn profile, chrishennes.com
Post Reply