2008-03-25 10:02:13 +00:00
|
|
|
#include "solvespace.h"
|
|
|
|
|
|
|
|
SolveSpace SS;
|
|
|
|
|
2008-04-24 06:22:16 +00:00
|
|
|
void SolveSpace::Init(char *cmdLine) {
|
|
|
|
if(strlen(cmdLine) == 0) {
|
|
|
|
NewFile();
|
|
|
|
} else {
|
|
|
|
LoadFromFile(cmdLine);
|
|
|
|
}
|
2008-04-08 12:54:53 +00:00
|
|
|
|
2008-05-25 13:11:44 +00:00
|
|
|
GenerateAll(false, 0, INT_MAX);
|
|
|
|
|
2008-04-13 10:57:41 +00:00
|
|
|
TW.Init();
|
|
|
|
GW.Init();
|
|
|
|
|
2008-05-26 09:56:50 +00:00
|
|
|
GenerateAll(true, 0, INT_MAX);
|
2008-04-25 08:26:15 +00:00
|
|
|
TW.Show();
|
2008-04-08 12:54:53 +00:00
|
|
|
}
|
|
|
|
|
2008-05-17 06:04:55 +00:00
|
|
|
bool SolveSpace::PruneOrphans(void) {
|
|
|
|
int i;
|
|
|
|
for(i = 0; i < request.n; i++) {
|
|
|
|
Request *r = &(request.elem[i]);
|
|
|
|
if(GroupExists(r->group)) continue;
|
|
|
|
|
|
|
|
(deleted.requests)++;
|
|
|
|
request.RemoveById(r->h);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
for(i = 0; i < constraint.n; i++) {
|
|
|
|
Constraint *c = &(constraint.elem[i]);
|
|
|
|
if(GroupExists(c->group)) continue;
|
|
|
|
|
|
|
|
(deleted.constraints)++;
|
|
|
|
constraint.RemoveById(c->h);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SolveSpace::GroupsInOrder(hGroup before, hGroup after) {
|
|
|
|
if(before.v == 0) return true;
|
|
|
|
if(after.v == 0) return true;
|
|
|
|
|
|
|
|
int beforep = -1, afterp = -1;
|
|
|
|
int i;
|
|
|
|
for(i = 0; i < group.n; i++) {
|
|
|
|
Group *g = &(group.elem[i]);
|
|
|
|
if(g->h.v == before.v) beforep = i;
|
|
|
|
if(g->h.v == after.v) afterp = i;
|
|
|
|
}
|
|
|
|
if(beforep < 0 || afterp < 0) return false;
|
|
|
|
if(beforep >= afterp) return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SolveSpace::GroupExists(hGroup hg) {
|
|
|
|
// A nonexistent group is not acceptable
|
|
|
|
return group.FindByIdNoOops(hg) ? true : false;
|
|
|
|
}
|
|
|
|
bool SolveSpace::EntityExists(hEntity he) {
|
|
|
|
// A nonexstient entity is acceptable, though, usually just means it
|
|
|
|
// doesn't apply.
|
|
|
|
if(he.v == Entity::NO_ENTITY.v) return true;
|
|
|
|
return entity.FindByIdNoOops(he) ? true : false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SolveSpace::PruneGroups(hGroup hg) {
|
|
|
|
Group *g = GetGroup(hg);
|
|
|
|
if(GroupsInOrder(g->opA, hg) &&
|
|
|
|
GroupsInOrder(g->opB, hg) &&
|
|
|
|
EntityExists(g->wrkpl.origin) &&
|
|
|
|
EntityExists(g->wrkpl.entityB) &&
|
|
|
|
EntityExists(g->wrkpl.entityC))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
(deleted.groups)++;
|
|
|
|
group.RemoveById(g->h);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SolveSpace::PruneRequests(hGroup hg) {
|
|
|
|
int i;
|
|
|
|
for(i = 0; i < entity.n; i++) {
|
|
|
|
Entity *e = &(entity.elem[i]);
|
|
|
|
if(e->group.v != hg.v) continue;
|
|
|
|
|
|
|
|
if(EntityExists(e->workplane)) continue;
|
|
|
|
|
|
|
|
if(!e->h.isFromRequest()) oops();
|
|
|
|
|
|
|
|
(deleted.requests)++;
|
|
|
|
request.RemoveById(e->h.request());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SolveSpace::PruneConstraints(hGroup hg) {
|
|
|
|
int i;
|
|
|
|
for(i = 0; i < constraint.n; i++) {
|
|
|
|
Constraint *c = &(constraint.elem[i]);
|
|
|
|
if(c->group.v != hg.v) continue;
|
|
|
|
|
|
|
|
if(EntityExists(c->workplane) &&
|
|
|
|
EntityExists(c->ptA) &&
|
|
|
|
EntityExists(c->ptB) &&
|
|
|
|
EntityExists(c->ptC) &&
|
|
|
|
EntityExists(c->entityA) &&
|
|
|
|
EntityExists(c->entityB))
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
(deleted.constraints)++;
|
|
|
|
constraint.RemoveById(c->h);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2008-04-27 09:03:01 +00:00
|
|
|
void SolveSpace::GenerateAll(bool andSolve) {
|
2008-05-25 13:11:44 +00:00
|
|
|
int i;
|
|
|
|
int firstShown = INT_MAX, lastShown = 0;
|
|
|
|
// The references don't count, so start from group 1
|
|
|
|
for(i = 1; i < group.n; i++) {
|
2008-05-26 09:56:50 +00:00
|
|
|
Group *g = &(group.elem[i]);
|
|
|
|
if(g->visible || (g->solved.how != Group::SOLVED_OKAY)) {
|
2008-05-25 13:11:44 +00:00
|
|
|
firstShown = min(firstShown, i);
|
|
|
|
lastShown = max(lastShown, i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Even if nothing is shown, we have to keep going; the entities get
|
|
|
|
// generated for hidden groups, even though they're not solved.
|
|
|
|
GenerateAll(andSolve, firstShown, lastShown);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SolveSpace::GenerateAll(bool andSolve, int first, int last) {
|
2008-04-27 09:03:01 +00:00
|
|
|
int i, j;
|
2008-04-08 12:54:53 +00:00
|
|
|
|
2008-05-17 06:04:55 +00:00
|
|
|
while(PruneOrphans())
|
|
|
|
;
|
|
|
|
|
2008-04-27 05:00:12 +00:00
|
|
|
// Don't lose our numerical guesses when we regenerate.
|
2008-04-13 10:57:41 +00:00
|
|
|
IdList<Param,hParam> prev;
|
|
|
|
param.MoveSelfInto(&prev);
|
2008-04-08 12:54:53 +00:00
|
|
|
entity.Clear();
|
2008-04-27 09:03:01 +00:00
|
|
|
|
|
|
|
for(i = 0; i < group.n; i++) {
|
|
|
|
Group *g = &(group.elem[i]);
|
|
|
|
|
2008-05-17 06:04:55 +00:00
|
|
|
// The group may depend on entities or other groups, to define its
|
|
|
|
// workplane geometry or for its operands. Those must already exist
|
|
|
|
// in a previous group, so check them before generating.
|
|
|
|
if(PruneGroups(g->h))
|
|
|
|
goto pruned;
|
|
|
|
|
2008-04-27 09:03:01 +00:00
|
|
|
for(j = 0; j < request.n; j++) {
|
|
|
|
Request *r = &(request.elem[j]);
|
|
|
|
if(r->group.v != g->h.v) continue;
|
|
|
|
|
|
|
|
r->Generate(&entity, ¶m);
|
|
|
|
}
|
|
|
|
g->Generate(&entity, ¶m);
|
|
|
|
|
2008-05-17 06:04:55 +00:00
|
|
|
// The requests and constraints depend on stuff in this or the
|
|
|
|
// previous group, so check them after generating.
|
|
|
|
if(PruneRequests(g->h) || PruneConstraints(g->h))
|
|
|
|
goto pruned;
|
|
|
|
|
2008-04-27 09:03:01 +00:00
|
|
|
// Use the previous values for params that we've seen before, as
|
|
|
|
// initial guesses for the solver.
|
|
|
|
for(j = 0; j < param.n; j++) {
|
|
|
|
Param *newp = &(param.elem[j]);
|
|
|
|
if(newp->known) continue;
|
|
|
|
|
|
|
|
Param *prevp = prev.FindByIdNoOops(newp->h);
|
|
|
|
if(prevp) newp->val = prevp->val;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(g->h.v == Group::HGROUP_REFERENCES.v) {
|
|
|
|
ForceReferences();
|
2008-05-26 09:56:50 +00:00
|
|
|
g->solved.how = Group::SOLVED_OKAY;
|
2008-04-27 09:03:01 +00:00
|
|
|
} else {
|
2008-05-25 13:11:44 +00:00
|
|
|
if(i >= first && i <= last) {
|
|
|
|
// The group falls inside the range, so really solve it,
|
|
|
|
// and then regenerate the mesh based on the solved stuff.
|
|
|
|
if(andSolve) SolveGroup(g->h);
|
|
|
|
g->MakePolygons();
|
|
|
|
} else {
|
|
|
|
// The group falls outside the range, so just assume that
|
|
|
|
// it's good wherever we left it. The mesh is unchanged,
|
|
|
|
// and the parameters must be marked as known.
|
|
|
|
for(j = 0; j < param.n; j++) {
|
|
|
|
Param *newp = &(param.elem[j]);
|
|
|
|
|
|
|
|
Param *prevp = prev.FindByIdNoOops(newp->h);
|
|
|
|
if(prevp) newp->known = true;
|
|
|
|
}
|
|
|
|
}
|
2008-04-13 10:57:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-04-20 11:35:10 +00:00
|
|
|
prev.Clear();
|
2008-04-24 06:22:16 +00:00
|
|
|
InvalidateGraphics();
|
2008-05-17 06:04:55 +00:00
|
|
|
|
2008-05-17 11:15:14 +00:00
|
|
|
// Remove nonexistent selection items, for same reason we waited till
|
|
|
|
// the end to put up a dialog box.
|
|
|
|
GW.ClearNonexistentSelectionItems();
|
|
|
|
|
2008-05-17 06:04:55 +00:00
|
|
|
if(deleted.requests > 0 || deleted.constraints > 0 || deleted.groups > 0) {
|
|
|
|
// Don't display any errors until we've regenerated fully. The
|
|
|
|
// sketch is not necessarily in a consistent state until we've
|
|
|
|
// pruned any orphaned etc. objects, and the message loop for the
|
|
|
|
// messagebox could allow us to repaint and crash. But now we must
|
|
|
|
// be fine.
|
|
|
|
Error("Additional sketch elements were deleted, because they depend "
|
|
|
|
"on the element that was just deleted explicitly. These "
|
|
|
|
"include: \r\n"
|
|
|
|
" %d request%s\r\n"
|
|
|
|
" %d constraint%s\r\n"
|
|
|
|
" %d group%s\r\n\r\n"
|
|
|
|
"Choose Edit -> Undo to undelete all elements.",
|
|
|
|
deleted.requests, deleted.requests == 1 ? "" : "s",
|
|
|
|
deleted.constraints, deleted.constraints == 1 ? "" : "s",
|
|
|
|
deleted.groups, deleted.groups == 1 ? "" : "s");
|
|
|
|
memset(&deleted, 0, sizeof(deleted));
|
2008-05-18 02:18:47 +00:00
|
|
|
// All sorts of interesting things could have happened; for example,
|
|
|
|
// the active group or active workplane could have been deleted. So
|
|
|
|
// clear all that out.
|
|
|
|
GW.ClearSuper();
|
2008-05-17 06:04:55 +00:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
|
|
|
|
pruned:
|
|
|
|
// Restore the numerical guesses
|
|
|
|
param.Clear();
|
|
|
|
prev.MoveSelfInto(¶m);
|
|
|
|
// Try again
|
2008-05-25 13:11:44 +00:00
|
|
|
GenerateAll(andSolve, first, last);
|
2008-04-12 16:28:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SolveSpace::ForceReferences(void) {
|
2008-04-09 08:39:01 +00:00
|
|
|
// Force the values of the paramters that define the three reference
|
|
|
|
// coordinate systems.
|
|
|
|
static const struct {
|
2008-05-05 06:18:01 +00:00
|
|
|
hRequest hr;
|
|
|
|
Quaternion q;
|
2008-04-09 08:39:01 +00:00
|
|
|
} Quat[] = {
|
2008-05-05 06:18:01 +00:00
|
|
|
{ Request::HREQUEST_REFERENCE_XY, { 1, 0, 0, 0, } },
|
|
|
|
{ Request::HREQUEST_REFERENCE_YZ, { 0.5, 0.5, 0.5, 0.5, } },
|
|
|
|
{ Request::HREQUEST_REFERENCE_ZX, { 0.5, -0.5, -0.5, -0.5, } },
|
2008-04-09 08:39:01 +00:00
|
|
|
};
|
2008-04-12 16:28:48 +00:00
|
|
|
for(int i = 0; i < 3; i++) {
|
2008-04-19 11:09:47 +00:00
|
|
|
hRequest hr = Quat[i].hr;
|
2008-05-05 06:18:01 +00:00
|
|
|
Entity *wrkpl = GetEntity(hr.entity(0));
|
2008-04-09 08:39:01 +00:00
|
|
|
// The origin for our coordinate system, always zero
|
2008-05-05 06:18:01 +00:00
|
|
|
Entity *origin = GetEntity(wrkpl->point[0]);
|
|
|
|
origin->PointForceTo(Vector::MakeFrom(0, 0, 0));
|
2008-04-27 09:03:01 +00:00
|
|
|
GetParam(origin->param[0])->known = true;
|
|
|
|
GetParam(origin->param[1])->known = true;
|
|
|
|
GetParam(origin->param[2])->known = true;
|
2008-04-09 08:39:01 +00:00
|
|
|
// The quaternion that defines the rotation, from the table.
|
2008-05-05 06:18:01 +00:00
|
|
|
Entity *normal = GetEntity(wrkpl->normal);
|
|
|
|
normal->NormalForceTo(Quat[i].q);
|
|
|
|
GetParam(normal->param[0])->known = true;
|
|
|
|
GetParam(normal->param[1])->known = true;
|
|
|
|
GetParam(normal->param[2])->known = true;
|
|
|
|
GetParam(normal->param[3])->known = true;
|
2008-04-08 12:54:53 +00:00
|
|
|
}
|
2008-03-25 10:02:13 +00:00
|
|
|
}
|
|
|
|
|
2008-05-26 09:56:50 +00:00
|
|
|
void SolveSpace::SolveGroup(hGroup hg) {
|
2008-04-20 11:35:10 +00:00
|
|
|
int i;
|
|
|
|
// Clear out the system to be solved.
|
|
|
|
sys.entity.Clear();
|
|
|
|
sys.param.Clear();
|
|
|
|
sys.eq.Clear();
|
|
|
|
// And generate all the params for requests in this group
|
|
|
|
for(i = 0; i < request.n; i++) {
|
|
|
|
Request *r = &(request.elem[i]);
|
|
|
|
if(r->group.v != hg.v) continue;
|
|
|
|
|
|
|
|
r->Generate(&(sys.entity), &(sys.param));
|
|
|
|
}
|
2008-04-27 09:03:01 +00:00
|
|
|
// And for the group itself
|
|
|
|
Group *g = SS.GetGroup(hg);
|
|
|
|
g->Generate(&(sys.entity), &(sys.param));
|
2008-04-20 11:35:10 +00:00
|
|
|
// Set the initial guesses for all the params
|
|
|
|
for(i = 0; i < sys.param.n; i++) {
|
|
|
|
Param *p = &(sys.param.elem[i]);
|
|
|
|
p->known = false;
|
|
|
|
p->val = GetParam(p->h)->val;
|
|
|
|
}
|
|
|
|
|
2008-05-26 09:56:50 +00:00
|
|
|
sys.Solve(g);
|
2008-04-25 10:11:29 +00:00
|
|
|
FreeAllTemporary();
|
2008-04-20 11:35:10 +00:00
|
|
|
}
|
|
|
|
|
2008-04-18 11:11:48 +00:00
|
|
|
void SolveSpace::MenuFile(int id) {
|
|
|
|
switch(id) {
|
|
|
|
case GraphicsWindow::MNU_NEW:
|
2008-04-24 06:22:16 +00:00
|
|
|
SS.NewFile();
|
2008-04-27 09:03:01 +00:00
|
|
|
SS.GenerateAll(false);
|
2008-04-25 07:04:09 +00:00
|
|
|
SS.TW.Init();
|
2008-05-25 13:11:44 +00:00
|
|
|
SS.GW.Init();
|
2008-04-25 08:26:15 +00:00
|
|
|
SS.TW.Show();
|
2008-04-24 06:22:16 +00:00
|
|
|
break;
|
|
|
|
|
2008-04-18 11:11:48 +00:00
|
|
|
case GraphicsWindow::MNU_OPEN:
|
2008-04-24 06:22:16 +00:00
|
|
|
break;
|
2008-04-18 11:11:48 +00:00
|
|
|
|
|
|
|
case GraphicsWindow::MNU_SAVE:
|
2008-04-24 06:22:16 +00:00
|
|
|
SS.SaveToFile("t.slvs");
|
2008-04-18 11:11:48 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case GraphicsWindow::MNU_SAVE_AS:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GraphicsWindow::MNU_EXIT:
|
|
|
|
break;
|
|
|
|
|
|
|
|
default: oops();
|
|
|
|
}
|
|
|
|
}
|