1# ##### BEGIN GPL LICENSE BLOCK #####
2#
3#  This program is free software; you can redistribute it and/or
4#  modify it under the terms of the GNU General Public License
5#  as published by the Free Software Foundation; either version 2
6#  of the License, or (at your option) any later version.
7#
8#  This program is distributed in the hope that it will be useful,
9#  but WITHOUT ANY WARRANTY; without even the implied warranty of
10#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11#  GNU General Public License for more details.
12#
13#  You should have received a copy of the GNU General Public License
14#  along with this program; if not, write to the Free Software Foundation,
15#  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16#
17# ##### END GPL LICENSE BLOCK #####
18
19import bpy
20from bpy.props import (
21    BoolProperty,
22    EnumProperty,
23    StringProperty,
24)
25
26class DXFExporter(bpy.types.Operator):
27    """
28    Export to the Autocad model format (.dxf)
29    """
30    bl_idname = "export.dxf"
31    bl_label = "Export DXF"
32    filepath: StringProperty(subtype='FILE_PATH')
33
34    entitylayer_from_items = (
35        ('default_LAYER', 'Default Layer', ''),
36        ('obj.name', 'Object name', ''),
37        ('obj.layer', 'Object layer', ''),
38        ('obj.material', 'Object material', ''),
39        ('obj.data.name', 'Object\' data name', ''),
40#        ('obj.data.material', 'Material of data', ''),
41        ('..vertexgroup', 'Vertex Group', ''),
42        ('..group', 'Group', ''),
43        ('..map_table', 'Table', '')
44    )
45    layerColorFromItems = (
46        ('default_COLOR', 'Vertex Group', ''),
47        ('BYLAYER', 'BYLAYER', ''),
48        ('BYBLOCK', 'BYBLOCK', ''),
49        ('obj.layer', 'Object Layer', ''),
50        ('obj.color', 'Object Color', ''),
51        ('obj.material', 'Object material', ''),
52        # I don'd know ?
53#        ('obj.data.material', 'Vertex Group', ''),
54#        ('..map_table', 'Vertex Group', ''),
55    )
56    layerNameFromItems = (
57        ('LAYERNAME_DEF', 'Default Name', ''),
58        ('drawing_name', 'From Drawing name', ''),
59        ('scene_name', 'From scene name', '')
60    )
61
62    exportModeItems = (
63        ('ALL', 'All Objects', ''),
64        ('SELECTION', 'Selected Objects', ''),
65    )
66#    spaceItems = (
67#        ('1', 'Paper-Space', ''),
68#        ('2', 'Model-Space', '')
69#    )
70
71    entityltype_fromItems = (
72        ('default_LTYPE', 'default_LTYPE', ''),
73        ('BYLAYER', 'BYLAYER', ''),
74        ('BYBLOCK', 'BYBLOCK', ''),
75        ('CONTINUOUS', 'CONTINUOUS', ''),
76        ('DOT', 'DOT', ''),
77        ('DASHED', 'DASHED', ''),
78        ('DASHDOT', 'DASHDOT', ''),
79        ('BORDER', 'BORDER', ''),
80        ('HIDDEN', 'HIDDEN', '')
81    )
82    material_toItems = (
83        ('NO', 'Do not export', ''),
84        ('COLOR', 'COLOR', ''),
85        ('LAYER', 'LAYER', ''),
86        ('..LINESTYLE', '..LINESTYLE', ''),
87        ('..BLOCK', '..BLOCK', ''),
88        ('..XDATA', '..XDATA', ''),
89        ('..INI-File', '..INI-File', '')
90    )
91    projectionItems=(
92        ('NO', 'No projection', 'Export 3D scene without any 2D projection'),
93        ('TOP', 'TOP view', 'Use TOP view for projection'),
94        ('BOTTOM', 'BOTTOM view', 'Use BOTTOM view for projection'),
95        ('LEFT', 'LEFT view', 'Use LEFT view for projection'),
96        ('RIGHT', 'RIGHT view', 'Use RIGHT view for projection'),
97        ('FRONT', 'FRONT view', 'Use FRONT view for projection'),
98        ('REAR', 'REAR view', 'Use REAR view for projection')
99    )
100    mesh_asItems = (
101        ('NO', 'Do not export', ''),
102        ('3DFACEs', '3DFACEs', ''),
103        ('POLYFACE', 'POLYFACE', ''),
104        ('POLYLINE', 'POLYLINE', ''),
105        ('LINEs', 'LINEs', 'export Mesh as multiple LINEs'),
106        ('POINTs', 'POINTs', 'Export Mesh as multiple POINTs')
107    )
108#    curve_asItems  = (
109#        ('NO', 'Do not export', ''),
110#        ('LINEs', 'LINEs', ''),
111#        ('POLYLINE', 'POLYLINE', ''),
112#        ('..LWPOLYLINE r14', '..LWPOLYLINE r14', ''),
113#        ('..SPLINE r14', '..SPLINE r14', ''),
114#        ('POINTs', 'POINTs',  '')
115#    )
116#    surface_asItems  = (
117#        ('NO', 'Do not export', ''),
118#        ('..3DFACEs', '..3DFACEs', ''),
119#        ('..POLYFACE', '..POLYFACE', ''),
120#        ('..POINTs', '..POINTs', ''),
121#        ('..NURBS', '..NURBS', '')
122#    )
123#    meta_asItems  = (
124#        ('NO', 'Do not export', ''),
125#        ('..3DFACEs', '..3DFACEs', ''),
126#        ('..POLYFACE', '..POLYFACE', ''),
127#        ('..3DSOLID', '..3DSOLID', '')
128#    )
129#    text_asItems  = (
130#        ('NO', 'Do not export', ''),
131#        ('TEXT', 'TEXT', ''),
132#        ('..MTEXT', '..MTEXT', ''),
133#        ('..ATTRIBUT', '..ATTRIBUT', '')
134#    )
135#    empty_asItems  = (
136#        ('NO', 'Do not export', ''),
137#        ('POINT', 'POINT', ''),
138#        ('..INSERT', '..INSERT', ''),
139#        ('..XREF', '..XREF', '')
140#    )
141#    group_asItems  = (
142#        ('NO', 'Do not export', ''),
143#        ('..GROUP', '..GROUP', ''),
144#        ('..BLOCK', '..BLOCK', ''),
145#        ('..ungroup', '..ungroup', '')
146#    )
147##    parent_asItems = ['..BLOCK','..ungroup'] # ???
148#    proxy_asItems = (
149#        ('NO', 'Do not export', ''),
150#        ('..BLOCK','..BLOCK', ''),
151#        ('..XREF', '..XREF', ''),
152#        ('..ungroup', '..ungroup', ''),
153#        ('..POINT', '..POINT', '')
154#    )
155#    camera_asItems = (
156#        ('NO', 'Do not export', ''),
157#        ('..BLOCK', '..BLOCK', ''),
158#        ('..A_CAMERA', '..A_CAMERA', ''),
159#        ('VPORT', 'VPORT', ''),
160#        ('VIEW', 'VIEW', ''),
161#        ('POINT', 'POINT', '')
162#    )
163#    light_asItems = (
164#        ('NO', 'Do not export', ''),
165#        ('..BLOCK', '..BLOCK', ''),
166#        ('..A_LIGHT', '..A_LIGHT', ''),
167#        ('POINT', 'POINT', '')
168#    )
169    # --------- CONTROL PROPERTIES --------------------------------------------
170    projectionThrough: EnumProperty(name="Projection", default="NO",
171                                    description="Select camera for use to 2D projection",
172                                    items=projectionItems)
173
174    onlySelected: BoolProperty(name="Only selected", default=True,
175                              description="What object will be exported? Only selected / all objects")
176
177    apply_modifiers: BoolProperty(name="Apply modifiers", default=True,
178                           description="Shall be modifiers applied during export?")
179    # GUI_B -----------------------------------------
180    mesh_as: EnumProperty( name="Export Mesh As", default='3DFACEs',
181                            description="Select representation of a mesh",
182                            items=mesh_asItems)
183#    curve_as: EnumProperty( name="Export Curve As:", default='NO',
184#                            description="Select representation of a curve",
185#                            items=curve_asItems)
186#    surface_as: EnumProperty( name="Export Surface As:", default='NO',
187#                            description="Select representation of a surface",
188#                            items=surface_asItems)
189#    meta_as: EnumProperty( name="Export meta As:", default='NO',
190#                            description="Select representation of a meta",
191#                            items=meta_asItems)
192#    text_as: EnumProperty( name="Export text As:", default='NO',
193#                            description="Select representation of a text",
194#                            items=text_asItems)
195#    empty_as: EnumProperty( name="Export empty As:", default='NO',
196#                            description="Select representation of a empty",
197#                            items=empty_asItems)
198#    group_as: EnumProperty( name="Export group As:", default='NO',
199#                            description="Select representation of a group",
200#                            items=group_asItems)
201##    parent_as: EnumProperty( name="Export parent As:", default='NO',
202##                            description="Select representation of a parent",
203##                            items=parent_asItems)
204#    proxy_as: EnumProperty( name="Export proxy As:", default='NO',
205#                            description="Select representation of a proxy",
206#                            items=proxy_asItems)
207#    camera_as: EnumProperty( name="Export camera As:", default='NO',
208#                            description="Select representation of a camera",
209#                            items=camera_asItems)
210#    light_as: EnumProperty( name="Export lamp As:", default='NO',
211#                            description="Select representation of a lamp",
212#                            items=light_asItems)
213    # ----------------------------------------------------------
214    entitylayer_from: EnumProperty(name="Entity Layer", default="obj.data.name",
215                                    description="Entity LAYER assigned to?",
216                                    items=entitylayer_from_items)
217    entitycolor_from: EnumProperty(name="Entity Color", default="default_COLOR",
218                                    description="Entity COLOR assigned to?",
219                                    items=layerColorFromItems)
220    entityltype_from: EnumProperty(name="Entity Linetype", default="CONTINUOUS",
221                                    description="Entity LINETYPE assigned to?",
222                                    items=entityltype_fromItems)
223
224    layerName_from: EnumProperty(name="Layer Name", default="LAYERNAME_DEF",
225                                    description="From where will layer name be taken?",
226                                    items=layerNameFromItems)
227    # GUI_A -----------------------------------------
228#    layFrozen_on: BoolProperty(name="LAYER.frozen status", description="(*todo) Support LAYER.frozen status   on/off", default=False)
229#    materialFilter_on: BoolProperty(name="Material filtering", description="(*todo) Material filtering   on/off", default=False)
230#    colorFilter_on: BoolProperty(name="Color filtering", description="(*todo) Color filtering   on/off", default=False)
231#    groupFilter_on: BoolProperty(name="Group filtering", description="(*todo) Group filtering   on/off", default=False)
232#    objectFilter_on: BoolProperty(name="Object filtering", description="(*todo) Object filtering   on/off", default=False)
233#    paper_space_on: EnumProperty(name="Space of export:", default="2",
234#                                    description="Select space that will be taken for export.",
235#                                    items=spaceItems)
236#    material_to: EnumProperty(name="Material assigned to?:", default="NO",
237#                                    description="Material assigned to?.",
238#                                    items=material_toItems)
239
240#    prefix_def: StringProperty(name="Prefix for LAYERs", default="DX_",
241#                                    description='Type Prefix for LAYERs')
242#    layername_def: StringProperty(name="default LAYER name", default="DEF_LAY",
243#                                    description='Type default LAYER name')
244#    layercolor_def: StringProperty(name="Default layer color:", default="1",
245#                                    description='Set default COLOR. (0=BYBLOCK,256=BYLAYER)')
246#    layerltype_def: StringProperty(name="Default LINETYPE", default="DEF_LAY_TYPE",
247#                                    description='Set default LINETYPE')
248
249    verbose: BoolProperty(name="Verbose", default=False,
250                           description="Run the exporter in debug mode.  Check the console for output")
251
252    def execute(self, context):
253        filePath = bpy.path.ensure_ext(self.filepath, ".dxf")
254        config = {
255            'projectionThrough' : self._checkNO(self.projectionThrough),
256            'onlySelected' : self.onlySelected,
257            'apply_modifiers' : self.apply_modifiers,
258            # GUI B
259            'mesh_as' : self._checkNO(self.mesh_as),
260#            'curve_as' : self._checkNO(self.curve_as),
261#            'surface_as' : self._checkNO(self.surface_as),
262#            'meta_as' : self._checkNO(self.meta_as),
263#            'text_as' : self._checkNO(self.text_as),
264#            'empty_as' : self._checkNO(self.empty_as),
265#            'group_as' : self._checkNO(self.group_as),
266#            'proxy_as' : self._checkNO(self.proxy_as),
267#            'camera_as' : self._checkNO(self.camera_as),
268#            'light_as' : self._checkNO(self.light_as),
269
270            'entitylayer_from' : self.entitylayer_from,
271            'entitycolor_from' : self.entitycolor_from,
272            'entityltype_from' : self.entityltype_from,
273            'layerName_from' : self.layerName_from,
274
275            # NOT USED
276#            'layFrozen_on' : self.layFrozen_on,
277#            'materialFilter_on' : self.materialFilter_on,
278#            'colorFilter_on' : self.colorFilter_on,
279#            'groupFilter_on' : self.groupFilter_on,
280#            'objectFilter_on' : self.objectFilter_on,
281#            'paper_space_on' : self.paper_space_on,
282#            'layercolor_def' : self.layercolor_def,
283#            'material_to' : self.material_to,
284
285            'verbose' : self.verbose
286        }
287
288        from .export_dxf import exportDXF
289        exportDXF(context, filePath, config)
290        return {'FINISHED'}
291
292    def _checkNO(self, val):
293        if val == 'NO': return None
294        else: return val
295
296    def invoke(self, context, event):
297        if not self.filepath:
298            self.filepath = bpy.path.ensure_ext(bpy.data.filepath, ".dxf")
299        WindowManager = context.window_manager
300        WindowManager.fileselect_add(self)
301        return {'RUNNING_MODAL'}
302