Rework of DXF/DWG Importer

This commit is contained in:
Mario Voigt 2020-08-24 15:55:25 +02:00
parent bb4d9afa44
commit a5bf2e41c6
2 changed files with 277 additions and 175 deletions

View File

@ -2,14 +2,15 @@
<inkscape-extension xmlns="http://www.inkscape.org/namespace/inkscape/extension">
<name>DXF/DWG Importer</name>
<id>fablabchemnitz.de.dxfdwgimporter_configure</id>
<hbox>
<vbox>
<label appearance="header">General</label>
<separator/>
<param name="tab" type="notebook">
<page name="general" gui-text="Input / General">
<label>The parser is the heart of your input-output chain because depending on the selection your result will greatly vary. You might receive no or bad results (e.g. wrong scalings, aborted imports, missing lines, duplicated lines, wrong rotations and other issues. Serious tip: ask the person who gave the input file to you about some measurement which you can compare with the processed file to ensure correct sizes)</label>
<param name="dxf_to_svg_parser" gui-text="DXF to SVG parser" type="optiongroup" appearance="combo">
<option value="bjnortier">bjnortier DXF (good quality, some polygon artifacts, very quick, correct scaling)</option>
<option value="uniconverter">sk1 UniConverter (best output, but fails sometimes, unknown scaling)</option>
<option value="ezdxf">ezdxf (higher quality, many single nodes, unknown scaling)</option>
<option value="bjnortier">bjnortier DXF (good quality, some polygon artifacts)</option>
<option value="uniconverter">sk1 UniConverter (best output, but fails for all unknown/unfiltered entities)</option>
<option value="ezdxf">ezdxf (higher quality, many single nodes)</option>
<option value="legacy">legacy InkScape importer</option>
</param>
<param name="resizetoimport" type="bool" gui-text="Resize the canvas to the imported drawing's bounding box">true</param>
<param name="extraborder" type="float" precision="3" gui-text="Add extra border around fitted canvas">0.0</param>
@ -20,11 +21,31 @@
<option value="pt">pt</option>
<option value="px">px</option>
</param>
<param name="inputfile" type="path" gui-text="Import File (*.dxf, .dwg)" gui-description="Full path to your file, e.g. 'C:\Users\Username\Documents\myimage.jpg'" filetypes="dxf,dwg" mode="file">C:\Users\</param>
<spacer/>
<label appearance="header">ODA File Converter - DWG to DXF / DXF to DXF</label>
<label appearance="header">About</label>
<separator/>
<param name="odafileconverter" type="path" gui-text="ODAFileConverter.exe" gui-description="Full path to 'ODAFileConverter.exe'" filetypes="exe" mode="file">C:\Program Files\ODA\ODAFileConverter_title 21.6.0\ODAFileConverter.exe</param>
<param name="outputformat" gui-text="AutoCAD output version" type="optiongroup" appearance="combo">
<label>DXF/DWG File Importer by Mario Voigt / Stadtfabrikanten e.V. (2020)</label>
<label>A strange looking ODA File Converter/sk1 UniConverter/ezdxf tool chain. But it works! The most versatile tool to import much hated AutoCAD data for InkScape yet.</label>
<label appearance="url">https://fablabchemnitz.de</label>
<label>License: GNU GPL v3</label>
</page>
<page name="dwg_dxf" gui-text="DWG to DXF / DXF to DXF">
<label appearance="header">Open Design Alliance (ODA) File Converter</label>
<param name="oda_fileconverter" type="path" gui-text="ODAFileConverter.exe" gui-description="Full path to 'ODAFileConverter executable'" filetypes="" mode="file">C:\Program Files\ODA\ODAFileConverter_title 21.6.0\ODAFileConverter.exe</param>
<param name="oda_keepconverted_dxf" type="bool" gui-text="Keep ODA converted DXF file" gui-description="Will put a copy of the converted DXF into the directory where your input file is located.">true</param>
<param name="oda_outputformat" gui-text="AutoCAD output version" type="optiongroup" appearance="combo">
<option value="ACAD2018_DXF">2018 ASCII DXF</option>
<option value="ACAD2013_DXF">2013 ASCII DXF</option>
<option value="ACAD2010_DXF">2010 ASCII DXF</option>
<option value="ACAD2007_DXF">2007 ASCII DXF</option>
<option value="ACAD2004_DXF">2004 ASCII DXF</option>
<option value="ACAD2000_DXF">2000 ASCII DXF</option>
<option value="ACAD14_DXF" >R14 ASCII DXF</option>
<option value="ACAD13_DXF" >R13 ASCII DXF</option>
<option value="ACAD12_DXF" >R12 ASCII DXF</option>
<option value="ACAD10_DXF" >R10 ASCII DXF</option>
<option value="ACAD9_DXF" >R9 ASCII DXF</option>
<!-- Unsupported stuff:
<option value="ACAD2018_DWG">2018 DWG</option>
<option value="ACAD2013_DWG">2013 DWG</option>
@ -32,51 +53,31 @@
<option value="ACAD2007_DWG">2007 DWG</option>
<option value="ACAD2004_DWG">2004 DWF</option>
<option value="ACAD2000_DWG">2000 DWG</option>
<option value="ACAD14_DWG">R14 DWG</option>
<option value="ACAD13_DWG">R13 DWG</option>
<option value="ACAD12_DWG">R12 DWG</option>
-->
<option value="ACAD2018_DXF">2018 ASCII DXF</option>
<option value="ACAD2013_DXF">2013 ASCII DXF</option>
<option value="ACAD2010_DXF">2010 ASCII DXF</option>
<option value="ACAD2007_DXF">2007 ASCII DXF</option>
<option value="ACAD2004_DXF">2004 ASCII DXF</option>
<option value="ACAD2000_DXF">2000 ASCII DXF</option>
<option value="ACAD14_DXF">R14 ASCII DXF</option>
<option value="ACAD13_DXF">R13 ASCII DXF</option>
<option value="ACAD12_DXF">R12 ASCII DXF</option>
<option value="ACAD10_DXF">R10 ASCII DXF</option>
<option value="ACAD9_DXF">R9 ASCII DXF</option>
<!-- Unsupported stuff:
<option value="ACAD14_DWG" >R14 DWG</option>
<option value="ACAD13_DWG" >R13 DWG</option>
<option value="ACAD12_DWG" >R12 DWG</option>
<option value="ACAD2018_DXB">2018 Binary DXF</option>
<option value="ACAD2013_DXB">2013 Binary DXF</option>
<option value="ACAD2010_DXB">2010 Binary DXF</option>
<option value="ACAD2007_DXB">2007 Binary DXF</option>
<option value="ACAD2004_DXB">2004 Binary DXF</option>
<option value="ACAD2000_DXB">2000 Binary DXF</option>
<option value="ACAD14_DXB">R14 Binary DXF</option>
<option value="ACAD13_DXB">R13 Binary DXF</option>
<option value="ACAD12_DXB">R12 Binary DXF</option>
<option value="ACAD10_DXB">R10 Binary DXF</option>
<option value="ACAD14_DXB" >R14 Binary DXF</option>
<option value="ACAD13_DXB" >R13 Binary DXF</option>
<option value="ACAD12_DXB" >R12 Binary DXF</option>
<option value="ACAD10_DXB" >R10 Binary DXF</option>
-->
</param>
<param name="odahidewindow" type="bool" gui-text="Hide ODA GUI window" gui-description="Hide ODA GUI window">true</param>
<param name="skip_dxf_to_dxf" type="bool" gui-text="Skip conversion from DXF to DXF" gui-description="This will skip ODA File Converter execution if input is already DXF file. Might save some seconds of time and maybe prevents errors.">false</param>
<param name="audit_repair" type="bool" gui-text="Perform audit (auto-repair)">true</param>
<spacer/>
<label appearance="header">sk1 UniConverter 1.1.X - DXF to SVG</label>
<separator/>
<param name="sk1uniconverter" type="path" gui-text="uniconvertor.cmd" gui-description="Full path to 'uniconvertor.cmd'" filetypes="cmd" mode="file">C:\Program Files (x86)\sK1 Project\UniConvertor-1.1.6\uniconvertor.cmd</param>
<param name="opendironerror" type="bool" gui-text="Open containing output directory on sk1 conversion errors">true</param>
</vbox>
<spacer/>
<separator/>
<spacer/>
<vbox>
<param name="oda_hidewindow" type="bool" gui-text="Hide ODA GUI window" gui-description="Hide ODA GUI window">true</param>
<param name="oda_skip_dxf_to_dxf" type="bool" gui-text="Skip conversion from DXF to DXF (keep input as is)" gui-description="This will skip ODA File Converter execution if input is already DXF file. If your input is DWG this option will not apply. Might save some seconds of time and maybe prevents errors.">false</param>
<param name="oda_audit_repair" type="bool" gui-text="Perform audit (auto-repair)">true</param>
</page>
<page name="dxf_svg" gui-text="DXF to SVG">
<label appearance="header">ezdxf DXF to DXF Pre-Processing (Filter)</label>
<separator/>
<label>This will apply regardless of the choice of the DXF to SVG converter.</label>
<param name="ezdxf_preprocessing" type="bool" gui-text="Filter entities with ezdxf (recommended if you use sk1 UniConverter)">true</param>
<label>This will apply regardless of the choice of the DXF to SVG converter.It will also preprocess if you select ezdxf as tool for DXF to SVG (means you run ezdxf twice. Once for entity filtering, once for conversion).</label>
<param name="ezdfx_keep_preprocessed" type="bool" gui-text="Keep ezdxf preprocessed DXF file" gui-description="Will put a copy of the converted DXF into the directory where your input file is located.">true</param>
<param name="ezdxf_output_version" type="optiongroup" appearance="combo" gui-text="ezdxf output version">
<option value="SAME">Same as AutoCAD output version</option>
<option value="R2018">2018 ASCII DXF</option>
@ -86,43 +87,71 @@
<option value="R2004">2004 ASCII DXF</option>
<option value="R2000">2000 ASCII DXF</option>
</param>
<label appearance="header">ezdxf Entity Filter</label>
<label>What entity types do you want to filter? Only the listed one's are possible at the moment.</label>
<param name="allentities" type="bool" gui-text="Use all entities (override selection)">false</param>
<hbox>
<vbox>
<param type="bool" gui-text="3DFACE" name="THREE_DFACE">true</param>
<param type="bool" gui-text="ARC" name="ARC">true</param>
<param type="bool" gui-text="BLOCK" name="BLOCK">true</param>
<param type="bool" gui-text="CIRCLE" name="CIRCLE">true</param>
<param type="bool" gui-text="ELLIPSE" name="ELLIPSE">true</param>
<param type="bool" gui-text="LINE" name="LINE">true</param>
<param type="bool" gui-text="LWPOLYLINE" name="LWPOLYLINE">true</param>
<param type="bool" gui-text="POINT" name="POINT">true</param>
</vbox>
<separator/>
<vbox>
<param type="bool" gui-text="POLYLINE" name="POLYLINE">true</param>
<param type="bool" gui-text="POP_TRAFO" name="POP_TRAFO">true</param>
<param type="bool" gui-text="SEQEND" name="SEQEND">true</param>
<param type="bool" gui-text="SOLID" name="SOLID">true</param>
<param type="bool" gui-text="SPLINE" name="SPLINE">true</param>
<param type="bool" gui-text="TABLE" name="TABLE">true</param>
<param type="bool" gui-text="VERTEX" name="VERTEX">true</param>
<param type="bool" gui-text="VIEWPORT" name="VIEWPORT">true</param>
</vbox>
<hbox>
<label appearance="header">sk1 Supported</label>
<separator/>
<vbox>
<param type="bool" gui-text="3DFACE" name="THREE_DFACE">true</param>
<param type="bool" gui-text="ARC" name="ARC">true</param>
<param type="bool" gui-text="BLOCK" name="BLOCK">true</param>
<param type="bool" gui-text="CIRCLE" name="CIRCLE">true</param>
<param type="bool" gui-text="ELLIPSE" name="ELLIPSE">true</param>
<param type="bool" gui-text="LINE" name="LINE">true</param>
<param type="bool" gui-text="LWPOLYLINE" name="LWPOLYLINE">true</param>
<param type="bool" gui-text="POINT" name="POINT">true</param>
</vbox>
<separator/>
<vbox>
<param type="bool" gui-text="POLYLINE" name="POLYLINE">true</param>
<param type="bool" gui-text="POP_TRAFO" name="POP_TRAFO">true</param>
<param type="bool" gui-text="SEQEND" name="SEQEND">true</param>
<param type="bool" gui-text="SOLID" name="SOLID">true</param>
<param type="bool" gui-text="SPLINE" name="SPLINE">true</param>
<param type="bool" gui-text="TABLE" name="TABLE">true</param>
<param type="bool" gui-text="VERTEX" name="VERTEX">true</param>
<param type="bool" gui-text="VIEWPORT" name="VIEWPORT">true</param>
</vbox>
</hbox>
<hbox>
<label appearance="header">Other Entities</label>
<separator/>
<vbox>
<param type="bool" gui-text="3DSOLID" name="THREE_DSOLID">true</param>
<param type="bool" gui-text="ATTRIB" name="ATTRIB">true</param>
<param type="bool" gui-text="BODY" name="BODY">true</param>
<param type="bool" gui-text="DIMENSION" name="DIMENSION">true</param>
<param type="bool" gui-text="ARC_DIMENSION" name="ARC_DIMENSION">true</param>
<param type="bool" gui-text="HATCH" name="HATCH">true</param>
<param type="bool" gui-text="IMAGE" name="IMAGE">true</param>
<param type="bool" gui-text="INSERT" name="INSERT">true</param>
<param type="bool" gui-text="LEADER" name="LEADER">true</param>
</vbox>
<separator/>
<vbox>
<param type="bool" gui-text="MESH" name="MESH">true</param>
<param type="bool" gui-text="MTEXT" name="MTEXT">true</param>
<param type="bool" gui-text="RAY" name="RAY">true</param>
<param type="bool" gui-text="REGION" name="REGION">true</param>
<param type="bool" gui-text="SHAPE" name="SHAPE">true</param>
<param type="bool" gui-text="SURFACE" name="SURFACE">true</param>
<param type="bool" gui-text="TRACE" name="TRACE">true</param>
<param type="bool" gui-text="UNDERLAY" name="UNDERLAY">true</param>
<param type="bool" gui-text="XLINE" name="XLINE">true</param>
</vbox>
</hbox>
</hbox>
</vbox>
</hbox>
<label appearance="header">Import File (*.dxf, .dwg)</label>
<separator/>
<param name="inputfile" type="path" gui-text="File path" gui-description="Full path to your file, e.g. 'C:\Users\Username\Documents\myimage.jpg'" filetypes="dxf,dwg" mode="file">C:\Users\</param>
<spacer/>
<label appearance="header">About</label>
<separator/>
<label>DXF/DWG File Importer by Mario Voigt / Stadtfabrikanten e.V. (2020)</label>
<label>A strange looking ODA File Converter/sk1 UniConverter/ezdxf tool chain. But it works! The most versatile tool to import much hated AutoCAD data for InkScape yet.</label>
<label appearance="url">https://fablabchemnitz.de</label>
<label>License: GNU GPL v3</label>
<spacer/>
<label appearance="header">sk1 UniConverter 1.1.X</label>
<label>sk1 UniConverter is only available for Windows platforms.</label>
<separator/>
<param name="sk1_uniconverter" type="path" gui-text="uniconvertor.cmd" gui-description="Full path to 'uniconvertor.cmd'" filetypes="cmd" mode="file">C:\Program Files (x86)\sK1 Project\UniConvertor-1.1.6\uniconvertor.cmd</param>
<param name="opendironerror" type="bool" gui-text="Open containing output directory on conversion errors">true</param>
</page>
</param>
<effect needs-live-preview="true">
<object-type>all</object-type>
<effects-menu>

View File

@ -8,7 +8,7 @@ Import any DWG or DXF file using ODA File Converter, sk1 UniConverter, ezdxf and
Author: Mario Voigt / FabLab Chemnitz
Mail: mario.voigt@stadtfabrikanten.org
Date: 23.08.2020
Last patch: 23.08.2020
Last patch: 24.08.2020
License: GNU GPL v3
Module licenses
@ -17,6 +17,9 @@ Module licenses
- https://github.com/bjnortier/dxf - MIT License
- ODA File Converter - not bundled (due to restrictions by vendor)
- sk1 UniConverter (https://github.com/sk1project/uniconvertor) - AGPL v3.0 - not bundled
ToDos:
- change copy commands to movefile commands (put into temp. sub directories where the input file is located). We need to copy files in this script because ODA File Converter will process whole dirs instead of single files only.DXF files can be really large, which slows the process)
"""
import inkex
@ -28,6 +31,8 @@ from lxml import etree
from subprocess import Popen, PIPE
import shutil
from pathlib import Path
from mimetypes import MimeTypes
import urllib.request as urllib
#ezdxf related imports
import matplotlib.pyplot as plt
@ -39,92 +44,140 @@ from ezdxf.addons import Importer
class DXFDWGImport(inkex.Effect):
def __init__(self):
inkex.Effect.__init__(self)
self.arg_parser.add_argument("--odafileconverter", default=r"C:\Program Files\ODA\ODAFileConverter_title 21.6.0\ODAFileConverter.exe", help="Full path to 'ODAFileConverter.exe'")
self.arg_parser.add_argument("--odahidewindow", type=inkex.Boolean, default=True, help="Hide ODA GUI window")
self.arg_parser.add_argument("--outputformat", default="ACAD2018_DXF", help="ODA AutoCAD Output version")
self.arg_parser.add_argument("--sk1uniconverter", default=r"C:\Program Files (x86)\sK1 Project\UniConvertor-1.1.6\uniconvertor.cmd", help="Full path to 'uniconvertor.cmd'")
self.arg_parser.add_argument("--opendironerror", type=inkex.Boolean, default=True, help="Open containing output directory on conversion errors")
self.arg_parser.add_argument("--skip_dxf_to_dxf", type=inkex.Boolean, default=False, help="Skip conversion from DXF to DXF")
self.arg_parser.add_argument("--audit_repair", type=inkex.Boolean, default=True, help="Perform audit / autorepair")
self.arg_parser.add_argument("--dxf_to_svg_parser", default="bjnortier", help="Choose a DXF to SVG parser")
self.arg_parser.add_argument("--resizetoimport", type=inkex.Boolean, default=True, help="Resize the canvas to the imported drawing's bounding box")
self.arg_parser.add_argument("--THREE_DFACE", type=inkex.Boolean, default=True) #3DFACE
self.arg_parser.add_argument("--ARC", type=inkex.Boolean, default=True)
self.arg_parser.add_argument("--BLOCK", type=inkex.Boolean, default=True)
self.arg_parser.add_argument("--CIRCLE", type=inkex.Boolean, default=True)
self.arg_parser.add_argument("--ELLIPSE", type=inkex.Boolean, default=True)
self.arg_parser.add_argument("--LINE", type=inkex.Boolean, default=True)
self.arg_parser.add_argument("--LWPOLYLINE", type=inkex.Boolean, default=True)
self.arg_parser.add_argument("--POINT", type=inkex.Boolean, default=True)
self.arg_parser.add_argument("--POLYLINE", type=inkex.Boolean, default=True)
self.arg_parser.add_argument("--POP_TRAFO", type=inkex.Boolean, default=True)
self.arg_parser.add_argument("--SEQEND", type=inkex.Boolean, default=True)
self.arg_parser.add_argument("--SOLID", type=inkex.Boolean, default=True)
self.arg_parser.add_argument("--SPLINE", type=inkex.Boolean, default=True)
self.arg_parser.add_argument("--TABLE", type=inkex.Boolean, default=True)
self.arg_parser.add_argument("--VERTEX", type=inkex.Boolean, default=True)
self.arg_parser.add_argument("--VIEWPORT", type=inkex.Boolean, default=True)
self.arg_parser.add_argument("--inputfile")
self.arg_parser.add_argument("--extraborder", type=float, default=0.0)
self.arg_parser.add_argument("--extraborder_units")
self.arg_parser.add_argument("--ezdxf_output_version", default="SAME")
self.arg_parser.add_argument("--ezdxf_preprocessing", type=inkex.Boolean, default=True)
self.arg_parser.add_argument("--allentities", type=inkex.Boolean, default=True)
def effect(self):
#blank tabs
self.arg_parser.add_argument("--tab")
#general
self.arg_parser.add_argument("--inputfile")
self.arg_parser.add_argument("--dxf_to_svg_parser", default="bjnortier", help="Choose a DXF to SVG parser")
self.arg_parser.add_argument("--resizetoimport", type=inkex.Boolean, default=True, help="Resize the canvas to the imported drawing's bounding box")
self.arg_parser.add_argument("--extraborder", type=float, default=0.0)
self.arg_parser.add_argument("--extraborder_units")
#ODA File Converter
self.arg_parser.add_argument("--oda_fileconverter", default=r"C:\Program Files\ODA\oda_fileconverter_title 21.6.0\oda_fileconverter.exe", help="Full path to 'oda_fileconverter.exe'")
self.arg_parser.add_argument("--oda_hidewindow", type=inkex.Boolean, default=True, help="Hide ODA GUI window")
self.arg_parser.add_argument("--oda_outputformat", default="ACAD2018_DXF", help="ODA AutoCAD Output version")
self.arg_parser.add_argument("--oda_keepconverted_dxf", type=inkex.Boolean, default=True, help="Keep ODA converted DXF file")
self.arg_parser.add_argument("--oda_skip_dxf_to_dxf", type=inkex.Boolean, default=False, help="Skip conversion from DXF to DXF")
self.arg_parser.add_argument("--oda_audit_repair", type=inkex.Boolean, default=True, help="Perform audit / autorepair")
#sk1 UniConverter
self.arg_parser.add_argument("--sk1_uniconverter", default=r"C:\Program Files (x86)\sK1 Project\UniConvertor-1.1.6\uniconvertor.cmd", help="Full path to 'uniconvertor.cmd'")
self.arg_parser.add_argument("--opendironerror", type=inkex.Boolean, default=True, help="Open containing output directory on conversion errors")
#ezdxf preprocessing
self.arg_parser.add_argument("--ezdxf_output_version", default="SAME", help="ezdxf output version")
self.arg_parser.add_argument("--ezdfx_keep_preprocessed", type=inkex.Boolean, default=True, help="Keep ezdxf preprocessed DXF file")
self.arg_parser.add_argument("--ezdxf_preprocessing", type=inkex.Boolean, default=True)
self.arg_parser.add_argument("--allentities", type=inkex.Boolean, default=True)
#sk1 compatible entities
self.arg_parser.add_argument("--THREE_DFACE", type=inkex.Boolean, default=True) #3DFACE
self.arg_parser.add_argument("--ARC", type=inkex.Boolean, default=True)
self.arg_parser.add_argument("--BLOCK", type=inkex.Boolean, default=True)
self.arg_parser.add_argument("--CIRCLE", type=inkex.Boolean, default=True)
self.arg_parser.add_argument("--ELLIPSE", type=inkex.Boolean, default=True)
self.arg_parser.add_argument("--LINE", type=inkex.Boolean, default=True)
self.arg_parser.add_argument("--LWPOLYLINE", type=inkex.Boolean, default=True)
self.arg_parser.add_argument("--POINT", type=inkex.Boolean, default=True)
self.arg_parser.add_argument("--POLYLINE", type=inkex.Boolean, default=True)
self.arg_parser.add_argument("--POP_TRAFO", type=inkex.Boolean, default=True)
self.arg_parser.add_argument("--SEQEND", type=inkex.Boolean, default=True)
self.arg_parser.add_argument("--SOLID", type=inkex.Boolean, default=True)
self.arg_parser.add_argument("--SPLINE", type=inkex.Boolean, default=True)
self.arg_parser.add_argument("--TABLE", type=inkex.Boolean, default=True)
self.arg_parser.add_argument("--VERTEX", type=inkex.Boolean, default=True)
self.arg_parser.add_argument("--VIEWPORT", type=inkex.Boolean, default=True)
#other entities
self.arg_parser.add_argument("--THREE_DSOLID", type=inkex.Boolean, default=True) #3DSOLID
self.arg_parser.add_argument("--ATTRIB", type=inkex.Boolean, default=True)
self.arg_parser.add_argument("--BODY", type=inkex.Boolean, default=True)
self.arg_parser.add_argument("--DIMENSION", type=inkex.Boolean, default=True)
self.arg_parser.add_argument("--ARC_DIMENSION", type=inkex.Boolean, default=True)
self.arg_parser.add_argument("--HATCH", type=inkex.Boolean, default=True)
self.arg_parser.add_argument("--IMAGE", type=inkex.Boolean, default=True)
self.arg_parser.add_argument("--INSERT", type=inkex.Boolean, default=True)
self.arg_parser.add_argument("--LEADER", type=inkex.Boolean, default=True)
self.arg_parser.add_argument("--MESH", type=inkex.Boolean, default=True)
self.arg_parser.add_argument("--MTEXT", type=inkex.Boolean, default=True)
self.arg_parser.add_argument("--RAY", type=inkex.Boolean, default=True)
self.arg_parser.add_argument("--REGION", type=inkex.Boolean, default=True)
self.arg_parser.add_argument("--SHAPE", type=inkex.Boolean, default=True)
self.arg_parser.add_argument("--SURFACE", type=inkex.Boolean, default=True)
self.arg_parser.add_argument("--TRACE", type=inkex.Boolean, default=True)
self.arg_parser.add_argument("--UNDERLAY", type=inkex.Boolean, default=True)
self.arg_parser.add_argument("--XLINE", type=inkex.Boolean, default=True)
def effect(self):
#get input file and copy it to some new temporary directory
inputfile = self.options.inputfile
temp_input_dir = os.path.join(tempfile.gettempdir(),"inkscape-oda-convert-input")
#remove the input directory before doing new job
shutil.rmtree(temp_input_dir, ignore_errors=True)
temp_input_dir = os.path.join(tempfile.gettempdir(),"dxfdwg_input")
shutil.rmtree(temp_input_dir, ignore_errors=True) #remove the input directory before doing new job
if not os.path.exists(temp_input_dir):
os.mkdir(temp_input_dir) #recreate blank dir
shutil.copy2(inputfile, os.path.join(temp_input_dir, Path(inputfile).name)) # complete target filename given
#Prepapre output conversion
outputfilebase = os.path.splitext(os.path.splitext(os.path.basename(inputfile))[0])[0]
inputfile_ending = os.path.splitext(os.path.splitext(os.path.basename(inputfile))[1])[0]
temp_output_dir = os.path.join(tempfile.gettempdir(),"inkscape-oda-convert-output")
#remove the output directory before doing new job
shutil.rmtree(temp_output_dir, ignore_errors=True)
outputfilebase = os.path.splitext(os.path.basename(inputfile))[0]
inputfile_ending = os.path.splitext(os.path.basename(inputfile))[1]
temp_output_dir = os.path.join(tempfile.gettempdir(),"dxfdwg_output")
shutil.rmtree(temp_output_dir, ignore_errors=True) #remove the output directory before doing new job
if not os.path.exists(temp_output_dir):
os.mkdir(temp_output_dir)
autocad_version = self.options.outputformat.split("_")[0]
autocad_format = self.options.outputformat.split("_")[1]
if self.options.audit_repair: #overwrite string bool with int value
self.options.audit_repair = "1"
else:
self.options.audit_repair = "0"
#Prepare some more options for proceeding
autocad_version = self.options.oda_outputformat.split("_")[0]
autocad_format = self.options.oda_outputformat.split("_")[1]
self.options.oda_audit_repair = "1" if self.options.oda_audit_repair else "0" #overwrite string bool with int value
entityspace = []
if self.options.allentities or self.options.THREE_DFACE: entityspace.append("3DFACE")
if self.options.allentities or self.options.ARC: entityspace.append("ARC")
if self.options.allentities or self.options.BLOCK: entityspace.append("BLOCK")
if self.options.allentities or self.options.CIRCLE: entityspace.append("CIRCLE")
if self.options.allentities or self.options.ELLIPSE: entityspace.append("ELLIPSE")
if self.options.allentities or self.options.LINE: entityspace.append("LINE")
if self.options.allentities or self.options.LWPOLYLINE: entityspace.append("LWPOLYLINE")
if self.options.allentities or self.options.POINT: entityspace.append("POINT")
if self.options.allentities or self.options.POLYLINE: entityspace.append("POLYLINE")
if self.options.allentities or self.options.POP_TRAFO: entityspace.append("POP_TRAFO")
if self.options.allentities or self.options.SEQEND: entityspace.append("SEQEND")
if self.options.allentities or self.options.SOLID: entityspace.append("SOLID")
if self.options.allentities or self.options.SPLINE: entityspace.append("SPLINE")
if self.options.allentities or self.options.TABLE: entityspace.append("TABLE")
if self.options.allentities or self.options.VERTEX: entityspace.append("VERTEX")
if self.options.allentities or self.options.VIEWPORT: entityspace.append("VIEWPORT")
if self.options.allentities or self.options.ARC: entityspace.append("ARC")
if self.options.allentities or self.options.BLOCK: entityspace.append("BLOCK")
if self.options.allentities or self.options.CIRCLE: entityspace.append("CIRCLE")
if self.options.allentities or self.options.ELLIPSE: entityspace.append("ELLIPSE")
if self.options.allentities or self.options.LINE: entityspace.append("LINE")
if self.options.allentities or self.options.LWPOLYLINE: entityspace.append("LWPOLYLINE")
if self.options.allentities or self.options.POINT: entityspace.append("POINT")
if self.options.allentities or self.options.POLYLINE: entityspace.append("POLYLINE")
if self.options.allentities or self.options.POP_TRAFO: entityspace.append("POP_TRAFO")
if self.options.allentities or self.options.SEQEND: entityspace.append("SEQEND")
if self.options.allentities or self.options.SOLID: entityspace.append("SOLID")
if self.options.allentities or self.options.SPLINE: entityspace.append("SPLINE")
if self.options.allentities or self.options.TABLE: entityspace.append("TABLE")
if self.options.allentities or self.options.VERTEX: entityspace.append("VERTEX")
if self.options.allentities or self.options.VIEWPORT: entityspace.append("VIEWPORT")
if self.options.allentities or self.options.THREE_DSOLID: entityspace.append("3DSOLID")
if self.options.allentities or self.options.ATTRIB: entityspace.append("ATTRIB")
if self.options.allentities or self.options.BODY: entityspace.append("BODY")
if self.options.allentities or self.options.DIMENSION: entityspace.append("DIMENSION")
if self.options.allentities or self.options.ARC_DIMENSION: entityspace.append("ARC_DIMENSION")
if self.options.allentities or self.options.HATCH: entityspace.append("HATCH")
if self.options.allentities or self.options.IMAGE: entityspace.append("IMAGE")
if self.options.allentities or self.options.INSERT: entityspace.append("INSERT")
if self.options.allentities or self.options.LEADER: entityspace.append("LEADER")
if self.options.allentities or self.options.MESH: entityspace.append("MESH")
if self.options.allentities or self.options.MTEXT: entityspace.append("MTEXT")
if self.options.allentities or self.options.RAY: entityspace.append("RAY")
if self.options.allentities or self.options.REGION: entityspace.append("REGION")
if self.options.allentities or self.options.SHAPE: entityspace.append("SHAPE")
if self.options.allentities or self.options.SURFACE: entityspace.append("SURFACE")
if self.options.allentities or self.options.TRACE: entityspace.append("TRACE")
if self.options.allentities or self.options.UNDERLAY: entityspace.append("UNDERLAY")
if self.options.allentities or self.options.XLINE: entityspace.append("XLINE")
#ODA to ezdxf mapping
oda_ezdxf_mapping = []
oda_ezdxf_mapping.append(["ACAD9","R12","AC1004"]) #this mapping is not supported directly. so we use the lowest possible which is R12
oda_ezdxf_mapping.append(["ACAD10","R12","AC1006"]) #this mapping is not supported directly. so we use the lowest possible which is R12
oda_ezdxf_mapping.append(["ACAD12","R12","AC1009"])
oda_ezdxf_mapping.append(["ACAD13","R2000","AC1012"]) #R13 was overwritten by R2000 which points to AC1015 instead of AC1014 (see documentation)
oda_ezdxf_mapping.append(["ACAD14","R2000","AC1014"]) #R14 was overwritten by R2000 which points to AC1015 instead of AC1014 (see documentation)
oda_ezdxf_mapping.append(["ACAD9", "R12", "AC1004"]) #this mapping is not supported directly. so we use the lowest possible which is R12
oda_ezdxf_mapping.append(["ACAD10", "R12", "AC1006"]) #this mapping is not supported directly. so we use the lowest possible which is R12
oda_ezdxf_mapping.append(["ACAD12", "R12", "AC1009"])
oda_ezdxf_mapping.append(["ACAD13", "R2000","AC1012"]) #R13 was overwritten by R2000 which points to AC1015 instead of AC1014 (see documentation)
oda_ezdxf_mapping.append(["ACAD14", "R2000","AC1014"]) #R14 was overwritten by R2000 which points to AC1015 instead of AC1014 (see documentation)
oda_ezdxf_mapping.append(["ACAD2000","R2000","AC1015"])
oda_ezdxf_mapping.append(["ACAD2004","R2004","AC1018"])
oda_ezdxf_mapping.append(["ACAD2007","R2007","AC1021"])
@ -139,36 +192,48 @@ class DXFDWGImport(inkex.Effect):
break
if ezdxf_autocad_format is None:
inkex.errormsg("ezdxf conversion format version unknown")
if self.options.skip_dxf_to_dxf == False or inputfile_ending == ".dwg":
# Build and run ODA File Converter command // "cmd.exe /c start \"\" /MIN /WAIT"
oda_cmd = [self.options.odafileconverter, temp_input_dir, temp_output_dir, autocad_version, autocad_format, "0", self.options.audit_repair]
if self.options.odahidewindow:
info = subprocess.STARTUPINFO() #hide the ODA File Converter window because it is annoying
info.dwFlags = 1
info.wShowWindow = 0
proc = subprocess.Popen(oda_cmd, startupinfo=info, shell=False, stdout=PIPE, stderr=PIPE)
else: proc = subprocess.Popen(oda_cmd, shell=False, stdout=PIPE, stderr=PIPE)
stdout, stderr = proc.communicate()
if proc.returncode != 0:
inkex.errormsg("ODA File Converter failed: %d %s %s" % (proc.returncode, stdout, stderr))
if self.options.skip_dxf_to_dxf: #if true we need to move the file to simulate "processed"
shutil.move(os.path.join(temp_input_dir, Path(inputfile).name), os.path.join(temp_output_dir, Path(inputfile).name))
# Prepare files
#Prepare DXF and SVG paths
dxf_file = os.path.join(temp_output_dir, outputfilebase + ".dxf")
svg_file = os.path.join(temp_output_dir, outputfilebase + ".svg")
# Run ODA File Converter
if self.options.oda_skip_dxf_to_dxf == False or inputfile_ending == ".dwg":
# Executable test (check for proper configuration by checking mime type. Should return octet stream for a binary executable)
url = urllib.pathname2url(self.options.oda_fileconverter)
if "application/octet-stream" not in str(MimeTypes().guess_type(url)):
inkex.utils.debug("ODA File Converter was not properly configured.")
else:
# Build and run ODA File Converter command
oda_cmd = [self.options.oda_fileconverter, temp_input_dir, temp_output_dir, autocad_version, autocad_format, "0", self.options.oda_audit_repair]
if self.options.oda_hidewindow:
info = subprocess.STARTUPINFO() #hide the ODA File Converter window because it is annoying
info.dwFlags = 1
info.wShowWindow = 0
proc = subprocess.Popen(oda_cmd, startupinfo=info, shell=False, stdout=PIPE, stderr=PIPE)
else: proc = subprocess.Popen(oda_cmd, shell=False, stdout=PIPE, stderr=PIPE)
stdout, stderr = proc.communicate()
if proc.returncode != 0:
inkex.errormsg("ODA File Converter failed: %d %s %s" % (proc.returncode, stdout, stderr))
exit(1)
# Preprocessing DXF to DXF (entity filter)
if self.options.oda_skip_dxf_to_dxf: #if true we need to move the file to simulate "processed"
shutil.move(os.path.join(temp_input_dir, Path(inputfile).name), os.path.join(temp_output_dir, Path(inputfile).name))
if self.options.oda_keepconverted_dxf and inputfile_ending != ".dxf": #if the input file already was DXF we don't need to make another copy
shutil.copy2(dxf_file, os.path.join(os.path.dirname(inputfile), outputfilebase + "_oda.dxf")) # complete target filename given
if self.options.ezdxf_preprocessing:
#uniconverter does not handle all entities. we parse the file to exlude stuff which lets uniconverter fail
# uniconverter does not handle all entities. we parse the file to exlude stuff which lets uniconverter fail
dxf = ezdxf.readfile(dxf_file)
modelspace = dxf.modelspace()
allowed_entities = []
# supported entities by UniConverter- impossible: MTEXT TEXT INSERT and a lot of others
query_string = str(entityspace)[1:-1].replace("'","").replace(",","")
for e in modelspace.query(query_string):
allowed_entities.append(e)
if query_string != "":
for e in modelspace.query(query_string):
allowed_entities.append(e)
#inkex.utils.debug(ezdxf_autocad_format)
#inkex.utils.debug(self.options.ezdxf_output_version)
if self.options.ezdxf_output_version == "SAME":
@ -179,10 +244,17 @@ class DXFDWGImport(inkex.Effect):
for e in allowed_entities:
msp.add_foreign_entity(e)
doc.saveas(dxf_file)
if self.options.ezdfx_keep_preprocessed:
shutil.copy2(dxf_file, os.path.join(os.path.dirname(inputfile), outputfilebase + "_ezdxf.dxf")) # complete target filename given
# make SVG from DXF
if self.options.dxf_to_svg_parser == "uniconverter":
uniconverter_cmd = [self.options.sk1uniconverter, dxf_file, svg_file]
# Make SVG from DXF
if sys.platform != "win32":
inkex.utils.debug("You selected sk1 UniConverter but you are not running on a Windows platform.")
sk1_command_ending = os.path.splitext(os.path.splitext(os.path.basename(self.options.sk1_uniconverter))[1])[0]
if sk1_command_ending != ".cmd":
inkex.utils.debug("You selected sk1 UniConverter but it was not configured properly. Check the path to the executable.")
elif self.options.dxf_to_svg_parser == "uniconverter":
uniconverter_cmd = [self.options.sk1_uniconverter, dxf_file, svg_file]
#inkex.utils.debug(uniconverter_cmd)
proc = subprocess.Popen(uniconverter_cmd, shell=False, stdout=PIPE, stderr=PIPE)
stdout, stderr = proc.communicate()
@ -221,17 +293,18 @@ class DXFDWGImport(inkex.Effect):
#plt.show()
#fig.savefig(os.path.join(temp_output_dir, outputfilebase + ".png"), dpi=300)
fig.savefig(svg_file) #see https://matplotlib.org/3.1.1/api/_as_gen/matplotlib.pyplot.savefig.html
elif self.options.dxf_to_svg_parser == "legacy":
inkex.utils.debug("The selected legacy DXF to SVG parser is not supported by this extension yet. Use File > Import > *.dxf. This calls the \"dxf_input.inx\" extension.")
exit(1)
else:
inkex.utils.debug("undefined parser")
exit(1)
# Write the generated SVG into canvas
# Write the generated SVG into InkScape's canvas
stream = open(svg_file, 'r')
p = etree.XMLParser(huge_tree=True)
doc = etree.parse(stream, parser=etree.XMLParser(huge_tree=True)).getroot()
stream.close()
#newGroup = self.document.getroot().add(inkex.Group())
doc.set('id', self.svg.get_unique_id('dxf_dwg_import'))
self.document.getroot().append(doc)