2013-07-29 06:08:34 +08:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// Routines to write and read our .slvs file format.
|
|
|
|
//
|
|
|
|
// Copyright 2008-2013 Jonathan Westhues.
|
|
|
|
//-----------------------------------------------------------------------------
|
2008-04-18 19:11:48 +08:00
|
|
|
#include "solvespace.h"
|
|
|
|
|
2013-09-19 14:35:56 +08:00
|
|
|
#define VERSION_STRING "\261\262\263" "SolveSpaceREVa"
|
2008-05-29 18:10:12 +08:00
|
|
|
|
2013-08-27 02:58:35 +08:00
|
|
|
static int StrStartsWith(const char *str, const char *start) {
|
2009-05-19 15:26:38 +08:00
|
|
|
return memcmp(str, start, strlen(start)) == 0;
|
|
|
|
}
|
|
|
|
|
2010-03-01 03:23:01 +08:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// Clear and free all the dynamic memory associated with our currently-loaded
|
|
|
|
// sketch. This does not leave the program in an acceptable state (with the
|
|
|
|
// references created, and so on), so anyone calling this must fix that later.
|
|
|
|
//-----------------------------------------------------------------------------
|
2016-05-05 13:54:05 +08:00
|
|
|
void SolveSpaceUI::ClearExisting() {
|
2010-03-01 03:23:01 +08:00
|
|
|
UndoClearStack(&redo);
|
|
|
|
UndoClearStack(&undo);
|
|
|
|
|
2016-02-17 18:03:07 +08:00
|
|
|
for(int i = 0; i < SK.groupOrder.n; i++) {
|
|
|
|
Group *g = SK.GetGroup(SK.groupOrder.elem[i]);
|
2010-03-01 03:23:01 +08:00
|
|
|
g->Clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
SK.constraint.Clear();
|
|
|
|
SK.request.Clear();
|
|
|
|
SK.group.Clear();
|
2016-02-17 18:03:07 +08:00
|
|
|
SK.groupOrder.Clear();
|
2010-03-01 03:23:01 +08:00
|
|
|
SK.style.Clear();
|
|
|
|
|
|
|
|
SK.entity.Clear();
|
|
|
|
SK.param.Clear();
|
2016-11-30 00:49:20 +08:00
|
|
|
images.clear();
|
2010-03-01 03:23:01 +08:00
|
|
|
}
|
|
|
|
|
2016-05-05 13:54:05 +08:00
|
|
|
hGroup SolveSpaceUI::CreateDefaultDrawingGroup() {
|
2015-03-27 23:31:23 +08:00
|
|
|
Group g = {};
|
2008-06-02 19:43:27 +08:00
|
|
|
|
|
|
|
// And an empty group, for the first stuff the user draws.
|
|
|
|
g.visible = true;
|
2017-01-07 14:41:13 +08:00
|
|
|
g.name = C_("group-name", "sketch-in-plane");
|
Convert all enumerations to use `enum class`.
Specifically, take the old code that looks like this:
class Foo {
enum { X = 1, Y = 2 };
int kind;
}
... foo.kind = Foo::X; ...
and convert it to this:
class Foo {
enum class Kind : uint32_t { X = 1, Y = 2 };
Kind kind;
}
... foo.kind = Foo::Kind::X;
(In some cases the enumeration would not be in the class namespace,
such as when it is generally useful.)
The benefits are as follows:
* The type of the field gives a clear indication of intent, both
to humans and tools (such as binding generators).
* The compiler is able to automatically warn when a switch is not
exhaustive; but this is currently suppressed by the
default: ssassert(false, ...)
idiom.
* Integers and plain enums are weakly type checked: they implicitly
convert into each other. This can hide bugs where type conversion
is performed but not intended. Enum classes are strongly type
checked.
* Plain enums pollute parent namespaces; enum classes do not.
Almost every defined enum we have already has a kind of ad-hoc
namespacing via `NAMESPACE_`, which is now explicit.
* Plain enums do not have a well-defined ABI size, which is
important for bindings. Enum classes can have it, if specified.
We specify the base type for all enums as uint32_t, which is
a safe choice and allows us to not change the numeric values
of any variants.
This commit introduces absolutely no functional change to the code,
just renaming and change of types. It handles almost all cases,
except GraphicsWindow::pending.operation, which needs minor
functional change.
2016-05-20 16:31:20 +08:00
|
|
|
g.type = Group::Type::DRAWING_WORKPLANE;
|
|
|
|
g.subtype = Group::Subtype::WORKPLANE_BY_POINT_ORTHO;
|
2016-02-17 18:03:07 +08:00
|
|
|
g.order = 1;
|
2008-06-02 19:43:27 +08:00
|
|
|
g.predef.q = Quaternion::From(1, 0, 0, 0);
|
|
|
|
hRequest hr = Request::HREQUEST_REFERENCE_XY;
|
|
|
|
g.predef.origin = hr.entity(1);
|
2009-04-19 13:53:16 +08:00
|
|
|
SK.group.AddAndAssignId(&g);
|
|
|
|
SK.GetGroup(g.h)->activeWorkplane = g.h.entity(0);
|
2008-06-02 19:43:27 +08:00
|
|
|
return g.h;
|
|
|
|
}
|
|
|
|
|
2016-05-05 13:54:05 +08:00
|
|
|
void SolveSpaceUI::NewFile() {
|
2010-03-01 03:23:01 +08:00
|
|
|
ClearExisting();
|
2008-04-24 14:22:16 +08:00
|
|
|
|
|
|
|
// Our initial group, that contains the references.
|
2015-03-27 23:31:23 +08:00
|
|
|
Group g = {};
|
2008-04-30 12:52:34 +08:00
|
|
|
g.visible = true;
|
2017-01-07 14:41:13 +08:00
|
|
|
g.name = C_("group-name", "#references");
|
Convert all enumerations to use `enum class`.
Specifically, take the old code that looks like this:
class Foo {
enum { X = 1, Y = 2 };
int kind;
}
... foo.kind = Foo::X; ...
and convert it to this:
class Foo {
enum class Kind : uint32_t { X = 1, Y = 2 };
Kind kind;
}
... foo.kind = Foo::Kind::X;
(In some cases the enumeration would not be in the class namespace,
such as when it is generally useful.)
The benefits are as follows:
* The type of the field gives a clear indication of intent, both
to humans and tools (such as binding generators).
* The compiler is able to automatically warn when a switch is not
exhaustive; but this is currently suppressed by the
default: ssassert(false, ...)
idiom.
* Integers and plain enums are weakly type checked: they implicitly
convert into each other. This can hide bugs where type conversion
is performed but not intended. Enum classes are strongly type
checked.
* Plain enums pollute parent namespaces; enum classes do not.
Almost every defined enum we have already has a kind of ad-hoc
namespacing via `NAMESPACE_`, which is now explicit.
* Plain enums do not have a well-defined ABI size, which is
important for bindings. Enum classes can have it, if specified.
We specify the base type for all enums as uint32_t, which is
a safe choice and allows us to not change the numeric values
of any variants.
This commit introduces absolutely no functional change to the code,
just renaming and change of types. It handles almost all cases,
except GraphicsWindow::pending.operation, which needs minor
functional change.
2016-05-20 16:31:20 +08:00
|
|
|
g.type = Group::Type::DRAWING_3D;
|
2016-02-17 18:03:07 +08:00
|
|
|
g.order = 0;
|
2008-04-24 14:22:16 +08:00
|
|
|
g.h = Group::HGROUP_REFERENCES;
|
2009-04-19 13:53:16 +08:00
|
|
|
SK.group.Add(&g);
|
2008-04-24 14:22:16 +08:00
|
|
|
|
|
|
|
// Let's create three two-d coordinate systems, for the coordinate
|
|
|
|
// planes; these are our references, present in every sketch.
|
2015-03-27 23:31:23 +08:00
|
|
|
Request r = {};
|
Convert all enumerations to use `enum class`.
Specifically, take the old code that looks like this:
class Foo {
enum { X = 1, Y = 2 };
int kind;
}
... foo.kind = Foo::X; ...
and convert it to this:
class Foo {
enum class Kind : uint32_t { X = 1, Y = 2 };
Kind kind;
}
... foo.kind = Foo::Kind::X;
(In some cases the enumeration would not be in the class namespace,
such as when it is generally useful.)
The benefits are as follows:
* The type of the field gives a clear indication of intent, both
to humans and tools (such as binding generators).
* The compiler is able to automatically warn when a switch is not
exhaustive; but this is currently suppressed by the
default: ssassert(false, ...)
idiom.
* Integers and plain enums are weakly type checked: they implicitly
convert into each other. This can hide bugs where type conversion
is performed but not intended. Enum classes are strongly type
checked.
* Plain enums pollute parent namespaces; enum classes do not.
Almost every defined enum we have already has a kind of ad-hoc
namespacing via `NAMESPACE_`, which is now explicit.
* Plain enums do not have a well-defined ABI size, which is
important for bindings. Enum classes can have it, if specified.
We specify the base type for all enums as uint32_t, which is
a safe choice and allows us to not change the numeric values
of any variants.
This commit introduces absolutely no functional change to the code,
just renaming and change of types. It handles almost all cases,
except GraphicsWindow::pending.operation, which needs minor
functional change.
2016-05-20 16:31:20 +08:00
|
|
|
r.type = Request::Type::WORKPLANE;
|
2008-04-24 14:22:16 +08:00
|
|
|
r.group = Group::HGROUP_REFERENCES;
|
2008-04-27 11:26:27 +08:00
|
|
|
r.workplane = Entity::FREE_IN_3D;
|
2008-04-24 14:22:16 +08:00
|
|
|
|
|
|
|
r.h = Request::HREQUEST_REFERENCE_XY;
|
2009-04-19 13:53:16 +08:00
|
|
|
SK.request.Add(&r);
|
2008-04-24 14:22:16 +08:00
|
|
|
|
|
|
|
r.h = Request::HREQUEST_REFERENCE_YZ;
|
2009-04-19 13:53:16 +08:00
|
|
|
SK.request.Add(&r);
|
2008-04-24 14:22:16 +08:00
|
|
|
|
|
|
|
r.h = Request::HREQUEST_REFERENCE_ZX;
|
2009-04-19 13:53:16 +08:00
|
|
|
SK.request.Add(&r);
|
2008-04-24 14:22:16 +08:00
|
|
|
|
2008-06-02 19:43:27 +08:00
|
|
|
CreateDefaultDrawingGroup();
|
2008-05-27 10:22:20 +08:00
|
|
|
}
|
2008-04-24 14:22:16 +08:00
|
|
|
|
2015-03-24 01:49:04 +08:00
|
|
|
const SolveSpaceUI::SaveTable SolveSpaceUI::SAVED[] = {
|
2008-05-27 10:22:20 +08:00
|
|
|
{ 'g', "Group.h.v", 'x', &(SS.sv.g.h.v) },
|
|
|
|
{ 'g', "Group.type", 'd', &(SS.sv.g.type) },
|
2008-02-07 17:53:52 +08:00
|
|
|
{ 'g', "Group.order", 'd', &(SS.sv.g.order) },
|
2015-11-06 16:40:12 +08:00
|
|
|
{ 'g', "Group.name", 'S', &(SS.sv.g.name) },
|
2008-05-27 10:22:20 +08:00
|
|
|
{ 'g', "Group.activeWorkplane.v", 'x', &(SS.sv.g.activeWorkplane.v) },
|
|
|
|
{ 'g', "Group.opA.v", 'x', &(SS.sv.g.opA.v) },
|
2008-06-21 18:18:20 +08:00
|
|
|
{ 'g', "Group.opB.v", 'x', &(SS.sv.g.opB.v) },
|
2008-06-14 16:43:38 +08:00
|
|
|
{ 'g', "Group.valA", 'f', &(SS.sv.g.valA) },
|
2008-06-23 16:25:17 +08:00
|
|
|
{ 'g', "Group.valB", 'f', &(SS.sv.g.valB) },
|
|
|
|
{ 'g', "Group.valC", 'f', &(SS.sv.g.valB) },
|
2013-11-04 07:09:17 +08:00
|
|
|
{ 'g', "Group.color", 'c', &(SS.sv.g.color) },
|
2008-05-27 10:22:20 +08:00
|
|
|
{ 'g', "Group.subtype", 'd', &(SS.sv.g.subtype) },
|
2008-06-12 12:36:33 +08:00
|
|
|
{ 'g', "Group.skipFirst", 'b', &(SS.sv.g.skipFirst) },
|
2008-05-27 10:22:20 +08:00
|
|
|
{ 'g', "Group.meshCombine", 'd', &(SS.sv.g.meshCombine) },
|
2009-05-21 17:06:26 +08:00
|
|
|
{ 'g', "Group.forceToMesh", 'd', &(SS.sv.g.forceToMesh) },
|
2008-06-01 16:29:59 +08:00
|
|
|
{ 'g', "Group.predef.q.w", 'f', &(SS.sv.g.predef.q.w) },
|
|
|
|
{ 'g', "Group.predef.q.vx", 'f', &(SS.sv.g.predef.q.vx) },
|
|
|
|
{ 'g', "Group.predef.q.vy", 'f', &(SS.sv.g.predef.q.vy) },
|
|
|
|
{ 'g', "Group.predef.q.vz", 'f', &(SS.sv.g.predef.q.vz) },
|
|
|
|
{ 'g', "Group.predef.origin.v", 'x', &(SS.sv.g.predef.origin.v) },
|
|
|
|
{ 'g', "Group.predef.entityB.v", 'x', &(SS.sv.g.predef.entityB.v) },
|
|
|
|
{ 'g', "Group.predef.entityC.v", 'x', &(SS.sv.g.predef.entityC.v) },
|
|
|
|
{ 'g', "Group.predef.swapUV", 'b', &(SS.sv.g.predef.swapUV) },
|
|
|
|
{ 'g', "Group.predef.negateU", 'b', &(SS.sv.g.predef.negateU) },
|
|
|
|
{ 'g', "Group.predef.negateV", 'b', &(SS.sv.g.predef.negateV) },
|
2008-05-27 10:22:20 +08:00
|
|
|
{ 'g', "Group.visible", 'b', &(SS.sv.g.visible) },
|
2008-02-15 19:35:15 +08:00
|
|
|
{ 'g', "Group.suppress", 'b', &(SS.sv.g.suppress) },
|
2009-10-01 19:22:56 +08:00
|
|
|
{ 'g', "Group.relaxConstraints", 'b', &(SS.sv.g.relaxConstraints) },
|
2016-01-21 23:01:43 +08:00
|
|
|
{ 'g', "Group.allowRedundant", 'b', &(SS.sv.g.allowRedundant) },
|
2010-05-10 09:06:09 +08:00
|
|
|
{ 'g', "Group.allDimsReference", 'b', &(SS.sv.g.allDimsReference) },
|
2009-12-15 20:26:22 +08:00
|
|
|
{ 'g', "Group.scale", 'f', &(SS.sv.g.scale) },
|
2008-05-27 10:22:20 +08:00
|
|
|
{ 'g', "Group.remap", 'M', &(SS.sv.g.remap) },
|
2017-03-11 22:43:21 +08:00
|
|
|
{ 'g', "Group.impFile", 'i', NULL },
|
|
|
|
{ 'g', "Group.impFileRel", 'P', &(SS.sv.g.linkFile) },
|
2008-05-27 10:22:20 +08:00
|
|
|
|
|
|
|
{ 'p', "Param.h.v.", 'x', &(SS.sv.p.h.v) },
|
|
|
|
{ 'p', "Param.val", 'f', &(SS.sv.p.val) },
|
|
|
|
|
|
|
|
{ 'r', "Request.h.v", 'x', &(SS.sv.r.h.v) },
|
|
|
|
{ 'r', "Request.type", 'd', &(SS.sv.r.type) },
|
2009-10-21 12:46:01 +08:00
|
|
|
{ 'r', "Request.extraPoints", 'd', &(SS.sv.r.extraPoints) },
|
2008-05-27 10:22:20 +08:00
|
|
|
{ 'r', "Request.workplane.v", 'x', &(SS.sv.r.workplane.v) },
|
|
|
|
{ 'r', "Request.group.v", 'x', &(SS.sv.r.group.v) },
|
|
|
|
{ 'r', "Request.construction", 'b', &(SS.sv.r.construction) },
|
2009-07-20 09:47:59 +08:00
|
|
|
{ 'r', "Request.style", 'x', &(SS.sv.r.style) },
|
2015-11-06 16:40:12 +08:00
|
|
|
{ 'r', "Request.str", 'S', &(SS.sv.r.str) },
|
|
|
|
{ 'r', "Request.font", 'S', &(SS.sv.r.font) },
|
2016-11-30 00:49:20 +08:00
|
|
|
{ 'r', "Request.file", 'P', &(SS.sv.r.file) },
|
2016-10-11 09:58:04 +08:00
|
|
|
{ 'r', "Request.aspectRatio", 'f', &(SS.sv.r.aspectRatio) },
|
2008-05-27 10:22:20 +08:00
|
|
|
|
|
|
|
{ 'e', "Entity.h.v", 'x', &(SS.sv.e.h.v) },
|
|
|
|
{ 'e', "Entity.type", 'd', &(SS.sv.e.type) },
|
|
|
|
{ 'e', "Entity.construction", 'b', &(SS.sv.e.construction) },
|
2009-07-20 09:47:59 +08:00
|
|
|
{ 'e', "Entity.style", 'x', &(SS.sv.e.style) },
|
2015-11-06 16:40:12 +08:00
|
|
|
{ 'e', "Entity.str", 'S', &(SS.sv.e.str) },
|
|
|
|
{ 'e', "Entity.font", 'S', &(SS.sv.e.font) },
|
2016-11-30 00:49:20 +08:00
|
|
|
{ 'e', "Entity.file", 'P', &(SS.sv.e.file) },
|
2008-05-27 10:22:20 +08:00
|
|
|
{ 'e', "Entity.point[0].v", 'x', &(SS.sv.e.point[0].v) },
|
|
|
|
{ 'e', "Entity.point[1].v", 'x', &(SS.sv.e.point[1].v) },
|
|
|
|
{ 'e', "Entity.point[2].v", 'x', &(SS.sv.e.point[2].v) },
|
|
|
|
{ 'e', "Entity.point[3].v", 'x', &(SS.sv.e.point[3].v) },
|
2009-10-21 12:46:01 +08:00
|
|
|
{ 'e', "Entity.point[4].v", 'x', &(SS.sv.e.point[4].v) },
|
|
|
|
{ 'e', "Entity.point[5].v", 'x', &(SS.sv.e.point[5].v) },
|
|
|
|
{ 'e', "Entity.point[6].v", 'x', &(SS.sv.e.point[6].v) },
|
|
|
|
{ 'e', "Entity.point[7].v", 'x', &(SS.sv.e.point[7].v) },
|
|
|
|
{ 'e', "Entity.point[8].v", 'x', &(SS.sv.e.point[8].v) },
|
|
|
|
{ 'e', "Entity.point[9].v", 'x', &(SS.sv.e.point[9].v) },
|
|
|
|
{ 'e', "Entity.point[10].v", 'x', &(SS.sv.e.point[10].v) },
|
|
|
|
{ 'e', "Entity.point[11].v", 'x', &(SS.sv.e.point[11].v) },
|
|
|
|
{ 'e', "Entity.extraPoints", 'd', &(SS.sv.e.extraPoints) },
|
2008-05-27 10:22:20 +08:00
|
|
|
{ 'e', "Entity.normal.v", 'x', &(SS.sv.e.normal.v) },
|
|
|
|
{ 'e', "Entity.distance.v", 'x', &(SS.sv.e.distance.v) },
|
|
|
|
{ 'e', "Entity.workplane.v", 'x', &(SS.sv.e.workplane.v) },
|
2008-05-29 18:10:12 +08:00
|
|
|
{ 'e', "Entity.actPoint.x", 'f', &(SS.sv.e.actPoint.x) },
|
|
|
|
{ 'e', "Entity.actPoint.y", 'f', &(SS.sv.e.actPoint.y) },
|
|
|
|
{ 'e', "Entity.actPoint.z", 'f', &(SS.sv.e.actPoint.z) },
|
|
|
|
{ 'e', "Entity.actNormal.w", 'f', &(SS.sv.e.actNormal.w) },
|
|
|
|
{ 'e', "Entity.actNormal.vx", 'f', &(SS.sv.e.actNormal.vx) },
|
|
|
|
{ 'e', "Entity.actNormal.vy", 'f', &(SS.sv.e.actNormal.vy) },
|
|
|
|
{ 'e', "Entity.actNormal.vz", 'f', &(SS.sv.e.actNormal.vz) },
|
|
|
|
{ 'e', "Entity.actDistance", 'f', &(SS.sv.e.actDistance) },
|
2008-06-12 12:36:33 +08:00
|
|
|
{ 'e', "Entity.actVisible", 'b', &(SS.sv.e.actVisible), },
|
2008-05-29 18:10:12 +08:00
|
|
|
|
2008-05-27 10:22:20 +08:00
|
|
|
|
|
|
|
{ 'c', "Constraint.h.v", 'x', &(SS.sv.c.h.v) },
|
|
|
|
{ 'c', "Constraint.type", 'd', &(SS.sv.c.type) },
|
|
|
|
{ 'c', "Constraint.group.v", 'x', &(SS.sv.c.group.v) },
|
|
|
|
{ 'c', "Constraint.workplane.v", 'x', &(SS.sv.c.workplane.v) },
|
2008-06-14 16:43:38 +08:00
|
|
|
{ 'c', "Constraint.valA", 'f', &(SS.sv.c.valA) },
|
2017-01-26 00:39:26 +08:00
|
|
|
{ 'c', "Constraint.valP.v", 'x', &(SS.sv.c.valP.v) },
|
2008-05-27 10:22:20 +08:00
|
|
|
{ 'c', "Constraint.ptA.v", 'x', &(SS.sv.c.ptA.v) },
|
|
|
|
{ 'c', "Constraint.ptB.v", 'x', &(SS.sv.c.ptB.v) },
|
|
|
|
{ 'c', "Constraint.entityA.v", 'x', &(SS.sv.c.entityA.v) },
|
|
|
|
{ 'c', "Constraint.entityB.v", 'x', &(SS.sv.c.entityB.v) },
|
2008-07-20 20:24:43 +08:00
|
|
|
{ 'c', "Constraint.entityC.v", 'x', &(SS.sv.c.entityC.v) },
|
|
|
|
{ 'c', "Constraint.entityD.v", 'x', &(SS.sv.c.entityD.v) },
|
2008-07-13 20:44:05 +08:00
|
|
|
{ 'c', "Constraint.other", 'b', &(SS.sv.c.other) },
|
2010-05-10 12:14:06 +08:00
|
|
|
{ 'c', "Constraint.other2", 'b', &(SS.sv.c.other2) },
|
2008-06-11 12:22:52 +08:00
|
|
|
{ 'c', "Constraint.reference", 'b', &(SS.sv.c.reference) },
|
2015-11-06 16:40:12 +08:00
|
|
|
{ 'c', "Constraint.comment", 'S', &(SS.sv.c.comment) },
|
2008-05-27 10:22:20 +08:00
|
|
|
{ 'c', "Constraint.disp.offset.x", 'f', &(SS.sv.c.disp.offset.x) },
|
|
|
|
{ 'c', "Constraint.disp.offset.y", 'f', &(SS.sv.c.disp.offset.y) },
|
|
|
|
{ 'c', "Constraint.disp.offset.z", 'f', &(SS.sv.c.disp.offset.z) },
|
2009-07-20 09:47:59 +08:00
|
|
|
{ 'c', "Constraint.disp.style", 'x', &(SS.sv.c.disp.style) },
|
2008-05-27 10:22:20 +08:00
|
|
|
|
2009-09-17 15:32:36 +08:00
|
|
|
{ 's', "Style.h.v", 'x', &(SS.sv.s.h.v) },
|
2015-11-06 16:40:12 +08:00
|
|
|
{ 's', "Style.name", 'S', &(SS.sv.s.name) },
|
2009-09-17 15:32:36 +08:00
|
|
|
{ 's', "Style.width", 'f', &(SS.sv.s.width) },
|
2009-09-24 23:52:48 +08:00
|
|
|
{ 's', "Style.widthAs", 'd', &(SS.sv.s.widthAs) },
|
|
|
|
{ 's', "Style.textHeight", 'f', &(SS.sv.s.textHeight) },
|
|
|
|
{ 's', "Style.textHeightAs", 'd', &(SS.sv.s.textHeightAs) },
|
2009-09-29 19:35:19 +08:00
|
|
|
{ 's', "Style.textAngle", 'f', &(SS.sv.s.textAngle) },
|
2009-09-24 23:52:48 +08:00
|
|
|
{ 's', "Style.textOrigin", 'x', &(SS.sv.s.textOrigin) },
|
2013-11-04 07:09:17 +08:00
|
|
|
{ 's', "Style.color", 'c', &(SS.sv.s.color) },
|
2013-11-04 07:41:11 +08:00
|
|
|
{ 's', "Style.fillColor", 'c', &(SS.sv.s.fillColor) },
|
2009-10-23 01:16:20 +08:00
|
|
|
{ 's', "Style.filled", 'b', &(SS.sv.s.filled) },
|
2009-09-17 15:32:36 +08:00
|
|
|
{ 's', "Style.visible", 'b', &(SS.sv.s.visible) },
|
|
|
|
{ 's', "Style.exportable", 'b', &(SS.sv.s.exportable) },
|
2016-02-24 02:00:39 +08:00
|
|
|
{ 's', "Style.stippleType", 'd', &(SS.sv.s.stippleType) },
|
|
|
|
{ 's', "Style.stippleScale", 'f', &(SS.sv.s.stippleScale) },
|
2009-09-17 15:32:36 +08:00
|
|
|
|
2013-08-27 04:54:04 +08:00
|
|
|
{ 0, NULL, 0, NULL }
|
2008-04-24 14:22:16 +08:00
|
|
|
};
|
|
|
|
|
2016-01-15 20:30:53 +08:00
|
|
|
struct SAVEDptr {
|
|
|
|
IdList<EntityMap,EntityId> &M() { return *((IdList<EntityMap,EntityId> *)this); }
|
|
|
|
std::string &S() { return *((std::string *)this); }
|
2017-03-11 22:43:21 +08:00
|
|
|
Platform::Path &P() { return *((Platform::Path *)this); }
|
2016-01-15 20:30:53 +08:00
|
|
|
bool &b() { return *((bool *)this); }
|
|
|
|
RgbaColor &c() { return *((RgbaColor *)this); }
|
|
|
|
int &d() { return *((int *)this); }
|
|
|
|
double &f() { return *((double *)this); }
|
|
|
|
uint32_t &x() { return *((uint32_t *)this); }
|
2013-11-13 13:46:40 +08:00
|
|
|
};
|
|
|
|
|
2017-03-11 22:43:21 +08:00
|
|
|
void SolveSpaceUI::SaveUsingTable(const Platform::Path &filename, int type) {
|
2008-04-24 14:22:16 +08:00
|
|
|
int i;
|
|
|
|
for(i = 0; SAVED[i].type != 0; i++) {
|
|
|
|
if(SAVED[i].type != type) continue;
|
2008-05-29 18:10:12 +08:00
|
|
|
|
|
|
|
int fmt = SAVED[i].fmt;
|
2016-01-15 20:30:53 +08:00
|
|
|
SAVEDptr *p = (SAVEDptr *)SAVED[i].ptr;
|
2008-05-29 18:10:12 +08:00
|
|
|
// Any items that aren't specified are assumed to be zero
|
2016-01-15 20:30:53 +08:00
|
|
|
if(fmt == 'S' && p->S().empty()) continue;
|
2017-03-11 22:43:21 +08:00
|
|
|
if(fmt == 'P' && p->P().IsEmpty()) continue;
|
2016-01-15 20:30:53 +08:00
|
|
|
if(fmt == 'd' && p->d() == 0) continue;
|
|
|
|
if(fmt == 'f' && EXACT(p->f() == 0.0)) continue;
|
|
|
|
if(fmt == 'x' && p->x() == 0) continue;
|
2017-03-11 22:43:21 +08:00
|
|
|
if(fmt == 'i') continue;
|
2008-05-29 18:10:12 +08:00
|
|
|
|
|
|
|
fprintf(fh, "%s=", SAVED[i].desc);
|
|
|
|
switch(fmt) {
|
2016-01-15 20:30:53 +08:00
|
|
|
case 'S': fprintf(fh, "%s", p->S().c_str()); break;
|
|
|
|
case 'b': fprintf(fh, "%d", p->b() ? 1 : 0); break;
|
|
|
|
case 'c': fprintf(fh, "%08x", p->c().ToPackedInt()); break;
|
|
|
|
case 'd': fprintf(fh, "%d", p->d()); break;
|
|
|
|
case 'f': fprintf(fh, "%.20f", p->f()); break;
|
|
|
|
case 'x': fprintf(fh, "%08x", p->x()); break;
|
2008-04-27 18:01:23 +08:00
|
|
|
|
2017-03-11 22:43:21 +08:00
|
|
|
case 'P': {
|
|
|
|
if(!p->P().IsEmpty()) {
|
|
|
|
Platform::Path relativePath = p->P().RelativeTo(filename.Parent());
|
|
|
|
ssassert(!relativePath.IsEmpty(), "Cannot relativize path");
|
|
|
|
fprintf(fh, "%s", relativePath.ToPortable().c_str());
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2008-04-27 18:01:23 +08:00
|
|
|
case 'M': {
|
|
|
|
int j;
|
|
|
|
fprintf(fh, "{\n");
|
2016-01-15 20:30:53 +08:00
|
|
|
for(j = 0; j < p->M().n; j++) {
|
|
|
|
EntityMap *em = &(p->M().elem[j]);
|
2015-03-29 08:30:52 +08:00
|
|
|
fprintf(fh, " %d %08x %d\n",
|
2008-04-27 18:01:23 +08:00
|
|
|
em->h.v, em->input.v, em->copyNumber);
|
|
|
|
}
|
|
|
|
fprintf(fh, "}");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-03-11 22:43:21 +08:00
|
|
|
case 'i': break;
|
|
|
|
|
2016-05-19 06:51:36 +08:00
|
|
|
default: ssassert(false, "Unexpected value format");
|
2008-04-24 14:22:16 +08:00
|
|
|
}
|
|
|
|
fprintf(fh, "\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-11 22:43:21 +08:00
|
|
|
bool SolveSpaceUI::SaveToFile(const Platform::Path &filename) {
|
|
|
|
// Make sure all the entities are regenerated up to date, since they will be exported.
|
2015-03-19 01:02:11 +08:00
|
|
|
SS.ScheduleShowTW();
|
Convert all enumerations to use `enum class`.
Specifically, take the old code that looks like this:
class Foo {
enum { X = 1, Y = 2 };
int kind;
}
... foo.kind = Foo::X; ...
and convert it to this:
class Foo {
enum class Kind : uint32_t { X = 1, Y = 2 };
Kind kind;
}
... foo.kind = Foo::Kind::X;
(In some cases the enumeration would not be in the class namespace,
such as when it is generally useful.)
The benefits are as follows:
* The type of the field gives a clear indication of intent, both
to humans and tools (such as binding generators).
* The compiler is able to automatically warn when a switch is not
exhaustive; but this is currently suppressed by the
default: ssassert(false, ...)
idiom.
* Integers and plain enums are weakly type checked: they implicitly
convert into each other. This can hide bugs where type conversion
is performed but not intended. Enum classes are strongly type
checked.
* Plain enums pollute parent namespaces; enum classes do not.
Almost every defined enum we have already has a kind of ad-hoc
namespacing via `NAMESPACE_`, which is now explicit.
* Plain enums do not have a well-defined ABI size, which is
important for bindings. Enum classes can have it, if specified.
We specify the base type for all enums as uint32_t, which is
a safe choice and allows us to not change the numeric values
of any variants.
This commit introduces absolutely no functional change to the code,
just renaming and change of types. It handles almost all cases,
except GraphicsWindow::pending.operation, which needs minor
functional change.
2016-05-20 16:31:20 +08:00
|
|
|
SS.GenerateAll(SolveSpaceUI::Generate::ALL);
|
2008-06-13 12:41:27 +08:00
|
|
|
|
2017-03-11 22:43:21 +08:00
|
|
|
for(Group &g : SK.group) {
|
|
|
|
if(g.type != Group::Type::LINKED) continue;
|
|
|
|
|
|
|
|
if(g.linkFile.RelativeTo(filename).IsEmpty()) {
|
|
|
|
Error("This sketch links the sketch '%s'; it can only be saved "
|
|
|
|
"on the same volume.", g.linkFile.raw.c_str());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fh = OpenFile(filename, "wb");
|
2015-03-29 08:30:52 +08:00
|
|
|
if(!fh) {
|
2017-03-11 22:43:21 +08:00
|
|
|
Error("Couldn't write to file '%s'", filename.raw.c_str());
|
2008-04-18 19:11:48 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2008-05-29 18:10:12 +08:00
|
|
|
fprintf(fh, "%s\n\n\n", VERSION_STRING);
|
2008-04-18 19:11:48 +08:00
|
|
|
|
2009-05-19 15:26:38 +08:00
|
|
|
int i, j;
|
2009-04-19 13:53:16 +08:00
|
|
|
for(i = 0; i < SK.group.n; i++) {
|
|
|
|
sv.g = SK.group.elem[i];
|
2017-03-11 22:43:21 +08:00
|
|
|
SaveUsingTable(filename, 'g');
|
2008-04-18 19:11:48 +08:00
|
|
|
fprintf(fh, "AddGroup\n\n");
|
|
|
|
}
|
|
|
|
|
2009-04-19 13:53:16 +08:00
|
|
|
for(i = 0; i < SK.param.n; i++) {
|
|
|
|
sv.p = SK.param.elem[i];
|
2017-03-11 22:43:21 +08:00
|
|
|
SaveUsingTable(filename, 'p');
|
2008-04-18 19:11:48 +08:00
|
|
|
fprintf(fh, "AddParam\n\n");
|
|
|
|
}
|
|
|
|
|
2009-04-19 13:53:16 +08:00
|
|
|
for(i = 0; i < SK.request.n; i++) {
|
|
|
|
sv.r = SK.request.elem[i];
|
2017-03-11 22:43:21 +08:00
|
|
|
SaveUsingTable(filename, 'r');
|
2008-04-18 19:11:48 +08:00
|
|
|
fprintf(fh, "AddRequest\n\n");
|
|
|
|
}
|
|
|
|
|
2009-04-19 13:53:16 +08:00
|
|
|
for(i = 0; i < SK.entity.n; i++) {
|
2016-05-25 20:08:19 +08:00
|
|
|
(SK.entity.elem[i]).CalculateNumerical(/*forExport=*/true);
|
2009-04-19 13:53:16 +08:00
|
|
|
sv.e = SK.entity.elem[i];
|
2017-03-11 22:43:21 +08:00
|
|
|
SaveUsingTable(filename, 'e');
|
2008-04-18 19:11:48 +08:00
|
|
|
fprintf(fh, "AddEntity\n\n");
|
|
|
|
}
|
|
|
|
|
2009-04-19 13:53:16 +08:00
|
|
|
for(i = 0; i < SK.constraint.n; i++) {
|
|
|
|
sv.c = SK.constraint.elem[i];
|
2017-03-11 22:43:21 +08:00
|
|
|
SaveUsingTable(filename, 'c');
|
2008-04-18 19:11:48 +08:00
|
|
|
fprintf(fh, "AddConstraint\n\n");
|
|
|
|
}
|
|
|
|
|
2009-09-17 15:32:36 +08:00
|
|
|
for(i = 0; i < SK.style.n; i++) {
|
|
|
|
sv.s = SK.style.elem[i];
|
|
|
|
if(sv.s.h.v >= Style::FIRST_CUSTOM) {
|
2017-03-11 22:43:21 +08:00
|
|
|
SaveUsingTable(filename, 's');
|
2009-09-17 15:32:36 +08:00
|
|
|
fprintf(fh, "AddStyle\n\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-21 17:06:26 +08:00
|
|
|
// A group will have either a mesh or a shell, but not both; but the code
|
|
|
|
// to print either of those just does nothing if the mesh/shell is empty.
|
|
|
|
|
2016-02-17 18:03:07 +08:00
|
|
|
Group *g = SK.GetGroup(SK.groupOrder.elem[SK.groupOrder.n - 1]);
|
|
|
|
SMesh *m = &g->runningMesh;
|
2008-05-29 18:10:12 +08:00
|
|
|
for(i = 0; i < m->l.n; i++) {
|
|
|
|
STriangle *tr = &(m->l.elem[i]);
|
2015-03-26 18:30:12 +08:00
|
|
|
fprintf(fh, "Triangle %08x %08x "
|
2008-06-04 14:39:32 +08:00
|
|
|
"%.20f %.20f %.20f %.20f %.20f %.20f %.20f %.20f %.20f\n",
|
Replaced RGB-color integers with dedicated data structure
RGB colors were represented using a uint32_t with the red, green and blue
values stuffed into the lower three octets (i.e. 0x00BBGGRR), like
Microsoft's COLORREF. This approach did not lend itself to type safety,
however, so this change replaces it with an RgbColor class that provides
the same infomation plus a handful of useful methods to work with it. (Note
that sizeof(RgbColor) == sizeof(uint32_t), so this change should not lead
to memory bloat.)
Some of the new methods/fields replace what were previously macro calls;
e.g. RED(c) is now c.red, REDf(c) is now c.redF(). The .Equals() method is
now used instead of == to compare colors.
RGB colors still need to be represented as packed integers in file I/O and
preferences, so the methods .FromPackedInt() and .ToPackedInt() are
provided. Also implemented are Cnf{Freeze,Thaw}Color(), type-safe wrappers
around Cnf{Freeze,Thaw}Int() that facilitate I/O with preferences.
(Cnf{Freeze,Thaw}Color() are defined outside of the system-dependent code
to minimize the footprint of the latter; because the same can be done with
Cnf{Freeze,Thaw}Bool(), those are also moved out of the system code with
this commit.)
Color integers were being OR'ed with 0x80000000 in some places for two
distinct purposes: One, to indicate use of a default color in
glxFillMesh(); this has been replaced by use of the .UseDefault() method.
Two, to indicate to TextWindow::Printf() that the format argument of a
"%Bp"/"%Fp" specifier is an RGB color rather than a color "code" from
TextWindow::bgColors[] or TextWindow::fgColors[] (as the specifier can
accept either); instead, we define a new flag "z" (as in "%Bz" or "%Fz") to
indicate an RGBcolor pointer, leaving "%Bp"/"%Fp" to indicate a color code
exclusively.
(This also allows TextWindow::meta[][].bg to be a char instead of an int,
partly compensating for the new .bgRgb field added immediately after.)
In array declarations, RGB colors could previously be specified as 0 (often
in a terminating element). As that no longer works, we define NULL_COLOR,
which serves much the same purpose for RgbColor variables as NULL serves
for pointers.
2013-10-17 04:00:58 +08:00
|
|
|
tr->meta.face, tr->meta.color.ToPackedInt(),
|
2008-05-29 18:10:12 +08:00
|
|
|
CO(tr->a), CO(tr->b), CO(tr->c));
|
|
|
|
}
|
|
|
|
|
2016-02-17 18:03:07 +08:00
|
|
|
SShell *s = &g->runningShell;
|
2009-05-19 15:26:38 +08:00
|
|
|
SSurface *srf;
|
|
|
|
for(srf = s->surface.First(); srf; srf = s->surface.NextAfter(srf)) {
|
|
|
|
fprintf(fh, "Surface %08x %08x %08x %d %d\n",
|
Replaced RGB-color integers with dedicated data structure
RGB colors were represented using a uint32_t with the red, green and blue
values stuffed into the lower three octets (i.e. 0x00BBGGRR), like
Microsoft's COLORREF. This approach did not lend itself to type safety,
however, so this change replaces it with an RgbColor class that provides
the same infomation plus a handful of useful methods to work with it. (Note
that sizeof(RgbColor) == sizeof(uint32_t), so this change should not lead
to memory bloat.)
Some of the new methods/fields replace what were previously macro calls;
e.g. RED(c) is now c.red, REDf(c) is now c.redF(). The .Equals() method is
now used instead of == to compare colors.
RGB colors still need to be represented as packed integers in file I/O and
preferences, so the methods .FromPackedInt() and .ToPackedInt() are
provided. Also implemented are Cnf{Freeze,Thaw}Color(), type-safe wrappers
around Cnf{Freeze,Thaw}Int() that facilitate I/O with preferences.
(Cnf{Freeze,Thaw}Color() are defined outside of the system-dependent code
to minimize the footprint of the latter; because the same can be done with
Cnf{Freeze,Thaw}Bool(), those are also moved out of the system code with
this commit.)
Color integers were being OR'ed with 0x80000000 in some places for two
distinct purposes: One, to indicate use of a default color in
glxFillMesh(); this has been replaced by use of the .UseDefault() method.
Two, to indicate to TextWindow::Printf() that the format argument of a
"%Bp"/"%Fp" specifier is an RGB color rather than a color "code" from
TextWindow::bgColors[] or TextWindow::fgColors[] (as the specifier can
accept either); instead, we define a new flag "z" (as in "%Bz" or "%Fz") to
indicate an RGBcolor pointer, leaving "%Bp"/"%Fp" to indicate a color code
exclusively.
(This also allows TextWindow::meta[][].bg to be a char instead of an int,
partly compensating for the new .bgRgb field added immediately after.)
In array declarations, RGB colors could previously be specified as 0 (often
in a terminating element). As that no longer works, we define NULL_COLOR,
which serves much the same purpose for RgbColor variables as NULL serves
for pointers.
2013-10-17 04:00:58 +08:00
|
|
|
srf->h.v, srf->color.ToPackedInt(), srf->face, srf->degm, srf->degn);
|
2009-05-19 15:26:38 +08:00
|
|
|
for(i = 0; i <= srf->degm; i++) {
|
|
|
|
for(j = 0; j <= srf->degn; j++) {
|
|
|
|
fprintf(fh, "SCtrl %d %d %.20f %.20f %.20f Weight %20.20f\n",
|
|
|
|
i, j, CO(srf->ctrl[i][j]), srf->weight[i][j]);
|
|
|
|
}
|
|
|
|
}
|
2015-03-29 08:30:52 +08:00
|
|
|
|
2009-05-19 15:26:38 +08:00
|
|
|
STrimBy *stb;
|
|
|
|
for(stb = srf->trim.First(); stb; stb = srf->trim.NextAfter(stb)) {
|
|
|
|
fprintf(fh, "TrimBy %08x %d %.20f %.20f %.20f %.20f %.20f %.20f\n",
|
|
|
|
stb->curve.v, stb->backwards ? 1 : 0,
|
|
|
|
CO(stb->start), CO(stb->finish));
|
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(fh, "AddSurface\n");
|
|
|
|
}
|
|
|
|
SCurve *sc;
|
|
|
|
for(sc = s->curve.First(); sc; sc = s->curve.NextAfter(sc)) {
|
|
|
|
fprintf(fh, "Curve %08x %d %d %08x %08x\n",
|
|
|
|
sc->h.v,
|
|
|
|
sc->isExact ? 1 : 0, sc->exact.deg,
|
|
|
|
sc->surfA.v, sc->surfB.v);
|
|
|
|
|
|
|
|
if(sc->isExact) {
|
|
|
|
for(i = 0; i <= sc->exact.deg; i++) {
|
|
|
|
fprintf(fh, "CCtrl %d %.20f %.20f %.20f Weight %.20f\n",
|
|
|
|
i, CO(sc->exact.ctrl[i]), sc->exact.weight[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
SCurvePt *scpt;
|
|
|
|
for(scpt = sc->pts.First(); scpt; scpt = sc->pts.NextAfter(scpt)) {
|
|
|
|
fprintf(fh, "CurvePt %d %.20f %.20f %.20f\n",
|
|
|
|
scpt->vertex ? 1 : 0, CO(scpt->p));
|
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(fh, "AddCurve\n");
|
|
|
|
}
|
|
|
|
|
2008-04-18 19:11:48 +08:00
|
|
|
fclose(fh);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-03-11 22:43:21 +08:00
|
|
|
void SolveSpaceUI::LoadUsingTable(const Platform::Path &filename, char *key, char *val) {
|
2008-04-27 18:01:23 +08:00
|
|
|
int i;
|
|
|
|
for(i = 0; SAVED[i].type != 0; i++) {
|
|
|
|
if(strcmp(SAVED[i].desc, key)==0) {
|
2016-01-15 20:30:53 +08:00
|
|
|
SAVEDptr *p = (SAVEDptr *)SAVED[i].ptr;
|
Use C99 integer types and C++ boolean types/values
This change comprehensively replaces the use of Microsoft-standard integer
and boolean types with their C99/C++ standard equivalents, as the latter is
more appropriate for a cross-platform application. With matter-of-course
exceptions in the Win32-specific code, the types/values have been converted
as follows:
QWORD --> uint64_t
SQWORD --> int64_t
DWORD --> uint32_t
SDWORD --> int32_t
WORD --> uint16_t
SWORD --> int16_t
BYTE --> uint8_t
BOOL --> bool
TRUE --> true
FALSE --> false
The following related changes are also included:
* Added C99 integer type definitions for Windows, as stdint.h is not
available prior to Visual Studio 2010
* Changed types of some variables in the SolveSpace class from 'int' to
'bool', as they actually represent boolean settings
* Implemented new Cnf{Freeze,Thaw}Bool() functions to support boolean
variables in the Registry
* Cnf{Freeze,Thaw}DWORD() are now Cnf{Freeze,Thaw}Int()
* TtfFont::Get{WORD,DWORD}() are now TtfFont::Get{USHORT,ULONG}() (names
inspired by the OpenType spec)
* RGB colors are packed into an integer of type uint32_t (nee DWORD), but
in a few places, these were represented by an int; these have been
corrected to uint32_t
2013-10-02 13:45:13 +08:00
|
|
|
unsigned int u = 0;
|
2008-04-27 18:01:23 +08:00
|
|
|
switch(SAVED[i].fmt) {
|
2016-01-15 20:30:53 +08:00
|
|
|
case 'S': p->S() = val; break;
|
|
|
|
case 'b': p->b() = (atoi(val) != 0); break;
|
|
|
|
case 'd': p->d() = atoi(val); break;
|
|
|
|
case 'f': p->f() = atof(val); break;
|
|
|
|
case 'x': sscanf(val, "%x", &u); p->x()= u; break;
|
2008-04-27 18:01:23 +08:00
|
|
|
|
2017-03-11 22:43:21 +08:00
|
|
|
case 'P': {
|
2016-11-30 00:49:20 +08:00
|
|
|
Platform::Path path = Platform::Path::FromPortable(val);
|
|
|
|
if(!path.IsEmpty()) {
|
|
|
|
p->P() = filename.Parent().Join(path).Expand();
|
|
|
|
}
|
2017-03-11 22:43:21 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-11-04 07:09:17 +08:00
|
|
|
case 'c':
|
|
|
|
sscanf(val, "%x", &u);
|
2016-01-15 20:30:53 +08:00
|
|
|
p->c() = RgbaColor::FromPackedInt(u);
|
2013-11-04 07:09:17 +08:00
|
|
|
break;
|
|
|
|
|
2008-04-27 18:01:23 +08:00
|
|
|
case 'M': {
|
2008-05-11 14:09:46 +08:00
|
|
|
// Don't clear this list! When the group gets added, it
|
|
|
|
// makes a shallow copy, so that would result in us
|
|
|
|
// freeing memory that we want to keep around. Just
|
|
|
|
// zero it out so that new memory is allocated.
|
2016-01-15 20:30:53 +08:00
|
|
|
p->M() = {};
|
2008-04-27 18:01:23 +08:00
|
|
|
for(;;) {
|
|
|
|
EntityMap em;
|
|
|
|
char line2[1024];
|
2013-08-27 04:48:41 +08:00
|
|
|
if (fgets(line2, (int)sizeof(line2), fh) == NULL)
|
|
|
|
break;
|
2008-04-27 18:01:23 +08:00
|
|
|
if(sscanf(line2, "%d %x %d", &(em.h.v), &(em.input.v),
|
|
|
|
&(em.copyNumber)) == 3)
|
|
|
|
{
|
2016-01-15 20:30:53 +08:00
|
|
|
p->M().Add(&em);
|
2008-04-27 18:01:23 +08:00
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-03-11 22:43:21 +08:00
|
|
|
case 'i': break;
|
|
|
|
|
2016-05-19 06:51:36 +08:00
|
|
|
default: ssassert(false, "Unexpected value format");
|
2008-04-27 18:01:23 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2009-12-15 20:26:22 +08:00
|
|
|
if(SAVED[i].type == 0) {
|
|
|
|
fileLoadError = true;
|
|
|
|
}
|
2008-04-27 18:01:23 +08:00
|
|
|
}
|
|
|
|
|
2017-03-11 22:43:21 +08:00
|
|
|
bool SolveSpaceUI::LoadFromFile(const Platform::Path &filename, bool canCancel) {
|
2008-06-25 13:14:49 +08:00
|
|
|
allConsistent = false;
|
2009-12-15 20:26:22 +08:00
|
|
|
fileLoadError = false;
|
2008-06-25 13:14:49 +08:00
|
|
|
|
2017-03-11 22:43:21 +08:00
|
|
|
fh = OpenFile(filename, "rb");
|
2015-03-29 08:30:52 +08:00
|
|
|
if(!fh) {
|
2017-03-11 22:43:21 +08:00
|
|
|
Error("Couldn't read from file '%s'", filename.raw.c_str());
|
2008-04-18 19:11:48 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-03-01 03:23:01 +08:00
|
|
|
ClearExisting();
|
2008-06-12 15:31:41 +08:00
|
|
|
|
2015-03-27 23:31:23 +08:00
|
|
|
sv = {};
|
2009-12-15 20:26:22 +08:00
|
|
|
sv.g.scale = 1; // default is 1, not 0; so legacy files need this
|
2016-02-27 14:15:15 +08:00
|
|
|
Style::FillDefaultStyle(&sv.s);
|
2008-04-24 14:22:16 +08:00
|
|
|
|
|
|
|
char line[1024];
|
2013-08-27 04:09:15 +08:00
|
|
|
while(fgets(line, (int)sizeof(line), fh)) {
|
2008-04-24 14:22:16 +08:00
|
|
|
char *s = strchr(line, '\n');
|
|
|
|
if(s) *s = '\0';
|
2009-01-10 16:18:54 +08:00
|
|
|
// We should never get files with \r characters in them, but mailers
|
|
|
|
// will sometimes mangle attachments.
|
|
|
|
s = strchr(line, '\r');
|
|
|
|
if(s) *s = '\0';
|
2008-04-24 14:22:16 +08:00
|
|
|
|
|
|
|
if(*line == '\0') continue;
|
2015-03-29 08:30:52 +08:00
|
|
|
|
2008-04-24 14:22:16 +08:00
|
|
|
char *e = strchr(line, '=');
|
|
|
|
if(e) {
|
|
|
|
*e = '\0';
|
|
|
|
char *key = line, *val = e+1;
|
2017-03-11 22:43:21 +08:00
|
|
|
LoadUsingTable(filename, key, val);
|
2008-04-24 14:22:16 +08:00
|
|
|
} else if(strcmp(line, "AddGroup")==0) {
|
2016-05-07 13:27:54 +08:00
|
|
|
// legacy files have a spurious dependency between linked groups
|
2016-02-02 13:43:41 +08:00
|
|
|
// and their parent groups, remove
|
Convert all enumerations to use `enum class`.
Specifically, take the old code that looks like this:
class Foo {
enum { X = 1, Y = 2 };
int kind;
}
... foo.kind = Foo::X; ...
and convert it to this:
class Foo {
enum class Kind : uint32_t { X = 1, Y = 2 };
Kind kind;
}
... foo.kind = Foo::Kind::X;
(In some cases the enumeration would not be in the class namespace,
such as when it is generally useful.)
The benefits are as follows:
* The type of the field gives a clear indication of intent, both
to humans and tools (such as binding generators).
* The compiler is able to automatically warn when a switch is not
exhaustive; but this is currently suppressed by the
default: ssassert(false, ...)
idiom.
* Integers and plain enums are weakly type checked: they implicitly
convert into each other. This can hide bugs where type conversion
is performed but not intended. Enum classes are strongly type
checked.
* Plain enums pollute parent namespaces; enum classes do not.
Almost every defined enum we have already has a kind of ad-hoc
namespacing via `NAMESPACE_`, which is now explicit.
* Plain enums do not have a well-defined ABI size, which is
important for bindings. Enum classes can have it, if specified.
We specify the base type for all enums as uint32_t, which is
a safe choice and allows us to not change the numeric values
of any variants.
This commit introduces absolutely no functional change to the code,
just renaming and change of types. It handles almost all cases,
except GraphicsWindow::pending.operation, which needs minor
functional change.
2016-05-20 16:31:20 +08:00
|
|
|
if(sv.g.type == Group::Type::LINKED)
|
2016-02-02 13:43:41 +08:00
|
|
|
sv.g.opA.v = 0;
|
|
|
|
|
2009-04-19 13:53:16 +08:00
|
|
|
SK.group.Add(&(sv.g));
|
2015-03-27 23:31:23 +08:00
|
|
|
sv.g = {};
|
2009-12-15 20:26:22 +08:00
|
|
|
sv.g.scale = 1; // default is 1, not 0; so legacy files need this
|
2008-04-24 14:22:16 +08:00
|
|
|
} else if(strcmp(line, "AddParam")==0) {
|
|
|
|
// params are regenerated, but we want to preload the values
|
|
|
|
// for initial guesses
|
2009-04-19 13:53:16 +08:00
|
|
|
SK.param.Add(&(sv.p));
|
2015-03-27 23:31:23 +08:00
|
|
|
sv.p = {};
|
2008-04-24 14:22:16 +08:00
|
|
|
} else if(strcmp(line, "AddEntity")==0) {
|
|
|
|
// entities are regenerated
|
|
|
|
} else if(strcmp(line, "AddRequest")==0) {
|
2009-04-19 13:53:16 +08:00
|
|
|
SK.request.Add(&(sv.r));
|
2015-03-27 23:31:23 +08:00
|
|
|
sv.r = {};
|
2008-04-24 14:22:16 +08:00
|
|
|
} else if(strcmp(line, "AddConstraint")==0) {
|
2009-04-19 13:53:16 +08:00
|
|
|
SK.constraint.Add(&(sv.c));
|
2015-03-27 23:31:23 +08:00
|
|
|
sv.c = {};
|
2009-09-17 15:32:36 +08:00
|
|
|
} else if(strcmp(line, "AddStyle")==0) {
|
|
|
|
SK.style.Add(&(sv.s));
|
2015-03-27 23:31:23 +08:00
|
|
|
sv.s = {};
|
2016-02-27 14:15:15 +08:00
|
|
|
Style::FillDefaultStyle(&sv.s);
|
2008-05-29 18:10:12 +08:00
|
|
|
} else if(strcmp(line, VERSION_STRING)==0) {
|
2008-04-24 14:22:16 +08:00
|
|
|
// do nothing, version string
|
2009-05-19 15:26:38 +08:00
|
|
|
} else if(StrStartsWith(line, "Triangle ") ||
|
|
|
|
StrStartsWith(line, "Surface ") ||
|
|
|
|
StrStartsWith(line, "SCtrl ") ||
|
|
|
|
StrStartsWith(line, "TrimBy ") ||
|
|
|
|
StrStartsWith(line, "Curve ") ||
|
|
|
|
StrStartsWith(line, "CCtrl ") ||
|
|
|
|
StrStartsWith(line, "CurvePt ") ||
|
|
|
|
strcmp(line, "AddSurface")==0 ||
|
|
|
|
strcmp(line, "AddCurve")==0)
|
|
|
|
{
|
|
|
|
// ignore the mesh or shell, since we regenerate that
|
2008-04-24 14:22:16 +08:00
|
|
|
} else {
|
2009-12-15 20:26:22 +08:00
|
|
|
fileLoadError = true;
|
2008-04-24 14:22:16 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose(fh);
|
|
|
|
|
2009-12-15 20:26:22 +08:00
|
|
|
if(fileLoadError) {
|
2017-01-07 14:41:13 +08:00
|
|
|
Error(_("Unrecognized data in file. This file may be corrupt, or "
|
|
|
|
"from a newer version of the program."));
|
2010-01-25 09:32:36 +08:00
|
|
|
// At least leave the program in a non-crashing state.
|
|
|
|
if(SK.group.n == 0) {
|
|
|
|
NewFile();
|
|
|
|
}
|
2009-12-15 20:26:22 +08:00
|
|
|
}
|
2016-11-30 00:49:20 +08:00
|
|
|
if(!ReloadAllLinked(filename, canCancel)) {
|
2016-11-29 00:20:59 +08:00
|
|
|
return false;
|
|
|
|
}
|
2016-10-11 09:58:04 +08:00
|
|
|
UpgradeLegacyData();
|
|
|
|
|
2008-04-18 19:11:48 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-10-11 09:58:04 +08:00
|
|
|
void SolveSpaceUI::UpgradeLegacyData() {
|
|
|
|
for(Request &r : SK.request) {
|
|
|
|
switch(r.type) {
|
|
|
|
// TTF text requests saved in versions prior to 3.0 only have two
|
|
|
|
// reference points (origin and origin plus v); version 3.0 adds two
|
|
|
|
// more points, and if we don't do anything, then they will appear
|
|
|
|
// at workplane origin, and the solver will mess up the sketch if
|
|
|
|
// it is not fully constrained.
|
|
|
|
case Request::Type::TTF_TEXT: {
|
|
|
|
IdList<Entity,hEntity> entity = {};
|
|
|
|
IdList<Param,hParam> param = {};
|
|
|
|
r.Generate(&entity, ¶m);
|
|
|
|
|
|
|
|
// If we didn't load all of the entities and params that this
|
|
|
|
// request would generate, then add them now, so that we can
|
|
|
|
// force them to their appropriate positions.
|
|
|
|
for(Param &p : param) {
|
|
|
|
if(SK.param.FindByIdNoOops(p.h) != NULL) continue;
|
|
|
|
SK.param.Add(&p);
|
|
|
|
}
|
|
|
|
bool allPointsExist = true;
|
|
|
|
for(Entity &e : entity) {
|
|
|
|
if(SK.entity.FindByIdNoOops(e.h) != NULL) continue;
|
|
|
|
SK.entity.Add(&e);
|
|
|
|
allPointsExist = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!allPointsExist) {
|
|
|
|
Entity *text = entity.FindById(r.h.entity(0));
|
|
|
|
Entity *b = entity.FindById(text->point[2]);
|
|
|
|
Entity *c = entity.FindById(text->point[3]);
|
|
|
|
ExprVector bex, cex;
|
2016-11-30 00:49:20 +08:00
|
|
|
text->RectGetPointsExprs(&bex, &cex);
|
2016-10-11 09:58:04 +08:00
|
|
|
b->PointForceParamTo(bex.Eval());
|
|
|
|
c->PointForceParamTo(cex.Eval());
|
|
|
|
}
|
|
|
|
entity.Clear();
|
|
|
|
param.Clear();
|
Rewrite equations generated for pt-on-line constraints.
Before this commit, pt-on-line constraints are buggy. To reproduce,
extrude a circle, then add a datum point and constrain it to the
axis of the circle, then move it. The cylinder will collapse.
To quote Jonathan:
> On investigation, I (a) confirm that the problem is
> the unconstrained extrusion depth going to zero, and (b) retract
> my earlier statement blaming extrude and other similar non-entity
> parameter treatment for this problem; you can easily reproduce it
> with a point in 3d constrained to lie on any line whose length
> is free.
>
> PT_ON_LINE is written using VectorsParallel, for no obvious reason.
> Rewriting that constraint to work on two projected distances (using
> any two basis vectors perpendicular to the line) should fix that
> problem, since replacing the "point on line in 3d" constraint with
> two "point on line in 2d" constraints works. That still has
> the hairy ball problem of choosing the basis vectors, which you
> can't do with a continuous function; you'd need Vector::Normal()
> or equivalent.
>
> You could write three equations and make the constraint itself
> introduce one new parameter for t. I don't know how well that
> would work numerically, but it would avoid the hairy ball problem,
> perhaps elegant at the cost of speed.
Indeed, this commit implements the latter solution: it introduces
an additional free parameter. The point being coincident with
the start of the line corresponds to the parameter being zero, and
point being coincident with the end corresponds to one).
In effect, instead of constraining two of three degrees of freedom
(for which the equations do not exist because of the hairy ball
theorem), it constrains three and adds one more.
2016-11-02 00:06:57 +08:00
|
|
|
break;
|
2016-10-11 09:58:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
Rewrite equations generated for pt-on-line constraints.
Before this commit, pt-on-line constraints are buggy. To reproduce,
extrude a circle, then add a datum point and constrain it to the
axis of the circle, then move it. The cylinder will collapse.
To quote Jonathan:
> On investigation, I (a) confirm that the problem is
> the unconstrained extrusion depth going to zero, and (b) retract
> my earlier statement blaming extrude and other similar non-entity
> parameter treatment for this problem; you can easily reproduce it
> with a point in 3d constrained to lie on any line whose length
> is free.
>
> PT_ON_LINE is written using VectorsParallel, for no obvious reason.
> Rewriting that constraint to work on two projected distances (using
> any two basis vectors perpendicular to the line) should fix that
> problem, since replacing the "point on line in 3d" constraint with
> two "point on line in 2d" constraints works. That still has
> the hairy ball problem of choosing the basis vectors, which you
> can't do with a continuous function; you'd need Vector::Normal()
> or equivalent.
>
> You could write three equations and make the constraint itself
> introduce one new parameter for t. I don't know how well that
> would work numerically, but it would avoid the hairy ball problem,
> perhaps elegant at the cost of speed.
Indeed, this commit implements the latter solution: it introduces
an additional free parameter. The point being coincident with
the start of the line corresponds to the parameter being zero, and
point being coincident with the end corresponds to one).
In effect, instead of constraining two of three degrees of freedom
(for which the equations do not exist because of the hairy ball
theorem), it constrains three and adds one more.
2016-11-02 00:06:57 +08:00
|
|
|
|
2016-11-27 15:59:47 +08:00
|
|
|
// Constraints saved in versions prior to 3.0 never had any params;
|
|
|
|
// version 3.0 introduced params to constraints to avoid the hairy ball problem,
|
|
|
|
// so force them where they belong.
|
Rewrite equations generated for pt-on-line constraints.
Before this commit, pt-on-line constraints are buggy. To reproduce,
extrude a circle, then add a datum point and constrain it to the
axis of the circle, then move it. The cylinder will collapse.
To quote Jonathan:
> On investigation, I (a) confirm that the problem is
> the unconstrained extrusion depth going to zero, and (b) retract
> my earlier statement blaming extrude and other similar non-entity
> parameter treatment for this problem; you can easily reproduce it
> with a point in 3d constrained to lie on any line whose length
> is free.
>
> PT_ON_LINE is written using VectorsParallel, for no obvious reason.
> Rewriting that constraint to work on two projected distances (using
> any two basis vectors perpendicular to the line) should fix that
> problem, since replacing the "point on line in 3d" constraint with
> two "point on line in 2d" constraints works. That still has
> the hairy ball problem of choosing the basis vectors, which you
> can't do with a continuous function; you'd need Vector::Normal()
> or equivalent.
>
> You could write three equations and make the constraint itself
> introduce one new parameter for t. I don't know how well that
> would work numerically, but it would avoid the hairy ball problem,
> perhaps elegant at the cost of speed.
Indeed, this commit implements the latter solution: it introduces
an additional free parameter. The point being coincident with
the start of the line corresponds to the parameter being zero, and
point being coincident with the end corresponds to one).
In effect, instead of constraining two of three degrees of freedom
(for which the equations do not exist because of the hairy ball
theorem), it constrains three and adds one more.
2016-11-02 00:06:57 +08:00
|
|
|
IdList<Param,hParam> oldParam = {};
|
|
|
|
SK.param.DeepCopyInto(&oldParam);
|
|
|
|
SS.GenerateAll(SolveSpaceUI::Generate::REGEN);
|
2016-11-27 15:59:47 +08:00
|
|
|
|
2017-01-26 00:39:26 +08:00
|
|
|
auto AllParamsExistFor = [&](Constraint &c) {
|
2016-11-27 15:59:47 +08:00
|
|
|
IdList<Param,hParam> param = {};
|
|
|
|
c.Generate(¶m);
|
|
|
|
bool allParamsExist = true;
|
|
|
|
for(Param &p : param) {
|
|
|
|
if(oldParam.FindByIdNoOops(p.h) != NULL) continue;
|
|
|
|
allParamsExist = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
param.Clear();
|
|
|
|
return allParamsExist;
|
|
|
|
};
|
|
|
|
|
Rewrite equations generated for pt-on-line constraints.
Before this commit, pt-on-line constraints are buggy. To reproduce,
extrude a circle, then add a datum point and constrain it to the
axis of the circle, then move it. The cylinder will collapse.
To quote Jonathan:
> On investigation, I (a) confirm that the problem is
> the unconstrained extrusion depth going to zero, and (b) retract
> my earlier statement blaming extrude and other similar non-entity
> parameter treatment for this problem; you can easily reproduce it
> with a point in 3d constrained to lie on any line whose length
> is free.
>
> PT_ON_LINE is written using VectorsParallel, for no obvious reason.
> Rewriting that constraint to work on two projected distances (using
> any two basis vectors perpendicular to the line) should fix that
> problem, since replacing the "point on line in 3d" constraint with
> two "point on line in 2d" constraints works. That still has
> the hairy ball problem of choosing the basis vectors, which you
> can't do with a continuous function; you'd need Vector::Normal()
> or equivalent.
>
> You could write three equations and make the constraint itself
> introduce one new parameter for t. I don't know how well that
> would work numerically, but it would avoid the hairy ball problem,
> perhaps elegant at the cost of speed.
Indeed, this commit implements the latter solution: it introduces
an additional free parameter. The point being coincident with
the start of the line corresponds to the parameter being zero, and
point being coincident with the end corresponds to one).
In effect, instead of constraining two of three degrees of freedom
(for which the equations do not exist because of the hairy ball
theorem), it constrains three and adds one more.
2016-11-02 00:06:57 +08:00
|
|
|
for(Constraint &c : SK.constraint) {
|
|
|
|
switch(c.type) {
|
|
|
|
case Constraint::Type::PT_ON_LINE: {
|
2016-11-27 15:59:47 +08:00
|
|
|
if(AllParamsExistFor(c)) continue;
|
|
|
|
|
|
|
|
EntityBase *eln = SK.GetEntity(c.entityA);
|
|
|
|
EntityBase *ea = SK.GetEntity(eln->point[0]);
|
|
|
|
EntityBase *eb = SK.GetEntity(eln->point[1]);
|
|
|
|
EntityBase *ep = SK.GetEntity(c.ptA);
|
|
|
|
|
|
|
|
ExprVector exp = ep->PointGetExprsInWorkplane(c.workplane);
|
|
|
|
ExprVector exa = ea->PointGetExprsInWorkplane(c.workplane);
|
|
|
|
ExprVector exb = eb->PointGetExprsInWorkplane(c.workplane);
|
|
|
|
ExprVector exba = exb.Minus(exa);
|
|
|
|
Param *p = SK.GetParam(c.h.param(0));
|
|
|
|
p->val = exba.Dot(exp.Minus(exa))->Eval() / exba.Dot(exba)->Eval();
|
|
|
|
break;
|
|
|
|
}
|
Rewrite equations generated for pt-on-line constraints.
Before this commit, pt-on-line constraints are buggy. To reproduce,
extrude a circle, then add a datum point and constrain it to the
axis of the circle, then move it. The cylinder will collapse.
To quote Jonathan:
> On investigation, I (a) confirm that the problem is
> the unconstrained extrusion depth going to zero, and (b) retract
> my earlier statement blaming extrude and other similar non-entity
> parameter treatment for this problem; you can easily reproduce it
> with a point in 3d constrained to lie on any line whose length
> is free.
>
> PT_ON_LINE is written using VectorsParallel, for no obvious reason.
> Rewriting that constraint to work on two projected distances (using
> any two basis vectors perpendicular to the line) should fix that
> problem, since replacing the "point on line in 3d" constraint with
> two "point on line in 2d" constraints works. That still has
> the hairy ball problem of choosing the basis vectors, which you
> can't do with a continuous function; you'd need Vector::Normal()
> or equivalent.
>
> You could write three equations and make the constraint itself
> introduce one new parameter for t. I don't know how well that
> would work numerically, but it would avoid the hairy ball problem,
> perhaps elegant at the cost of speed.
Indeed, this commit implements the latter solution: it introduces
an additional free parameter. The point being coincident with
the start of the line corresponds to the parameter being zero, and
point being coincident with the end corresponds to one).
In effect, instead of constraining two of three degrees of freedom
(for which the equations do not exist because of the hairy ball
theorem), it constrains three and adds one more.
2016-11-02 00:06:57 +08:00
|
|
|
|
2016-11-27 15:59:47 +08:00
|
|
|
case Constraint::Type::CUBIC_LINE_TANGENT: {
|
|
|
|
if(AllParamsExistFor(c)) continue;
|
|
|
|
|
|
|
|
EntityBase *cubic = SK.GetEntity(c.entityA);
|
|
|
|
EntityBase *line = SK.GetEntity(c.entityB);
|
|
|
|
|
|
|
|
ExprVector a;
|
|
|
|
if(c.other) {
|
|
|
|
a = cubic->CubicGetFinishTangentExprs();
|
|
|
|
} else {
|
|
|
|
a = cubic->CubicGetStartTangentExprs();
|
Rewrite equations generated for pt-on-line constraints.
Before this commit, pt-on-line constraints are buggy. To reproduce,
extrude a circle, then add a datum point and constrain it to the
axis of the circle, then move it. The cylinder will collapse.
To quote Jonathan:
> On investigation, I (a) confirm that the problem is
> the unconstrained extrusion depth going to zero, and (b) retract
> my earlier statement blaming extrude and other similar non-entity
> parameter treatment for this problem; you can easily reproduce it
> with a point in 3d constrained to lie on any line whose length
> is free.
>
> PT_ON_LINE is written using VectorsParallel, for no obvious reason.
> Rewriting that constraint to work on two projected distances (using
> any two basis vectors perpendicular to the line) should fix that
> problem, since replacing the "point on line in 3d" constraint with
> two "point on line in 2d" constraints works. That still has
> the hairy ball problem of choosing the basis vectors, which you
> can't do with a continuous function; you'd need Vector::Normal()
> or equivalent.
>
> You could write three equations and make the constraint itself
> introduce one new parameter for t. I don't know how well that
> would work numerically, but it would avoid the hairy ball problem,
> perhaps elegant at the cost of speed.
Indeed, this commit implements the latter solution: it introduces
an additional free parameter. The point being coincident with
the start of the line corresponds to the parameter being zero, and
point being coincident with the end corresponds to one).
In effect, instead of constraining two of three degrees of freedom
(for which the equations do not exist because of the hairy ball
theorem), it constrains three and adds one more.
2016-11-02 00:06:57 +08:00
|
|
|
}
|
2016-11-27 15:59:47 +08:00
|
|
|
|
|
|
|
ExprVector b = line->VectorGetExprs();
|
|
|
|
|
|
|
|
Param *param = SK.GetParam(c.h.param(0));
|
|
|
|
param->val = a.Dot(b)->Eval() / b.Dot(b)->Eval();
|
2016-11-27 16:43:21 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case Constraint::Type::SAME_ORIENTATION: {
|
|
|
|
if(AllParamsExistFor(c)) continue;
|
|
|
|
|
|
|
|
EntityBase *an = SK.GetEntity(c.entityA);
|
|
|
|
EntityBase *bn = SK.GetEntity(c.entityB);
|
|
|
|
|
|
|
|
ExprVector a = an->NormalExprsN();
|
|
|
|
ExprVector b = bn->NormalExprsN();
|
|
|
|
|
|
|
|
Param *param = SK.GetParam(c.h.param(0));
|
|
|
|
param->val = a.Dot(b)->Eval() / b.Dot(b)->Eval();
|
Rewrite equations generated for pt-on-line constraints.
Before this commit, pt-on-line constraints are buggy. To reproduce,
extrude a circle, then add a datum point and constrain it to the
axis of the circle, then move it. The cylinder will collapse.
To quote Jonathan:
> On investigation, I (a) confirm that the problem is
> the unconstrained extrusion depth going to zero, and (b) retract
> my earlier statement blaming extrude and other similar non-entity
> parameter treatment for this problem; you can easily reproduce it
> with a point in 3d constrained to lie on any line whose length
> is free.
>
> PT_ON_LINE is written using VectorsParallel, for no obvious reason.
> Rewriting that constraint to work on two projected distances (using
> any two basis vectors perpendicular to the line) should fix that
> problem, since replacing the "point on line in 3d" constraint with
> two "point on line in 2d" constraints works. That still has
> the hairy ball problem of choosing the basis vectors, which you
> can't do with a continuous function; you'd need Vector::Normal()
> or equivalent.
>
> You could write three equations and make the constraint itself
> introduce one new parameter for t. I don't know how well that
> would work numerically, but it would avoid the hairy ball problem,
> perhaps elegant at the cost of speed.
Indeed, this commit implements the latter solution: it introduces
an additional free parameter. The point being coincident with
the start of the line corresponds to the parameter being zero, and
point being coincident with the end corresponds to one).
In effect, instead of constraining two of three degrees of freedom
(for which the equations do not exist because of the hairy ball
theorem), it constrains three and adds one more.
2016-11-02 00:06:57 +08:00
|
|
|
break;
|
|
|
|
}
|
2016-11-02 01:15:15 +08:00
|
|
|
|
|
|
|
case Constraint::Type::PARALLEL: {
|
2016-11-27 15:59:47 +08:00
|
|
|
if(AllParamsExistFor(c)) continue;
|
2016-11-02 01:15:15 +08:00
|
|
|
|
2016-11-27 15:59:47 +08:00
|
|
|
EntityBase *ea = SK.GetEntity(c.entityA),
|
|
|
|
*eb = SK.GetEntity(c.entityB);
|
|
|
|
ExprVector a = ea->VectorGetExprsInWorkplane(c.workplane);
|
|
|
|
ExprVector b = eb->VectorGetExprsInWorkplane(c.workplane);
|
2016-11-02 01:15:15 +08:00
|
|
|
|
2016-11-27 15:59:47 +08:00
|
|
|
Param *param = SK.GetParam(c.h.param(0));
|
|
|
|
param->val = a.Dot(b)->Eval() / b.Dot(b)->Eval();
|
2016-11-02 01:15:15 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
Rewrite equations generated for pt-on-line constraints.
Before this commit, pt-on-line constraints are buggy. To reproduce,
extrude a circle, then add a datum point and constrain it to the
axis of the circle, then move it. The cylinder will collapse.
To quote Jonathan:
> On investigation, I (a) confirm that the problem is
> the unconstrained extrusion depth going to zero, and (b) retract
> my earlier statement blaming extrude and other similar non-entity
> parameter treatment for this problem; you can easily reproduce it
> with a point in 3d constrained to lie on any line whose length
> is free.
>
> PT_ON_LINE is written using VectorsParallel, for no obvious reason.
> Rewriting that constraint to work on two projected distances (using
> any two basis vectors perpendicular to the line) should fix that
> problem, since replacing the "point on line in 3d" constraint with
> two "point on line in 2d" constraints works. That still has
> the hairy ball problem of choosing the basis vectors, which you
> can't do with a continuous function; you'd need Vector::Normal()
> or equivalent.
>
> You could write three equations and make the constraint itself
> introduce one new parameter for t. I don't know how well that
> would work numerically, but it would avoid the hairy ball problem,
> perhaps elegant at the cost of speed.
Indeed, this commit implements the latter solution: it introduces
an additional free parameter. The point being coincident with
the start of the line corresponds to the parameter being zero, and
point being coincident with the end corresponds to one).
In effect, instead of constraining two of three degrees of freedom
(for which the equations do not exist because of the hairy ball
theorem), it constrains three and adds one more.
2016-11-02 00:06:57 +08:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
oldParam.Clear();
|
2016-10-11 09:58:04 +08:00
|
|
|
}
|
|
|
|
|
2017-03-11 22:43:21 +08:00
|
|
|
bool SolveSpaceUI::LoadEntitiesFromFile(const Platform::Path &filename, EntityList *le,
|
2015-03-27 23:31:23 +08:00
|
|
|
SMesh *m, SShell *sh)
|
2009-05-19 15:26:38 +08:00
|
|
|
{
|
2015-03-27 23:31:23 +08:00
|
|
|
SSurface srf = {};
|
|
|
|
SCurve crv = {};
|
2009-05-19 15:26:38 +08:00
|
|
|
|
2017-03-11 22:43:21 +08:00
|
|
|
fh = OpenFile(filename, "rb");
|
2008-05-29 18:10:12 +08:00
|
|
|
if(!fh) return false;
|
|
|
|
|
|
|
|
le->Clear();
|
2015-03-27 23:31:23 +08:00
|
|
|
sv = {};
|
2008-05-29 18:10:12 +08:00
|
|
|
|
|
|
|
char line[1024];
|
2013-08-27 04:09:15 +08:00
|
|
|
while(fgets(line, (int)sizeof(line), fh)) {
|
2008-05-29 18:10:12 +08:00
|
|
|
char *s = strchr(line, '\n');
|
|
|
|
if(s) *s = '\0';
|
2009-01-10 16:18:54 +08:00
|
|
|
// We should never get files with \r characters in them, but mailers
|
|
|
|
// will sometimes mangle attachments.
|
|
|
|
s = strchr(line, '\r');
|
|
|
|
if(s) *s = '\0';
|
2008-05-29 18:10:12 +08:00
|
|
|
|
|
|
|
if(*line == '\0') continue;
|
2015-03-29 08:30:52 +08:00
|
|
|
|
2008-05-29 18:10:12 +08:00
|
|
|
char *e = strchr(line, '=');
|
|
|
|
if(e) {
|
|
|
|
*e = '\0';
|
|
|
|
char *key = line, *val = e+1;
|
2017-03-11 22:43:21 +08:00
|
|
|
LoadUsingTable(filename, key, val);
|
2008-05-29 18:10:12 +08:00
|
|
|
} else if(strcmp(line, "AddGroup")==0) {
|
2008-06-04 18:22:30 +08:00
|
|
|
// Don't leak memory; these get allocated whether we want them
|
|
|
|
// or not.
|
|
|
|
sv.g.remap.Clear();
|
2008-05-29 18:10:12 +08:00
|
|
|
} else if(strcmp(line, "AddParam")==0) {
|
|
|
|
|
|
|
|
} else if(strcmp(line, "AddEntity")==0) {
|
|
|
|
le->Add(&(sv.e));
|
2015-03-27 23:31:23 +08:00
|
|
|
sv.e = {};
|
2008-05-29 18:10:12 +08:00
|
|
|
} else if(strcmp(line, "AddRequest")==0) {
|
|
|
|
|
|
|
|
} else if(strcmp(line, "AddConstraint")==0) {
|
2008-06-14 16:43:38 +08:00
|
|
|
|
2009-09-24 23:52:48 +08:00
|
|
|
} else if(strcmp(line, "AddStyle")==0) {
|
|
|
|
|
2008-05-29 18:10:12 +08:00
|
|
|
} else if(strcmp(line, VERSION_STRING)==0) {
|
|
|
|
|
2009-05-19 15:26:38 +08:00
|
|
|
} else if(StrStartsWith(line, "Triangle ")) {
|
2015-03-27 23:31:23 +08:00
|
|
|
STriangle tr = {};
|
2015-03-26 18:30:12 +08:00
|
|
|
unsigned int rgba = 0;
|
2008-05-30 14:09:41 +08:00
|
|
|
if(sscanf(line, "Triangle %x %x "
|
|
|
|
"%lf %lf %lf %lf %lf %lf %lf %lf %lf",
|
2015-03-26 18:30:12 +08:00
|
|
|
&(tr.meta.face), &rgba,
|
2015-03-29 08:30:52 +08:00
|
|
|
&(tr.a.x), &(tr.a.y), &(tr.a.z),
|
|
|
|
&(tr.b.x), &(tr.b.y), &(tr.b.z),
|
2015-03-26 18:30:12 +08:00
|
|
|
&(tr.c.x), &(tr.c.y), &(tr.c.z)) != 11) {
|
2016-05-19 06:51:36 +08:00
|
|
|
ssassert(false, "Unexpected Triangle format");
|
2008-05-29 18:10:12 +08:00
|
|
|
}
|
2015-07-10 19:54:39 +08:00
|
|
|
tr.meta.color = RgbaColor::FromPackedInt((uint32_t)rgba);
|
2008-05-29 18:10:12 +08:00
|
|
|
m->AddTriangle(&tr);
|
2009-05-19 15:26:38 +08:00
|
|
|
} else if(StrStartsWith(line, "Surface ")) {
|
2015-03-26 18:30:12 +08:00
|
|
|
unsigned int rgba = 0;
|
2009-05-19 15:26:38 +08:00
|
|
|
if(sscanf(line, "Surface %x %x %x %d %d",
|
2015-03-26 18:30:12 +08:00
|
|
|
&(srf.h.v), &rgba, &(srf.face),
|
|
|
|
&(srf.degm), &(srf.degn)) != 5) {
|
2016-05-19 06:51:36 +08:00
|
|
|
ssassert(false, "Unexpected Surface format");
|
2009-05-19 15:26:38 +08:00
|
|
|
}
|
2015-07-10 19:54:39 +08:00
|
|
|
srf.color = RgbaColor::FromPackedInt((uint32_t)rgba);
|
2009-05-19 15:26:38 +08:00
|
|
|
} else if(StrStartsWith(line, "SCtrl ")) {
|
|
|
|
int i, j;
|
|
|
|
Vector c;
|
|
|
|
double w;
|
|
|
|
if(sscanf(line, "SCtrl %d %d %lf %lf %lf Weight %lf",
|
|
|
|
&i, &j, &(c.x), &(c.y), &(c.z), &w) != 6)
|
|
|
|
{
|
2016-05-19 06:51:36 +08:00
|
|
|
ssassert(false, "Unexpected SCtrl format");
|
2009-05-19 15:26:38 +08:00
|
|
|
}
|
|
|
|
srf.ctrl[i][j] = c;
|
|
|
|
srf.weight[i][j] = w;
|
|
|
|
} else if(StrStartsWith(line, "TrimBy ")) {
|
2015-03-27 23:31:23 +08:00
|
|
|
STrimBy stb = {};
|
2009-05-19 15:26:38 +08:00
|
|
|
int backwards;
|
|
|
|
if(sscanf(line, "TrimBy %x %d %lf %lf %lf %lf %lf %lf",
|
|
|
|
&(stb.curve.v), &backwards,
|
|
|
|
&(stb.start.x), &(stb.start.y), &(stb.start.z),
|
|
|
|
&(stb.finish.x), &(stb.finish.y), &(stb.finish.z)) != 8)
|
|
|
|
{
|
2016-05-19 06:51:36 +08:00
|
|
|
ssassert(false, "Unexpected TrimBy format");
|
2009-05-19 15:26:38 +08:00
|
|
|
}
|
|
|
|
stb.backwards = (backwards != 0);
|
|
|
|
srf.trim.Add(&stb);
|
|
|
|
} else if(strcmp(line, "AddSurface")==0) {
|
|
|
|
sh->surface.Add(&srf);
|
2015-03-27 23:31:23 +08:00
|
|
|
srf = {};
|
2009-05-19 15:26:38 +08:00
|
|
|
} else if(StrStartsWith(line, "Curve ")) {
|
|
|
|
int isExact;
|
|
|
|
if(sscanf(line, "Curve %x %d %d %x %x",
|
|
|
|
&(crv.h.v),
|
|
|
|
&(isExact),
|
|
|
|
&(crv.exact.deg),
|
|
|
|
&(crv.surfA.v), &(crv.surfB.v)) != 5)
|
|
|
|
{
|
2016-05-19 06:51:36 +08:00
|
|
|
ssassert(false, "Unexpected Curve format");
|
2009-05-19 15:26:38 +08:00
|
|
|
}
|
|
|
|
crv.isExact = (isExact != 0);
|
|
|
|
} else if(StrStartsWith(line, "CCtrl ")) {
|
|
|
|
int i;
|
|
|
|
Vector c;
|
|
|
|
double w;
|
|
|
|
if(sscanf(line, "CCtrl %d %lf %lf %lf Weight %lf",
|
|
|
|
&i, &(c.x), &(c.y), &(c.z), &w) != 5)
|
|
|
|
{
|
2016-05-19 06:51:36 +08:00
|
|
|
ssassert(false, "Unexpected CCtrl format");
|
2009-05-19 15:26:38 +08:00
|
|
|
}
|
|
|
|
crv.exact.ctrl[i] = c;
|
|
|
|
crv.exact.weight[i] = w;
|
|
|
|
} else if(StrStartsWith(line, "CurvePt ")) {
|
|
|
|
SCurvePt scpt;
|
|
|
|
int vertex;
|
|
|
|
if(sscanf(line, "CurvePt %d %lf %lf %lf",
|
|
|
|
&vertex,
|
|
|
|
&(scpt.p.x), &(scpt.p.y), &(scpt.p.z)) != 4)
|
|
|
|
{
|
2016-05-19 06:51:36 +08:00
|
|
|
ssassert(false, "Unexpected CurvePt format");
|
2009-05-19 15:26:38 +08:00
|
|
|
}
|
|
|
|
scpt.vertex = (vertex != 0);
|
|
|
|
crv.pts.Add(&scpt);
|
|
|
|
} else if(strcmp(line, "AddCurve")==0) {
|
|
|
|
sh->curve.Add(&crv);
|
2015-03-27 23:31:23 +08:00
|
|
|
crv = {};
|
2016-05-19 06:51:36 +08:00
|
|
|
} else ssassert(false, "Unexpected operation");
|
2008-05-29 18:10:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
fclose(fh);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-11-30 00:49:20 +08:00
|
|
|
bool SolveSpaceUI::ReloadAllLinked(const Platform::Path &saveFile, bool canCancel) {
|
2017-03-11 22:43:21 +08:00
|
|
|
std::map<Platform::Path, Platform::Path, Platform::PathLess> linkMap;
|
2015-12-27 14:35:46 +08:00
|
|
|
|
2008-06-25 13:14:49 +08:00
|
|
|
allConsistent = false;
|
2016-11-30 00:49:20 +08:00
|
|
|
|
2017-03-11 22:43:21 +08:00
|
|
|
for(Group &g : SK.group) {
|
|
|
|
if(g.type != Group::Type::LINKED) continue;
|
2008-06-25 13:14:49 +08:00
|
|
|
|
2017-03-11 22:43:21 +08:00
|
|
|
g.impEntity.Clear();
|
|
|
|
g.impMesh.Clear();
|
|
|
|
g.impShell.Clear();
|
2008-06-25 13:14:49 +08:00
|
|
|
|
2017-03-11 22:43:21 +08:00
|
|
|
// If we prompted for this specific file before, don't ask again.
|
|
|
|
if(linkMap.count(g.linkFile)) {
|
|
|
|
g.linkFile = linkMap[g.linkFile];
|
2016-01-11 20:18:18 +08:00
|
|
|
}
|
|
|
|
|
2017-03-11 22:43:21 +08:00
|
|
|
try_again:
|
|
|
|
if(LoadEntitiesFromFile(g.linkFile, &g.impEntity, &g.impMesh, &g.impShell)) {
|
2016-11-30 00:49:20 +08:00
|
|
|
// We loaded the data, good. Now import its dependencies as well.
|
|
|
|
for(Entity &e : g.impEntity) {
|
|
|
|
if(e.type != Entity::Type::IMAGE) continue;
|
|
|
|
if(!ReloadLinkedImage(g.linkFile, &e.file, canCancel)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2017-03-11 22:43:21 +08:00
|
|
|
} else if(linkMap.count(g.linkFile) == 0) {
|
|
|
|
// The file was moved; prompt the user for its new location.
|
2016-11-30 00:49:20 +08:00
|
|
|
switch(LocateImportedFileYesNoCancel(g.linkFile.RelativeTo(saveFile), canCancel)) {
|
2016-01-11 20:18:18 +08:00
|
|
|
case DIALOG_YES: {
|
2017-03-11 22:43:21 +08:00
|
|
|
Platform::Path newLinkFile;
|
|
|
|
if(GetOpenFile(&newLinkFile, "", SlvsFileFilter)) {
|
|
|
|
linkMap[g.linkFile] = newLinkFile;
|
|
|
|
g.linkFile = newLinkFile;
|
|
|
|
goto try_again;
|
2016-01-11 20:18:18 +08:00
|
|
|
} else {
|
2017-03-11 22:43:21 +08:00
|
|
|
if(canCancel) return false;
|
|
|
|
break;
|
2016-01-11 20:18:18 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
case DIALOG_NO:
|
2017-03-11 22:43:21 +08:00
|
|
|
linkMap[g.linkFile].Clear();
|
|
|
|
// Geometry will be pruned by GenerateAll().
|
2016-01-11 20:18:18 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case DIALOG_CANCEL:
|
|
|
|
return false;
|
|
|
|
}
|
2008-06-25 13:14:49 +08:00
|
|
|
} else {
|
2017-03-11 22:43:21 +08:00
|
|
|
// User was already asked to and refused to locate a missing linked file.
|
2008-05-29 18:10:12 +08:00
|
|
|
}
|
|
|
|
}
|
2016-01-11 20:18:18 +08:00
|
|
|
|
2016-11-30 00:49:20 +08:00
|
|
|
for(Request &r : SK.request) {
|
|
|
|
if(r.type != Request::Type::IMAGE) continue;
|
|
|
|
|
|
|
|
if(!ReloadLinkedImage(saveFile, &r.file, canCancel)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-11 20:18:18 +08:00
|
|
|
return true;
|
2008-05-29 18:10:12 +08:00
|
|
|
}
|
|
|
|
|
2016-11-30 00:49:20 +08:00
|
|
|
bool SolveSpaceUI::ReloadLinkedImage(const Platform::Path &saveFile,
|
|
|
|
Platform::Path *filename, bool canCancel) {
|
|
|
|
std::shared_ptr<Pixmap> pixmap;
|
|
|
|
bool promptOpenFile = false;
|
|
|
|
if(filename->IsEmpty()) {
|
|
|
|
// We're prompting the user for a new image.
|
|
|
|
promptOpenFile = true;
|
|
|
|
} else {
|
|
|
|
auto image = SS.images.find(*filename);
|
|
|
|
if(image != SS.images.end()) return true;
|
|
|
|
|
|
|
|
pixmap = Pixmap::ReadPng(*filename);
|
|
|
|
if(pixmap == NULL) {
|
|
|
|
// The file was moved; prompt the user for its new location.
|
|
|
|
switch(LocateImportedFileYesNoCancel(filename->RelativeTo(saveFile), canCancel)) {
|
|
|
|
case DIALOG_YES:
|
|
|
|
promptOpenFile = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DIALOG_NO:
|
|
|
|
// We don't know where the file is, record it as absent.
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DIALOG_CANCEL:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(promptOpenFile) {
|
|
|
|
if(GetOpenFile(filename, "", RasterFileFilter)) {
|
|
|
|
pixmap = Pixmap::ReadPng(*filename);
|
|
|
|
if(pixmap == NULL) {
|
|
|
|
Error("The image '%s' is corrupted.", filename->raw.c_str());
|
|
|
|
}
|
|
|
|
// We know where the file is now, good.
|
|
|
|
} else if(canCancel) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// We loaded the data, good.
|
|
|
|
SS.images[*filename] = pixmap;
|
|
|
|
return true;
|
|
|
|
}
|