[Bf-blender-cvs] [4816fa64c50] greasepencil-object: GPencil: Basic structure of XML with GP Object

Antonio Vazquez noreply at git.blender.org
Sat Jul 25 16:33:28 CEST 2020


Commit: 4816fa64c50838b44dc2e3bf685c67b5547683f8
Author: Antonio Vazquez
Date:   Fri Jul 24 11:41:58 2020 +0200
Branches: greasepencil-object
https://developer.blender.org/rB4816fa64c50838b44dc2e3bf685c67b5547683f8

GPencil: Basic structure of XML with GP Object

===================================================================

M	source/blender/editors/io/io_gpencil.c
M	source/blender/io/gpencil/intern/gpencil_io_capi.cc
M	source/blender/io/gpencil/intern/gpencil_io_svg.cc
M	source/blender/io/gpencil/intern/gpencil_io_svg.h

===================================================================

diff --git a/source/blender/editors/io/io_gpencil.c b/source/blender/editors/io/io_gpencil.c
index a85cfc54167..5685dd87abe 100644
--- a/source/blender/editors/io/io_gpencil.c
+++ b/source/blender/editors/io/io_gpencil.c
@@ -33,19 +33,15 @@
 
 #include "MEM_guardedalloc.h"
 
-#include "DNA_mesh_types.h"
-#include "DNA_modifier_types.h"
-#include "DNA_object_types.h"
-#include "DNA_scene_types.h"
+#include "DNA_gpencil_types.h"
 #include "DNA_space_types.h"
 
 #include "BKE_context.h"
-#include "BKE_global.h"
+#include "BKE_gpencil.h"
 #include "BKE_main.h"
 #include "BKE_report.h"
 
 #include "BLI_listbase.h"
-#include "BLI_math_vector.h"
 #include "BLI_path_util.h"
 #include "BLI_string.h"
 #include "BLI_utildefines.h"
@@ -54,9 +50,6 @@
 
 #include "RNA_access.h"
 #include "RNA_define.h"
-#include "RNA_enum_types.h"
-
-#include "ED_object.h"
 
 #include "UI_interface.h"
 #include "UI_resources.h"
@@ -188,7 +181,17 @@ static bool wm_gpencil_export_check(bContext *UNUSED(C), wmOperator *op)
 bool wm_gpencil_export_poll(bContext *C)
 {
   if (CTX_wm_window(C) == NULL) {
-    // TODO: Add GPencil check/layer, etc.
+    return false;
+  }
+  Object *ob = CTX_data_active_object(C);
+  if ((ob == NULL) || (ob->type != OB_GPENCIL)) {
+    return false;
+  }
+
+  bGPdata *gpd = (bGPdata *)ob->data;
+  bGPDlayer *gpl = BKE_gpencil_layer_active_get(gpd);
+
+  if (gpl == NULL) {
     return false;
   }
 
diff --git a/source/blender/io/gpencil/intern/gpencil_io_capi.cc b/source/blender/io/gpencil/intern/gpencil_io_capi.cc
index 9c470f34e34..033e349ebfc 100644
--- a/source/blender/io/gpencil/intern/gpencil_io_capi.cc
+++ b/source/blender/io/gpencil/intern/gpencil_io_capi.cc
@@ -23,10 +23,19 @@
 
 #include "gpencil_io_svg.h"
 
+using blender::io::gpencil::GpencilExporterSVG;
+
 bool gpencil_io_export(const GpencilExportParams *params)
 {
-  blender::io::gpencil::Gpencilwriter mywriter(params);
-  mywriter.export_object();
+  switch (params->mode) {
+    case GP_EXPORT_TO_SVG: {
+      GpencilExporterSVG writter = GpencilExporterSVG(params);
+      writter.write();
+      break;
+    }
+    default:
+      break;
+  }
 
   return true;
 }
diff --git a/source/blender/io/gpencil/intern/gpencil_io_svg.cc b/source/blender/io/gpencil/intern/gpencil_io_svg.cc
index cdb8128b1e2..44bc22e0ef1 100644
--- a/source/blender/io/gpencil/intern/gpencil_io_svg.cc
+++ b/source/blender/io/gpencil/intern/gpencil_io_svg.cc
@@ -22,9 +22,11 @@
 #include "BKE_context.h"
 #include "BKE_main.h"
 
+#include "BLI_blenlib.h"
 #include "BLI_path_util.h"
 #include "BLI_string.h"
 
+#include "DNA_gpencil_types.h"
 #include "DNA_object_types.h"
 
 #ifdef WIN32
@@ -42,79 +44,107 @@ namespace blender {
 namespace io {
 namespace gpencil {
 
+void GpencilExporter::set_out_filename(struct bContext *C, char *filename)
+{
+  Main *bmain = CTX_data_main(C);
+  BLI_strncpy(out_filename, filename, FILE_MAX);
+  BLI_path_abs(out_filename, BKE_main_blendfile_path(bmain));
+
+  //#ifdef WIN32
+  //  UTF16_ENCODE(svg_filename);
+  //#endif
+}
+
 /* Constructor. */
-Gpencilwriter::Gpencilwriter(const struct GpencilExportParams *params)
+GpencilExporterSVG::GpencilExporterSVG(const struct GpencilExportParams *params)
 {
-  this->params = GpencilExportParams();
   this->params.frame_start = params->frame_start;
   this->params.frame_end = params->frame_end;
   this->params.ob = params->ob;
   this->params.C = params->C;
   this->params.filename = params->filename;
   this->params.mode = params->mode;
-}
 
-/**
- * Select type of export
- * @return
- */
-bool Gpencilwriter::export_object(void)
-{
-  switch (this->params.mode) {
-    case GP_EXPORT_TO_SVG: {
-      GpencilwriterSVG writter = GpencilwriterSVG(&params);
-      writter.write();
-      break;
-    }
-    default:
-      break;
-  }
-
-  return true;
-}
-
-/* Constructor. */
-GpencilwriterSVG::GpencilwriterSVG(struct GpencilExportParams *params)
-{
-  this->params = params;
+  /* Prepare output filename with full path. */
+  set_out_filename(params->C, params->filename);
 }
 
 /* Main write method for SVG format. */
-bool GpencilwriterSVG::write(void)
+bool GpencilExporterSVG::write(void)
 {
-  Main *bmain = CTX_data_main(this->params->C);
-  char svg_filename[FILE_MAX];
-  BLI_strncpy(svg_filename, this->params->filename, FILE_MAX);
-  BLI_path_abs(svg_filename, BKE_main_blendfile_path(bmain));
-
-  Object *ob = this->params->ob;
-
-  //#ifdef WIN32
-  //  UTF16_ENCODE(svg_filename);
-  //#endif
+  create_document_header();
+  layers_loop();
 
-  /* Create simple XML. */
-  // tag::code[]
-  // add node with some name
-  pugi::xml_node node = this->doc.append_child("node");
+  //// add description node with text child
+  // pugi::xml_node descr = main_node.append_child("object");
+  // descr.append_child(pugi::node_pcdata).set_value(ob->id.name + 2);
 
-  // add description node with text child
-  pugi::xml_node descr = node.append_child("object");
-  descr.append_child(pugi::node_pcdata).set_value(ob->id.name + 2);
+  //// add param node before the description
+  // pugi::xml_node param = main_node.insert_child_before("param", descr);
 
-  // add param node before the description
-  pugi::xml_node param = node.insert_child_before("param", descr);
+  //// add attributes to param node
+  // param.append_attribute("name") = "version";
+  // param.append_attribute("value") = 1.1;
+  // param.insert_attribute_after("type", param.attribute("name")) = "float";
 
-  // add attributes to param node
-  param.append_attribute("name") = "version";
-  param.append_attribute("value") = 1.1;
-  param.insert_attribute_after("type", param.attribute("name")) = "float";
   // end::code[]
-  doc.save_file(svg_filename);
+  doc.save_file(out_filename);
 
   return true;
 }
 
+/* Create document header and main svg node. */
+void GpencilExporterSVG::create_document_header(void)
+{
+  /* Add a custom document declaration node */
+  pugi::xml_node decl = doc.prepend_child(pugi::node_declaration);
+  decl.append_attribute("version") = "1.0";
+  decl.append_attribute("encoding") = "UTF-8";
+
+  pugi::xml_node comment = doc.append_child(pugi::node_comment);
+  comment.set_value(" Generator: Blender, SVG Export for Grease Pencil ");
+
+  pugi::xml_node doctype = doc.append_child(pugi::node_doctype);
+  doctype.set_value(
+      "svg PUBLIC \"-//W3C//DTD SVG 1.0//EN\" "
+      "\"http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd\"");
+
+  main_node = doc.append_child("svg");
+  main_node.append_attribute("version").set_value("1.0");
+  main_node.append_attribute("x").set_value("0px");
+  main_node.append_attribute("y").set_value("0px");
+  main_node.append_attribute("width").set_value("841px");
+  main_node.append_attribute("height").set_value("600px");
+  main_node.append_attribute("viewBox").set_value("0 0 841 600");
+}
+
+/* Main layer loop. */
+void GpencilExporterSVG::layers_loop(void)
+{
+  Object *ob = params.ob;
+  bGPdata *gpd = (bGPdata *)ob->data;
+  LISTBASE_FOREACH (bGPDlayer *, gpl, &gpd->layers) {
+    /* Layer node. */
+    std::string txt = "Layer: ";
+    txt.append(gpl->info);
+    main_node.append_child(pugi::node_comment).set_value(txt.c_str());
+    pugi::xml_node gpl_node = main_node.append_child("g");
+    gpl_node.append_attribute("id").set_value(gpl->info);
+
+    bGPDframe *gpf = gpl->actframe;
+    if (gpf == NULL) {
+      continue;
+    }
+
+    LISTBASE_FOREACH (bGPDstroke *, gps, &gpf->strokes) {
+      pugi::xml_node gps_node = gpl_node.append_child("path");
+      gps_node.append_attribute("fill").set_value("#000000");
+      gps_node.append_attribute("stroke").set_value("#000000");
+      gps_node.append_attribute("stroke-width").set_value("0.5");
+    }
+  }
+}
+
 }  // namespace gpencil
 }  // namespace io
 }  // namespace blender
diff --git a/source/blender/io/gpencil/intern/gpencil_io_svg.h b/source/blender/io/gpencil/intern/gpencil_io_svg.h
index 95d560ebc2f..bd14029181e 100644
--- a/source/blender/io/gpencil/intern/gpencil_io_svg.h
+++ b/source/blender/io/gpencil/intern/gpencil_io_svg.h
@@ -18,6 +18,8 @@
 /** \file
  * \ingroup bgpencil
  */
+#include "BLI_path_util.h"
+
 #include "pugixml.hpp"
 
 struct Main;
@@ -27,25 +29,31 @@ namespace blender {
 namespace io {
 namespace gpencil {
 
-class Gpencilwriter {
+class GpencilExporter {
 
  public:
-  Gpencilwriter(const struct GpencilExportParams *params);
-  bool export_object(void);
+  virtual bool write(void) = 0;
+  void set_out_filename(struct bContext *C, char *filename);
 
- private:
+ protected:
   GpencilExportParams params;
+  char out_filename[FILE_MAX];
 };
 
-class GpencilwriterSVG {
+class GpencilExporterSVG : public GpencilExporter {
 
  public:
-  GpencilwriterSVG(struct GpencilExportParams *params);
+  GpencilExporterSVG(const struct GpencilExportParams *params);
   bool write(void);
 
  private:
-  GpencilExportParams *params;
+  /* XML doc. */
   pugi::xml_document doc;
+  /* Main document node. */
+  pugi::xml_node main_node;
+
+  void create_document_header(void);
+  void layers_loop(void);
 };
 
 }  // namespace gpencil



More information about the Bf-blender-cvs mailing list