You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

336 lines
7.2 KiB

#include "stdafx.h"
#include "Connector.h"
#include <string>
#include <map>
#include <iomanip>
#include <vector>
#include <cmath>
#include <fstream>
#include <sstream>
using namespace std;
string ini = "--";
// 针脚
struct Pin
{
string name, id;
P coord;
Pin(string name1, string id1)
{
name = name1;
id = id1;
}
Pin(string name1, string id1, P coord1)
{
name = name1;
id = id1;
coord = coord1;
}
Pin()
{
name = id = "";
}
};
// 逻辑上的导线
struct Wire
{
string name;
Pin sn, en;
double diameter;
Wire(string name1, Pin sn1, Pin en1)
{
name = name1;
sn = sn1;
en = en1;
}
Wire()
{
name = "";
}
};
// 逻辑上的线束
struct Bund
{
vector<Wire> wirelist;
vector<string> wirenamelist;
P start, goal;
vector<P> path;
vector<double> d;
double dia;
double length;
Bund()
{
dia = length = 0;
}
};
map<pair<P, P>, Bund> wire_node;
vector<Bund> wire_pairs;
map<string, P> mp;
map<string, Wire> wiremap;
map<string, string> pinidmap;
map<string, Pin> pinmap;
int cnt = 0;
void init_readxml()
{
wire_node.clear();
wire_pairs.clear();
mp.clear();
wiremap.clear();
pinidmap.clear();
pinmap.clear();
cnt = 0;
}
void producePin(TiXmlElement *pin)
{
pinidmap[pin->Attribute("id")] = pin->Attribute("name");
}
string safegave(TiXmlElement *pEle, const char *t)
{
if (pEle->Attribute(t) == NULL)
return ini;
else
{
string v = pEle->Attribute(t);
if (v == "")
return ini;
else
return v;
}
}
TiXmlElement *GetNodePointerByName(TiXmlElement *pRootEle, const char *strNodeName)
{
// if equal root node then return
if (0 == strcmp(strNodeName, pRootEle->Value()))
{
return pRootEle;
}
TiXmlElement *pEle = pRootEle;
for (pEle = pRootEle->FirstChildElement(); pEle; pEle = pEle->NextSiblingElement())
{
// recursive find sub node return node pointer
if (0 != strcmp(pEle->Value(), strNodeName))
{
TiXmlElement *res = GetNodePointerByName(pEle, strNodeName);
if (res != NULL)
return res;
}
else
return pEle;
}
return NULL;
}
TiXmlElement *GetNextNodePointerByName(TiXmlElement *pRootEle, const char *strNodeName)
{
// if equal root node then return
TiXmlElement *pEle;
for (pEle = pRootEle->NextSiblingElement(); pEle; pEle = pEle->NextSiblingElement())
{
// recursive find sub node return node pointer
if (0 == strcmp(pEle->Value(), strNodeName))
return pEle;
}
return NULL;
}
void produceConnector(TiXmlElement *pcon)
{
Connector con;
con.id = pcon->Attribute("id");
con.name = pcon->Attribute("name");
con.partnumber = pcon->Attribute("partnumber");
con.coord = mp[con.name];
con.dir = P(0, 0, 0);
connectorNum++;
con.ID = connectorNum;
con.coord.type = 3;
con.coord.ref = connectorNum;
connectors[connectorNum] = con;
TiXmlElement *pEle;
TiXmlElement *p;
for (pEle = pcon->FirstChildElement(); pEle; pEle = pEle->NextSiblingElement())
{
if (strcmp(pEle->Value(), "backshell") == 0)
{
for (p = pEle->FirstChildElement(); p; p = p->NextSiblingElement())
{
if (strcmp(p->Value(), "termination") == 0)
{
string id = p->Attribute("id");
string name = p->Attribute("name");
pinmap[id] = Pin(name, id, con.coord);
break;
}
}
break;
}
}
for (pEle = pcon->FirstChildElement(); pEle; pEle = pEle->NextSiblingElement())
{
if (strcmp(pEle->Value(), "pin") == 0)
{
string id = pEle->Attribute("id");
string name = pEle->Attribute("name");
pinmap[id] = Pin(name, id, con.coord);
}
}
}
void produceWire(TiXmlElement *pwire)
{
Pin sn, en;
TiXmlElement *pconnection = GetNodePointerByName(pwire, "connection");
if (pconnection != NULL)
{
sn = pinmap[pconnection->Attribute("pinref")];
TiXmlElement *pconnection2 = GetNextNodePointerByName(pconnection, "connection");
if (pconnection2 != NULL)
en = pinmap[pconnection2->Attribute("pinref")];
else
en = sn;
}
Wire wire;
wire.sn = sn;
wire.en = en;
wire.name = pwire->Attribute("name");
if (pwire->Attribute("outsidediameter") != NULL)
{
wire.diameter = atof(pwire->Attribute("outsidediameter"));
}
else if (pwire->Attribute("wirecsa") != NULL)
{
wire.diameter = atof(pwire->Attribute("wirecsa")) * 2;
}
else
wire.diameter = 0.4;
wiremap[wire.name] = wire;
if (sn.coord == P(0, 0, 0) || en.coord == P(0, 0, 0) || sn.coord == en.coord)
return;
pair<P, P> p = make_pair(sn.coord, en.coord);
pair<P, P> pr = make_pair(en.coord, sn.coord);
if (wire_node.find(p) != wire_node.end())
{
Bund bd = wire_node[p];
bd.wirelist.push_back(wire);
bd.d.push_back(wire.diameter);
wire_node[p] = bd;
}
else if (wire_node.find(pr) != wire_node.end())
{
Bund bd = wire_node[pr];
bd.wirelist.push_back(wire);
bd.d.push_back(wire.diameter);
wire_node[pr] = bd;
}
else
{
Bund bd;
bd.start = sn.coord;
bd.goal = en.coord;
bd.wirelist.push_back(wire);
bd.d.push_back(wire.diameter);
wire_node[p] = bd;
}
}
void produce(TiXmlElement *pEle)
{
if (strcmp(pEle->Value(), "wire") == 0)
produceWire(pEle);
else
produceConnector(pEle);
return;
}
void ProduceNodePointerByName(TiXmlElement *pRootEle, const char *strNodeName)
{
// if equal root node then return
if (0 == strcmp(strNodeName, pRootEle->Value()))
{
produce(pRootEle);
return;
}
TiXmlElement *pEle = pRootEle;
for (pEle = pRootEle->FirstChildElement(); pEle; pEle = pEle->NextSiblingElement())
{
// recursive find sub node return node pointer
if (0 != strcmp(pEle->Value(), strNodeName))
{
ProduceNodePointerByName(pEle, strNodeName);
}
else
{
produce(pEle);
}
}
return;
}
void readxml(const char *xml, const char *connectorFile)
{
init_readxml();
ifstream infile;
infile.open(connectorFile); // XXXX是csv的路径
string s;
while (getline(infile, s))
{
// cout<<s<<endl;
istringstream sin(s); // 将整行字符串line读入到字符串流istringstream中
vector<string> fields; // 声明一个字符串向量
string field;
while (getline(sin, field, ',')) // 将字符串流sin中的字符读入到field字符串中,以逗号为分隔符
{
fields.push_back(field); // 将刚刚读取的字符串添加到向量fields中
}
// cout<<fields[0]<<endl;
// cout<<"read_xml getline"<<endl;
// cout<<fields[0]<<" "<<fields[1]<<" "<<fields[2]<<" "<<fields[3]<<endl;
mp[fields[0]] = P(atof(fields[1].c_str()), atof(fields[2].c_str()), atof(fields[3].c_str()));
}
TiXmlDocument *pDoc = new TiXmlDocument(xml);
if (!(pDoc->LoadFile()))
return;
// pDoc->Print();
// 获得根元素,即Persons。
// cout<<"connectivity"<<endl<<endl;
TiXmlElement *RootElement = pDoc->RootElement();
TiXmlElement *connectivity = GetNodePointerByName(RootElement, "connectivity");
// cout<<(connectivity->Value())<<endl;
// cout<<(connectivity->Attribute("id")==NULL)<<endl;
ProduceNodePointerByName(connectivity, "connector");
ProduceNodePointerByName(connectivity, "splice");
ProduceNodePointerByName(connectivity, "wire");
for (map<pair<P, P>, Bund>::iterator it = wire_node.begin(); it != wire_node.end(); it++)
{
wire_pairs.push_back(it->second);
}
for (int i = 0; i < wire_pairs.size(); i++)
{
Bund &bd = wire_pairs[i];
for (int j = 0; j < bd.d.size(); j++)
{
bd.dia += (bd.d[j]) * (bd.d[j]);
// cout<<bd.d[j]<<" ";
}
bd.dia = sqrt(bd.dia);
for (int j = 0; j < bd.wirelist.size(); j++)
{
Wire &wire = bd.wirelist[j];
bd.wirenamelist.push_back(wire.name);
}
}
cout << "read xml Finish" << endl;
}