Skip to content

Commit 65fd024

Browse files
AntonChaAnton Chalakovnoelwarren
authored
Implementation and tests for Texture (#20)
* implementation and tests for Drawingelement * initial texture class and tests * implement average_color, filename, image_height, image_width * add image_rep to not_implemented * add ImageRep * update sketchup.c * fix typo * texture_valid? * add write * update README * add removed check * add missing text for image_width * remove tab * add width and height --------- Co-authored-by: Anton Chalakov <anton.chalakov@chaosgroup.com> Co-authored-by: Noel <37342147+noelwarren@users.noreply.github.com>
1 parent d13024c commit 65fd024

7 files changed

Lines changed: 183 additions & 8 deletions

File tree

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ The SketchUp C API is much more limmited than the ruby api. There is no gui eve
1313
* Entity
1414
* Material
1515
* Drawingelement
16+
* Texture
1617

1718
# Partially implemented classes
1819
* Attribute Dictionaries
1920
* Behavior
2021
* Component Instance
2122
* Entities
2223
* Materials
23-
* Model
24-
* Texture
24+
* Model

ext/not_implemented.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,11 @@ VALUE Text_Init(VALUE Sketchup, VALUE DrawingElement)
7878
VALUE Sketchup_Text = rb_define_class_under(Sketchup, TEXT, DrawingElement);
7979
rb_undef_alloc_func(Sketchup_Text);
8080
return Sketchup_Text;
81+
}
82+
83+
VALUE ImageRep_Init(VALUE Sketchup, VALUE Sketchup_Object)
84+
{
85+
VALUE Sketchup_ImageRep = rb_define_class_under(Sketchup, IMAGEREP, Sketchup_Object);
86+
rb_undef_alloc_func(Sketchup_ImageRep);
87+
return Sketchup_ImageRep;
8188
}

ext/not_implemented.h

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
#ifndef SCRIPTUP_NOT_IMPLEMENTED_H
2-
#define SCRIPTUP_NOT_IMPLEMENTED_H
3-
1+
#ifndef SCRIPTUP_NOT_IMPLEMENTED_H
2+
#define SCRIPTUP_NOT_IMPLEMENTED_H
3+
44
#include <ruby.h>
55

66
VALUE BoundingBox_Init(VALUE namespace_object, VALUE parent_class);
@@ -14,6 +14,7 @@ VALUE Group_Init(VALUE namespace_object, VALUE parent_class);
1414
VALUE Image_Init(VALUE namespace_object, VALUE parent_class);
1515
VALUE SectionPlane_Init(VALUE namespace_object, VALUE parent_class);
1616
VALUE Text_Init(VALUE namespace_object, VALUE parent_class);
17-
18-
19-
#endif // SCRIPTUP_NOT_IMPLEMENTED_H
17+
VALUE ImageRep_Init(VALUE namespace_object, VALUE parent_class);
18+
19+
20+
#endif // SCRIPTUP_NOT_IMPLEMENTED_H

ext/sketchup.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ void Init_sketchup() {
7373

7474
Model_Init(Sketchup, rb_cObject);
7575
Color_Init(Sketchup, rb_cObject);
76+
ImageRep_Init(Sketchup, rb_cObject);
7677

7778
Entities_Init(Sketchup, rb_cObject);
7879
VALUE Sketchup_Entity = Entity_Init(Sketchup, rb_cObject);

ext/texture.c

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,109 @@
11
#include <stdbool.h>
22
#include <texture.h>
33
#include <SketchUpAPI/sketchup.h>
4+
#include <not_implemented.h>
45
#include <utils.h>
56

7+
static VALUE Sketchup_Texture_average_color(VALUE self)
8+
{
9+
SUTextureRef texture = {DATA_PTR(self)};
10+
VALUE c = rb_obj_alloc(rb_path2class(SKETCHUP_COLOR));
11+
SUColor* color = DATA_PTR(c);
12+
enum SUResult result = SUTextureGetAverageColor(texture, color);
13+
if (result != SU_ERROR_NONE)
14+
return Qnil;
15+
return c;
16+
}
17+
18+
static VALUE Sketchup_Texture_filename(VALUE self)
19+
{
20+
SUTextureRef texture = {DATA_PTR(self)};
21+
VALUE output;
22+
GETSTRING(SUTextureGetFileName, texture, output);
23+
return output;
24+
}
25+
26+
static VALUE Sketchup_Texture_image_height(VALUE self)
27+
{
28+
SUTextureRef texture = {DATA_PTR(self)};
29+
size_t width = 0;
30+
size_t height = 0;
31+
double s_scale = 0.0;
32+
double t_scale = 0.0;
33+
SUTextureGetDimensions(texture, &width, &height, &s_scale, &t_scale);
34+
return ULL2NUM(height);
35+
}
36+
37+
static VALUE Sketchup_Texture_height(VALUE self)
38+
{
39+
SUTextureRef texture = {DATA_PTR(self)};
40+
size_t width = 0;
41+
size_t height = 0;
42+
double s_scale = 0.0;
43+
double t_scale = 0.0;
44+
SUTextureGetDimensions(texture, &width, &height, &s_scale, &t_scale);
45+
return DBL2NUM(1.0 / t_scale);
46+
}
47+
48+
static VALUE Sketchup_Texture_width(VALUE self)
49+
{
50+
SUTextureRef texture = {DATA_PTR(self)};
51+
size_t width = 0;
52+
size_t height = 0;
53+
double s_scale = 0.0;
54+
double t_scale = 0.0;
55+
SUTextureGetDimensions(texture, &width, &height, &s_scale, &t_scale);
56+
return DBL2NUM(1.0 / s_scale);
57+
}
58+
59+
static VALUE Sketchup_Texture_image_width(VALUE self)
60+
{
61+
SUTextureRef texture = {DATA_PTR(self)};
62+
size_t width = 0;
63+
size_t height = 0;
64+
double s_scale = 0.0;
65+
double t_scale = 0.0;
66+
SUTextureGetDimensions(texture, &width, &height, &s_scale, &t_scale);
67+
return ULL2NUM(width);
68+
}
69+
70+
static VALUE Sketchup_Texture_image_rep(VALUE self)
71+
{
72+
SUTextureRef texture = {DATA_PTR(self)};
73+
SUImageRepRef image = SU_INVALID;
74+
SUTextureGetImageRep(texture, &image);
75+
return Data_Wrap_Struct(rb_path2class(SKETCHUP_IMAGEREP), 0, 0, image.ptr);
76+
}
77+
78+
static VALUE Sketchup_Texture_Get_valid(VALUE self)
79+
{
80+
SUTextureRef texture = {DATA_PTR(self)};
81+
return SUIsValid(texture) ? Qtrue : Qfalse;
82+
}
83+
84+
static VALUE Sketchup_Texture_write(VALUE self, VALUE path, VALUE colorize)
85+
{
86+
SUTextureRef texture = {DATA_PTR(self)};
87+
const char* path_ptr = StringValuePtr(path);
88+
enum SUResult result = SU_ERROR_UNSUPPORTED;
89+
if (RTEST(colorize))
90+
result = SUTextureWriteToFile(texture, path_ptr);
91+
else
92+
result = SUTextureWriteOriginalToFile(texture, path_ptr);
93+
return result != SU_ERROR_NONE ? Qfalse : Qtrue;
94+
}
95+
696
void Texture_Init(VALUE Sketchup, VALUE Sketchup_Entity)
797
{
898
VALUE Sketchup_Texture = rb_define_class_under(Sketchup, TEXTURE, Sketchup_Entity);
999
rb_undef_alloc_func(Sketchup_Texture);
100+
rb_define_method(Sketchup_Texture, "average_color", Sketchup_Texture_average_color, 0);
101+
rb_define_method(Sketchup_Texture, "filename", Sketchup_Texture_filename, 0);
102+
rb_define_method(Sketchup_Texture, "image_height", Sketchup_Texture_image_height, 0);
103+
rb_define_method(Sketchup_Texture, "height", Sketchup_Texture_height, 0);
104+
rb_define_method(Sketchup_Texture, "image_width", Sketchup_Texture_image_width, 0);
105+
rb_define_method(Sketchup_Texture, "width", Sketchup_Texture_width, 0);
106+
rb_define_method(Sketchup_Texture, "image_rep", Sketchup_Texture_image_rep, 0);
107+
rb_define_method(Sketchup_Texture, "valid?", Sketchup_Texture_Get_valid, 0);
108+
rb_define_method(Sketchup_Texture, "write", Sketchup_Texture_write, 2);
10109
}

ext/utils.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#define SECTIONPLANE "SectionPlane"
3434
#define GROUP "Group"
3535
#define TEXT "Text"
36+
#define IMAGEREP "ImageRep"
3637
#define SKETCHUP_MATERIAL SKETCHUP "::" MATERIAL
3738
#define SKETCHUP_MATERIALS SKETCHUP "::" MATERIALS
3839
#define SKETCHUP_TEXTURE SKETCHUP "::" TEXTURE
@@ -56,6 +57,7 @@
5657
#define SKETCHUP_SECTIONPLANE SKETCHUP "::" SECTIONPLANE
5758
#define SKETCHUP_GROUP SKETCHUP "::" GROUP
5859
#define SKETCHUP_TEXT SKETCHUP "::" TEXT
60+
#define SKETCHUP_IMAGEREP SKETCHUP "::" IMAGEREP
5961

6062
#pragma clang diagnostic push
6163
#pragma clang diagnostic ignored "-Wbackslash-newline-escape"

test/test_texture.rb

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
require 'minitest/autorun'
2+
require 'sketchup'
3+
4+
class TestTexture < Minitest::Test
5+
6+
def setup
7+
Sketchup.open_file("#{ENV['TEST_RESOURCES']}/Untitled.skp")
8+
@material = Sketchup.active_model.materials['Heather_Shirt']
9+
@material.texture = "#{ENV['TEST_RESOURCES']}/texture_map.png"
10+
@texture = @material.texture
11+
end
12+
13+
def test_average_color
14+
assert_instance_of(Sketchup::Color, @texture.average_color)
15+
end
16+
17+
def test_filename
18+
assert_instance_of(String, @texture.filename)
19+
end
20+
21+
def test_image_height
22+
assert_equal(1024, @texture.image_height)
23+
end
24+
25+
def test_height
26+
assert_equal(1.0, @texture.height)
27+
end
28+
29+
def test_image_width
30+
assert_equal(1024, @texture.image_width)
31+
end
32+
33+
def test_width
34+
assert_equal(1.0, @texture.width)
35+
end
36+
37+
def test_image_rep
38+
assert_instance_of(Sketchup::ImageRep, @texture.image_rep)
39+
end
40+
41+
def test_image_width
42+
assert_equal(1024, @texture.image_width)
43+
end
44+
45+
def test_valid?
46+
assert(@texture.valid?)
47+
end
48+
49+
def test_write
50+
basename = @texture.filename
51+
Dir.mktmpdir { |dir|
52+
path = File.join("#{dir}", basename)
53+
refute(File.exist?(path))
54+
@texture.write(path, true)
55+
assert(File.exist?(path))
56+
}
57+
Dir.mktmpdir { |dir|
58+
path = File.join("#{dir}", basename)
59+
refute(File.exist?(path))
60+
@texture.write(path, false)
61+
assert(File.exist?(path))
62+
}
63+
end
64+
65+
end

0 commit comments

Comments
 (0)