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 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 dae9a46..7e6e212 100644 --- a/ext/not_implemented.h +++ b/ext/not_implemented.h @@ -1,6 +1,6 @@ -#ifndef SCRIPTUP_NOT_IMPLEMENTED_H -#define SCRIPTUP_NOT_IMPLEMENTED_H - +#ifndef SCRIPTUP_NOT_IMPLEMENTED_H +#define SCRIPTUP_NOT_IMPLEMENTED_H + #include VALUE BoundingBox_Init(VALUE namespace_object, VALUE parent_class); @@ -14,6 +14,7 @@ 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); - - -#endif // SCRIPTUP_NOT_IMPLEMENTED_H \ No newline at end of file +VALUE ImageRep_Init(VALUE namespace_object, VALUE parent_class); + + +#endif // SCRIPTUP_NOT_IMPLEMENTED_H 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); diff --git a/ext/texture.c b/ext/texture.c index b6fc26a..65bb854 100644 --- a/ext/texture.c +++ b/ext/texture.c @@ -1,10 +1,109 @@ #include #include #include +#include #include +static VALUE Sketchup_Texture_average_color(VALUE self) +{ + SUTextureRef texture = {DATA_PTR(self)}; + 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 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_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)}; + 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); +} + +static VALUE Sketchup_Texture_Get_valid(VALUE self) +{ + SUTextureRef texture = {DATA_PTR(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); 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, "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/ext/utils.h b/ext/utils.h index 744af94..fdf2c4d 100644 --- a/ext/utils.h +++ b/ext/utils.h @@ -33,6 +33,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 @@ -56,6 +57,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 new file mode 100644 index 0000000..a615c61 --- /dev/null +++ b/test/test_texture.rb @@ -0,0 +1,65 @@ +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 = "#{ENV['TEST_RESOURCES']}/texture_map.png" + @texture = @material.texture + end + + def test_average_color + assert_instance_of(Sketchup::Color, @texture.average_color) + end + + def test_filename + assert_instance_of(String, @texture.filename) + end + + 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 + + def test_image_width + assert_equal(1024, @texture.image_width) + end + + def test_valid? + assert(@texture.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