DWG: convertToDwg() returns without verifying converter output, so Automatic mode silently fails to export
- Dominant language
- C++
- Stars
- 33.6k
- Forks
- 6k
- Avg merge
- 3d 15h
- Merged PRs (30d)
- 196
Description
### Is there an existing issue for this?
- [x] I have searched the existing issues
### Problem description
`importDWG.convertToDwg()` runs the first available converter and then returns unconditionally, without checking whether an output file was actually produced. The import counterpart, `convertToDxf()`, *does* check.
The practical effect: with **DWG conversion set to "Automatic"** and LibreDWG installed, DWG **export silently fails**. LibreDWG is tried first; when it fails, `convertToDwg()` returns the output path as if it had succeeded, so the ODA and QCAD branches are never reached. No error is surfaced to the user — the export simply produces nothing.
This affects every user who has LibreDWG installed and leaves the converter preference at its "Automatic" default, which is the natural choice when more than one converter is present.
**Import path — correct** (`Mod/Draft/importDWG.py`, `convertToDxf`, ~line 291):
```python
if conv in [0, 1]: # LibreDWG
libredwg = get_libredwg_converter("dwg2dxf")
if libredwg is not None:
...
proc.communicate()
if os.path.exists(result): # <-- checked
FCC.PrintMessage(...)
return result
else:
FCC.PrintError(error_msg) # <-- falls through to ODA
```
**Export path — missing the check** (`convertToDwg`, lines 383-390):
```python
if conv in [0, 1]: # LibreDWG
libredwg = get_libredwg_converter("dxf2dwg")
if libredwg is not None:
cmdline = [libredwg, dxffilename, "-y", "-o", dwgfilename]
FCC.PrintMessage(translate("draft", "Converting:") + " " + str(cmdline) + "\n")
proc = subprocess.Popen(cmdline)
proc.communicate()
return dwgfilename # <-- returns unconditionally
```
The same pattern repeats in the ODA branch (line ~404) and the QCAD branch (line ~415), so a failure in any of the three prevents the remaining converters from being tried.
The trigger in my case is that LibreDWG `dxf2dwg` cannot read FreeCAD's own DXF output, which I have filed separately. But that is incidental — this fallback bug would surface with any converter failure, and it is worth fixing independently since it converts a recoverable failure into a silent one.
### Workbench affected?
Draft
### Steps to reproduce
1. Install GNU LibreDWG (0.14) and the ODA File Converter (27.1.0).
2. **Edit → Preferences → Import/Export → DWG** — set the converter to **Automatic** (`DWGConversion = 0`).
3. Create any 2D geometry, e.g. a Draft Wire and a Draft Circle.
4. **File → Export…** and choose `.dwg`.
Reproduced headlessly with:
```python
import FreeCAD as App, Draft, importDWG
App.ParamGet("User parameter:BaseApp/Preferences/Mod/Draft").SetInt("DWGConversion", 0)
doc = App.newDocument("RT")
V = App.Vector
Draft.make_wire([V(0,0,0), V(100,0,0), V(100,60,0), V(0,60,0)], closed=True)
Draft.make_circle(25, placement=App.Placement(V(50,30,0), App.Rotation()))
doc.recompute()
importDWG.export(list(doc.Objects), "C:/tmp/out.dwg")
import os; print("exists:", os.path.exists("C:/tmp/out.dwg")) # -> False
```
### Expected behavior
`convertToDwg()` should verify the output file exists before returning, mirroring `convertToDxf()`. When the first converter fails in Automatic mode, the next configured converter should be tried; if all fail, an error should be reported to the user.
With the converter forced to ODA (`DWGConversion = 2`) the same export succeeds and produces a valid 97,348-byte DWG, confirming a working converter was available the whole time and would have been reached had the fallback worked.
### Actual behavior
`convertToDwg()` returns the output path unconditionally. No file is created, no error is shown, and the ODA/QCAD fallbacks are never attempted. The user sees an apparently successful export that produced nothing.
Console output (Automatic mode, both converters installed):
```
Converting: ['C:/Program Files/LibreDWG/dxf2dwg.exe', '...roundtrip.dxf', '-y', '-o', '...roundtrip.dwg']
ERROR: Out of memory
ERROR: Failed to decode DXF file: ...roundtrip.dxf
READ ERROR 0x1000 ...roundtrip.dxf
```
No "Converting:" line for ODA ever follows.
### Additional context
Suggested fix, matching the existing import-path pattern:
```python
proc.communicate()
if os.path.exists(dwgfilename):
FCC.PrintMessage(translate("draft", "Conversion successful") + "\n")
return dwgfilename
elif conv != 0:
FCC.PrintError(error_msg)
```
applied to all three branches, with the final `PrintError` at the end of the function reporting total failure.
### Full version info
```
OS: Windows 11 Home Single Language (10.0.26200)
Word size of FreeCAD: 64-bit
Version: 1.1.3.20260725 (Git shallow)
Hash: 145529fe741292ff0b3977a01195bf0247425794
Python 3.11.14
LibreDWG: 0.14 (win64)
ODA File Converter: 27.1.0
```
Contributor guide
Research direction
Start in Mod/Draft/importDWG.py at convertToDwg(), comparing its LibreDWG, ODA, and QCAD branches with convertToDxf(). Reproduce the Automatic-mode export failure if needed, then verify each branch's output before returning so failed converters fall through and total failure reports an error.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- desktop
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Quiet
- Clarity
- Clearly specified
- Newbie friendliness
- 78/100