home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
DOS/V Power Report 1997 March
/
VPR9703A.ISO
/
VPR_DATA
/
DOGA
/
SOURCES
/
PASM.LZH
/
DESIGN.CPP
< prev
next >
Wrap
C/C++ Source or Header
|
1996-07-29
|
31KB
|
1,197 lines
#include <owl\owlpch.h>
#pragma hdrstop
#include <dir.h>
#include <owl\scrollba.h>
#include <owl\statusba.h>
#include <owl\buttonga.h>
#include <owl\controlg.h>
#include <owl\button.h>
#include "matrix.h"
#include "suflib.h"
#include "design.h"
#include "display.h"
#include "status.h"
#include "pers.h"
#include "winframe.h"
#include "parts.h"
#include "files.h"
#include "syscolor.h"
#include "polyline.h"
#include "log.h"
#if 0
static const TColor& cWireSelect = TColor::LtBlue;
static const TColor& cWireNoSelect = TColor::LtGreen;
static const TColor& cMarker = TColor::LtRed;
static const TColor& cEye = TColor::LtCyan;
static const TColor& cCamera = TColor::LtYellow;
#endif
const double scalemax = 4.0;
const double scalemin = (1/32.0);
const double scalerate = 1.414213562373;
static const int EyeLength = 500;
//const int scrollunit = 50;
DesignData::DesignData()
{
// for (int i = 0; i < MaxSufFile; ++i) {
// suffile[i] = NULL;
// }
displayscale = 0.125;
displayoffset = Vector(0.0, 0.0, 0.0);
zoommode = ZoomNone;
wireframe.line = NULL;
hiddenremove = FALSE;
meshflag = TRUE;
meshspacing = 200;
gridflag = TRUE;
gridspacing = 50;
rotspacing = 10;
scalespacing = (int)(0.1 * scalespacingfactor);
getcwd(filedir, filenamedirlength);
filename[0] = '\0';
cataloglastmenu = 0;
cataloglastparts = -1;
polygons = 0;
// SetPolygons(0);
dragtype = DragCompound;
parts = select = NULL;
selectstatus = SelNone;
camera = new CameraParts(Vector(1200,-900,900),Vector(0,0,0));
combined = new CombinedParts();
editflag = 0;
color = ColorWhite;
if (rendcolor == "青") color = ColorBlue;
if (rendcolor == "赤") color = ColorRed;
if (rendcolor == "緑") color = ColorGreen;
if (rendcolor == "紫") color = ColorMagenta;
if (rendlight == "左上") Parts::SetLightType(LightLeftUpper);
if (rendlight == "左下") Parts::SetLightType(LightLeftLower);
if (rendlight == "右上") Parts::SetLightType(LightRightUpper);
if (rendlight == "右下") Parts::SetLightType(LightRightLower);
// parts = camera;
}
#if 0
void DesignData::ShowCube(Matrix& m, int flag)
{
if (select == camera) {
TopView->ShowCamera(m, flag);
SideView->ShowCamera(m, flag);
FrontView->ShowCamera(m, flag);
} else {
SelectType t[3];
SelectAxis(t, m);
TopView->ShowCube(m, flag, t[0]);
SideView->ShowCube(m, flag, t[1]);
FrontView->ShowCube(m, flag, t[2]);
}
}
#endif
void DesignData::ShowCube(Matrix& omat, Matrix& nmat)
{
if (select == camera) {
TopView->ShowCamera(omat, FALSE);
TopView->ShowCamera(nmat, FALSE);
SideView->ShowCamera(omat, FALSE);
SideView->ShowCamera(nmat, FALSE);
FrontView->ShowCamera(omat, FALSE);
FrontView->ShowCamera(nmat, FALSE);
} else {
TopView->ShowCube(omat, FALSE, SelNone);
TopView->ShowCube(nmat, FALSE, SelNone);
SideView->ShowCube(omat, FALSE, SelNone);
SideView->ShowCube(nmat, FALSE, SelNone);
FrontView->ShowCube(omat, FALSE, SelNone);
FrontView->ShowCube(nmat, FALSE, SelNone);
}
}
void DesignData::CalcView(Parts *p)
{
int fx, fy;
fx = PersView->Attr.W;
fy = PersView->Attr.H;
Matrix cmat;
cmat = camera->GetMatrix().inv();
Matrix tmpmat(0);
tmpmat.v[0].z = tmpmat.v[1].x = tmpmat.v[2].y = 1.0;
cmat = tmpmat * cmat;
tmpmat = Matrix(1);
tmpmat.v[2].x = double(fx)/2.0;
tmpmat.v[2].y = double(fy)/2.0;
tmpmat.v[0].x = - fx / 2.0 / tan(camera->angle/2.0);
tmpmat.v[1].y = tmpmat.v[0].x;
cmat = tmpmat * cmat;
p->CalcView(cmat * p->GetMatrix());
}
void DesignData::CalcViewAll(void)
{
for (Parts *p = parts; p != NULL; p = p->next) {
CalcView(p);
}
BuildWireFrame();
}
void DesignData::CalcPoints(Parts *p)
{
p->CalcPoints(GetMatrix(p));
CalcView(p);
delete [] wireframe.line;
wireframe.line = NULL;
}
void DesignData::ShowObject(Parts *p, SelectType type, int flag)
{
int centerx, centery, centerz;
centerx = TopView->Attr.W / 2;
centery = TopView->Attr.H / 2;
centerz = SideView->Attr.H / 2;
Matrix m = GetMatrix(p);
if (flag == TRUE) {
CalcPoints(p);
}
if (type & SelX) {
TClientDC dc(*FrontView);
int i = 0;
int begin, end = -1;
int cx, cy;
cx = centery;// + m.v[3].y;
cy = centerz;// - m.v[3].z;
if (p == select) {
dc.SelectObject(TPen(cWireSelect));
} else if (p == camera) {
if (select != NULL) {
dc.SelectObject(TPen(cWireCameraNoSelect));
} else {
dc.SelectObject(TPen(cWireCamera));
}
#if 0
&& select == NULL) {
i = 1;
dc.SelectObject(TPen(cEye));
begin = p->line_1[0];
end = p->line_2[0];
dc.MoveTo(cx + p->point_y[begin], cy - p->point_z[begin]);
dc.LineTo(cx + p->point_y[end], cy - p->point_z[end]);
dc.SelectObject(TPen(cCamera));
#endif
} else {
if (select != NULL) {
dc.SelectObject(TPen(cWireNoSelect));
} else {
dc.SelectObject(TPen(cWireNormal));
}
}
for (; i < p->lines; ++i) {
if ((begin = p->line_1[i]) != end) {
dc.MoveTo(cx + p->point_y[begin], cy - p->point_z[begin]);
}
end = p->line_2[i];
dc.LineTo(cx + p->point_y[end], cy - p->point_z[end]);
}
if (select == NULL) {
dc.SelectObject(TPen(cMarker));
dc.SelectObject(TBrush(cMarker));
dc.Rectangle(cx+m.v[3].y-markersize, cy-m.v[3].z-markersize,
cx+m.v[3].y+markersize, cy-m.v[3].z+markersize);
}
}
if (type & SelY) {
TClientDC dc(*SideView);
int i = 0;
int begin, end = -1;
int cx, cy;
cx = centerx;// + m.v[3].x;
cy = centerz;// - m.v[3].z;
if (p == select) {
dc.SelectObject(TPen(cWireSelect));
} else if (p == camera) {
if (select != NULL) {
dc.SelectObject(TPen(cWireCameraNoSelect));
} else {
dc.SelectObject(TPen(cWireCamera));
}
#if 0
&& select == NULL) {
i = 1;
dc.SelectObject(TPen(cEye));
begin = p->line_1[0];
end = p->line_2[0];
dc.MoveTo(cx + p->point_y[begin], cy - p->point_z[begin]);
dc.LineTo(cx + p->point_y[end], cy - p->point_z[end]);
dc.SelectObject(TPen(cCamera));
#endif
} else {
if (select != NULL) {
dc.SelectObject(TPen(cWireNoSelect));
} else {
dc.SelectObject(TPen(cWireNormal));
}
}
for (; i < p->lines; ++i) {
if ((begin = p->line_1[i]) != end) {
dc.MoveTo(cx + p->point_x[begin], cy - p->point_z[begin]);
}
end = p->line_2[i];
dc.LineTo(cx + p->point_x[end], cy - p->point_z[end]);
}
if (select == NULL) {
dc.SelectObject(TPen(cMarker));
dc.SelectObject(TBrush(cMarker));
dc.Rectangle(cx+m.v[3].x-markersize, cy-m.v[3].z-markersize,
cx+m.v[3].x+markersize, cy-m.v[3].z+markersize);
}
}
if (type & SelZ) {
TClientDC dc(*TopView);
int i = 0;
int begin, end = -1;
int cx, cy;
cx = centerx;// + m.v[3].x;
cy = centery;// - m.v[3].y;
if (p == select) {
dc.SelectObject(TPen(cWireSelect));
} else if (p == camera) {
if (select != NULL) {
dc.SelectObject(TPen(cWireCameraNoSelect));
} else {
dc.SelectObject(TPen(cWireCamera));
}
#if 0
&& select == NULL) {
i = 1;
dc.SelectObject(TPen(cEye));
begin = p->line_1[0];
end = p->line_2[0];
dc.MoveTo(cx + p->point_y[begin], cy - p->point_z[begin]);
dc.LineTo(cx + p->point_y[end], cy - p->point_z[end]);
dc.SelectObject(TPen(cCamera));
#endif
} else {
if (select != NULL) {
dc.SelectObject(TPen(cWireNoSelect));
} else {
dc.SelectObject(TPen(cWireNormal));
}
}
for (; i < p->lines; ++i) {
if ((begin = p->line_1[i]) != end) {
dc.MoveTo(cx + p->point_x[begin], cy - p->point_y[begin]);
}
end = p->line_2[i];
dc.LineTo(cx + p->point_x[end], cy - p->point_y[end]);
}
if (select == NULL) {
dc.SelectObject(TPen(cMarker));
dc.SelectObject(TBrush(cMarker));
dc.Rectangle(cx+m.v[3].x-markersize, cy-m.v[3].y-markersize,
cx+m.v[3].x+markersize, cy-m.v[3].y+markersize);
}
}
if (type & SelPers) {
TClientDC dc(*PersView);
if (p == select) {
dc.SelectObject(TPen(cWireSelect));
} else if (p == camera) {
dc.SelectObject(TPen(cWireCamera));
} else if (select != NULL && select != camera) {
dc.SelectObject(TPen(cWireNoSelect));
} else {
dc.SelectObject(TPen(cWireNormal));
}
int begin, end = -1;
for (int i = 0; i < p->lines; ++i) {
if (p->view_z[p->line_1[i]] <= 0 || p->view_z[p->line_2[i]] <= 0) {
end = -1;
} else {
if ((begin = p->line_1[i]) != end) {
dc.MoveTo(p->view_x[begin], p->view_y[begin]);
}
end = p->line_2[i];
dc.LineTo(p->view_x[end], p->view_y[end]);
}
}
}
}
Matrix DesignData::GetMatrix(Parts *p)
{
Matrix m;
// m = Matrix(1).move(displayoffset).scale(displayscale) * p->GetMatrix(mat);
m = Matrix::m_move(displayoffset).scale(displayscale) * p->GetMatrix();
return m;
}
void DesignData::GetCubeMatrix(Parts*p, Matrix& move, Matrix& rot, Matrix& scal)
{
double maxx, maxy, maxz;
const int minimumsize = markersize * 8;
if (p == camera) {
#if 0
maxx = (camera->target - camera->position).length();
maxy = int(double(maxx) * tan(camera->angle/2.0));
maxz = maxy * 3 / 4;
#endif
maxx = EyeLength;
maxy = maxx * tan(camera->angle/2.0);
maxz = maxy * 3 / 4;
} else {
if (p->maxx > -p->minx) {
maxx = p->maxx;
} else {
maxx = -p->minx;
}
if (p->maxy > -p->miny) {
maxy = p->maxy;
} else {
maxy = -p->miny;
}
if (p->maxz > -p->minz) {
maxz = p->maxz;
} else {
maxz = -p->minz;
}
}
p->GetMatrix(move, rot, scal);
move = Matrix(1).move(displayoffset).scale(displayscale) * move;
scal = scal.scale(maxx, maxy, maxz);
if (p != camera) {
if (scal.v[0].x > 0.0) {
if (scal.v[0].x < minimumsize/displayscale) scal.v[0].x = minimumsize/displayscale;
} else {
if (scal.v[0].x > -minimumsize/displayscale) scal.v[0].x = -minimumsize/displayscale;
}
if (scal.v[1].y > 0.0) {
if (scal.v[1].y < minimumsize/displayscale) scal.v[1].y = minimumsize/displayscale;
} else {
if (scal.v[1].y > -minimumsize/displayscale) scal.v[1].y = -minimumsize/displayscale;
}
if (scal.v[2].z > 0.0) {
if (scal.v[2].z < minimumsize/displayscale) scal.v[2].z = minimumsize/displayscale;
} else {
if (scal.v[2].z > -minimumsize/displayscale) scal.v[2].z = -minimumsize/displayscale;
}
}
}
Matrix DesignData::GetCubeMatrix(Parts *p)
{
Matrix m,r,s;
GetCubeMatrix(p, m, r, s);
return m * r * s;
}
void DesignData::PartsAdd(Object *obj, Vector& m, Vector& r, Vector& s)
{
Parts *p = new Parts(obj, m, r, s);
p->next = parts;
SetPolygons(polygons + obj->polygons);
parts = p;
SelectParts(p);
CalcPoints(p);
BuildWireFrame();
Redraw();
editflag++;
}
void DesignData::PartsDelete(Parts *p)
{
if (p == combined) {
Parts * nparts = NULL;
Parts *next;
combined->DeleteAllParts();
for (Parts *pt = parts; pt != NULL; pt = next) {
next = pt->next;
if (pt->sel) {
polygons -= pt->polygons;
delete pt;
} else {
pt->next = nparts;
nparts = pt;
}
}
parts = nparts;
SetPolygons(polygons);
} else if (p != NULL && p != camera) {
for (Parts *pt = parts; pt != NULL; pt = pt->next) {
if (pt->next == p) {
pt->next = p->next;
break;
}
}
SetPolygons(polygons - p->polygons);
if (parts == p) {
parts = p->next;
}
delete p;
}
SelectParts(NULL);
delete [] wireframe.line;
wireframe.line = NULL;
Redraw();
editflag++;
}
void DesignData::SetScrollLimit(int flag)
{
int WX = TopView->Attr.W;
int WY = TopView->Attr.H;
int WZ = SideView->Attr.H;
int lx = limitdim/scrollstep - (WX/2) / displayscale / scrollstep;
int ly = limitdim/scrollstep - (WY/2) / displayscale / scrollstep;
int lz = limitdim/scrollstep - (WZ/2) / displayscale / scrollstep;
scrollX->SetRange(-lx, lx);
scrollY->SetRange(-ly, ly);
scrollZ->SetRange(-lz, lz);
scrollX->PageMagnitude = WX / displayscale / scrollstep;
scrollX->LineMagnitude = (scrollX->PageMagnitude+7) / 8;
scrollY->PageMagnitude = WY / displayscale / scrollstep;
scrollY->LineMagnitude = (scrollY->PageMagnitude+7) / 8;
scrollZ->PageMagnitude = WZ / displayscale / scrollstep;
scrollZ->LineMagnitude = (scrollZ->PageMagnitude+7) / 8;
if (flag) {
scrollX->SetPosition(-displayoffset.x / displayscale / scrollstep);
scrollY->SetPosition(displayoffset.y / displayscale / scrollstep);
scrollZ->SetPosition(displayoffset.z / displayscale / scrollstep);
}
if (-displayoffset.x / displayscale / scrollstep < -lx) {
displayoffset.x = lx * displayscale * scrollstep;
scrollX->SetPosition(-lx);
} else if (-displayoffset.x / displayscale / scrollstep> lx) {
displayoffset.x = -lx * displayscale * scrollstep;
scrollX->SetPosition(lx);
}
if (displayoffset.y / displayscale / scrollstep < -ly) {
displayoffset.y = -ly * displayscale * scrollstep;
scrollX->SetPosition(-ly);
} else if (displayoffset.y / displayscale / scrollstep> ly) {
displayoffset.y = ly * displayscale * scrollstep;
scrollX->SetPosition(ly);
}
if (displayoffset.z / displayscale / scrollstep< -lz) {
displayoffset.z = -lz * displayscale * scrollstep;
scrollX->SetPosition(-lz);
} else if (displayoffset.z / displayscale / scrollstep> lz) {
displayoffset.z = lz * displayscale * scrollstep;
scrollX->SetPosition(lz);
}
// if (zoommode == ZoomNone) {
scrollX->Show(SW_HIDE); scrollX->Show(SW_SHOW);
scrollY->Show(SW_HIDE); scrollY->Show(SW_SHOW);
scrollZ->Show(SW_HIDE); scrollZ->Show(SW_SHOW);
// }
}
void DesignData::ScaleUp(void)
{
if (displayscale * scalerate <= scalemax) {
displayscale *= scalerate;
displayoffset *= scalerate;
SetScrollLimit();
Redraw(SelXYZ, TRUE);
}
}
void DesignData::ScaleDown(void)
{
if (displayscale / scalerate >= scalemin) {
displayscale /= scalerate;
displayoffset *= (1.0/scalerate);
SetScrollLimit();
Redraw(SelXYZ, TRUE);
}
}
int DesignData::ScaleUpLimitCheck(void)
{
return zoommode == ZoomNone && (displayscale <= scalemax/scalerate);
}
int DesignData::ScaleDownLimitCheck(void)
{
return zoommode == ZoomNone && (displayscale >= scalemin*scalerate);
}
void DesignData::PartsCopy(Parts *p, SelectType t)
{
if (p != NULL && p != camera) {
Parts *np;
if (p == combined) {
combined->DeleteAllParts();
np = NULL;
Parts *tp, *ntp, *lp;
for (tp = parts; tp != NULL; tp = tp->next) {
lp = tp;
if (tp->sel) {
ntp = new Parts(tp);
ntp->next = np;
np = ntp;
np->Trans(t);
np->sel = TRUE;
polygons += np->polygons;
combined->AddParts(np);
tp->sel = FALSE;
}
}
lp->next = np;
combined->CalcSizeAll();
np = combined;
SetPolygons(polygons);
} else {
np = new Parts(p);
np->next = parts;
parts = np;
np->Trans(t);
SetPolygons(polygons + p->polygons);
}
Vector wh(TopView->Attr.W, TopView->Attr.H, SideView->Attr.H);
if (t == SelNone) {
#if 0
np->position += wh * (1.0/displayscale/8.0);
if (np->position.x > limitdim) {
np->position.x -= wh.x * (2.0 * 1.0/displayscale/8.0);
}
if (np->position.y > limitdim) {
np->position.y -= wh.y * (2.0 * 1.0/displayscale/8.0);
}
if (np->position.z > limitdim) {
np->position.z -= wh.z * (2.0 * 1.0/displayscale/8.0);
}
#else
np->position += Vector(wh.x, wh.y, 0.0) * (1.0/displayscale/8.0);
if (np->position.x > limitdim) {
np->position.x -= wh.x * (2.0 * 1.0/displayscale/8.0);
}
if (np->position.y > limitdim) {
np->position.y -= wh.y * (2.0 * 1.0/displayscale/8.0);
}
#endif
}
Vector off = displayoffset * (-1.0 / displayscale);
wh *= (0.5 / displayscale);
SelectParts(np);
CalcPoints(np);
if (off.x + wh.x < np->position.x + np->minx
|| off.x - wh.x > np->position.x + np->maxx
|| off.y + wh.y < np->position.y + np->miny
|| off.y - wh.y > np->position.y + np->maxy
|| off.z + wh.z < np->position.z + np->minz
|| off.z - wh.z > np->position.z + np->maxz) {
displayoffset = np->position * (-displayscale);
SetScrollLimit();
Redraw(SelAll, TRUE);
} else {
Redraw();
}
editflag++;
}
}
//void DesignData::PartsAdd(SufFile* suf)
//{
// if (suf->object[0] != NULL) {
// PartsAdd(suf->object[0]);
// }
//}
void DesignData::PartsAdd(char *filename, Vector& m, Vector& r, Vector& s)
{
Object *obj;
char str[_MAX_PATH];
if ((obj = SufFile::OpenObject(filename)) != NULL) {
PartsAdd(obj);
return;
}
strcpy(str, simplepartsdir.c_str());
strcat(str, filename);
if ((obj = SufFile::OpenObject(str)) != NULL) {
PartsAdd(obj, m, r, s);
return;
}
// sprintf(str, "部品(%s)が読み込めません", filename);
// MessageBox(str, "部品追加", MB_OK);
}
void DesignData::Redraw(SelectType t, int flag)
{
#if 0
TDC *dc;
if (t & SelX) {
dc = new TClientDC(*FrontView);
dc->SelectObject(TPen(cBackGround));
dc->SelectObject(TBrush(cBackGround));
// dc->SelectStockObject(WHITE_PEN);
dc->Rectangle(dc->GetClipBox());
FrontView->ShowGrid();
dc->SetBkColor(cBackGround);
dc->SetTextColor(TColor::White);
dc->TextOut(0,0,"前");
delete(dc);
}
if (t & SelY) {
dc = new TClientDC(*SideView);
dc->SelectObject(TPen(cBackGround));
dc->SelectObject(TBrush(cBackGround));
// dc->SelectStockObject(WHITE_PEN);
dc->Rectangle(dc->GetClipBox());
SideView->ShowGrid();
dc->SetBkColor(cBackGround);
dc->SetTextColor(TColor::White);
dc->TextOut(0,0,"横");
delete(dc);
}
if (t & SelZ) {
dc = new TClientDC(*TopView);
dc->SelectObject(TPen(cBackGround));
dc->SelectObject(TBrush(cBackGround));
// dc->SelectStockObject(WHITE_PEN);
dc->Rectangle(dc->GetClipBox());
TopView->ShowGrid();
dc->SetBkColor(cBackGround);
dc->SetTextColor(TColor::White);
dc->TextOut(0,0,"上");
delete(dc);
}
if (t & SelPers) {
dc = new TClientDC(*PersView);
// dc->SelectStockObject(WHITE_PEN);
dc->SelectObject(TPen(cBackGround));
dc->SelectObject(TBrush(cBackGround));
dc->Rectangle(dc->GetClipBox());
delete(dc);
}
for (Parts *p = parts; p != NULL; p = p->next) {
if (p != select) {
ShowObject(p, t, flag);
}
}
if (camera != select) {
ShowObject(camera, t, flag);
}
if (select != NULL) {
if (select != camera) {
ShowObject(select, t, flag);
} else {
if (flag == TRUE) {
CalcPoints(camera);
}
}
Matrix m = GetCubeMatrix(select);
if (select == camera) {
if (t & SelX) FrontView->ShowCamera(m, TRUE);
if (t & SelY) SideView->ShowCamera(m, TRUE);
if (t & SelZ) TopView->ShowCamera(m, TRUE);
} else {
SelectType mt[3];
SelectAxis(mt, m);
if (t & SelZ) TopView->ShowCube(m, TRUE, mt[0]);
if (t & SelY) SideView->ShowCube(m, TRUE, mt[1]);
if (t & SelX) FrontView->ShowCube(m, TRUE, mt[2]);
}
}
if (t & SelStat) Status->Redraw();
#else
if (flag) {
for (Parts *p = parts; p != NULL; p = p->next) {
CalcPoints(p);
}
CalcPoints(camera);
}
if (flag || wireframe.line == NULL) {
BuildWireFrame();
}
if (t & SelX) FrontView->Redraw();
if (t & SelY) SideView->Redraw();
if (t & SelZ) TopView->Redraw();
if (t & SelPers) PersView->Redraw();
if (select != NULL) {
Matrix m = GetCubeMatrix(select);
if (select == camera) {
if (t & SelX) FrontView->ShowCamera(m, TRUE);
if (t & SelY) SideView->ShowCamera(m, TRUE);
if (t & SelZ) TopView->ShowCamera(m, TRUE);
} else {
SelectType mt[3];
SelectAxis(mt, m);
if (t & SelZ) TopView->ShowCube(m, TRUE, mt[0]);
if (t & SelY) SideView->ShowCube(m, TRUE, mt[1]);
if (t & SelX) FrontView->ShowCube(m, TRUE, mt[2]);
}
}
if (t & SelStat) Status->Redraw();
#endif
}
static void split(char *dir, char *name, char *all)
{
char *p;
strcpy(dir, all);
if ((p = strrchr(dir, '\\')) != NULL) {
*p = '\0';
strcpy(name, p+1);
} else if ((p = strrchr(dir, '/')) != NULL) {
*p = '\0';
strcpy(name, p+1);
} else {
dir[0] = '\0';
strcpy(name, all);
}
}
static void merge(char *all, char *dir, char *name)
{
if (dir[0] == '\0') {
strcpy(all, name);
} else if (dir[1] == ':' && dir[2] == '\0') {
strcpy(all, dir);
strcat(all, name);
} else {
strcpy(all, dir);
strcat(all, "\\");
strcat(all, name);
}
}
int DesignData::FileWrite(char *fname)
{
char str[filenamedirlength];
char str1[filenamedirlength];
char str2[filenamedirlength];
if (fname != NULL) {
split(str1, str2, fname);
#if 0
if (isdigit(str2[0])) {
Frame->MessageBox("数字で始まるファイル名は使えません", Frame->GetApplication()->GetName(), MB_OK);
return FALSE;
}
#else
int i;
if (!isalpha(str2[0]) && str2[0] != '_') {
if (isdigit(str2[0])) {
Frame->MessageBox("数字で始まるファイル名は使えません", Frame->GetApplication()->GetName(), MB_OK);
} else {
Frame->MessageBox("ファイル名にはアルファベット、数字、アンダーバーしか使えません", Frame->GetApplication()->GetName(), MB_OK);
}
return FALSE;
} else {
for (i = 1; str2[i] && str2[i] != '.'; i++) {
if (!isalpha(str2[i]) && !isdigit(str2[i]) && str2[i] != '_') {
Frame->MessageBox("ファイル名にはアルファベット、数字、アンダーバーしか使えません", Frame->GetApplication()->GetName(), MB_OK);
return FALSE;
}
}
}
#endif
strcpy(filedir, str1);
strcpy(filename, str2);
SetCaption();
}
merge(str, filedir, filename);
if (parts->FileWrite(camera, str, filedir) == FALSE && errormessage[0]) {
PersView->GetApplication()->GetMainWindow()->MessageBox(errormessage);
}
editflag = 0;
return FALSE;
}
void DesignData::FileRead(char *fname)
{
Parts *np;
Parts *next;
CameraParts *nc;
char nfiledir[_MAX_PATH], nfilename[_MAX_PATH];
split(nfiledir, nfilename, fname);
if (Parts::FileRead(np, nc, fname, filedir) != FALSE) {
strcpy(filedir, nfiledir);
strcpy(filename, nfilename);
for (Parts *p = parts; p != NULL; p = next) {
next = p->next;
delete p;
}
delete camera;
select = NULL;
parts = np;
camera = nc;
polygons = 0;
for (p = parts; p != NULL; p = p->next) {
polygons += p->polygons;
}
if (zoommode != ZoomNone) SelectZoom();
SetPolygons(polygons);
Redraw(SelAll, TRUE);
editflag = 0;
SetCaption();
}
}
void DesignData::FileNew(void)
{
Parts *next, *p;
for (p = parts; p != NULL; p = next) {
next = p->next;
delete p;
}
filename[0] = '\0';
SetCaption();
select = NULL;
parts = NULL;
if (zoommode != ZoomNone) SelectZoom();
displayscale = 0.125;
displayoffset = Vector(0.0, 0.0, 0.0);
meshspacing = 200;
selectstatus = SelNone;
camera->position = Vector(1200,-900,900);
camera->SetTarget(Vector(0,0,0));
SetPolygons(0);
Redraw(SelAll, TRUE);
editflag = 0;
}
void DesignData::SetCaption(void)
{
TApplication *app = TopView->GetApplication();
TWindow *frame = app->GetMainWindow();
if (filename[0]) {
char str[_MAX_PATH];
sprintf(str, "%s - %s", app->GetName(), filename);
frame->SetCaption(str);
} else {
frame->SetCaption(app->GetName());
}
}
TStatusBar *DesignData::InitStatusBar(void)
{
statusbar = new TStatusBar(0, TGadget::Recessed);
statusbar_polygons = new TTextGadget(0, TGadget::Recessed, TTextGadget::Right, 8);
statusbar_polygons->SetText("Total: 0面");
// button_ok = new TButton(0, , "確定", 0, 0, 32, 16);
// button_ok = new TButton(Frame, BUTTON_OK, "確定", 0, 0, 34, 18);
// statusbar_button = new TControlGadget(*button_ok);
statusbar->Insert(*statusbar_polygons);
// statusbar->Insert(*statusbar_button);
return statusbar;
}
void DesignData::SetPolygons(int p)
{
polygons = p;
char str[16];
sprintf(str, "Total:%4d面", p);
statusbar_polygons->SetText(str);
}
void DesignData::TotalScaling(double rate)
{
for (Parts *p = parts; p != NULL; p = p->next) {
p->position *= rate;
p->scale *= rate;
}
Redraw(SelAll, TRUE);
}
void DesignData::SelectAxis(SelectType t[3], Matrix& m)
{
Vector vx = m.v[0].unit() ;
Vector vy = m.v[1].unit() ;
Vector vz = m.v[2].unit() ;
if (vx.x < 0) vx.x = -vx.x;
if (vx.y < 0) vx.y = -vx.y;
if (vx.z < 0) vx.z = -vx.z;
if (vy.x < 0) vy.x = -vy.x;
if (vy.y < 0) vy.y = -vy.y;
if (vy.z < 0) vy.z = -vy.z;
if (vz.x < 0) vz.x = -vz.x;
if (vz.y < 0) vz.y = -vz.y;
if (vz.z < 0) vz.z = -vz.z;
if (vx.z > vy.z && vx.z > vz.z) { // Z成分のもっとも少ないベクトルを選択
t[0] = SelX;
if (vy.y > vz.y) { // Y成分のもっとも少ないベクトルを選択
t[1] = SelY;
t[2] = SelZ;
} else {
t[1] = SelZ;
t[2] = SelY;
}
} else if (vy.z > vz.z) {
t[0] = SelY;
if (vx.y > vz.y) {
t[1] = SelX;
t[2] = SelZ;
} else {
t[1] = SelZ;
t[2] = SelX;
}
} else {
t[0] = SelZ;
if (vx.y > vy.y) {
t[1] = SelX;
t[2] = SelY;
} else {
t[1] = SelY;
t[2] = SelX;
}
}
// t[2] = SelXYZ - t[0] - t[1];
}
void DesignData::SelectParts(Parts *p)
{
if (p == select) {
return;
}
select = p;
if (select == camera) {
backup_position = camera->position;
backup_rotation = camera->target;
} else if (select != NULL) {
backup_position = select->position;
backup_rotation = select->rotation;
backup_scale = select->scale;
}
}
void DesignData::CancelParts(void)
{
if (select == NULL) {
return;
}
if (select == camera) {
camera->position = backup_position;
camera->SetTarget(backup_rotation);
select = NULL;
Redraw(SelAll, TRUE);
} else if (select == combined) {
#if 0
Matrix m0 = Matrix::m_move(combined->orig_position);
Matrix m1 = combined->GetMatrix();
combined->MoveParts(m0 * m1.inv(), 1.0);
#else
combined->position = combined->orig_position;
combined->rotation = Vector(0,0,0);
combined->scale = Vector(1,1,1);
combined->MoveParts();
#endif
CalcPoints(select);
select = NULL;
Redraw();
} else {
select->position = backup_position;
select->rotation = backup_rotation;
select->scale = backup_scale;
CalcPoints(select);
select = NULL;
Redraw();
}
}
void DesignData::SelectZoom(void)
{
if (zoommode == ZoomNone) {
backup_displayscale = displayscale;
backup_displayoffset = displayoffset;
zoommode = ZoomSelect;
#if 0
scrollX->Show(SW_HIDE);
scrollY->Show(SW_HIDE);
scrollZ->Show(SW_HIDE);
#endif
statusbar->SetText("ズームしたい位置をクリックしてください");
button_ok->Show(SW_HIDE);
button_cancel->Show(SW_HIDE);
} else {
displayscale = backup_displayscale;
displayoffset = backup_displayoffset;
SetScrollLimit(TRUE);
// scrollX->SetPosition(-displayoffset.x / scrollunit);
// scrollY->SetPosition(-displayoffset.y / scrollunit);
// scrollZ->SetPosition(-displayoffset.z / scrollunit);
#if 0
scrollX->Show(SW_SHOW);
scrollY->Show(SW_SHOW);
scrollZ->Show(SW_SHOW);
#endif
zoommode = ZoomNone;
Redraw(SelXYZ | SelStat, TRUE);
}
}
int DesignData::GetLinesNoBackFace(LineSegment **tmpline, PolyData *polydata)
{
int W = PersView->Attr.W;
int H = PersView->Attr.H;
Parts *p;
polydata->Init(W, H);
for (p = parts; p != NULL; p= p->next) {
if (p->object == NULL) continue;
int inverseflag = FALSE;
if (p->scale.x < 0) inverseflag = !inverseflag;
if (p->scale.y < 0) inverseflag = !inverseflag;
if (p->scale.z < 0) inverseflag = !inverseflag;
int offp;
offp = polydata->points;
for (int i = 0; i < p->object->points; ++i) {
polydata->point[i+offp].x = p->view_x[i];
polydata->point[i+offp].y = p->view_y[i];
polydata->point[i+offp].z = p->view_z[i];
}
polydata->points += p->object->points;
for (i = 0; i < p->object->polys; ++i) {
int *pid = p->object->polypoint + p->object->poly[i];
PolyPoint *p1 = &polydata->point[pid[0]+offp];
PolyPoint *p2 = &polydata->point[pid[1]+offp];
PolyPoint *p3 = &polydata->point[pid[2]+offp];
long m = (long)(p1->x-p2->x)*(long)(p3->y-p2->y) - (long)(p1->y-p2->y)*(long)(p3->x-p2->x);
if (inverseflag && m <= 0) {
polydata->AddPolyInv(p->object->polypoint + p->object->poly[i], offp, p);
} else if (!inverseflag && m >= 0) {
polydata->AddPoly(p->object->polypoint + p->object->poly[i], offp, p);
}
}
}
polydata->ConvertLines();
int lines = 0;
for (int i = 0; i < polydata->lines; ++i) {
if (polydata->line[i].p1 >= 0
&& polydata->line[i].p2 >= 0) {
PolyPoint &p1 = polydata->point[polydata->line[i].p1];
PolyPoint &p2 = polydata->point[polydata->line[i].p2];
if (p1.z > 0 && p2.z > 0
&& ((p1.x != p2.x) || (p1.y != p2.y))
&& (p1.x >= 0 || p2.x >= 0)
&& (p1.x < W || p2.x < W)
&& (p1.y >= 0 || p2.y >= 0)
&& (p1.y < H || p2.y < H)) {
lines++;
}
}
}
int l = 0;
*tmpline = new LineSegment[lines];
for (i = 0; i < polydata->lines; ++i) {
if (polydata->line[i].p1 >= 0 && polydata->line[i].p2 >= 0) {
PolyPoint &p1 = polydata->point[polydata->line[i].p1];
PolyPoint &p2 = polydata->point[polydata->line[i].p2];
if (p1.z > 0 && p2.z > 0
&& ((p1.x != p2.x) || (p1.y != p2.y))
&& (p1.x >= 0 || p2.x >= 0)
&& (p1.x < W || p2.x < W)
&& (p1.y >= 0 || p2.y >= 0)
&& (p1.y < H || p2.y < H)) {
(*tmpline)[l].x1 = (short)p1.x;
(*tmpline)[l].y1 = (short)p1.y;
(*tmpline)[l].x2 = (short)p2.x;
(*tmpline)[l].y2 = (short)p2.y;
(*tmpline)[l].id = polydata->line[i].id;
l++;
}
}
}
return l;
}
void DesignData::BuildWireFrame(void)
{
if (!hiddenremove) {
return;
}
if (wireframe.line != NULL) {
delete[] wireframe.line;
wireframe.line = NULL;
}
int maxlines = 0;
int maxpolys = 0;
int maxpoints = 0;
int maxpolypoints = 0;
for (Parts *p = parts; p != NULL; p = p->next) {
if (p->object != NULL) {
maxpoints += p->object->points;
maxlines += p->lines;
maxpolys += p->object->polys;
maxpolypoints += p->object->polypoints;
}
}
PolyData polydata(maxpoints, maxlines, maxpolys, maxpolypoints);
int l = GetLinesNoBackFace(&wireframe.line, &polydata);
wireframe.lines = l;
}