From 5d26b27f72a0a8c32005ac9d45b70d5c21dd8782 Mon Sep 17 00:00:00 2001 From: moethu Date: Sun, 30 Aug 2020 11:16:20 +0200 Subject: [PATCH] #19 attempt to write scenes --- SketchUpNET.Unittest/BasicTests.cs | 18 ++++++++++++++++++ SketchUpNET/Component.cpp | 2 +- SketchUpNET/Curve.cpp | 21 ++++++++++++++------- SketchUpNET/Edge.cpp | 23 ++++++++++++++++------- SketchUpNET/Group.cpp | 20 ++++++++++++-------- SketchUpNET/Instance.cpp | 16 ++++++++++------ SketchUpNET/Scene.cpp | 30 +++++++++++++++++++++++++----- SketchUpNET/SketchUpNET.cpp | 13 +++++++++---- SketchUpNET/Surface.cpp | 24 +++++++++++++++--------- SketchUpNET/Utilities.cpp | 5 +++++ 10 files changed, 125 insertions(+), 47 deletions(-) diff --git a/SketchUpNET.Unittest/BasicTests.cs b/SketchUpNET.Unittest/BasicTests.cs index 0b6c5bf..d5a7e13 100644 --- a/SketchUpNET.Unittest/BasicTests.cs +++ b/SketchUpNET.Unittest/BasicTests.cs @@ -87,6 +87,24 @@ public void TestGetScene() } } + /// + /// Test writing and loading scenes + /// + [TestMethod] + public void TestWriteScene() + { + SketchUpNET.SketchUp skp = new SketchUp(); + skp.LoadModel(TestFile, false); + var edge = new Edge(new Vertex(0, 0, 0), new Vertex(10, 10, 10), skp.Layers[0].Name); + skp.Edges.Add(edge); + var scene = new Scene("3", new System.Collections.Generic.List() { edge }); + skp.Scenes.Add(scene); + skp.WriteNewModel("tmp.skp"); + + skp = new SketchUp(); + skp.LoadModel("tmp.skp", false); + } + /// /// Test getting Materials from model /// diff --git a/SketchUpNET/Component.cpp b/SketchUpNET/Component.cpp index 34c41d8..72a91c0 100644 --- a/SketchUpNET/Component.cpp +++ b/SketchUpNET/Component.cpp @@ -74,7 +74,7 @@ namespace SketchUpNET Component(){}; internal: - static Component^ FromSU(SUComponentDefinitionRef comp, bool includeMeshes, System::Collections::Generic::Dictionary^ materials, System::Collections::Generic::Dictionary^ entitycontainer) + static Component^ FromSU(SUComponentDefinitionRef comp, bool includeMeshes, System::Collections::Generic::Dictionary^ materials, System::Collections::Generic::Dictionary^ entitycontainer) { SUStringRef name = SU_INVALID; SUStringCreate(&name); diff --git a/SketchUpNET/Curve.cpp b/SketchUpNET/Curve.cpp index 94f0ced..6f408b3 100644 --- a/SketchUpNET/Curve.cpp +++ b/SketchUpNET/Curve.cpp @@ -41,23 +41,26 @@ using namespace System::Collections::Generic; namespace SketchUpNET { - public ref class Curve + public ref class Curve : IEntity { public: List^ Edges = gcnew List(); bool isArc; - Int32 ID; + Int64 ID; - Curve(Int32 id, List^ edges, bool isarc) + Curve(List^ edges, bool isarc) { this->Edges = edges; this->isArc = isarc; - this->ID = id; }; Curve(){}; + virtual Int64 GetID() { + return this->ID; + } + internal: static Curve^ FromSU(SUCurveRef curve) @@ -84,8 +87,8 @@ namespace SketchUpNET if (type == SUCurveType::SUCurveType_Arc) isArc = true; - Curve^ v = gcnew Curve(id, edgelist, isArc); - + Curve^ v = gcnew Curve(edgelist, isArc); + v->ID = id; return v; }; @@ -101,6 +104,10 @@ namespace SketchUpNET edges[i] = this->Edges[i]->ToSU(); } SUCurveCreateWithEdges(&curve, edges, size); + SUEntityRef e = SUCurveToEntity(curve); + int32_t id = -1; + SUEntityGetID(e, &id); + this->ID = id; return curve; } @@ -115,7 +122,7 @@ namespace SketchUpNET return result; } - static List^ GetEntityCurves(SUEntitiesRef entities, System::Collections::Generic::Dictionary^ entitycontainer) + static List^ GetEntityCurves(SUEntitiesRef entities, System::Collections::Generic::Dictionary^ entitycontainer) { List^ curves = gcnew List(); diff --git a/SketchUpNET/Edge.cpp b/SketchUpNET/Edge.cpp index b07af5e..d2d0a74 100644 --- a/SketchUpNET/Edge.cpp +++ b/SketchUpNET/Edge.cpp @@ -44,24 +44,28 @@ using namespace System::Collections::Generic; namespace SketchUpNET { - public ref class Edge + public ref class Edge : IEntity { public: Vertex^ Start; Vertex^ End; System::String^ Layer; - Int32 ID; + Int64 ID; - Edge(Int32 id, Vertex ^ start, Vertex ^ end, System::String^ layer) + Edge(Vertex ^ start, Vertex ^ end, System::String^ layer) { this->Start = start; this->End = end; this->Layer = layer; - this->ID = id; }; Edge(){}; + + virtual Int64 GetID() { + return this->ID; + } + internal: static Edge^ FromSU(SUEdgeRef edge) { @@ -85,8 +89,8 @@ namespace SketchUpNET layername = SketchUpNET::Utilities::GetLayerName(layer); } - Edge^ v = gcnew Edge(id, Vertex::FromSU(start), Vertex::FromSU(end), layername); - + Edge^ v = gcnew Edge(Vertex::FromSU(start), Vertex::FromSU(end), layername); + v->ID = id; return v; }; @@ -96,6 +100,11 @@ namespace SketchUpNET SUPoint3D start = this->Start->ToSU(); SUPoint3D end = this->End->ToSU(); SUEdgeCreate(&edge,&start,&end); + SUEntityRef e = SUEdgeToEntity(edge); + int32_t id = -1; + SUEntityGetID(e, &id); + Console::WriteLine(id); + this->ID = id; return edge; } @@ -110,7 +119,7 @@ namespace SketchUpNET return result; } - static List^ GetEntityEdges(SUEntitiesRef entities,System::Collections::Generic::Dictionary^ entitycontainer) + static List^ GetEntityEdges(SUEntitiesRef entities,System::Collections::Generic::Dictionary^ entitycontainer) { List^ edges = gcnew List(); diff --git a/SketchUpNET/Group.cpp b/SketchUpNET/Group.cpp index 88004b5..c8acf58 100644 --- a/SketchUpNET/Group.cpp +++ b/SketchUpNET/Group.cpp @@ -47,7 +47,7 @@ using namespace System::Collections::Generic; namespace SketchUpNET { - public ref class Group + public ref class Group : IEntity { public: System::String^ Name; @@ -55,7 +55,7 @@ namespace SketchUpNET /// /// Surfaces /// - Int32 ID; + Int64 ID; List^ Surfaces; List^ Edges; List^ Curves; @@ -64,7 +64,7 @@ namespace SketchUpNET Transform^ Transformation; System::String^ Layer; - Group(Int32 id, System::String^ name, List^ surfaces, List^ curves, List^ edges, List^ insts, List^ group, Transform^ transformation, System::String^ layername) + Group(System::String^ name, List^ surfaces, List^ curves, List^ edges, List^ insts, List^ group, Transform^ transformation, System::String^ layername) { this->Name = name; this->Surfaces = surfaces; @@ -74,12 +74,16 @@ namespace SketchUpNET this->Groups = group; this->Transformation = transformation; this->Layer = layername; - this->ID = id; }; Group(){}; + + virtual Int64 GetID() { + return this->ID; + } + internal: - static Group^ FromSU(SUGroupRef group, bool includeMeshes, System::Collections::Generic::Dictionary^ materials, System::Collections::Generic::Dictionary^ entitycontainer) + static Group^ FromSU(SUGroupRef group, bool includeMeshes, System::Collections::Generic::Dictionary^ materials, System::Collections::Generic::Dictionary^ entitycontainer) { SUStringRef name = SU_INVALID; SUStringCreate(&name); @@ -112,12 +116,12 @@ namespace SketchUpNET } int32_t id = -1; SUEntityGetID(SUGroupToEntity(group), &id); - Group^ v = gcnew Group(id, SketchUpNET::Utilities::GetString(name), surfaces, curves, edges, inst, grps, Transform::FromSU(transform), layername); - + Group^ v = gcnew Group(SketchUpNET::Utilities::GetString(name), surfaces, curves, edges, inst, grps, Transform::FromSU(transform), layername); + v->ID = id; return v; }; - static List^ GetEntityGroups(SUEntitiesRef entities, bool includeMeshes, System::Collections::Generic::Dictionary^ materials, System::Collections::Generic::Dictionary^ entitycontainer) + static List^ GetEntityGroups(SUEntitiesRef entities, bool includeMeshes, System::Collections::Generic::Dictionary^ materials, System::Collections::Generic::Dictionary^ entitycontainer) { List^ groups = gcnew List(); diff --git a/SketchUpNET/Instance.cpp b/SketchUpNET/Instance.cpp index 157d561..b7740c3 100644 --- a/SketchUpNET/Instance.cpp +++ b/SketchUpNET/Instance.cpp @@ -47,7 +47,7 @@ using namespace System::Collections::Generic; namespace SketchUpNET { - public ref class Instance + public ref class Instance : IEntity { public: System::String^ Name; @@ -58,18 +58,22 @@ namespace SketchUpNET System::String^ Layer; Int32 ID; - Instance(Int32 id, System::String^ name, System::String^ guid, String^ parent, Transform^ transformation, System::String^ layername) + Instance(System::String^ name, System::String^ guid, String^ parent, Transform^ transformation, System::String^ layername) { this->Name = name; this->Transformation = transformation; this->ParentID = parent; this->Guid = guid; this->Layer = layername; - this->ID = id; }; Instance(){}; + + virtual Int64 GetID() { + return this->ID; + } + internal: static Instance^ FromSU(SUComponentInstanceRef comp) { @@ -110,11 +114,11 @@ namespace SketchUpNET int32_t id = -1; SUEntityGetID(SUComponentInstanceToEntity(comp), &id); - Instance^ v = gcnew Instance(id, SketchUpNET::Utilities::GetString(name), SketchUpNET::Utilities::GetString(instanceguid), parent, Transform::FromSU(transform), layername); - + Instance^ v = gcnew Instance(SketchUpNET::Utilities::GetString(name), SketchUpNET::Utilities::GetString(instanceguid), parent, Transform::FromSU(transform), layername); + v->ID = id; return v; }; - static List^ GetEntityInstances(SUEntitiesRef entities, System::Collections::Generic::Dictionary^ entitycontainer) + static List^ GetEntityInstances(SUEntitiesRef entities, System::Collections::Generic::Dictionary^ entitycontainer) { List^ instancelist = gcnew List(); diff --git a/SketchUpNET/Scene.cpp b/SketchUpNET/Scene.cpp index 44323c6..bfaa424 100644 --- a/SketchUpNET/Scene.cpp +++ b/SketchUpNET/Scene.cpp @@ -27,6 +27,7 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SO #include #include #include +#include #include #include "utilities.h" #include "Layer.h" @@ -44,9 +45,9 @@ namespace SketchUpNET public: System::String^ Name; - List^ HiddenEntities; + List^ HiddenEntities; - Scene(System::String^ name, List^ hiddenEntities) + Scene(System::String^ name, List^ hiddenEntities) { this->Name = name; this->HiddenEntities = hiddenEntities; @@ -54,7 +55,7 @@ namespace SketchUpNET Scene() {}; internal: - static Scene^ FromSU(SUSceneRef scene, System::Collections::Generic::Dictionary^ entitycontainer) + static Scene^ FromSU(SUSceneRef scene, System::Collections::Generic::Dictionary^ entitycontainer) { SUStringRef name = SU_INVALID; SUStringCreate(&name); @@ -62,7 +63,7 @@ namespace SketchUpNET size_t eCount = 0; SUSceneGetNumHiddenEntities(scene, &eCount); - List^ entities = gcnew List(); + List^ entities = gcnew List(); if (eCount > 0) { std::vector ents(eCount); @@ -81,12 +82,31 @@ namespace SketchUpNET return gcnew Scene(SketchUpNET::Utilities::GetString(name), entities); }; - SUSceneRef ToSU() { + SUSceneRef ToSU(SUModelRef model) { SUSceneRef scene = SU_INVALID; SUSceneCreate(&scene); SUSceneSetName(scene, Utilities::ToString(this->Name)); + + size_t num_pids = this->HiddenEntities->Count; + int64_t * pids = (int64_t *)malloc(*&num_pids * sizeof(int64_t)); + for (size_t i = 0; i < num_pids; i++) { + Int64 id = this->HiddenEntities[i]->GetID(); + pids[i] = id; + Console::WriteLine(id); + } + + SUEntityRef * entites = (SUEntityRef *)malloc(*&num_pids * sizeof(SUEntityRef)); + SUModelGetEntitiesByPersistentIDs(model, num_pids, pids, entites); + + for (size_t i = 0; i < num_pids; i++) { + SUDrawingElementRef sd = SUDrawingElementFromEntity(entites[i]); + SUSceneSetDrawingElementHidden(scene, sd, true); + } + return scene; } + + }; } \ No newline at end of file diff --git a/SketchUpNET/SketchUpNET.cpp b/SketchUpNET/SketchUpNET.cpp index 52ae684..f576a87 100644 --- a/SketchUpNET/SketchUpNET.cpp +++ b/SketchUpNET/SketchUpNET.cpp @@ -96,7 +96,7 @@ namespace SketchUpNET /// /// Containing All Model Entities /// - System::Collections::Generic::Dictionary^ Entities; + System::Collections::Generic::Dictionary^ Entities; /// /// Containing Model Component Instances @@ -154,7 +154,7 @@ namespace SketchUpNET Groups = gcnew System::Collections::Generic::List(); Components = gcnew System::Collections::Generic::Dictionary(); Materials = gcnew System::Collections::Generic::Dictionary(); - Entities = gcnew System::Collections::Generic::Dictionary(); + Entities = gcnew System::Collections::Generic::Dictionary(); SUEntitiesRef entities = SU_INVALID; SUModelGetEntities(model, &entities); @@ -349,7 +349,7 @@ namespace SketchUpNET SUEntitiesAddFaces(entities, Surfaces->Count, Surface::ListToSU(Surfaces)); SUEntitiesAddEdges(entities, Edges->Count, Edge::ListToSU(Edges)); SUEntitiesAddCurves(entities, Curves->Count, Curve::ListToSU(Curves)); - + SUModelSaveToFile(model, Utilities::ToString(filename)); SUModelRelease(&model); @@ -378,6 +378,12 @@ namespace SketchUpNET SUEntitiesAddEdges(entities, Edges->Count, Edge::ListToSU(Edges)); SUEntitiesAddCurves(entities, Curves->Count, Curve::ListToSU(Curves)); + for (int i = 0; i < Scenes->Count; i++) + { + SUSceneRef suscene = Scenes[i]->ToSU(model); + int id = -1; + SUModelAddScene(model, -1, suscene, &id); + } SUModelSaveToFile(model, Utilities::ToString(filename)); SUModelRelease(&model); @@ -388,7 +394,6 @@ namespace SketchUpNET private: - void FixRefs(Component^ comp) { for each (Instance^ var in comp->Instances) diff --git a/SketchUpNET/Surface.cpp b/SketchUpNET/Surface.cpp index 61436c5..3118475 100644 --- a/SketchUpNET/Surface.cpp +++ b/SketchUpNET/Surface.cpp @@ -48,10 +48,10 @@ using namespace System::Collections::Generic; namespace SketchUpNET { - public ref class Surface + public ref class Surface : IEntity { public: - Int32 ID; + Int64 ID; Loop^ OuterEdges; List^ InnerEdges; List^ Vertices; @@ -63,7 +63,7 @@ namespace SketchUpNET System::String^ Layer; - Surface(Int32 id, Loop^ outer, List^ inner, Vector^ normal, double area, List^ vertices, Mesh^ m, System::String^ layername, Material^ backmat, Material^ frontmat) + Surface(Loop^ outer, List^ inner, Vector^ normal, double area, List^ vertices, Mesh^ m, System::String^ layername, Material^ backmat, Material^ frontmat) { this->OuterEdges = outer; this->InnerEdges = inner; @@ -73,11 +73,14 @@ namespace SketchUpNET this->FrontMaterial = frontmat; this->Area = area; this->Vertices = vertices; - this->Layer = layername; - this->ID = id; }; Surface(){}; + + virtual Int64 GetID() { + return this->ID; + } + internal: static Vertex^ GetCentroid(List^ vertices, int vertexCount) @@ -139,7 +142,10 @@ namespace SketchUpNET SUFaceRef face = SU_INVALID; SUFaceCreate(&face, points, &outer_loop); - + SUEntityRef e = SUFaceToEntity(face); + int32_t id = -1; + SUEntityGetID(e, &id); + this->ID = id; return face; } @@ -231,13 +237,13 @@ namespace SketchUpNET Material^ backMat = (materials->ContainsKey(mbackName)) ? materials[mbackName] : Material::FromSU(mback); Material^ frontMat = (materials->ContainsKey(minnerName)) ? materials[minnerName] : Material::FromSU(minner); - Surface^ v = gcnew Surface(id, Loop::FromSU(outer), inner, normal, area, vertices,m, layername, backMat, frontMat); - + Surface^ v = gcnew Surface(Loop::FromSU(outer), inner, normal, area, vertices,m, layername, backMat, frontMat); + v->ID = id; return v; } - static List^ GetEntitySurfaces(SUEntitiesRef entities, bool includeMeshes, System::Collections::Generic::Dictionary^ materials, System::Collections::Generic::Dictionary^ entitycontainer) + static List^ GetEntitySurfaces(SUEntitiesRef entities, bool includeMeshes, System::Collections::Generic::Dictionary^ materials, System::Collections::Generic::Dictionary^ entitycontainer) { List^ surfaces = gcnew List(); diff --git a/SketchUpNET/Utilities.cpp b/SketchUpNET/Utilities.cpp index 42de79f..80b7b9d 100644 --- a/SketchUpNET/Utilities.cpp +++ b/SketchUpNET/Utilities.cpp @@ -38,6 +38,11 @@ using namespace System::Collections::Generic; namespace SketchUpNET { + public interface class IEntity + { + Int64 GetID(); + }; + public class Utilities { public: