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) {
|
2008-06-11 04:22:52 +00:00
|
|
|
int i;
|
|
|
|
// Default list of colors for the model material
|
|
|
|
modelColor[0] = CnfThawDWORD(RGB(150, 150, 150), "ModelColor_0");
|
|
|
|
modelColor[1] = CnfThawDWORD(RGB(100, 100, 100), "ModelColor_1");
|
|
|
|
modelColor[2] = CnfThawDWORD(RGB( 30, 30, 30), "ModelColor_2");
|
|
|
|
modelColor[3] = CnfThawDWORD(RGB(150, 0, 0), "ModelColor_3");
|
|
|
|
modelColor[4] = CnfThawDWORD(RGB( 0, 100, 0), "ModelColor_4");
|
|
|
|
modelColor[5] = CnfThawDWORD(RGB( 0, 80, 80), "ModelColor_5");
|
|
|
|
modelColor[6] = CnfThawDWORD(RGB( 0, 0, 130), "ModelColor_6");
|
|
|
|
modelColor[7] = CnfThawDWORD(RGB( 80, 0, 80), "ModelColor_7");
|
|
|
|
// Light intensities
|
2008-07-08 07:41:29 +00:00
|
|
|
lightIntensity[0] = CnfThawFloat(1.0f, "LightIntensity_0");
|
|
|
|
lightIntensity[1] = CnfThawFloat(0.5f, "LightIntensity_1");
|
2008-06-11 04:22:52 +00:00
|
|
|
// Light positions
|
2008-07-08 07:41:29 +00:00
|
|
|
lightDir[0].x = CnfThawFloat(-1.0f, "LightDir_0_Right" );
|
|
|
|
lightDir[0].y = CnfThawFloat( 1.0f, "LightDir_0_Up" );
|
|
|
|
lightDir[0].z = CnfThawFloat( 0.0f, "LightDir_0_Forward" );
|
|
|
|
lightDir[1].x = CnfThawFloat( 1.0f, "LightDir_1_Right" );
|
|
|
|
lightDir[1].y = CnfThawFloat( 0.0f, "LightDir_1_Up" );
|
|
|
|
lightDir[1].z = CnfThawFloat( 0.0f, "LightDir_1_Forward" );
|
2008-07-10 05:26:08 +00:00
|
|
|
// Chord tolerance
|
|
|
|
chordTol = CnfThawFloat(2.0f, "ChordTolerance");
|
2008-06-14 09:51:25 +00:00
|
|
|
// View units
|
|
|
|
viewUnits = (Unit)CnfThawDWORD((DWORD)UNIT_MM, "ViewUnits");
|
2008-06-17 19:12:25 +00:00
|
|
|
// Camera tangent (determines perspective)
|
2008-07-08 07:41:29 +00:00
|
|
|
cameraTangent = CnfThawFloat(0.0f, "CameraTangent");
|
2008-07-06 07:56:24 +00:00
|
|
|
// Color for edges (drawn as lines for emphasis)
|
|
|
|
edgeColor = CnfThawDWORD(RGB(0, 0, 0), "EdgeColor");
|
2008-07-08 07:41:29 +00:00
|
|
|
// Export scale factor
|
|
|
|
exportScale = CnfThawFloat(1.0f, "ExportScale");
|
2008-06-11 04:22:52 +00:00
|
|
|
// Recent files menus
|
|
|
|
for(i = 0; i < MAX_RECENT; i++) {
|
|
|
|
char name[100];
|
|
|
|
sprintf(name, "RecentFile_%d", i);
|
|
|
|
strcpy(RecentFile[i], "");
|
|
|
|
CnfThawString(RecentFile[i], MAX_PATH, name);
|
|
|
|
}
|
|
|
|
RefreshRecentMenus();
|
|
|
|
|
|
|
|
// Start with either an empty file, or the file specified on the
|
|
|
|
// command line.
|
2008-05-29 10:10:12 +00:00
|
|
|
NewFile();
|
|
|
|
AfterNewFile();
|
|
|
|
if(strlen(cmdLine) != 0) {
|
2008-05-28 10:10:31 +00:00
|
|
|
if(LoadFromFile(cmdLine)) {
|
|
|
|
strcpy(saveFile, cmdLine);
|
|
|
|
} else {
|
|
|
|
NewFile();
|
|
|
|
}
|
2008-04-24 06:22:16 +00:00
|
|
|
}
|
2008-05-28 10:10:31 +00:00
|
|
|
AfterNewFile();
|
|
|
|
}
|
|
|
|
|
2008-06-11 04:22:52 +00:00
|
|
|
void SolveSpace::Exit(void) {
|
|
|
|
int i;
|
|
|
|
char name[100];
|
|
|
|
// Recent files
|
|
|
|
for(i = 0; i < MAX_RECENT; i++) {
|
|
|
|
sprintf(name, "RecentFile_%d", i);
|
|
|
|
CnfFreezeString(RecentFile[i], name);
|
|
|
|
}
|
|
|
|
// Model colors
|
|
|
|
for(i = 0; i < MODEL_COLORS; i++) {
|
|
|
|
sprintf(name, "ModelColor_%d", i);
|
|
|
|
CnfFreezeDWORD(modelColor[i], name);
|
|
|
|
}
|
|
|
|
// Light intensities
|
2008-07-08 07:41:29 +00:00
|
|
|
CnfFreezeFloat((float)lightIntensity[0], "LightIntensity_0");
|
|
|
|
CnfFreezeFloat((float)lightIntensity[1], "LightIntensity_1");
|
2008-06-11 04:30:18 +00:00
|
|
|
// Light directions
|
2008-07-08 07:41:29 +00:00
|
|
|
CnfFreezeFloat((float)lightDir[0].x, "LightDir_0_Right");
|
|
|
|
CnfFreezeFloat((float)lightDir[0].y, "LightDir_0_Up");
|
|
|
|
CnfFreezeFloat((float)lightDir[0].z, "LightDir_0_Forward");
|
|
|
|
CnfFreezeFloat((float)lightDir[1].x, "LightDir_1_Right");
|
|
|
|
CnfFreezeFloat((float)lightDir[1].y, "LightDir_1_Up");
|
|
|
|
CnfFreezeFloat((float)lightDir[1].z, "LightDir_1_Forward");
|
2008-07-10 05:26:08 +00:00
|
|
|
// Chord tolerance
|
|
|
|
CnfFreezeFloat((float)chordTol, "ChordTolerance");
|
2008-06-14 09:51:25 +00:00
|
|
|
// Display/entry units
|
2008-07-08 07:41:29 +00:00
|
|
|
CnfFreezeDWORD((DWORD)viewUnits, "ViewUnits");
|
2008-06-17 19:12:25 +00:00
|
|
|
// Camera tangent (determines perspective)
|
2008-07-08 07:41:29 +00:00
|
|
|
CnfFreezeFloat((float)cameraTangent, "CameraTangent");
|
2008-07-06 07:56:24 +00:00
|
|
|
// Color for edges (drawn as lines for emphasis)
|
|
|
|
CnfFreezeDWORD(edgeColor, "EdgeColor");
|
2008-07-08 07:41:29 +00:00
|
|
|
// Export scale (a float, stored as a DWORD)
|
|
|
|
CnfFreezeFloat(exportScale, "ExportScale");
|
|
|
|
|
2008-06-11 04:22:52 +00:00
|
|
|
ExitNow();
|
|
|
|
}
|
|
|
|
|
2008-06-03 18:28:41 +00:00
|
|
|
void SolveSpace::DoLater(void) {
|
|
|
|
if(later.generateAll) GenerateAll();
|
|
|
|
if(later.showTW) TW.Show();
|
|
|
|
ZERO(&later);
|
|
|
|
}
|
|
|
|
|
2008-06-11 04:22:52 +00:00
|
|
|
int SolveSpace::CircleSides(double r) {
|
2008-07-10 05:26:08 +00:00
|
|
|
// Let the pwl segment be symmetric about the x axis; then the curve
|
|
|
|
// goes out to r, and if there's n segments, then the endpoints are
|
|
|
|
// at +/- (2pi/n)/2 = +/- pi/n. So the chord goes to x = r cos pi/n,
|
|
|
|
// from x = r, so it's
|
|
|
|
// tol = r - r cos pi/n
|
|
|
|
// tol = r(1 - cos pi/n)
|
|
|
|
// tol ~ r(1 - (1 - (pi/n)^2/2)) (Taylor expansion)
|
|
|
|
// tol = r((pi/n)^2/2)
|
|
|
|
// 2*tol/r = (pi/n)^2
|
|
|
|
// sqrt(2*tol/r) = pi/n
|
|
|
|
// n = pi/sqrt(2*tol/r);
|
|
|
|
|
|
|
|
double tol = chordTol/GW.scale;
|
|
|
|
int n = 3 + (int)(PI/sqrt(2*tol/r));
|
|
|
|
|
|
|
|
return max(7, min(n, 40));
|
2008-06-11 04:22:52 +00:00
|
|
|
}
|
|
|
|
|
2008-06-14 09:51:25 +00:00
|
|
|
char *SolveSpace::MmToString(double v) {
|
|
|
|
static int WhichBuf;
|
|
|
|
static char Bufs[8][128];
|
|
|
|
|
|
|
|
WhichBuf++;
|
|
|
|
if(WhichBuf >= 8 || WhichBuf < 0) WhichBuf = 0;
|
|
|
|
|
|
|
|
char *s = Bufs[WhichBuf];
|
|
|
|
if(viewUnits == UNIT_INCHES) {
|
|
|
|
sprintf(s, "%.3f", v/25.4);
|
|
|
|
} else {
|
|
|
|
sprintf(s, "%.2f", v);
|
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
double SolveSpace::ExprToMm(Expr *e) {
|
|
|
|
if(viewUnits == UNIT_INCHES) {
|
|
|
|
return (e->Eval())*25.4;
|
|
|
|
} else {
|
|
|
|
return e->Eval();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-05-28 10:10:31 +00:00
|
|
|
void SolveSpace::AfterNewFile(void) {
|
2008-05-29 10:10:12 +00:00
|
|
|
ReloadAllImported();
|
2008-06-02 09:31:26 +00:00
|
|
|
GenerateAll(-1, -1);
|
2008-05-25 13:11:44 +00:00
|
|
|
|
2008-04-13 10:57:41 +00:00
|
|
|
TW.Init();
|
|
|
|
GW.Init();
|
|
|
|
|
2008-06-03 18:28:41 +00:00
|
|
|
unsaved = false;
|
2008-06-12 07:31:41 +00:00
|
|
|
|
|
|
|
int w, h;
|
|
|
|
GetGraphicsWindowSize(&w, &h);
|
|
|
|
GW.width = w;
|
|
|
|
GW.height = h;
|
2008-06-21 10:18:20 +00:00
|
|
|
|
|
|
|
// The triangles haven't been generated yet, but zoom to fit the entities
|
|
|
|
// roughly in the window, since that sets the mesh tolerance.
|
2008-06-12 07:31:41 +00:00
|
|
|
GW.ZoomToFit();
|
2008-06-12 08:58:58 +00:00
|
|
|
|
|
|
|
GenerateAll(0, INT_MAX);
|
|
|
|
later.showTW = true;
|
2008-06-21 10:18:20 +00:00
|
|
|
// Then zoom to fit again, to fit the triangles
|
|
|
|
GW.ZoomToFit();
|
2008-07-08 08:02:22 +00:00
|
|
|
|
|
|
|
UpdateWindowTitle();
|
2008-04-08 12:54:53 +00:00
|
|
|
}
|
|
|
|
|
2008-05-28 10:10:31 +00:00
|
|
|
void SolveSpace::RemoveFromRecentList(char *file) {
|
|
|
|
int src, dest;
|
|
|
|
dest = 0;
|
|
|
|
for(src = 0; src < MAX_RECENT; src++) {
|
|
|
|
if(strcmp(file, RecentFile[src]) != 0) {
|
|
|
|
if(src != dest) strcpy(RecentFile[dest], RecentFile[src]);
|
|
|
|
dest++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while(dest < MAX_RECENT) strcpy(RecentFile[dest++], "");
|
|
|
|
RefreshRecentMenus();
|
|
|
|
}
|
|
|
|
void SolveSpace::AddToRecentList(char *file) {
|
|
|
|
RemoveFromRecentList(file);
|
|
|
|
|
|
|
|
int src;
|
|
|
|
for(src = MAX_RECENT - 2; src >= 0; src--) {
|
|
|
|
strcpy(RecentFile[src+1], RecentFile[src]);
|
|
|
|
}
|
|
|
|
strcpy(RecentFile[0], file);
|
|
|
|
RefreshRecentMenus();
|
|
|
|
}
|
|
|
|
|
2008-06-03 18:28:41 +00:00
|
|
|
bool SolveSpace::GetFilenameAndSave(bool saveAs) {
|
|
|
|
char newFile[MAX_PATH];
|
|
|
|
strcpy(newFile, saveFile);
|
|
|
|
if(saveAs || strlen(newFile)==0) {
|
|
|
|
if(!GetSaveFile(newFile, SLVS_EXT, SLVS_PATTERN)) return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(SaveToFile(newFile)) {
|
|
|
|
AddToRecentList(newFile);
|
|
|
|
strcpy(saveFile, newFile);
|
|
|
|
unsaved = false;
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SolveSpace::OkayToStartNewFile(void) {
|
|
|
|
if(!unsaved) return true;
|
|
|
|
|
|
|
|
switch(SaveFileYesNoCancel()) {
|
|
|
|
case IDYES:
|
|
|
|
return GetFilenameAndSave(false);
|
|
|
|
|
|
|
|
case IDNO:
|
|
|
|
return true;
|
|
|
|
|
|
|
|
case IDCANCEL:
|
|
|
|
return false;
|
|
|
|
|
|
|
|
default: oops();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-07-08 08:02:22 +00:00
|
|
|
void SolveSpace::UpdateWindowTitle(void) {
|
|
|
|
if(strlen(saveFile) == 0) {
|
|
|
|
SetWindowTitle("SolveSpace - (not yet saved)");
|
|
|
|
} else {
|
|
|
|
char buf[MAX_PATH+100];
|
|
|
|
sprintf(buf, "SolveSpace - %s", saveFile);
|
|
|
|
SetWindowTitle(buf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-04-18 11:11:48 +00:00
|
|
|
void SolveSpace::MenuFile(int id) {
|
2008-05-28 10:10:31 +00:00
|
|
|
if(id >= RECENT_OPEN && id < (RECENT_OPEN+MAX_RECENT)) {
|
2008-07-10 06:11:56 +00:00
|
|
|
if(!SS.OkayToStartNewFile()) return;
|
|
|
|
|
2008-05-28 10:10:31 +00:00
|
|
|
char newFile[MAX_PATH];
|
|
|
|
strcpy(newFile, RecentFile[id-RECENT_OPEN]);
|
|
|
|
RemoveFromRecentList(newFile);
|
|
|
|
if(SS.LoadFromFile(newFile)) {
|
|
|
|
strcpy(SS.saveFile, newFile);
|
|
|
|
AddToRecentList(newFile);
|
|
|
|
} else {
|
|
|
|
strcpy(SS.saveFile, "");
|
|
|
|
SS.NewFile();
|
|
|
|
}
|
|
|
|
SS.AfterNewFile();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-04-18 11:11:48 +00:00
|
|
|
switch(id) {
|
|
|
|
case GraphicsWindow::MNU_NEW:
|
2008-06-03 18:28:41 +00:00
|
|
|
if(!SS.OkayToStartNewFile()) break;
|
|
|
|
|
2008-05-28 10:10:31 +00:00
|
|
|
strcpy(SS.saveFile, "");
|
2008-04-24 06:22:16 +00:00
|
|
|
SS.NewFile();
|
2008-05-28 10:10:31 +00:00
|
|
|
SS.AfterNewFile();
|
2008-04-24 06:22:16 +00:00
|
|
|
break;
|
|
|
|
|
2008-05-28 10:10:31 +00:00
|
|
|
case GraphicsWindow::MNU_OPEN: {
|
2008-06-03 18:28:41 +00:00
|
|
|
if(!SS.OkayToStartNewFile()) break;
|
|
|
|
|
2008-05-28 10:10:31 +00:00
|
|
|
char newFile[MAX_PATH] = "";
|
2008-05-29 10:10:12 +00:00
|
|
|
if(GetOpenFile(newFile, SLVS_EXT, SLVS_PATTERN)) {
|
2008-05-28 10:10:31 +00:00
|
|
|
if(SS.LoadFromFile(newFile)) {
|
|
|
|
strcpy(SS.saveFile, newFile);
|
|
|
|
AddToRecentList(newFile);
|
|
|
|
} else {
|
|
|
|
strcpy(SS.saveFile, "");
|
|
|
|
SS.NewFile();
|
|
|
|
}
|
|
|
|
SS.AfterNewFile();
|
|
|
|
}
|
2008-04-24 06:22:16 +00:00
|
|
|
break;
|
2008-05-28 10:10:31 +00:00
|
|
|
}
|
2008-04-18 11:11:48 +00:00
|
|
|
|
|
|
|
case GraphicsWindow::MNU_SAVE:
|
2008-06-03 18:28:41 +00:00
|
|
|
SS.GetFilenameAndSave(false);
|
|
|
|
break;
|
2008-04-18 11:11:48 +00:00
|
|
|
|
2008-06-03 18:28:41 +00:00
|
|
|
case GraphicsWindow::MNU_SAVE_AS:
|
|
|
|
SS.GetFilenameAndSave(true);
|
2008-04-18 11:11:48 +00:00
|
|
|
break;
|
|
|
|
|
2008-06-18 08:35:14 +00:00
|
|
|
case GraphicsWindow::MNU_EXPORT_PNG: {
|
|
|
|
char exportFile[MAX_PATH] = "";
|
|
|
|
if(!GetSaveFile(exportFile, PNG_EXT, PNG_PATTERN)) break;
|
|
|
|
SS.ExportAsPngTo(exportFile);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2008-07-08 06:30:13 +00:00
|
|
|
case GraphicsWindow::MNU_EXPORT_DXF: {
|
|
|
|
char exportFile[MAX_PATH] = "";
|
|
|
|
if(!GetSaveFile(exportFile, DXF_EXT, DXF_PATTERN)) break;
|
|
|
|
SS.ExportDxfTo(exportFile);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2008-07-06 09:24:31 +00:00
|
|
|
case GraphicsWindow::MNU_EXPORT_MESH: {
|
|
|
|
char exportFile[MAX_PATH] = "";
|
|
|
|
if(!GetSaveFile(exportFile, STL_EXT, STL_PATTERN)) break;
|
|
|
|
SS.ExportMeshTo(exportFile);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2008-04-18 11:11:48 +00:00
|
|
|
case GraphicsWindow::MNU_EXIT:
|
2008-06-03 18:28:41 +00:00
|
|
|
if(!SS.OkayToStartNewFile()) break;
|
2008-06-11 04:22:52 +00:00
|
|
|
SS.Exit();
|
2008-04-18 11:11:48 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default: oops();
|
|
|
|
}
|
2008-07-08 08:02:22 +00:00
|
|
|
|
|
|
|
SS.UpdateWindowTitle();
|
2008-04-18 11:11:48 +00:00
|
|
|
}
|