From 7eca6bddc3c93039f2654935e138fe27295bad86 Mon Sep 17 00:00:00 2001
From: LS <lyubomir.savekov@engview.com>
Date: Wed, 6 Nov 2019 15:42:59 +0200
Subject: [PATCH 4/8] BLOCKS in DXF and DXB were writing handle instead of
 block name

---
 src/dwg.spec   | 15 +++++++++++----
 src/out_dxf.c  | 32 ++++++++++++++------------------
 src/out_dxfb.c | 35 +++++++++++++++++------------------
 3 files changed, 42 insertions(+), 40 deletions(-)

diff --git a/src/dwg.spec b/src/dwg.spec
index 16a7dc62..99ee3f3a 100644
--- a/src/dwg.spec
+++ b/src/dwg.spec
@@ -504,7 +504,11 @@ DWG_ENTITY(INSERT)
     }
 
   COMMON_ENTITY_HANDLE_DATA;
-  FIELD_HANDLE (block_header, 5, 0);
+  if(!IF_IS_DXF)
+  { //DXF Spec. states this should be name, not handle
+    FIELD_HANDLE (block_header, 5, 2);
+  }
+
   //There is a typo in the spec. it says "R13-R200:".
   //I guess it means "R13-R2000:" (just like in MINSERT)
   VERSIONS(R_13, R_2000)
@@ -635,7 +639,11 @@ DWG_ENTITY(MINSERT)
   FIELD_BD (row_spacing, 45);
 
   COMMON_ENTITY_HANDLE_DATA;
-  FIELD_HANDLE (block_header, 5, 0);
+  if(!IF_IS_DXF)
+  { //DXF Spec. states this should be name, not handle
+    FIELD_HANDLE (block_header, 5, 2);
+  }
+
   VERSIONS(R_13, R_2000)
   {
     if (FIELD_VALUE(has_attribs))
@@ -2421,8 +2429,7 @@ DWG_ENTITY_END
 DWG_OBJECT(BLOCK_CONTROL)
 
   DXF {
-    VALUE_RL (FIELD_VALUE(num_entries) +
-              (dwg->header_vars.BLOCK_RECORD_PSPACE ? 2 : 1), 70);
+    VALUE_RL (FIELD_VALUE(num_entries), 70);
   } else {
     FIELD_BL (num_entries, 70);
   }
diff --git a/src/out_dxf.c b/src/out_dxf.c
index 5adce060..fa5f7e0d 100644
--- a/src/out_dxf.c
+++ b/src/out_dxf.c
@@ -1313,8 +1313,15 @@ decl_dxf_process_VERTEX (PFACE)
                                   int *restrict i)                            \
   {                                                                           \
     int error = 0;                                                            \
-    Dwg_Entity_##token *_obj = obj->tio.entity->tio.token;                    \
-                                                                              \
+    { /*Highjack the name '_obj' just for a little*/                          \
+      Dwg_Object_BLOCK_HEADER* _obj =                                         \
+      obj->tio.entity->tio.token->block_header->obj->tio.object->tio.BLOCK_HEADER; \
+      BLOCK_NAME(name, 2);                                                    \
+    }                                                                         \
+    Dwg_Entity_##token* _obj = obj->tio.entity->tio.token;                    \
+    if (!_obj->has_attribs)                                                   \
+      return error;                                                           \
+    FIELD_B (has_attribs, 66);/*DXF Spec. states flags is optional*/          \
     VERSIONS (R_13, R_2000)                                                   \
     {                                                                         \
       Dwg_Object *last_attrib                                                 \
@@ -1391,23 +1398,12 @@ static int dwg_dxf_object (Bit_Chain *restrict dat,
       return 0; // dwg_dxf_SEQEND(dat, obj);
 
     case DWG_TYPE_INSERT:
-      error = dwg_dxf_INSERT (dat, obj);
-      {
-        Dwg_Entity_INSERT *_obj = obj->tio.entity->tio.INSERT;
-        if (_obj->has_attribs)
-          return error | dxf_process_INSERT (dat, obj, i);
-        else
-          return error;
-      }
+      error = dwg_dxf_INSERT(dat, obj);
+      return error | dxf_process_INSERT(dat, obj, i);
     case DWG_TYPE_MINSERT:
-      error = dwg_dxf_MINSERT (dat, obj);
-      {
-        Dwg_Entity_MINSERT *_obj = obj->tio.entity->tio.MINSERT;
-        if (_obj->has_attribs)
-          return error | dxf_process_MINSERT (dat, obj, i);
-        else
-          return error;
-      }
+      error = dwg_dxf_MINSERT(dat, obj);
+      return error | dxf_process_MINSERT(dat, obj, i);
+
     case DWG_TYPE_POLYLINE_2D:
       error = dwg_dxf_POLYLINE_2D (dat, obj);
       return error | dxf_process_VERTEX_2D (dat, obj, i);
diff --git a/src/out_dxfb.c b/src/out_dxfb.c
index d60c2c11..b4d42612 100644
--- a/src/out_dxfb.c
+++ b/src/out_dxfb.c
@@ -1153,8 +1153,15 @@ decl_dxfb_process_VERTEX (2D) decl_dxfb_process_VERTEX (3D)
                                    int *restrict i)                           \
   {                                                                           \
     int error = 0;                                                            \
-    Dwg_Entity_##token *_obj = obj->tio.entity->tio.token;                    \
-                                                                              \
+        { /*Highjack the name '_obj' just for a little*/                      \
+      Dwg_Object_BLOCK_HEADER* _obj =                                         \
+      obj->tio.entity->tio.token->block_header->obj->tio.object->tio.BLOCK_HEADER; \
+      BLOCK_NAME(name, 2);                                                    \
+    }                                                                         \
+    Dwg_Entity_##token* _obj = obj->tio.entity->tio.token;                    \
+    if (!_obj->has_attribs)                                                   \
+      return error;                                                           \
+    FIELD_B (has_attribs, 66);/*DXF Spec. states flags is optional*/          \
     VERSIONS (R_13, R_2000)                                                   \
     {                                                                         \
       Dwg_Object *last_attrib = _obj->last_attrib->obj;                       \
@@ -1224,23 +1231,15 @@ decl_dxfb_process_VERTEX (2D) decl_dxfb_process_VERTEX (3D)
       return dwg_dxfb_SEQEND (dat, obj);
 
     case DWG_TYPE_INSERT:
-      error = dwg_dxfb_INSERT (dat, obj);
-      {
-        Dwg_Entity_INSERT *_obj = obj->tio.entity->tio.INSERT;
-        if (_obj->has_attribs)
-          return error | dxfb_process_INSERT (dat, obj, i);
-        else
-          return error;
-      }
+    {
+      error = dwg_dxfb_INSERT(dat, obj);
+      return error | dxfb_process_INSERT(dat, obj, i);
+    }
     case DWG_TYPE_MINSERT:
-      error = dwg_dxfb_MINSERT (dat, obj);
-      {
-        Dwg_Entity_MINSERT *_obj = obj->tio.entity->tio.MINSERT;
-        if (_obj->has_attribs)
-          return error | dxfb_process_MINSERT (dat, obj, i);
-        else
-          return error;
-      }
+    {
+      error = dwg_dxfb_MINSERT(dat, obj);
+      return error | dxfb_process_MINSERT(dat, obj, i);
+    }
 
     case DWG_TYPE_POLYLINE_2D:
       error = dwg_dxfb_POLYLINE_2D (dat, obj);
-- 
2.22.0.windows.1

