Skip to content
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ 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
* Behavior
* Component Instance
* Entities
* Materials
* Model
* Texture
* Model
7 changes: 7 additions & 0 deletions ext/not_implemented.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
13 changes: 7 additions & 6 deletions ext/not_implemented.h
Original file line number Diff line number Diff line change
@@ -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 <ruby.h>

VALUE BoundingBox_Init(VALUE namespace_object, VALUE parent_class);
Expand All @@ -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
VALUE ImageRep_Init(VALUE namespace_object, VALUE parent_class);


#endif // SCRIPTUP_NOT_IMPLEMENTED_H
1 change: 1 addition & 0 deletions ext/sketchup.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
99 changes: 99 additions & 0 deletions ext/texture.c
Original file line number Diff line number Diff line change
@@ -1,10 +1,109 @@
#include <stdbool.h>
#include <texture.h>
#include <SketchUpAPI/sketchup.h>
#include <not_implemented.h>
#include <utils.h>

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);
}
2 changes: 2 additions & 0 deletions ext/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"
Expand Down
65 changes: 65 additions & 0 deletions test/test_texture.rb
Original file line number Diff line number Diff line change
@@ -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
Loading