📄 sourcedcode.cpp
字号:
// sourcedCode.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <string>
#include <vector>
#include "lit_list.h"
#include "Point2D.h"
#include "ModelSpace.h"
#include "Layer.h"
#include "Circle.h"
#include "Polygon.h"
#include "Arc.h"
#include "line2d.h"
#include "math.h"
//////////////////////////////////////////////////////////////////////////
//the definition of globe functions and variants
ModelSpace * pModelSpace = NULL;
ModelSpace *curModelSpace()
{
return pModelSpace;
}
//////////////////////////////////////////////////////////////////////////
//
lit_list<Point2D> cal_polygon(int num,Point2D cenPt,double r,int flag)
{
lit_list<Point2D> verPts;
double pi = atan(1)*4;//
double detal = pi*2/num;
double fa = detal * 0.5;
Point2D pt;
if (flag == 'i' || flag == 'I')
{
//内接
for (int i = 0; i < num; ++ i)
{
pt.x = cenPt.x + r*sin(fa + detal * i);
pt.y = cenPt.y -r*cos(fa +detal * i);
verPts.addNode(pt);
}
}
else if (flag == 'c' || flag == 'C')
{
r = r /cos(fa);
verPts = cal_polygon(num,cenPt,r,'i');
}
return verPts;
}
//////////////////////////////////////////////////////////////////////////
Layer * createLayer(string strLayer = "\0")
{
if (!pModelSpace)
{
return NULL;
}
if (strLayer.size() == 0)
{
printf("input the layer name:");
cin>>strLayer;
}
Layer * pLayer = NULL;
if (strLayer.size() !=0)
{
pLayer = new Layer(strLayer.c_str());
if (pLayer)
{
unsigned long layerId = pModelSpace->AddObjectToModelSpace((EntityObject*&)pLayer);
if (layerId != NULLID)
{
pModelSpace->layers.addNode(layerId);
}
else
return NULL;
}
else
return NULL;
printf("create layer named %s successful.\n",strLayer.c_str());
}
else
{
return NULL;
}
return pLayer;
}
vector<string> splitString(string str,string flag)
{
vector<string> ret;
string cpStr = str;
string::size_type pos;
while((pos = cpStr.find(flag)) != string::npos)
{
string subStr;
subStr = cpStr.substr(0, pos);
ret.push_back(subStr);
cpStr.erase(0, pos + flag.size());
}
ret.push_back(cpStr);
return ret;
}
bool str2Point(Point2D & pt,string strPt)
{
vector<string> ret = splitString(strPt,",");
if (ret.size() != 2)
{
return false;
}
pt.x = atof(ret[0].c_str());
pt.y = atof(ret[1].c_str());
return true;
}
Layer * getLayer()
{
Layer * pLayer = NULL;
if (!pModelSpace)
{
return pLayer;
}
string strLayer = "\0";
while (1)
{
printf("input layer's name:");
cin>>strLayer;
if (strLayer.size() == 0)
{
printf("input error!\n");
break;
}
if (pModelSpace->open((EntityObject*&)pLayer,strLayer.c_str(),forRead)!=eOk)
{
printf("The layer named \"%s\" does not exist.Create one<Y/N>:",strLayer.c_str());
string sYN = "\0";
cin >> sYN;
if (sYN!="Y" && sYN !="y")
{
continue;
}
pLayer = createLayer(strLayer);
if (pLayer)
{
break;
}
}
else
break;
}
return pLayer;
}
//////////////////////////////////////////////////////////////////////////
//the definition of main function
int main(int argc, char* argv[])
{
//printf("Hello World!\n");
lit_list<int> intList;
intList.addNode(0);
intList.addNode(1,intList.count() - 1);
intList.addNode(2);
intList.addNode(3);
int t = intList.count();
intList.removeByValue(2);
printf("this is demo for course of C++.If there were some bugs,please let me know.\n");
printf("Author:shinf\n");
printf("=================================================\n");
pModelSpace = new ModelSpace();
if (!pModelSpace)
{
printf("Failed creating a ModelSpace\n");
}
printf("Create ModelSpace Successful.\n");
char cOperation = '\0';
bool bExit = false;
bool bSaved = false;
string lastFile = "\0";
while (!bExit)
{
printf(">>Manage entity/List information/Save file/Open file/Exit/<M>:");
cin >> cOperation;
printf("\n");
if (cOperation == 'e' || cOperation == 'E')
{
bExit = true;
}
else if (cOperation == 'M' || cOperation == 'm')
{
bSaved = false;
//create or edit or erase entity
create:
printf(">>Create/eRased/reTurn/eXplode<T>:");
string sInput = "\0";
cin >> sInput;
if (sInput == "C" || sInput == "c")
{
printf(">>Line2d/Arc/Circle/Polygon/laYer/Return/<R>:");
cin >> cOperation;
if (cOperation == 'R' || cOperation == 'r')
{
//return the main menu
}
else if(cOperation == 'l' || cOperation == 'L')
{
//create Line2D
printf("first point(x,y):");
cin >> sInput;
Point2D sPt,ePt;
if (!str2Point(sPt,sInput))
{
printf("input error!\n");
goto create;
}
printf("next point(x,y):");
cin >>sInput;
if (!str2Point(ePt,sInput))
{
printf("input error!\n");
goto create;
}
Layer * pLayer = getLayer();
if (!pLayer)
{
printf("input error!\n");
goto create;
}
Line2D *pLine = new Line2D(ePt,sPt);
if (pLine)
{
pLine->layerId(pLayer->id());
unsigned long lineID = NULLID;
if ((lineID = pModelSpace->AddObjectToModelSpace((EntityObject*&)pLine)) != NULLID)
{
pLayer->AddSubEntity(lineID);
}
}
}
else if(cOperation == 'p' || cOperation == 'P')
{
//create Polygon
printf("Specify radius of circle:");
double r = 0;
cin >> r;
if (r <= 0)
{
printf("input error!\n");
goto create;
}
printf("Specify center of polygon (x,y):");
cin>>sInput;
Point2D sPt;
if (!str2Point(sPt,sInput))
{
printf("input error!\n");
goto create;
}
printf("Enter number of sides:");
int num = 0;
cin>>num;
if (num < 3)
{
printf("input error! num should be greater than 2.\n");
goto create;
}
printf("Enter an option [Inscribed in circle/Circumscribed about circle]:");
string iOption = "\0";
cin >> iOption;
lit_list<Point2D> vertexPts;
vertexPts = cal_polygon(num,sPt,r,*(iOption.c_str()));
if (vertexPts.count() == 0)
{
goto create;
}
Layer * pLayer = getLayer();
if (!pLayer)
{
printf("input error!\n");
goto create;
}
Polygon *pCircle = new Polygon(vertexPts);
if (pCircle)
{
pCircle->layerId(pLayer->id());
unsigned long lineID = NULLID;
if ((lineID = pModelSpace->AddObjectToModelSpace((EntityObject*&)pCircle)) != NULLID)
{
pLayer->AddSubEntity(lineID);
}
}
}
else if(cOperation == 'C' || cOperation == 'c')
{
//create circle
printf("Specify center of circle(x,y):");
cin>>sInput;
Point2D sPt;
if (!str2Point(sPt,sInput))
{
printf("input error!\n");
goto create;
}
printf("Enter radius:");
double dRadius = 0;
cin>>dRadius;
if (dRadius == 0)
{
printf("input error! radius should be greater than 0.\n");
goto create;
}
Layer * pLayer = getLayer();
if (!pLayer)
{
printf("input error!\n");
goto create;
}
Circle *pCircle = new Circle(dRadius,sPt);
if (pCircle)
{
pCircle->layerId(pLayer->id());
unsigned long lineID = NULLID;
if ((lineID = pModelSpace->AddObjectToModelSpace((EntityObject*&)pCircle)) != NULLID)
{
pLayer->AddSubEntity(lineID);
}
}
}
else if(cOperation == 'A' || cOperation == 'a')
{
//create Arc
printf("Specify center of arc (x,y):");
cin>>sInput;
Point2D sPt;
if (!str2Point(sPt,sInput))
{
printf("input error!\n");
goto create;
}
printf("Enter radius:");
double dRadius = 0;
cin>>dRadius;
if (dRadius == 0)
{
printf("input error! radius should be greater than 0.\n");
goto create;
}
double dStartAngle =0,dEndAngle = 0;
printf("Enter startAngle:");
cin >>dStartAngle;
printf("Enter endAngle:");
cin>>dEndAngle;
Layer * pLayer = getLayer();
if (!pLayer)
{
printf("input error!\n");
goto create;
}
Arc *pCircle = new Arc(sPt,dRadius,dStartAngle,dEndAngle);
if (pCircle)
{
pCircle->layerId(pLayer->id());
unsigned long lineID = NULLID;
if ((lineID = pModelSpace->AddObjectToModelSpace((EntityObject*&)pCircle)) != NULLID)
{
pLayer->AddSubEntity(lineID);
}
}
}
else if(cOperation == 'Y' || cOperation == 'y')
{
//create layer
if(!createLayer())
{
printf("failed creating layer!\n");
}
}
}
else if (sInput == "E" || sInput == "e")
{
}
else if (sInput == "R" || sInput == "r")
{
printf("Enter id to be erased:");
unsigned long id;
cin >>id;
if (id == NULLID)
{
printf("input error!\n");
goto create;
}
pModelSpace->erase(id);
}
else if (sInput == "X" || sInput == "x")
{
printf("Enter id to be exploded:");
unsigned long id;
cin >>id;
if (id == NULLID)
{
printf("input error!\n");
goto create;
}
GeometryPrimitive * pEnt = NULL;
if (pModelSpace->open((EntityObject*&)pEnt,id,forRead) !=eOk)
{
printf("input error!\n");
goto create;
}
lit_list<unsigned long> ids;
pEnt->explode(ids);
pModelSpace->close((EntityObject*&)pEnt);
}
else if (sInput == "T" || sInput == "t")
{
continue;//return the main menu.
}
goto create;
}
else if (cOperation == 'L' || cOperation == 'l')
{
//list information
listInfo:
printf(">>Layer/Entity/All/Return/<A>:");
cin >> cOperation;
printf("\n");
if (cOperation == 'L' || cOperation == 'l')
{
cout << "Enter name of layer:";
string strLayer = "\0";
cin >> strLayer;
Layer * pEnt = NULL;
if (pModelSpace->open((EntityObject*&)pEnt,strLayer.c_str(),forRead) != eOk)
{
printf("the %s does not exist!\n",strLayer.c_str());
continue;
}
pEnt->list();
pModelSpace->close((EntityObject*&)pEnt);
}
else if (cOperation == 'A' || cOperation == 'a')
{
EntityObject * pEnt = NULL;
lit_node<unsigned long> * pCur = pModelSpace->subEntityIds().get_head();
while (pCur)
{
if (pModelSpace->open(pEnt,pCur->get_value(),forRead)!= eOk)
{
break;
}
pEnt->list();
pCur = pCur->next();
}
}
else if(cOperation == 'E' || cOperation == 'e')
{
//list the information
cout <<"Enter id of entity:";
unsigned long lId = NULLID;
cin >> lId;
if (lId == NULLID)
{
printf("id = %d does not exist!\n",lId);
goto listInfo;
}
EntityObject * pEnt = NULL;
if (pModelSpace->open(pEnt,lId,forRead) != eOk)
{
printf("id = %d does not exist!\n",lId);
goto listInfo;
}
pEnt->list();
pModelSpace->close((EntityObject*&)pEnt);
}
else if (cOperation == 'R' || cOperation == 'r')
{
//return the main menu
continue;
}
goto listInfo;
}
else if (cOperation == 's' || cOperation == 'S')
{
//save the modelspace to file
}
else if (cOperation == 'O' || cOperation == 'o')
{
//open the file to current modelspace.
}
}
if (pModelSpace)
{
if (!pModelSpace->isEmpty() && !bSaved)
{
printf("modelSpace is not saved.Save it now?(Y/N)<N>");
string strYn = "\0";
cin>>strYn;
if (strYn == "Y" || strYn == "y")
{
//save
}
}
delete pModelSpace;
pModelSpace = NULL;
}
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -