Skip to content

Commit

Permalink
#19 attempt to write scenes
Browse files Browse the repository at this point in the history
  • Loading branch information
moethu committed Aug 30, 2020
1 parent 761b496 commit 5d26b27
Show file tree
Hide file tree
Showing 10 changed files with 125 additions and 47 deletions.
18 changes: 18 additions & 0 deletions SketchUpNET.Unittest/BasicTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,24 @@ public void TestGetScene()
}
}

/// <summary>
/// Test writing and loading scenes
/// </summary>
[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<IEntity>() { edge });
skp.Scenes.Add(scene);
skp.WriteNewModel("tmp.skp");

skp = new SketchUp();
skp.LoadModel("tmp.skp", false);
}

/// <summary>
/// Test getting Materials from model
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion SketchUpNET/Component.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ namespace SketchUpNET

Component(){};
internal:
static Component^ FromSU(SUComponentDefinitionRef comp, bool includeMeshes, System::Collections::Generic::Dictionary<String^, Material^>^ materials, System::Collections::Generic::Dictionary<Int32, Object^>^ entitycontainer)
static Component^ FromSU(SUComponentDefinitionRef comp, bool includeMeshes, System::Collections::Generic::Dictionary<String^, Material^>^ materials, System::Collections::Generic::Dictionary<Int64, IEntity^>^ entitycontainer)
{
SUStringRef name = SU_INVALID;
SUStringCreate(&name);
Expand Down
21 changes: 14 additions & 7 deletions SketchUpNET/Curve.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,23 +41,26 @@ using namespace System::Collections::Generic;

namespace SketchUpNET
{
public ref class Curve
public ref class Curve : IEntity
{
public:

List<Edge^>^ Edges = gcnew List<Edge^>();
bool isArc;
Int32 ID;
Int64 ID;

Curve(Int32 id, List<Edge^>^ edges, bool isarc)
Curve(List<Edge^>^ 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)
Expand All @@ -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;
};

Expand All @@ -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;
}

Expand All @@ -115,7 +122,7 @@ namespace SketchUpNET
return result;
}

static List<Curve^>^ GetEntityCurves(SUEntitiesRef entities, System::Collections::Generic::Dictionary<Int32, Object^>^ entitycontainer)
static List<Curve^>^ GetEntityCurves(SUEntitiesRef entities, System::Collections::Generic::Dictionary<Int64, IEntity^>^ entitycontainer)
{
List<Curve^>^ curves = gcnew List<Curve^>();

Expand Down
23 changes: 16 additions & 7 deletions SketchUpNET/Edge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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;
};

Expand All @@ -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;
}

Expand All @@ -110,7 +119,7 @@ namespace SketchUpNET
return result;
}

static List<Edge^>^ GetEntityEdges(SUEntitiesRef entities,System::Collections::Generic::Dictionary<Int32, Object^>^ entitycontainer)
static List<Edge^>^ GetEntityEdges(SUEntitiesRef entities,System::Collections::Generic::Dictionary<Int64, IEntity^>^ entitycontainer)
{
List<Edge^>^ edges = gcnew List<Edge^>();

Expand Down
20 changes: 12 additions & 8 deletions SketchUpNET/Group.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,15 @@ using namespace System::Collections::Generic;

namespace SketchUpNET
{
public ref class Group
public ref class Group : IEntity
{
public:
System::String^ Name;

/// <summary>
/// Surfaces
/// </summary>
Int32 ID;
Int64 ID;
List<Surface^>^ Surfaces;
List<Edge^>^ Edges;
List<Curve^>^ Curves;
Expand All @@ -64,7 +64,7 @@ namespace SketchUpNET
Transform^ Transformation;
System::String^ Layer;

Group(Int32 id, System::String^ name, List<Surface^>^ surfaces, List<Curve^>^ curves, List<Edge^>^ edges, List<Instance^>^ insts, List<Group^>^ group, Transform^ transformation, System::String^ layername)
Group(System::String^ name, List<Surface^>^ surfaces, List<Curve^>^ curves, List<Edge^>^ edges, List<Instance^>^ insts, List<Group^>^ group, Transform^ transformation, System::String^ layername)
{
this->Name = name;
this->Surfaces = surfaces;
Expand All @@ -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<String^, Material^>^ materials, System::Collections::Generic::Dictionary<Int32, Object^>^ entitycontainer)
static Group^ FromSU(SUGroupRef group, bool includeMeshes, System::Collections::Generic::Dictionary<String^, Material^>^ materials, System::Collections::Generic::Dictionary<Int64, IEntity^>^ entitycontainer)
{
SUStringRef name = SU_INVALID;
SUStringCreate(&name);
Expand Down Expand Up @@ -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<Group^>^ GetEntityGroups(SUEntitiesRef entities, bool includeMeshes, System::Collections::Generic::Dictionary<String^, Material^>^ materials, System::Collections::Generic::Dictionary<Int32, Object^>^ entitycontainer)
static List<Group^>^ GetEntityGroups(SUEntitiesRef entities, bool includeMeshes, System::Collections::Generic::Dictionary<String^, Material^>^ materials, System::Collections::Generic::Dictionary<Int64, IEntity^>^ entitycontainer)
{
List<Group^>^ groups = gcnew List<Group^>();

Expand Down
16 changes: 10 additions & 6 deletions SketchUpNET/Instance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ using namespace System::Collections::Generic;

namespace SketchUpNET
{
public ref class Instance
public ref class Instance : IEntity
{
public:
System::String^ Name;
Expand All @@ -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)
{
Expand Down Expand Up @@ -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<Instance^>^ GetEntityInstances(SUEntitiesRef entities, System::Collections::Generic::Dictionary<Int32, Object^>^ entitycontainer)
static List<Instance^>^ GetEntityInstances(SUEntitiesRef entities, System::Collections::Generic::Dictionary<Int64, IEntity^>^ entitycontainer)
{
List<Instance^>^ instancelist = gcnew List<Instance^>();

Expand Down
30 changes: 25 additions & 5 deletions SketchUpNET/Scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SO
#include <SketchUpAPI/model/entities.h>
#include <SketchUpAPI/model/scene.h>
#include <SketchUpAPI/model/entity.h>
#include <SketchUpAPI/model/drawing_element.h>
#include <msclr/marshal.h>
#include "utilities.h"
#include "Layer.h"
Expand All @@ -44,25 +45,25 @@ namespace SketchUpNET
public:

System::String^ Name;
List<Object^>^ HiddenEntities;
List<IEntity^>^ HiddenEntities;

Scene(System::String^ name, List<Object^>^ hiddenEntities)
Scene(System::String^ name, List<IEntity^>^ hiddenEntities)
{
this->Name = name;
this->HiddenEntities = hiddenEntities;
};

Scene() {};
internal:
static Scene^ FromSU(SUSceneRef scene, System::Collections::Generic::Dictionary<Int32, Object^>^ entitycontainer)
static Scene^ FromSU(SUSceneRef scene, System::Collections::Generic::Dictionary<Int64, IEntity^>^ entitycontainer)
{
SUStringRef name = SU_INVALID;
SUStringCreate(&name);
SUSceneGetName(scene, &name);

size_t eCount = 0;
SUSceneGetNumHiddenEntities(scene, &eCount);
List<Object^>^ entities = gcnew List<Object^>();
List<IEntity^>^ entities = gcnew List<IEntity^>();

if (eCount > 0) {
std::vector<SUEntityRef> ents(eCount);
Expand All @@ -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;
}


};

}
13 changes: 9 additions & 4 deletions SketchUpNET/SketchUpNET.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ namespace SketchUpNET
/// <summary>
/// Containing All Model Entities
/// </summary>
System::Collections::Generic::Dictionary<Int32, Object^>^ Entities;
System::Collections::Generic::Dictionary<Int64, IEntity^>^ Entities;

/// <summary>
/// Containing Model Component Instances
Expand Down Expand Up @@ -154,7 +154,7 @@ namespace SketchUpNET
Groups = gcnew System::Collections::Generic::List<Group^>();
Components = gcnew System::Collections::Generic::Dictionary<String^,Component^>();
Materials = gcnew System::Collections::Generic::Dictionary<String^, Material^>();
Entities = gcnew System::Collections::Generic::Dictionary<Int32, Object^>();
Entities = gcnew System::Collections::Generic::Dictionary<Int64, IEntity^>();

SUEntitiesRef entities = SU_INVALID;
SUModelGetEntities(model, &entities);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -388,7 +394,6 @@ namespace SketchUpNET

private:


void FixRefs(Component^ comp)
{
for each (Instance^ var in comp->Instances)
Expand Down
Loading

0 comments on commit 5d26b27

Please sign in to comment.