Export_3ds: Improved distance cue node search
[blender-addons.git] / io_import_dxf / dxfimport / groupsort.py
blob2dd6c9b960a447a1d4df0cb8006122463abaaf53
1 # SPDX-FileCopyrightText: 2014-2022 Blender Foundation
3 # SPDX-License-Identifier: GPL-2.0-or-later
5 import itertools
6 from . import is_
7 from mathutils import Vector
10 def map_dxf_to_blender_type(TYPE):
11 """
12 TYPE: DXF entity type (String)
13 """
14 if is_.mesh(TYPE):
15 return "object_mesh"
16 if is_.curve(TYPE):
17 return "object_curve"
18 if is_.nurbs(TYPE):
19 return "object_surface"
20 else:
21 print("groupsort: not mergeable type ", TYPE)
22 return "not_mergeable"
25 def by_blender_type(entities):
26 """
27 entities: list of DXF entities
28 """
29 keyf = lambda e: map_dxf_to_blender_type(e.dxftype)
30 return itertools.groupby(sorted(entities, key=keyf), key=keyf)
33 def by_layer(entities):
34 """
35 entities: list of DXF entities
36 """
37 keyf = lambda e: e.layer
38 return itertools.groupby(sorted(entities, key=keyf), key=keyf)
40 def by_closed_poly_no_bulge(entities):
41 """
42 entities: list of DXF entities
43 """
44 keyf = lambda e: is_.closed_poly_no_bulge(e)
45 return itertools.groupby(sorted(entities, key=keyf), key=keyf)
48 def by_dxftype(entities):
49 """
50 entities: list of DXF entities
51 """
52 keyf = lambda e: e.dxftype
53 return itertools.groupby(sorted(entities, key=keyf), key=keyf)
56 def by_attributes(entities):
57 """
58 entities: list of DXF entities
59 attributes: thickness and width occurring in curve types; subdivision_levels occurring in MESH dxf types
60 """
61 def attributes(entity):
62 width = [(0, 0)]
63 subd = -1
64 extrusion = entity.extrusion
65 if hasattr(entity, "width"):
66 if any((w != 0 for ww in entity.width for w in ww)):
67 width = entity.width
68 if hasattr(entity, "subdivision_levels"):
69 subd = entity.subdivision_levels
70 if entity.dxftype in {"LINE", "POINT"}:
71 extrusion = (0.0, 0.0, 1.0)
72 if extrusion is None:
73 # This can happen for entities of type "SPLINE" for example.
74 # But the sort comparison does not work between 'tuple' and 'NoneType'.
75 extrusion = ()
76 return entity.thickness, subd, width, extrusion
78 return itertools.groupby(sorted(entities, key=attributes), key=attributes)
80 def by_insert_block_name(inserts):
81 """
82 entities: list of DXF inserts
83 """
84 keyf = lambda e: e.name
85 return itertools.groupby(sorted(inserts, key=keyf), key=keyf)