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.

275 lines
6.4 KiB

#pragma once
#include <string>
#include <map>
#include <iomanip>
#include <vector>
#include <cmath>
#include <fstream>
#include <sstream>
#include "tinyxml.h"
#include "ReadXML.h"
using namespace std;
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 string xml, const string 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.c_str());
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;
}