From 3852f81bab8def2f70466312d4eadfc949523374 Mon Sep 17 00:00:00 2001 From: Anton Chalakov Date: Thu, 7 Nov 2024 00:40:34 +0200 Subject: [PATCH 01/14] implementation and tests for Drawingelement --- ext/drawing_element.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/ext/drawing_element.c b/ext/drawing_element.c index f50b8c5..3231542 100644 --- a/ext/drawing_element.c +++ b/ext/drawing_element.c @@ -77,8 +77,6 @@ static VALUE Sketchup_DrawingElement_Get_material(VALUE self) SUDrawingElementRef drawing_element = {DATA_PTR(self)}; SUMaterialRef material = SU_INVALID; SUDrawingElementGetMaterial(drawing_element, &material); - if (SUIsInvalid(material)) - return Qnil; return Data_Wrap_Struct(rb_path2class(SKETCHUP_MATERIAL), 0, 0, material.ptr); } From 7089b4cd4d3a58be2b9f8c53e579a420ed547223 Mon Sep 17 00:00:00 2001 From: Anton Chalakov Date: Thu, 27 Mar 2025 10:26:28 +0200 Subject: [PATCH 02/14] initial texture class and tests --- ext/texture.c | 12 ++++++++++++ test/test_texture.rb | 44 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 test/test_texture.rb diff --git a/ext/texture.c b/ext/texture.c index b6fc26a..3872ca8 100644 --- a/ext/texture.c +++ b/ext/texture.c @@ -3,8 +3,20 @@ #include #include +static VALUE Sketchup_Texture_average_color(VALUE self) +{ + SUTextureRef texture = {DATA_PTR(self)}; + SUColor* color_val; + enum SUResult result = SUTextureGetAverageColor(texture, &color_val); + if (result != SU_ERROR_NONE || !color_val) + return Qnil; + + return Data_Wrap_Struct(rb_path2class(SKETCHUP_COLOR), 0, 0, color_val); +} + void Texture_Init(VALUE Sketchup, VALUE Sketchup_Entity) { VALUE Sketchup_Texture = rb_define_class_under(Sketchup, TEXTURE, Sketchup_Entity); rb_undef_alloc_func(Sketchup_Texture); + rb_define_method(Sketchup_Texture, "average_color", Sketchup_Texture_average_color, 0); } diff --git a/test/test_texture.rb b/test/test_texture.rb new file mode 100644 index 0000000..ca83fba --- /dev/null +++ b/test/test_texture.rb @@ -0,0 +1,44 @@ +require 'minitest/autorun' +require 'sketchup' + +class TestTexture < Minitest::Test + + def setup + Sketchup.open_file("#{ENV['TEST_RESOURCES']}/Untitled.skp") + material = Sketchup.active_model.materials['Heather_Shirt'] + material.texture = "#{__dir__}/texture_map.png" + @texture = material.texture + end + + def test_average_color + assert_instance_of(Sketchup::Color, @texture.average_color) + end + + def test_filename + end + + def test_height + end + + def test_image_height + end + + def test_image_rep + end + + def test_image_width + end + + def test_size + end + + def test_valid? + end + + def test_width + end + + def test_write + end + +end From 8bebf794f1dd3bf22c2ec4fccd3fc639c14a7fa3 Mon Sep 17 00:00:00 2001 From: Anton Chalakov Date: Fri, 28 Mar 2025 10:19:20 +0200 Subject: [PATCH 03/14] implement average_color, filename, image_height, image_width --- ext/texture.c | 51 ++++++++++++++++++++++++++++++++++++++++---- ext/utils.h | 2 ++ test/test_texture.rb | 16 ++++++-------- 3 files changed, 56 insertions(+), 13 deletions(-) diff --git a/ext/texture.c b/ext/texture.c index 3872ca8..e4cba9c 100644 --- a/ext/texture.c +++ b/ext/texture.c @@ -6,12 +6,51 @@ static VALUE Sketchup_Texture_average_color(VALUE self) { SUTextureRef texture = {DATA_PTR(self)}; - SUColor* color_val; - enum SUResult result = SUTextureGetAverageColor(texture, &color_val); - if (result != SU_ERROR_NONE || !color_val) + VALUE c = rb_obj_alloc(rb_path2class(SKETCHUP_COLOR)); + SUColor* color = DATA_PTR(c); + enum SUResult result = SUTextureGetAverageColor(texture, color); + if (result != SU_ERROR_NONE) return Qnil; - return Data_Wrap_Struct(rb_path2class(SKETCHUP_COLOR), 0, 0, color_val); + return c; +} + +static VALUE Sketchup_Texture_filename(VALUE self) +{ + SUTextureRef texture = {DATA_PTR(self)}; + VALUE output; + GETSTRING(SUTextureGetFileName, texture, output); + return output; +} + +static VALUE Sketchup_Texture_image_height(VALUE self) +{ + SUTextureRef texture = {DATA_PTR(self)}; + size_t width = 0; + size_t height = 0; + double s_scale = 0.0; + double t_scale = 0.0; + SUTextureGetDimensions(texture, &width, &height, &s_scale, &t_scale); + return ULL2NUM(height); +} + +static VALUE Sketchup_Texture_image_width(VALUE self) +{ + SUTextureRef texture = {DATA_PTR(self)}; + size_t width = 0; + size_t height = 0; + double s_scale = 0.0; + double t_scale = 0.0; + SUTextureGetDimensions(texture, &width, &height, &s_scale, &t_scale); + return ULL2NUM(width); +} + +static VALUE Sketchup_Texture_image_rep(VALUE self) +{ + SUTextureRef texture = {DATA_PTR(self)}; + SUImageRepRef image = SU_INVALID; + SUTextureGetImageRep(texture, &image); + return Data_Wrap_Struct(rb_path2class(SKETCHUP_IMAGEREP), 0, 0, image.ptr); } void Texture_Init(VALUE Sketchup, VALUE Sketchup_Entity) @@ -19,4 +58,8 @@ void Texture_Init(VALUE Sketchup, VALUE Sketchup_Entity) VALUE Sketchup_Texture = rb_define_class_under(Sketchup, TEXTURE, Sketchup_Entity); rb_undef_alloc_func(Sketchup_Texture); rb_define_method(Sketchup_Texture, "average_color", Sketchup_Texture_average_color, 0); + rb_define_method(Sketchup_Texture, "filename", Sketchup_Texture_filename, 0); + rb_define_method(Sketchup_Texture, "image_height", Sketchup_Texture_image_height, 0); + rb_define_method(Sketchup_Texture, "image_width", Sketchup_Texture_image_width, 0); + rb_define_method(Sketchup_Texture, "image_rep", Sketchup_Texture_image_rep, 0); } diff --git a/ext/utils.h b/ext/utils.h index a576c11..0407097 100644 --- a/ext/utils.h +++ b/ext/utils.h @@ -31,6 +31,7 @@ #define SECTIONPLANE "SectionPlane" #define GROUP "Group" #define TEXT "Text" +#define IMAGEREP "ImageRep" #define SKETCHUP_MATERIAL SKETCHUP "::" MATERIAL #define SKETCHUP_MATERIALS SKETCHUP "::" MATERIALS #define SKETCHUP_TEXTURE SKETCHUP "::" TEXTURE @@ -54,6 +55,7 @@ #define SKETCHUP_SECTIONPLANE SKETCHUP "::" SECTIONPLANE #define SKETCHUP_GROUP SKETCHUP "::" GROUP #define SKETCHUP_TEXT SKETCHUP "::" TEXT +#define SKETCHUP_IMAGEREP SKETCHUP "::" IMAGEREP #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wbackslash-newline-escape" diff --git a/test/test_texture.rb b/test/test_texture.rb index ca83fba..2a348ac 100644 --- a/test/test_texture.rb +++ b/test/test_texture.rb @@ -5,9 +5,9 @@ class TestTexture < Minitest::Test def setup Sketchup.open_file("#{ENV['TEST_RESOURCES']}/Untitled.skp") - material = Sketchup.active_model.materials['Heather_Shirt'] - material.texture = "#{__dir__}/texture_map.png" - @texture = material.texture + @material = Sketchup.active_model.materials['Heather_Shirt'] + @material.texture = "#{ENV['TEST_RESOURCES']}/texture_map.png" + @texture = @material.texture end def test_average_color @@ -15,18 +15,19 @@ def test_average_color end def test_filename - end - - def test_height + assert_instance_of(String, @texture.filename) end def test_image_height + assert_equal(1024, @texture.image_height) end def test_image_rep + assert_instance_of(Sketchup::ImageRep, @texture.image_repa) end def test_image_width + assert_equal(1024, @texture.image_width) end def test_size @@ -35,9 +36,6 @@ def test_size def test_valid? end - def test_width - end - def test_write end From f3e051ea916e360c3d3ce4a9ad2f31f9bf3421af Mon Sep 17 00:00:00 2001 From: Anton Chalakov Date: Fri, 28 Mar 2025 10:22:20 +0200 Subject: [PATCH 04/14] add image_rep to not_implemented --- ext/not_implemented.c | 7 +++++++ ext/not_implemented.h | 1 + 2 files changed, 8 insertions(+) diff --git a/ext/not_implemented.c b/ext/not_implemented.c index 4b675f9..6d44296 100644 --- a/ext/not_implemented.c +++ b/ext/not_implemented.c @@ -78,4 +78,11 @@ VALUE Text_Init(VALUE Sketchup, VALUE DrawingElement) VALUE Sketchup_Text = rb_define_class_under(Sketchup, TEXT, DrawingElement); rb_undef_alloc_func(Sketchup_Text); return Sketchup_Text; +} + +VALUE ImageRep_Init(VALUE Sketchup, VALUE Sketchup_Object) +{ + VALUE Sketchup_ImageRep = rb_define_class_under(Sketchup, IMAGEREP, Sketchup_Object); + rb_undef_alloc_func(Sketchup_ImageRep); + return Sketchup_ImageRep; } \ No newline at end of file diff --git a/ext/not_implemented.h b/ext/not_implemented.h index 2e0b5d4..f279deb 100644 --- a/ext/not_implemented.h +++ b/ext/not_implemented.h @@ -11,3 +11,4 @@ VALUE Group_Init(VALUE namespace_object, VALUE parent_class); VALUE Image_Init(VALUE namespace_object, VALUE parent_class); VALUE SectionPlane_Init(VALUE namespace_object, VALUE parent_class); VALUE Text_Init(VALUE namespace_object, VALUE parent_class); +VALUE ImageRep_Init(VALUE namespace_object, VALUE parent_class); From 7771cf1ccfb0591739d4ee44a870552f7fae74a9 Mon Sep 17 00:00:00 2001 From: Anton Chalakov Date: Fri, 28 Mar 2025 10:24:13 +0200 Subject: [PATCH 05/14] add ImageRep --- ext/texture.c | 1 + 1 file changed, 1 insertion(+) diff --git a/ext/texture.c b/ext/texture.c index e4cba9c..4214ea1 100644 --- a/ext/texture.c +++ b/ext/texture.c @@ -1,6 +1,7 @@ #include #include #include +#include #include static VALUE Sketchup_Texture_average_color(VALUE self) From 46bc4df7a8703cfde31bef68d064c4ed1abc52cf Mon Sep 17 00:00:00 2001 From: Anton Chalakov Date: Fri, 28 Mar 2025 10:27:40 +0200 Subject: [PATCH 06/14] update sketchup.c --- ext/sketchup.c | 1 + 1 file changed, 1 insertion(+) diff --git a/ext/sketchup.c b/ext/sketchup.c index 5863920..cd11d34 100644 --- a/ext/sketchup.c +++ b/ext/sketchup.c @@ -73,6 +73,7 @@ void Init_sketchup() { Model_Init(Sketchup, rb_cObject); Color_Init(Sketchup, rb_cObject); + ImageRep_Init(Sketchup, rb_cObject); Entities_Init(Sketchup, rb_cObject); VALUE Sketchup_Entity = Entity_Init(Sketchup, rb_cObject); From ba5cb73c011057e30c2650e76976401eeefcfa6c Mon Sep 17 00:00:00 2001 From: Anton Chalakov Date: Fri, 28 Mar 2025 10:37:31 +0200 Subject: [PATCH 07/14] fix typo --- test/test_texture.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test_texture.rb b/test/test_texture.rb index 2a348ac..41b94d2 100644 --- a/test/test_texture.rb +++ b/test/test_texture.rb @@ -23,7 +23,7 @@ def test_image_height end def test_image_rep - assert_instance_of(Sketchup::ImageRep, @texture.image_repa) + assert_instance_of(Sketchup::ImageRep, @texture.image_rep) end def test_image_width From 7753130c6b52a73f5e233d077c22488576d0794c Mon Sep 17 00:00:00 2001 From: Anton Chalakov Date: Fri, 28 Mar 2025 10:46:44 +0200 Subject: [PATCH 08/14] texture_valid? --- ext/texture.c | 7 +++++++ test/test_texture.rb | 4 +--- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/ext/texture.c b/ext/texture.c index 4214ea1..9fe2007 100644 --- a/ext/texture.c +++ b/ext/texture.c @@ -54,6 +54,12 @@ static VALUE Sketchup_Texture_image_rep(VALUE self) return Data_Wrap_Struct(rb_path2class(SKETCHUP_IMAGEREP), 0, 0, image.ptr); } +static VALUE Sketchup_Texture_Get_valid(VALUE self) +{ + SUTextureRef texture = {DATA_PTR(self)}; + return SUIsValid(texture) ? Qtrue : Qfalse; +} + void Texture_Init(VALUE Sketchup, VALUE Sketchup_Entity) { VALUE Sketchup_Texture = rb_define_class_under(Sketchup, TEXTURE, Sketchup_Entity); @@ -63,4 +69,5 @@ void Texture_Init(VALUE Sketchup, VALUE Sketchup_Entity) rb_define_method(Sketchup_Texture, "image_height", Sketchup_Texture_image_height, 0); rb_define_method(Sketchup_Texture, "image_width", Sketchup_Texture_image_width, 0); rb_define_method(Sketchup_Texture, "image_rep", Sketchup_Texture_image_rep, 0); + rb_define_method(Sketchup_Texture, "valid?", Sketchup_Texture_Get_valid, 0); } diff --git a/test/test_texture.rb b/test/test_texture.rb index 41b94d2..e586b10 100644 --- a/test/test_texture.rb +++ b/test/test_texture.rb @@ -30,10 +30,8 @@ def test_image_width assert_equal(1024, @texture.image_width) end - def test_size - end - def test_valid? + assert(@texture.valid?) end def test_write From 144e24039a51a29dab7dbc61b16557bfd56f3077 Mon Sep 17 00:00:00 2001 From: Anton Chalakov Date: Mon, 31 Mar 2025 09:46:55 +0300 Subject: [PATCH 09/14] add write --- ext/texture.c | 13 +++++++++++++ test/test_texture.rb | 13 +++++++++++++ 2 files changed, 26 insertions(+) diff --git a/ext/texture.c b/ext/texture.c index 9fe2007..e88e36e 100644 --- a/ext/texture.c +++ b/ext/texture.c @@ -60,6 +60,18 @@ static VALUE Sketchup_Texture_Get_valid(VALUE self) return SUIsValid(texture) ? Qtrue : Qfalse; } +static VALUE Sketchup_Texture_write(VALUE self, VALUE path, VALUE colorize) +{ + SUTextureRef texture = {DATA_PTR(self)}; + const char* path_ptr = StringValuePtr(path); + enum SUResult result = SU_ERROR_UNSUPPORTED; + if (RTEST(colorize)) + result = SUTextureWriteToFile(texture, path_ptr); + else + result = SUTextureWriteOriginalToFile(texture, path_ptr); + return result != SU_ERROR_NONE ? Qfalse : Qtrue; +} + void Texture_Init(VALUE Sketchup, VALUE Sketchup_Entity) { VALUE Sketchup_Texture = rb_define_class_under(Sketchup, TEXTURE, Sketchup_Entity); @@ -70,4 +82,5 @@ void Texture_Init(VALUE Sketchup, VALUE Sketchup_Entity) rb_define_method(Sketchup_Texture, "image_width", Sketchup_Texture_image_width, 0); rb_define_method(Sketchup_Texture, "image_rep", Sketchup_Texture_image_rep, 0); rb_define_method(Sketchup_Texture, "valid?", Sketchup_Texture_Get_valid, 0); + rb_define_method(Sketchup_Texture, "write", Sketchup_Texture_write, 2); } diff --git a/test/test_texture.rb b/test/test_texture.rb index e586b10..b536c5e 100644 --- a/test/test_texture.rb +++ b/test/test_texture.rb @@ -35,6 +35,19 @@ def test_valid? end def test_write + basename = @texture.filename + Dir.mktmpdir { |dir| + path = File.join("#{dir}", basename) + refute(File.exist?(path)) + @texture.write(path, true) + assert(File.exist?(path)) + } + Dir.mktmpdir { |dir| + path = File.join("#{dir}", basename) + refute(File.exist?(path)) + @texture.write(path, false) + assert(File.exist?(path)) + } end end From c6edac87375c2a1651728841a748d185f2c8aee9 Mon Sep 17 00:00:00 2001 From: Anton Chalakov Date: Mon, 31 Mar 2025 09:52:30 +0300 Subject: [PATCH 10/14] update README --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index fa65459..8c05e72 100644 --- a/README.md +++ b/README.md @@ -13,6 +13,7 @@ The SketchUp C API is much more limmited than the ruby api. There is no gui eve * Entity * Material * Drawingelement +* Texture # Partially implemented classes * Attribute Dictionaries @@ -20,5 +21,4 @@ The SketchUp C API is much more limmited than the ruby api. There is no gui eve * Component Instance * Entities * Materials -* Model -* Texture \ No newline at end of file +* Model \ No newline at end of file From d399b177735ee8dd486955f69f60717b9011429f Mon Sep 17 00:00:00 2001 From: Anton Chalakov Date: Tue, 1 Apr 2025 17:02:44 +0300 Subject: [PATCH 11/14] add removed check --- ext/drawing_element.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/ext/drawing_element.c b/ext/drawing_element.c index 3231542..f50b8c5 100644 --- a/ext/drawing_element.c +++ b/ext/drawing_element.c @@ -77,6 +77,8 @@ static VALUE Sketchup_DrawingElement_Get_material(VALUE self) SUDrawingElementRef drawing_element = {DATA_PTR(self)}; SUMaterialRef material = SU_INVALID; SUDrawingElementGetMaterial(drawing_element, &material); + if (SUIsInvalid(material)) + return Qnil; return Data_Wrap_Struct(rb_path2class(SKETCHUP_MATERIAL), 0, 0, material.ptr); } From 5041ef13b853f092e90a09b0be11b77d653bab7f Mon Sep 17 00:00:00 2001 From: Anton Chalakov Date: Tue, 1 Apr 2025 17:20:04 +0300 Subject: [PATCH 12/14] add missing text for image_width --- test/test_texture.rb | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/test/test_texture.rb b/test/test_texture.rb index b536c5e..965cd25 100644 --- a/test/test_texture.rb +++ b/test/test_texture.rb @@ -22,6 +22,10 @@ def test_image_height assert_equal(1024, @texture.image_height) end + def test_image_width + assert_equal(1024, @texture.image_width) + end + def test_image_rep assert_instance_of(Sketchup::ImageRep, @texture.image_rep) end From 5bbd921ae6d03d61f02f2d7334cb7259046aca7c Mon Sep 17 00:00:00 2001 From: Anton Chalakov Date: Tue, 1 Apr 2025 17:29:10 +0300 Subject: [PATCH 13/14] remove tab --- ext/texture.c | 1 - 1 file changed, 1 deletion(-) diff --git a/ext/texture.c b/ext/texture.c index e88e36e..52ab20c 100644 --- a/ext/texture.c +++ b/ext/texture.c @@ -12,7 +12,6 @@ static VALUE Sketchup_Texture_average_color(VALUE self) enum SUResult result = SUTextureGetAverageColor(texture, color); if (result != SU_ERROR_NONE) return Qnil; - return c; } From af518434fe6d6881d74b332396279ec0f59b6cbd Mon Sep 17 00:00:00 2001 From: Anton Chalakov Date: Tue, 1 Apr 2025 18:35:26 +0300 Subject: [PATCH 14/14] add width and height --- ext/texture.c | 24 ++++++++++++++++++++++++ test/test_texture.rb | 8 ++++++++ 2 files changed, 32 insertions(+) diff --git a/ext/texture.c b/ext/texture.c index 52ab20c..65bb854 100644 --- a/ext/texture.c +++ b/ext/texture.c @@ -34,6 +34,28 @@ static VALUE Sketchup_Texture_image_height(VALUE self) return ULL2NUM(height); } +static VALUE Sketchup_Texture_height(VALUE self) +{ + SUTextureRef texture = {DATA_PTR(self)}; + size_t width = 0; + size_t height = 0; + double s_scale = 0.0; + double t_scale = 0.0; + SUTextureGetDimensions(texture, &width, &height, &s_scale, &t_scale); + return DBL2NUM(1.0 / t_scale); +} + +static VALUE Sketchup_Texture_width(VALUE self) +{ + SUTextureRef texture = {DATA_PTR(self)}; + size_t width = 0; + size_t height = 0; + double s_scale = 0.0; + double t_scale = 0.0; + SUTextureGetDimensions(texture, &width, &height, &s_scale, &t_scale); + return DBL2NUM(1.0 / s_scale); +} + static VALUE Sketchup_Texture_image_width(VALUE self) { SUTextureRef texture = {DATA_PTR(self)}; @@ -78,7 +100,9 @@ void Texture_Init(VALUE Sketchup, VALUE Sketchup_Entity) rb_define_method(Sketchup_Texture, "average_color", Sketchup_Texture_average_color, 0); rb_define_method(Sketchup_Texture, "filename", Sketchup_Texture_filename, 0); rb_define_method(Sketchup_Texture, "image_height", Sketchup_Texture_image_height, 0); + rb_define_method(Sketchup_Texture, "height", Sketchup_Texture_height, 0); rb_define_method(Sketchup_Texture, "image_width", Sketchup_Texture_image_width, 0); + rb_define_method(Sketchup_Texture, "width", Sketchup_Texture_width, 0); rb_define_method(Sketchup_Texture, "image_rep", Sketchup_Texture_image_rep, 0); rb_define_method(Sketchup_Texture, "valid?", Sketchup_Texture_Get_valid, 0); rb_define_method(Sketchup_Texture, "write", Sketchup_Texture_write, 2); diff --git a/test/test_texture.rb b/test/test_texture.rb index 965cd25..a615c61 100644 --- a/test/test_texture.rb +++ b/test/test_texture.rb @@ -22,10 +22,18 @@ def test_image_height assert_equal(1024, @texture.image_height) end + def test_height + assert_equal(1.0, @texture.height) + end + def test_image_width assert_equal(1024, @texture.image_width) end + def test_width + assert_equal(1.0, @texture.width) + end + def test_image_rep assert_instance_of(Sketchup::ImageRep, @texture.image_rep) end