From ba129f362d5ea4640776a193e81ec00789207e94 Mon Sep 17 00:00:00 2001 From: Anton Chalakov Date: Thu, 7 Nov 2024 00:40:34 +0200 Subject: [PATCH 1/8] implementation and tests for Drawingelement --- ext/drawing_element.c | 127 +++++++++++++++++++++++++++++++++++ ext/entities.c | 16 +++++ ext/not_implemented.c | 81 ++++++++++++++++++++++ ext/not_implemented.h | 13 ++++ ext/sketchup.c | 14 ++++ ext/utils.h | 23 +++++++ test/test_drawing_element.rb | 55 +++++++++++++++ 7 files changed, 329 insertions(+) create mode 100644 ext/not_implemented.c create mode 100644 ext/not_implemented.h create mode 100644 test/test_drawing_element.rb diff --git a/ext/drawing_element.c b/ext/drawing_element.c index b35e725..b0aab8d 100644 --- a/ext/drawing_element.c +++ b/ext/drawing_element.c @@ -3,9 +3,136 @@ #include #include +static VALUE Sketchup_DrawingElement_bounds(VALUE self) +{ + SUDrawingElementRef drawing_element = {DATA_PTR(self)}; + struct SUBoundingBox3D bbox; + SUDrawingElementGetBoundingBox(drawing_element, &bbox); + return Data_Wrap_Struct(rb_path2class(GEOM_BOUNDINGBOX), 0, 0, &bbox); +} + +static VALUE Sketchup_DrawingElement_Get_casts_shadows(VALUE self) +{ + SUDrawingElementRef drawing_element = {DATA_PTR(self)}; + bool casts_shadows_flag = false; + SUDrawingElementGetCastsShadows(drawing_element, &casts_shadows_flag); + return casts_shadows_flag ? Qtrue : Qfalse; +} + +static VALUE Sketchup_DrawingElement_Set_casts_shadows(VALUE self, VALUE casts_shadows_flag) +{ + SUDrawingElementRef drawing_element = {DATA_PTR(self)}; + enum SUResult result = SUDrawingElementSetCastsShadows(drawing_element, casts_shadows_flag); + if(result != SU_ERROR_NONE) + return Qnil; + return casts_shadows_flag; +} + +static VALUE Sketchup_DrawingElement_Get_receives_shadows(VALUE self) +{ + SUDrawingElementRef drawing_element = {DATA_PTR(self)}; + bool receives_shadows_flag = false; + SUDrawingElementGetReceivesShadows(drawing_element, &receives_shadows_flag); + return receives_shadows_flag ? Qtrue : Qfalse; +} + +static VALUE Sketchup_DrawingElement_Set_receives_shadows(VALUE self, VALUE receives_shadows_flag) +{ + SUDrawingElementRef drawing_element = {DATA_PTR(self)}; + enum SUResult result = SUDrawingElementSetReceivesShadows(drawing_element, receives_shadows_flag); + if(result != SU_ERROR_NONE) + return Qnil; + return receives_shadows_flag; +} + +static VALUE Sketchup_DrawingElement_Get_hidden(VALUE self) +{ + SUDrawingElementRef drawing_element = {DATA_PTR(self)}; + bool hide_flag = false; + SUDrawingElementGetHidden(drawing_element, &hide_flag); + return hide_flag ? Qtrue : Qfalse; +} + +static VALUE Sketchup_DrawingElement_Set_hidden(VALUE self, VALUE hide_flag) +{ + SUDrawingElementRef drawing_element = {DATA_PTR(self)}; + enum SUResult result = SUDrawingElementSetHidden(drawing_element, hide_flag); + if(result != SU_ERROR_NONE) + return Qnil; + return hide_flag; +} + +static VALUE Sketchup_DrawingElement_Get_visible(VALUE self) +{ + SUDrawingElementRef drawing_element = {DATA_PTR(self)}; + bool hide_flag = false; + SUDrawingElementGetHidden(drawing_element, &hide_flag); + return hide_flag ? Qfalse : Qtrue; +} + +static VALUE Sketchup_DrawingElement_Set_visible(VALUE self, VALUE visible_flag) +{ + SUDrawingElementRef drawing_element = {DATA_PTR(self)}; + enum SUResult result = SUDrawingElementSetHidden(drawing_element, visible_flag); + if(result != SU_ERROR_NONE) + return Qnil; + return visible_flag; +} + +static VALUE Sketchup_DrawingElement_Get_material(VALUE self) +{ + SUDrawingElementRef drawing_element = {DATA_PTR(self)}; + SUMaterialRef material = SU_INVALID; + SUDrawingElementGetMaterial(drawing_element, &material); + return Data_Wrap_Struct(rb_path2class(SKETCHUP_MATERIAL), 0, 0, material.ptr); +} + +static VALUE Sketchup_DrawingElement_Set_material(VALUE self, VALUE material) +{ + if (!rb_obj_is_kind_of(material, rb_path2class(SKETCHUP_MATERIAL))) + rb_raise(rb_eTypeError, "Wrong type of object given"); + + SUDrawingElementRef drawing_element = {DATA_PTR(self)}; + SUMaterialRef drawing_element_material = {DATA_PTR(material)}; + SUDrawingElementSetMaterial(drawing_element, drawing_element_material); + return material; +} + +static VALUE Sketchup_DrawingElement_Get_layer(VALUE self) +{ + SUDrawingElementRef drawing_element = {DATA_PTR(self)}; + SULayerRef layer = SU_INVALID; + SUDrawingElementGetLayer(drawing_element, &layer); + return Data_Wrap_Struct(rb_path2class(SKETCHUP_LAYER), 0, 0, layer.ptr); +} + +static VALUE Sketchup_DrawingElement_Set_layer(VALUE self, VALUE layer) +{ + if (!rb_obj_is_kind_of(layer, rb_path2class(SKETCHUP_LAYER))) + rb_raise(rb_eArgError, "Wrong type of object given"); + + SUDrawingElementRef drawing_element = {DATA_PTR(self)}; + SULayerRef drawing_element_layer = {DATA_PTR(layer)}; + SUDrawingElementSetLayer(drawing_element, drawing_element_layer); + return layer; +} + VALUE DrawingElement_Init(VALUE Sketchup, VALUE Sketchup_Entity) { VALUE Sketchup_DrawingElement = rb_define_class_under(Sketchup, DRAWINGELEMENT, Sketchup_Entity); rb_undef_alloc_func(Sketchup_DrawingElement); + rb_define_method(Sketchup_DrawingElement, "bounds", Sketchup_DrawingElement_bounds, 0); + rb_define_method(Sketchup_DrawingElement, "casts_shadows?", Sketchup_DrawingElement_Get_casts_shadows, 0); + rb_define_method(Sketchup_DrawingElement, "casts_shadows=", Sketchup_DrawingElement_Set_casts_shadows, 1); + rb_define_method(Sketchup_DrawingElement, "receives_shadows?", Sketchup_DrawingElement_Get_receives_shadows, 0); + rb_define_method(Sketchup_DrawingElement, "receives_shadows=", Sketchup_DrawingElement_Set_receives_shadows, 1); + rb_define_method(Sketchup_DrawingElement, "hidden?", Sketchup_DrawingElement_Get_hidden, 0); + rb_define_method(Sketchup_DrawingElement, "hidden=", Sketchup_DrawingElement_Set_hidden, 1); + rb_define_method(Sketchup_DrawingElement, "visible?", Sketchup_DrawingElement_Get_visible, 0); + rb_define_method(Sketchup_DrawingElement, "visible=", Sketchup_DrawingElement_Set_visible, 1); + rb_define_method(Sketchup_DrawingElement, "material", Sketchup_DrawingElement_Get_material, 0); + rb_define_method(Sketchup_DrawingElement, "material=", Sketchup_DrawingElement_Set_material, 1); + rb_define_method(Sketchup_DrawingElement, "layer", Sketchup_DrawingElement_Get_layer, 0); + rb_define_method(Sketchup_DrawingElement, "layer=", Sketchup_DrawingElement_Set_layer, 1); return Sketchup_DrawingElement; } \ No newline at end of file diff --git a/ext/entities.c b/ext/entities.c index 3bcb048..7edcc2a 100644 --- a/ext/entities.c +++ b/ext/entities.c @@ -3,10 +3,26 @@ #include #include +void Sketchup_Entities_Iterator(SUFaceRef face, void* _) +{ + rb_yield(Data_Wrap_Struct(rb_path2class(SKETCHUP_FACE), 0, 0, face.ptr)); +} + +static VALUE Sketchup_Entities_each(VALUE self) +{ + SUComponentDefinitionRef component_definition = {DATA_PTR(self)}; + SUEntitiesRef entities = SU_INVALID; + SUComponentDefinitionGetEntities(component_definition, &entities); + FOREACH(SUEntitiesGetNumFaces, SUEntitiesGetFaces, SUFaceRef, entities, Sketchup_Entities_Iterator, 0); + return self; +} + VALUE Entities_Init(VALUE Sketchup, VALUE rb_cObject) { VALUE Sketchup_Entities = rb_define_class_under(Sketchup, ENTITIES, rb_cObject); rb_undef_alloc_func(Sketchup_Entities); + rb_include_module(Sketchup_Entities, rb_mEnumerable); + rb_define_method(Sketchup_Entities, "each", Sketchup_Entities_each, 0); return Sketchup_Entities; } \ No newline at end of file diff --git a/ext/not_implemented.c b/ext/not_implemented.c new file mode 100644 index 0000000..70e48e1 --- /dev/null +++ b/ext/not_implemented.c @@ -0,0 +1,81 @@ +#include +#include +#include +#include + +VALUE BoundingBox_Init(VALUE Sketchup, VALUE Sketchup_Object) +{ + VALUE Sketchup_BoundingBox = rb_define_class_under(Sketchup, BOUNDINGBOX, Sketchup_Object); + rb_undef_alloc_func(Sketchup_BoundingBox); + return Sketchup_BoundingBox; +} + +VALUE Face_Init(VALUE Sketchup, VALUE DrawingElement) +{ + VALUE Sketchup_Face = rb_define_class_under(Sketchup, FACE, DrawingElement); + rb_undef_alloc_func(Sketchup_Face); + return Sketchup_Face; +} + +VALUE Layer_Init(VALUE Sketchup, VALUE Entity) +{ + VALUE Sketchup_Layer = rb_define_class_under(Sketchup, LAYER, Entity); + rb_undef_alloc_func(Sketchup_Layer); + return Sketchup_Layer; +} + +VALUE ConstructionLine_Init(VALUE Sketchup, VALUE DrawingElement) +{ + VALUE Sketchup_ConstructionLine = rb_define_class_under(Sketchup, CONSTRUCTIONLINE, DrawingElement); + rb_undef_alloc_func(Sketchup_ConstructionLine); + return Sketchup_ConstructionLine; +} + +VALUE ConstructionPoint_Init(VALUE Sketchup, VALUE DrawingElement) +{ + VALUE Sketchup_ConstructionPoint = rb_define_class_under(Sketchup, CONSTRUCTIONPOINT, DrawingElement); + rb_undef_alloc_func(Sketchup_ConstructionPoint); + return Sketchup_ConstructionPoint; +} + +VALUE Dimension_Init(VALUE Sketchup, VALUE DrawingElement) +{ + VALUE Sketchup_Dimension = rb_define_class_under(Sketchup, DIMENSION, DrawingElement); + rb_undef_alloc_func(Sketchup_Dimension); + return Sketchup_Dimension; +} + +VALUE Edge_Init(VALUE Sketchup, VALUE DrawingElement) +{ + VALUE Sketchup_Edge = rb_define_class_under(Sketchup, EDGE, DrawingElement); + rb_undef_alloc_func(Sketchup_Edge); + return Sketchup_Edge; +} + +VALUE Group_Init(VALUE Sketchup, VALUE DrawingElement) +{ + VALUE Sketchup_Group = rb_define_class_under(Sketchup, GROUP, DrawingElement); + rb_undef_alloc_func(Sketchup_Group); + return Sketchup_Group; +} + +VALUE Image_Init(VALUE Sketchup, VALUE DrawingElement) +{ + VALUE Sketchup_Image = rb_define_class_under(Sketchup, IMAGE, DrawingElement); + rb_undef_alloc_func(Sketchup_Image); + return Sketchup_Image; +} + +VALUE SectionPlane_Init(VALUE Sketchup, VALUE DrawingElement) +{ + VALUE Sketchup_SectionPlane = rb_define_class_under(Sketchup, SECTIONPLANE, DrawingElement); + rb_undef_alloc_func(Sketchup_SectionPlane); + return Sketchup_SectionPlane; +} + +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; +} \ No newline at end of file diff --git a/ext/not_implemented.h b/ext/not_implemented.h new file mode 100644 index 0000000..2e0b5d4 --- /dev/null +++ b/ext/not_implemented.h @@ -0,0 +1,13 @@ +#include + +VALUE BoundingBox_Init(VALUE namespace_object, VALUE parent_class); +VALUE Face_Init(VALUE namespace_object, VALUE parent_class); +VALUE Layer_Init(VALUE namespace_object, VALUE parent_class); +VALUE ConstructionLine_Init(VALUE namespace_object, VALUE parent_class); +VALUE ConstructionPoint_Init(VALUE namespace_object, VALUE parent_class); +VALUE Dimension_Init(VALUE namespace_object, VALUE parent_class); +VALUE Edge_Init(VALUE namespace_object, VALUE parent_class); +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); diff --git a/ext/sketchup.c b/ext/sketchup.c index ec85aa9..f92c876 100644 --- a/ext/sketchup.c +++ b/ext/sketchup.c @@ -15,6 +15,7 @@ #include #include #include +#include #include SUModelRef active_model = SU_INVALID; @@ -82,8 +83,21 @@ void Init_sketchup() { AttributeDictionaries_Init(Sketchup, Sketchup_Entity); Texture_Init(Sketchup, Sketchup_Entity); Behavior_Init(Sketchup, Sketchup_Entity); + Layer_Init(Sketchup, Sketchup_Entity); VALUE Sketchup_DrawingElement = DrawingElement_Init(Sketchup, Sketchup_Entity); ComponentInstance_Init(Sketchup, Sketchup_DrawingElement); ComponentDefinition_Init(Sketchup, Sketchup_DrawingElement); + Face_Init(Sketchup, Sketchup_DrawingElement); + ConstructionLine_Init(Sketchup, Sketchup_DrawingElement); + ConstructionPoint_Init(Sketchup, Sketchup_DrawingElement); + Dimension_Init(Sketchup, Sketchup_DrawingElement); + Edge_Init(Sketchup, Sketchup_DrawingElement); + Group_Init(Sketchup, Sketchup_DrawingElement); + Image_Init(Sketchup, Sketchup_DrawingElement); + SectionPlane_Init(Sketchup, Sketchup_DrawingElement); + Text_Init(Sketchup, Sketchup_DrawingElement); + + VALUE Geom = rb_define_module(GEOM); + BoundingBox_Init(Geom, rb_cObject); } \ No newline at end of file diff --git a/ext/utils.h b/ext/utils.h index 51b28b7..e9b8574 100644 --- a/ext/utils.h +++ b/ext/utils.h @@ -2,6 +2,7 @@ #ifndef UTILS_H #define SKETCHUP "Sketchup" +#define GEOM "Geom" #define MODEL "Model" #define MATERIAL "Material" #define MATERIALS "Materials" @@ -16,6 +17,17 @@ #define ATTRIBUTEDICTIONARIES "AttributeDictionaries" #define DRAWINGELEMENT "DrawingElement" #define BEHAVIOR "Behavior" +#define BOUNDINGBOX "BoundingBox" +#define FACE "Face" +#define LAYER "Layer" +#define CONSTRUCTIONLINE "ConstructionLine" +#define CONSTRUCTIONPOINT "ConstructionPoint" +#define DIMENSION "Dimension" +#define EDGE "Edge" +#define IMAGE "Image" +#define SECTIONPLANE "SectionPlane" +#define GROUP "Group" +#define TEXT "Text" #define SKETCHUP_MATERIAL SKETCHUP "::" MATERIAL #define SKETCHUP_MATERIALS SKETCHUP "::" MATERIALS #define SKETCHUP_TEXTURE SKETCHUP "::" TEXTURE @@ -28,6 +40,17 @@ #define SKETCHUP_ATTRIBUTEDICTIONARIES SKETCHUP "::" ATTRIBUTEDICTIONARIES #define SKETCHUP_DEFINITIONLIST SKETCHUP "::" DEFINITIONLIST #define SKETCHUP_BEHAVIOR SKETCHUP "::" BEHAVIOR +#define GEOM_BOUNDINGBOX GEOM "::" BOUNDINGBOX +#define SKETCHUP_FACE SKETCHUP "::" FACE +#define SKETCHUP_LAYER SKETCHUP "::" LAYER +#define SKETCHUP_CONSTRUCTIONLINE SKETCHUP "::" CONSTRUCTIONLINE +#define SKETCHUP_CONSTRUCTIONPOINT SKETCHUP "::" CONSTRUCTIONPOINT +#define SKETCHUP_DIMENSION SKETCHUP "::" DIMENSION +#define SKETCHUP_EDGER SKETCHUP "::" EDGE +#define SKETCHUP_IMAGE SKETCHUP "::" IMAGE +#define SKETCHUP_SECTIONPLANE SKETCHUP "::" SECTIONPLANE +#define SKETCHUP_GROUP SKETCHUP "::" GROUP +#define SKETCHUP_TEXT SKETCHUP "::" TEXT #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wbackslash-newline-escape" diff --git a/test/test_drawing_element.rb b/test/test_drawing_element.rb new file mode 100644 index 0000000..c9faea4 --- /dev/null +++ b/test/test_drawing_element.rb @@ -0,0 +1,55 @@ +require 'minitest/autorun' +require 'sketchup' + +class TestDrawingElement < Minitest::Test + + def setup + Sketchup.open_file("#{__dir__}/../test-resources/Untitled.skp") + @drawing_element = Sketchup.active_model.definitions.find { |d| d.name == 'Heather' } + end + + def test_object + assert_kind_of(Sketchup::DrawingElement, @drawing_element) + end + + def test_bounds + assert_kind_of(Geom::BoundingBox, @drawing_element.bounds) + end + + def test_cast_shadows + assert(@drawing_element.entities.grep(Sketchup::Face).first.casts_shadows = true) + assert(@drawing_element.entities.grep(Sketchup::Face).first.casts_shadows?) + refute(@drawing_element.entities.grep(Sketchup::Face).first.casts_shadows = false) + refute(@drawing_element.entities.grep(Sketchup::Face).first.casts_shadows?) + end + + def test_hidden + refute(@drawing_element.hidden?) + assert(@drawing_element.hidden = true) + end + + def test_layer + assert_kind_of(Sketchup::Layer, @drawing_element.layer) + assert_raises(ArgumentError) { + @drawing_element.layer = Object.new + } + end + + def test_material + assert_kind_of(Sketchup::Material, @drawing_element.material) + assert_raises(TypeError) { + @drawing_element.material = Object.new + } + end + + def test_receives_shadows + assert(@drawing_element.receives_shadows?) + assert(@drawing_element.receives_shadows = true) + end + + def test_visible + assert(@drawing_element.visible?) + refute(@drawing_element.visible = false) + end + +end From 5f482fb2ae84eb62241092e81ea1ad34a4859e46 Mon Sep 17 00:00:00 2001 From: Anton Chalakov Date: Thu, 7 Nov 2024 00:57:34 +0200 Subject: [PATCH 2/8] update entities each loop --- ext/entities.c | 22 ++++++++++++++++++++-- ext/sketchup.c | 2 +- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/ext/entities.c b/ext/entities.c index 7edcc2a..94c6d06 100644 --- a/ext/entities.c +++ b/ext/entities.c @@ -10,9 +10,27 @@ void Sketchup_Entities_Iterator(SUFaceRef face, void* _) static VALUE Sketchup_Entities_each(VALUE self) { - SUComponentDefinitionRef component_definition = {DATA_PTR(self)}; SUEntitiesRef entities = SU_INVALID; - SUComponentDefinitionGetEntities(component_definition, &entities); + if (rb_obj_is_kind_of(self, rb_path2class(SKETCHUP_MODEL))) + { + SUModelRef model = {DATA_PTR(self)}; + SUModelGetEntities(model, &entities); + } + else if (rb_obj_is_kind_of(self, rb_path2class(SKETCHUP_GROUP))) + { + SUGroupRef group = {DATA_PTR(self)}; + SUGroupGetEntities(group, &entities); + } + else if (rb_obj_is_kind_of(self, rb_path2class(SKETCHUP_COMPONENTDEFINITION))) + { + SUComponentDefinitionRef component_definition = {DATA_PTR(self)}; + SUComponentDefinitionGetEntities(component_definition, &entities); + } + if SUIsInvalid(entities) + return Qnil; + + // This is only used for the current tests + // ToDo implement foreach for all possible types FOREACH(SUEntitiesGetNumFaces, SUEntitiesGetFaces, SUFaceRef, entities, Sketchup_Entities_Iterator, 0); return self; } diff --git a/ext/sketchup.c b/ext/sketchup.c index f92c876..5e2461e 100644 --- a/ext/sketchup.c +++ b/ext/sketchup.c @@ -84,7 +84,7 @@ void Init_sketchup() { Texture_Init(Sketchup, Sketchup_Entity); Behavior_Init(Sketchup, Sketchup_Entity); Layer_Init(Sketchup, Sketchup_Entity); - + VALUE Sketchup_DrawingElement = DrawingElement_Init(Sketchup, Sketchup_Entity); ComponentInstance_Init(Sketchup, Sketchup_DrawingElement); ComponentDefinition_Init(Sketchup, Sketchup_DrawingElement); From 06f3c7e8dea4641aa162562e53d157d58d7c0348 Mon Sep 17 00:00:00 2001 From: Anton Chalakov Date: Thu, 7 Nov 2024 01:00:07 +0200 Subject: [PATCH 3/8] update README file --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 0df90c5..fa65459 100644 --- a/README.md +++ b/README.md @@ -12,12 +12,12 @@ The SketchUp C API is much more limmited than the ruby api. There is no gui eve * Definition List * Entity * Material +* Drawingelement # Partially implemented classes * Attribute Dictionaries * Behavior * Component Instance -* Drawing Element * Entities * Materials * Model From db6d41c6abd75c9f58aebb8bc898bd2d68c91018 Mon Sep 17 00:00:00 2001 From: Anton Chalakov Date: Thu, 7 Nov 2024 01:14:11 +0200 Subject: [PATCH 4/8] fix each loop from the entities --- ext/entities.c | 22 +++------------------- 1 file changed, 3 insertions(+), 19 deletions(-) diff --git a/ext/entities.c b/ext/entities.c index 94c6d06..d4a148e 100644 --- a/ext/entities.c +++ b/ext/entities.c @@ -10,27 +10,11 @@ void Sketchup_Entities_Iterator(SUFaceRef face, void* _) static VALUE Sketchup_Entities_each(VALUE self) { - SUEntitiesRef entities = SU_INVALID; - if (rb_obj_is_kind_of(self, rb_path2class(SKETCHUP_MODEL))) - { - SUModelRef model = {DATA_PTR(self)}; - SUModelGetEntities(model, &entities); - } - else if (rb_obj_is_kind_of(self, rb_path2class(SKETCHUP_GROUP))) - { - SUGroupRef group = {DATA_PTR(self)}; - SUGroupGetEntities(group, &entities); - } - else if (rb_obj_is_kind_of(self, rb_path2class(SKETCHUP_COMPONENTDEFINITION))) - { - SUComponentDefinitionRef component_definition = {DATA_PTR(self)}; - SUComponentDefinitionGetEntities(component_definition, &entities); - } - if SUIsInvalid(entities) - return Qnil; - // This is only used for the current tests // ToDo implement foreach for all possible types + SUComponentDefinitionRef component_definition = {DATA_PTR(self)}; + SUEntitiesRef entities = SU_INVALID; + SUComponentDefinitionGetEntities(component_definition, &entities); FOREACH(SUEntitiesGetNumFaces, SUEntitiesGetFaces, SUFaceRef, entities, Sketchup_Entities_Iterator, 0); return self; } From 442f3ce4ef87d824acd7603bed2322d210b1afc4 Mon Sep 17 00:00:00 2001 From: Anton Chalakov Date: Thu, 7 Nov 2024 01:16:47 +0200 Subject: [PATCH 5/8] fix path to file opened for tests --- test/test_drawing_element.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test_drawing_element.rb b/test/test_drawing_element.rb index c9faea4..4f4f9c9 100644 --- a/test/test_drawing_element.rb +++ b/test/test_drawing_element.rb @@ -4,7 +4,7 @@ class TestDrawingElement < Minitest::Test def setup - Sketchup.open_file("#{__dir__}/../test-resources/Untitled.skp") + Sketchup.open_file("#{ENV['TEST_RESOURCES']}/Untitled.skp") @drawing_element = Sketchup.active_model.definitions.find { |d| d.name == 'Heather' } end From 57d176c12820e2f01b9cbc2c8f79bc15860fad35 Mon Sep 17 00:00:00 2001 From: Anton Chalakov Date: Thu, 7 Nov 2024 19:13:13 +0200 Subject: [PATCH 6/8] undef TEXT --- ext/utils.h | 3 +++ 1 file changed, 3 insertions(+) diff --git a/ext/utils.h b/ext/utils.h index e9b8574..a576c11 100644 --- a/ext/utils.h +++ b/ext/utils.h @@ -1,6 +1,9 @@ #include #ifndef UTILS_H +#ifdef TEXT +#undef TEXT +#endif //TEXT #define SKETCHUP "Sketchup" #define GEOM "Geom" #define MODEL "Model" From 71ae398e61352c6ebca503da2f1509410309cd0e Mon Sep 17 00:00:00 2001 From: Anton Chalakov Date: Mon, 16 Dec 2024 07:43:21 +0200 Subject: [PATCH 7/8] changes based on pull request comments --- ext/drawing_element.c | 36 +++++++++++++++++++++++++++--------- ext/entities.c | 7 +------ ext/not_implemented.c | 4 ++-- ext/sketchup.c | 8 ++++---- test/test_drawing_element.rb | 4 +++- 5 files changed, 37 insertions(+), 22 deletions(-) diff --git a/ext/drawing_element.c b/ext/drawing_element.c index b0aab8d..bd5d95c 100644 --- a/ext/drawing_element.c +++ b/ext/drawing_element.c @@ -1,14 +1,15 @@ #include #include +#include #include #include static VALUE Sketchup_DrawingElement_bounds(VALUE self) { SUDrawingElementRef drawing_element = {DATA_PTR(self)}; - struct SUBoundingBox3D bbox; - SUDrawingElementGetBoundingBox(drawing_element, &bbox); - return Data_Wrap_Struct(rb_path2class(GEOM_BOUNDINGBOX), 0, 0, &bbox); + struct SUBoundingBox3D* bbox; + SUDrawingElementGetBoundingBox(drawing_element, bbox); + return Data_Make_Struct(rb_path2class(GEOM_BOUNDINGBOX), struct SUBoundingBox3D, 0, RUBY_DEFAULT_FREE, bbox); } static VALUE Sketchup_DrawingElement_Get_casts_shadows(VALUE self) @@ -22,7 +23,7 @@ static VALUE Sketchup_DrawingElement_Get_casts_shadows(VALUE self) static VALUE Sketchup_DrawingElement_Set_casts_shadows(VALUE self, VALUE casts_shadows_flag) { SUDrawingElementRef drawing_element = {DATA_PTR(self)}; - enum SUResult result = SUDrawingElementSetCastsShadows(drawing_element, casts_shadows_flag); + enum SUResult result = SUDrawingElementSetCastsShadows(drawing_element, RTEST(casts_shadows_flag)); if(result != SU_ERROR_NONE) return Qnil; return casts_shadows_flag; @@ -39,7 +40,7 @@ static VALUE Sketchup_DrawingElement_Get_receives_shadows(VALUE self) static VALUE Sketchup_DrawingElement_Set_receives_shadows(VALUE self, VALUE receives_shadows_flag) { SUDrawingElementRef drawing_element = {DATA_PTR(self)}; - enum SUResult result = SUDrawingElementSetReceivesShadows(drawing_element, receives_shadows_flag); + enum SUResult result = SUDrawingElementSetReceivesShadows(drawing_element, RTEST(receives_shadows_flag)); if(result != SU_ERROR_NONE) return Qnil; return receives_shadows_flag; @@ -56,7 +57,7 @@ static VALUE Sketchup_DrawingElement_Get_hidden(VALUE self) static VALUE Sketchup_DrawingElement_Set_hidden(VALUE self, VALUE hide_flag) { SUDrawingElementRef drawing_element = {DATA_PTR(self)}; - enum SUResult result = SUDrawingElementSetHidden(drawing_element, hide_flag); + enum SUResult result = SUDrawingElementSetHidden(drawing_element, RTEST(hide_flag)); if(result != SU_ERROR_NONE) return Qnil; return hide_flag; @@ -73,7 +74,7 @@ static VALUE Sketchup_DrawingElement_Get_visible(VALUE self) static VALUE Sketchup_DrawingElement_Set_visible(VALUE self, VALUE visible_flag) { SUDrawingElementRef drawing_element = {DATA_PTR(self)}; - enum SUResult result = SUDrawingElementSetHidden(drawing_element, visible_flag); + enum SUResult result = SUDrawingElementSetHidden(drawing_element, RTEST(visible_flag)); if(result != SU_ERROR_NONE) return Qnil; return visible_flag; @@ -84,16 +85,24 @@ 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); } static VALUE Sketchup_DrawingElement_Set_material(VALUE self, VALUE material) { + SUDrawingElementRef drawing_element = {DATA_PTR(self)}; + SUMaterialRef drawing_element_material = SU_INVALID; + if (material == Qnil) + { + SUDrawingElementSetMaterial(drawing_element, drawing_element_material); + return Qnil; + } if (!rb_obj_is_kind_of(material, rb_path2class(SKETCHUP_MATERIAL))) rb_raise(rb_eTypeError, "Wrong type of object given"); - SUDrawingElementRef drawing_element = {DATA_PTR(self)}; - SUMaterialRef drawing_element_material = {DATA_PTR(material)}; + drawing_element_material.ptr = DATA_PTR(material); SUDrawingElementSetMaterial(drawing_element, drawing_element_material); return material; } @@ -108,6 +117,15 @@ static VALUE Sketchup_DrawingElement_Get_layer(VALUE self) static VALUE Sketchup_DrawingElement_Set_layer(VALUE self, VALUE layer) { + if (rb_type(layer) == T_NIL) + { + SUDrawingElementRef drawing_element = {DATA_PTR(self)}; + SULayerRef drawing_element_layer = SU_INVALID; + enum SUResult result = SUDrawingElementSetLayer(drawing_element, drawing_element_layer); + if (result != SU_ERROR_NONE) + return Qfalse; + return Qnil; + } if (!rb_obj_is_kind_of(layer, rb_path2class(SKETCHUP_LAYER))) rb_raise(rb_eArgError, "Wrong type of object given"); diff --git a/ext/entities.c b/ext/entities.c index d4a148e..d17fed4 100644 --- a/ext/entities.c +++ b/ext/entities.c @@ -10,16 +10,11 @@ void Sketchup_Entities_Iterator(SUFaceRef face, void* _) static VALUE Sketchup_Entities_each(VALUE self) { - // This is only used for the current tests - // ToDo implement foreach for all possible types - SUComponentDefinitionRef component_definition = {DATA_PTR(self)}; - SUEntitiesRef entities = SU_INVALID; - SUComponentDefinitionGetEntities(component_definition, &entities); + SUEntitiesRef entities = {DATA_PTR(self)}; FOREACH(SUEntitiesGetNumFaces, SUEntitiesGetFaces, SUFaceRef, entities, Sketchup_Entities_Iterator, 0); return self; } - VALUE Entities_Init(VALUE Sketchup, VALUE rb_cObject) { VALUE Sketchup_Entities = rb_define_class_under(Sketchup, ENTITIES, rb_cObject); diff --git a/ext/not_implemented.c b/ext/not_implemented.c index 70e48e1..09063dd 100644 --- a/ext/not_implemented.c +++ b/ext/not_implemented.c @@ -3,9 +3,9 @@ #include #include -VALUE BoundingBox_Init(VALUE Sketchup, VALUE Sketchup_Object) +VALUE BoundingBox_Init(VALUE Geom, VALUE Sketchup_Object) { - VALUE Sketchup_BoundingBox = rb_define_class_under(Sketchup, BOUNDINGBOX, Sketchup_Object); + VALUE Sketchup_BoundingBox = rb_define_class_under(Geom, BOUNDINGBOX, Sketchup_Object); rb_undef_alloc_func(Sketchup_BoundingBox); return Sketchup_BoundingBox; } diff --git a/ext/sketchup.c b/ext/sketchup.c index 5e2461e..5863920 100644 --- a/ext/sketchup.c +++ b/ext/sketchup.c @@ -84,6 +84,9 @@ void Init_sketchup() { Texture_Init(Sketchup, Sketchup_Entity); Behavior_Init(Sketchup, Sketchup_Entity); Layer_Init(Sketchup, Sketchup_Entity); + + VALUE Geom = rb_define_module(GEOM); + BoundingBox_Init(Geom, rb_cObject); VALUE Sketchup_DrawingElement = DrawingElement_Init(Sketchup, Sketchup_Entity); ComponentInstance_Init(Sketchup, Sketchup_DrawingElement); @@ -96,8 +99,5 @@ void Init_sketchup() { Group_Init(Sketchup, Sketchup_DrawingElement); Image_Init(Sketchup, Sketchup_DrawingElement); SectionPlane_Init(Sketchup, Sketchup_DrawingElement); - Text_Init(Sketchup, Sketchup_DrawingElement); - - VALUE Geom = rb_define_module(GEOM); - BoundingBox_Init(Geom, rb_cObject); + Text_Init(Sketchup, Sketchup_DrawingElement); } \ No newline at end of file diff --git a/test/test_drawing_element.rb b/test/test_drawing_element.rb index 4f4f9c9..4bcead6 100644 --- a/test/test_drawing_element.rb +++ b/test/test_drawing_element.rb @@ -30,13 +30,15 @@ def test_hidden def test_layer assert_kind_of(Sketchup::Layer, @drawing_element.layer) + assert_nil(@drawing_element.layer = nil) assert_raises(ArgumentError) { @drawing_element.layer = Object.new } end def test_material - assert_kind_of(Sketchup::Material, @drawing_element.material) + assert_nil(@drawing_element.material) + assert_nil(@drawing_element.material = nil) assert_raises(TypeError) { @drawing_element.material = Object.new } From 5f525a1be16fa4d98325629d66710d7c84c31f61 Mon Sep 17 00:00:00 2001 From: Anton Chalakov Date: Tue, 1 Apr 2025 13:23:37 +0300 Subject: [PATCH 8/8] fixes based on pull request comments --- ext/drawing_element.c | 30 ++++++++++-------------------- ext/not_implemented.c | 6 +++--- 2 files changed, 13 insertions(+), 23 deletions(-) diff --git a/ext/drawing_element.c b/ext/drawing_element.c index bd5d95c..f50b8c5 100644 --- a/ext/drawing_element.c +++ b/ext/drawing_element.c @@ -7,7 +7,7 @@ static VALUE Sketchup_DrawingElement_bounds(VALUE self) { SUDrawingElementRef drawing_element = {DATA_PTR(self)}; - struct SUBoundingBox3D* bbox; + struct SUBoundingBox3D* bbox = 0; SUDrawingElementGetBoundingBox(drawing_element, bbox); return Data_Make_Struct(rb_path2class(GEOM_BOUNDINGBOX), struct SUBoundingBox3D, 0, RUBY_DEFAULT_FREE, bbox); } @@ -23,9 +23,7 @@ static VALUE Sketchup_DrawingElement_Get_casts_shadows(VALUE self) static VALUE Sketchup_DrawingElement_Set_casts_shadows(VALUE self, VALUE casts_shadows_flag) { SUDrawingElementRef drawing_element = {DATA_PTR(self)}; - enum SUResult result = SUDrawingElementSetCastsShadows(drawing_element, RTEST(casts_shadows_flag)); - if(result != SU_ERROR_NONE) - return Qnil; + SUDrawingElementSetCastsShadows(drawing_element, RTEST(casts_shadows_flag)); return casts_shadows_flag; } @@ -40,9 +38,7 @@ static VALUE Sketchup_DrawingElement_Get_receives_shadows(VALUE self) static VALUE Sketchup_DrawingElement_Set_receives_shadows(VALUE self, VALUE receives_shadows_flag) { SUDrawingElementRef drawing_element = {DATA_PTR(self)}; - enum SUResult result = SUDrawingElementSetReceivesShadows(drawing_element, RTEST(receives_shadows_flag)); - if(result != SU_ERROR_NONE) - return Qnil; + SUDrawingElementSetReceivesShadows(drawing_element, RTEST(receives_shadows_flag)); return receives_shadows_flag; } @@ -57,9 +53,7 @@ static VALUE Sketchup_DrawingElement_Get_hidden(VALUE self) static VALUE Sketchup_DrawingElement_Set_hidden(VALUE self, VALUE hide_flag) { SUDrawingElementRef drawing_element = {DATA_PTR(self)}; - enum SUResult result = SUDrawingElementSetHidden(drawing_element, RTEST(hide_flag)); - if(result != SU_ERROR_NONE) - return Qnil; + SUDrawingElementSetHidden(drawing_element, RTEST(hide_flag)); return hide_flag; } @@ -74,9 +68,7 @@ static VALUE Sketchup_DrawingElement_Get_visible(VALUE self) static VALUE Sketchup_DrawingElement_Set_visible(VALUE self, VALUE visible_flag) { SUDrawingElementRef drawing_element = {DATA_PTR(self)}; - enum SUResult result = SUDrawingElementSetHidden(drawing_element, RTEST(visible_flag)); - if(result != SU_ERROR_NONE) - return Qnil; + SUDrawingElementSetHidden(drawing_element, RTEST(visible_flag)); return visible_flag; } @@ -93,16 +85,16 @@ static VALUE Sketchup_DrawingElement_Get_material(VALUE self) static VALUE Sketchup_DrawingElement_Set_material(VALUE self, VALUE material) { SUDrawingElementRef drawing_element = {DATA_PTR(self)}; - SUMaterialRef drawing_element_material = SU_INVALID; - if (material == Qnil) + if (rb_type(material) == T_NIL) { - SUDrawingElementSetMaterial(drawing_element, drawing_element_material); + SUMaterialRef empty_material = SU_INVALID; + SUDrawingElementSetMaterial(drawing_element, empty_material); return Qnil; } if (!rb_obj_is_kind_of(material, rb_path2class(SKETCHUP_MATERIAL))) rb_raise(rb_eTypeError, "Wrong type of object given"); - drawing_element_material.ptr = DATA_PTR(material); + SUMaterialRef drawing_element_material = {DATA_PTR(material)}; SUDrawingElementSetMaterial(drawing_element, drawing_element_material); return material; } @@ -121,9 +113,7 @@ static VALUE Sketchup_DrawingElement_Set_layer(VALUE self, VALUE layer) { SUDrawingElementRef drawing_element = {DATA_PTR(self)}; SULayerRef drawing_element_layer = SU_INVALID; - enum SUResult result = SUDrawingElementSetLayer(drawing_element, drawing_element_layer); - if (result != SU_ERROR_NONE) - return Qfalse; + SUDrawingElementSetLayer(drawing_element, drawing_element_layer); return Qnil; } if (!rb_obj_is_kind_of(layer, rb_path2class(SKETCHUP_LAYER))) diff --git a/ext/not_implemented.c b/ext/not_implemented.c index 09063dd..4b675f9 100644 --- a/ext/not_implemented.c +++ b/ext/not_implemented.c @@ -5,9 +5,9 @@ VALUE BoundingBox_Init(VALUE Geom, VALUE Sketchup_Object) { - VALUE Sketchup_BoundingBox = rb_define_class_under(Geom, BOUNDINGBOX, Sketchup_Object); - rb_undef_alloc_func(Sketchup_BoundingBox); - return Sketchup_BoundingBox; + VALUE Geom_BoundingBox = rb_define_class_under(Geom, BOUNDINGBOX, Sketchup_Object); + rb_undef_alloc_func(Geom_BoundingBox); + return Geom_BoundingBox; } VALUE Face_Init(VALUE Sketchup, VALUE DrawingElement)