-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiogIgesReader.cpp
439 lines (371 loc) · 13.1 KB
/
iogIgesReader.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
//=============================================================================
//
// Copyright (c) China Automotive Innovation Corporation.
//
// Author : Xia Lei
// Date : 2022/09/05
//
//=============================================================================
#include "core.h"
#include "base.h"
#include "comobj.h"
#include "appinterface.h"
#include "qui.h"
#include "config.h"
#include "iogBinaryTree.h"
#include "iogIgesReader.h"
#include "iogOccKernelTools.h"
#define Mesh_Linear_Defl 0.5
#define Sew_Tole 0.1
#define mergeTolerance 9
#define Igs_Import_MaxHoleArea "iog_param/parameter/import_igs/holearea"
#define Igs_Import_Unit "iog_param/parameter/import_igs/unit"
#define Igs_Import_UnitType "iog_param/parameter/import_igs/unittype"
#define Igs_Import_DiscreType "iog_param/parameter/import_igs/discretype"
#define Igs_Import_DiscreLinear "iog_param/parameter/import_igs/discretlinear"
#define Igs_Import_DiscreAngle "iog_param/parameter/import_igs/discretangle"
#define Igs_Import_SewingTolerance "iog_param/parameter/import_igs/sewingtolerance"
//////////////////////////////////////////////////////////////////
//
iogIgesReader::iogIgesReader(const char * pFilename)
{
m_pFileName = _strdup(pFilename);
}
iogIgesReader::~iogIgesReader()
{
free(m_pFileName);
}
//////////////////////////////////////////////////////////////////
//
bool iogIgesReader::ReadIGES()
{
iogBasicTools iobasictools;
char pName[128];
iobasictools.GetPartName(pName, m_pFileName);
string strImportinfo, strImport, strFilename;
strImport = "Import ";
strFilename = pName;
strImportinfo = strImport + strFilename + ".iges";
TheExe.ProgressInit(strImportinfo.c_str());
TheExe.ProgressSetValue(0);
QString strMsg;
GetXmlValueD(Igs_Import_MaxHoleArea, m_dHoleRad);
m_dHoleRad = abs(m_dHoleRad);
GetXmlValueD(Igs_Import_Unit, m_nUnit);
GetXmlValueN(Igs_Import_DiscreType, m_nDiscreType);
GetXmlValueD(Igs_Import_DiscreLinear, m_dLinearDelVal);
GetXmlValueD(Igs_Import_DiscreAngle, m_dAngleDelVal);
GetXmlValueD(Igs_Import_SewingTolerance, m_dSewingTol);
if (m_dLinearDelVal == 0 || m_dAngleDelVal == 0)
{
QString strMsgError = QObject::tr("Linear Deflection and Angle Deflection can not be 0, please check.");
TheExe.PrintMessage(strMsgError, apiExe::MSG_NORMAL);
TheExe.ProgressExit();
return false;
}
if (!GetIGESFile())
{
strMsg = QObject::tr("The file %1 was failed import").arg(m_pFileName);
TheExe.PrintMessage(strMsg, apiExe::MSG_ERROR);
return false;
}
else
{
strMsg = QObject::tr("The file %1 was successful import").arg(m_pFileName);
TheExe.PrintMessage(strMsg, apiExe::MSG_NORMAL);
std::string strFilePath = m_pFileName;
ConfigAddRecentFile(strFilePath);
}
return true;
}
bool iogIgesReader::GetIGESFile()
{
DWORD t1 = GetTickCount();
Standard_CString path = (Standard_CString)m_pFileName;
TopoDS_Shape igesshape;
IGESCAFControl_Reader reader;
IFSelect_ReturnStatus status = reader.ReadFile(path);
if (status != IFSelect_RetDone)
{
return false;
}
if (status == IFSelect_RetDone)
{
reader.TransferRoots();
igesshape = reader.OneShape();
}
if (igesshape.IsNull())
{
// Collecting resulting entities
Standard_Integer nbs = reader.NbShapes();
BRep_Builder builder;
TopoDS_Compound cmp;
builder.MakeCompound(cmp);
for (Standard_Integer i = 1; i <= nbs; i++)
{
builder.Add(cmp, reader.Shape(i));
}
igesshape = cmp;
}
//map faces
std::vector<TopoDS_Shape> vecFaces;
TopTools_ListOfShape faceList;
TopTools_IndexedMapOfShape aShapeMap;
TopExp::MapShapes(igesshape, TopAbs_FACE, aShapeMap);
for (auto i = aShapeMap.cbegin(); i != aShapeMap.cend(); i++)
{
faceList.Append(*i);
}
for (auto iter : faceList)
{
vecFaces.push_back(iter);
}
TheExe.ProgressSetValue(30);
//retrieve all faces
/*std::vector<TopoDS_Shape> vecAllFace;
TopExp_Explorer facesex(igesshape, TopAbs_FACE);
for (; facesex.More(); facesex.Next())
{
vecAllFace.push_back(facesex.Current());
}*/
std::vector<std::pair<TopoDS_Shape, std::string>> vecnamemap, vecFaceNameMap;
Handle(TDocStd_Document) doc;
Handle(XCAFApp_Application) anApp = XCAFApp_Application::GetApplication();
anApp->NewDocument("BinOcaf", doc);
reader.Transfer(doc);
/*BinDrivers::DefineFormat(anApp);
doc->ChangeStorageFormat("BinOcaf");
TCollection_ExtendedString TPath = "d:\\text.cbf";
anApp->SaveAs(doc, TPath);*/
//namemap method1
/*Handle(XCAFDoc_ShapeTool) assembly = XCAFDoc_DocumentTool::ShapeTool(doc->Main());
TDF_LabelSequence freeshapes;
assembly->GetFreeShapes(freeshapes);
for (int i = 1; i <= freeshapes.Length(); i++)
{
TDF_Label freeshape = freeshapes.Value(i);
Handle(TDataStd_Name) name;
TCollection_ExtendedString nametext;
if (freeshape.FindAttribute(TDataStd_Name::GetID(), name))
{
nametext = name->Get();
std::string s1 = TCollection_AsciiString(nametext).ToCString();
TopoDS_Shape compareShape = assembly->GetShape(freeshape);
vecnamemap.push_back(std::pair<TopoDS_Shape, std::string>(compareShape, s1));
}
}*/
//namemap method3
Handle(XCAFDoc_ShapeTool) assembly = XCAFDoc_DocumentTool::ShapeTool(doc->Main());
TDF_LabelSequence freeshapes;
assembly->GetFreeShapes(freeshapes);
for (int i = 1; i <= freeshapes.Length(); i++)
{
TDF_Label freeshape = freeshapes.Value(i);
Handle(TDataStd_Name) name;
TCollection_ExtendedString nametext;
if (freeshape.FindAttribute(TDataStd_Name::GetID(), name))
{
nametext = name->Get();
std::string s1 = TCollection_AsciiString(nametext).ToCString();
TopoDS_Shape compareShape = assembly->GetShape(freeshape);
if (compareShape.ShapeType() != TopAbs_COMPOUND || compareShape.ShapeType() != TopAbs_SOLID)
continue;
vecnamemap.push_back(std::pair<TopoDS_Shape, std::string>(compareShape, s1));
}
iogOccKernelTools::GetShapeNameMap(assembly, freeshape, vecnamemap);
}
//retrive namemap method 2
QHash<QString, std::string> hashFace;
std::vector<string> vecAllFaceName, vecUniqName;
std::string strSewed = "Sewed Surface";
vecUniqName.push_back(strSewed);
Handle(XSControl_TransferReader) treader = reader.WS()->TransferReader();
for (int i = 0; i < vecFaces.size(); i++)
{
Handle(Standard_Transient) transient = treader->EntityFromShapeResult(vecFaces[i], -1);
Handle(IGESData_IGESEntity) item = Handle(IGESData_IGESEntity)::DownCast(transient);
if (item)
{
TopoDS_Shape compareShape = vecFaces[i];
Handle(TCollection_HAsciiString) hascii = item->NameValue();
if (!hascii)
continue;
TCollection_AsciiString ascii = hascii->String();
std::string s1 = ascii.ToCString();
vecFaceNameMap.push_back(std::pair<TopoDS_Shape, std::string>(compareShape, s1));
vecAllFaceName.push_back(s1);
QString qstrCompare1;
{
BRepAdaptor_Surface adp_sur(TopoDS::Face(compareShape));
double ustart = adp_sur.FirstUParameter();
double uend = adp_sur.LastUParameter();
double vstart = adp_sur.FirstVParameter();
double vend = adp_sur.LastVParameter();
gp_Pnt pntUsVsSurf = adp_sur.Value(ustart, vstart);
double dXPntUsVsSurf = pntUsVsSurf.X();
double dYPntUsVsSurf = pntUsVsSurf.Y();
double dZPntUsVsSurf = pntUsVsSurf.Z();
QString qstr1 = QString::number(dXPntUsVsSurf, 'f', 5) + ","
+ QString::number(dYPntUsVsSurf, 'f', 5) + ","
+ QString::number(dZPntUsVsSurf, 'f', 5) + ",";
gp_Pnt pntUeVsSurf = adp_sur.Value(uend, vstart);
double dXPntUeVsSurf = pntUeVsSurf.X();
double dYPntUeVsSurf = pntUeVsSurf.Y();
double dZPntUeVsSurf = pntUeVsSurf.Z();
QString qstr3 = QString::number(dXPntUeVsSurf, 'f', 5) + ","
+ QString::number(dYPntUeVsSurf, 'f', 5) + ","
+ QString::number(dZPntUeVsSurf, 'f', 5) + ",";
gp_Pnt pntUsVeSurf = adp_sur.Value(ustart, vend);
double dXPntUsVeSurf = pntUsVeSurf.X();
double dYPntUsVeSurf = pntUsVeSurf.Y();
double dZPntUsVeSurf = pntUsVeSurf.Z();
QString qstr5 = QString::number(dXPntUsVeSurf, 'f', 5) + ","
+ QString::number(dYPntUsVeSurf, 'f', 5) + ","
+ QString::number(dZPntUsVeSurf, 'f', 5) + ",";
gp_Pnt pntUeVeSurf = adp_sur.Value(vend, vend);
double dXPntUeVeSurf = pntUeVeSurf.X();
double dYPntUeVeSurf = pntUeVeSurf.Y();
double dZPntUeVeSurf = pntUeVeSurf.Z();
QString qstr7 = QString::number(dXPntUeVeSurf, 'f', 5) + ","
+ QString::number(dYPntUeVeSurf, 'f', 5) + ","
+ QString::number(dZPntUeVeSurf, 'f', 5) + ",";
gp_Pnt pntUfVfSurf = adp_sur.Value(0.5 * (ustart + uend), 0.5 * (ustart + uend));
double dXPntUfVfSurf = pntUfVfSurf.X();
double dYPntUfVfSurf = pntUfVfSurf.Y();
double dZPntUfVfSurf = pntUfVfSurf.Z();
QString qstr9 = QString::number(dXPntUfVfSurf, 'f', 5) + ","
+ QString::number(dYPntUfVfSurf, 'f', 5) + ","
+ QString::number(dZPntUfVfSurf, 'f', 5) + ",";
qstrCompare1 = qstr1 + qstr3 + qstr5 + qstr7 + qstr9; //qstr1 + qstr3 + qstr5 + qstr7 + qstr9
}
hashFace.insert(qstrCompare1, s1);
}
}
for (int i = 0; i < vecAllFaceName.size(); i++)
{
bool bIfAdd = true;
std::string strCompair1 = vecAllFaceName[i];
for (int j = 0; j < vecUniqName.size(); j++)
{
std::string strCompair2 = vecUniqName[j];
if (strCompair1 == strCompair2)
{
bIfAdd = false;
break;
}
}
if (bIfAdd)
{
vecUniqName.push_back(strCompair1);
}
}
//test free edges
/*TopoDS_Compound compound;
BRep_Builder b;
b.MakeCompound(compound);
TopExp_Explorer shellex(igesshape, TopAbs_SHELL);
for (; shellex.More(); shellex.Next())
{
TopoDS_Shell theShell = TopoDS::Shell(shellex.Current());
ShapeAnalysis_Shell aCheckShell;
aCheckShell.LoadShells(theShell);
if (aCheckShell.HasBadEdges())
{
TopoDS_Compound badedges = aCheckShell.BadEdges();
b.Add(compound, badedges);
}
}*/
TheExe.ProgressSetValue(40);
/*TopoDS_HShape* pHShape1 = new TopoDS_HShape(igesshape);
TopoDS_HShape* pHShape = static_cast<TopoDS_HShape*>(pHShape1);
TopoDS_Shape mainshape = pHShape->Shape();*/
//sewing
if (m_dSewingTol != 0)
{
BRepBuilderAPI_Sewing bas(m_dSewingTol);
bas.Load(igesshape);
bas.Perform();
igesshape = bas.SewedShape();
}
TheExe.ProgressSetValue(100);
TheExe.ProgressExit();
//scale shape
int nUnitSelIndex = -1;
GetXmlValueN(Igs_Import_UnitType, nUnitSelIndex);
int nCurLength = ConfigGetUnitCurrentValue(0);
double dCurMeter = ConfigGetUnitFactor(0, nCurLength);//current unit to meter
double dSelMeter = ConfigGetUnitFactor(0, nUnitSelIndex);//select unit to meter
double scale = dSelMeter / dCurMeter;
/*gp_Trsf theTransformation;
theTransformation.SetScale(gp_Pnt(0, 0, 0), scale);
BRepBuilderAPI_Transform transform(theTransformation);
transform.Perform(igesshape);
igesshape = transform.ModifiedShape(igesshape);*/
//avoid root is not compound
TopoDS_Compound compound;
BRep_Builder b;
b.MakeCompound(compound);
if (igesshape.ShapeType() != TopAbs_COMPOUND)
{
b.Add(compound, igesshape);
igesshape = compound;
}
//fix shape
iogBasicTools iobasictools;
iobasictools.Heal(igesshape, 0.0001, m_dHoleRad, 5);
string strBuildinfo = "Reconstructing...";
TheExe.ProgressInit(strBuildinfo.c_str());
TheExe.ProgressSetValue(0);
/*ShapeUpgrade_UnifySameDomain samedomain(igesshape);
samedomain.Build();
igesshape = samedomain.Shape();*/
int nCount = 0;
std::vector<std::pair<TopoDS_Shape, std::string>> vecReCSNameMap, vecReFaceNameMap;
QHash<QString, int> hashUsedFace;
igesshape = iogOccKernelTools::ReBuildShape(faceList.Size(), nCount, igesshape, vecUniqName, vecnamemap, vecFaceNameMap, hashFace
, vecReCSNameMap, vecReFaceNameMap, hashUsedFace);
TopoDS_HShape* entryshape = new TopoDS_HShape(igesshape);
TheExe.ProgressExit();
//record max index
comFaceMgr *pFaceMgr = comFace::Manager();
comVerticeMgr * pVertMgr = comVertice::Manager();
comSurfaceMgr *pSurfaceMgr = comSurface::Manager();
UINT uMaxSurfIx = pSurfaceMgr->MaxIndex();
UINT uMaxFaceIx = pFaceMgr->MaxIndex();
UINT uMaxVtxIx = pVertMgr->MaxIndex();
bool firstloop = false;
if (uMaxFaceIx == CORE_INVALID_INDEX && uMaxSurfIx == CORE_INVALID_INDEX)
{
firstloop = true;
}
string strMeshinfo = "Start discretizing...";
TheExe.ProgressInit(strMeshinfo.c_str());
TheExe.ProgressSetValue(0);
//export iges
//IGESCAFControl_Writer writer;
//writer.AddShape(igesshape);
//Standard_CString outpath = "D:/newigess.igs";
//writer.Write(outpath);
QHash<QString, UINT> hashVertIdx;
if (!iobasictools.DiscretizeShape(m_dLinearDelVal, m_dAngleDelVal, faceList.Size(), firstloop, uMaxSurfIx, scale, vecUniqName, vecReCSNameMap,
vecReFaceNameMap, m_pFileName, entryshape, hashVertIdx))
{
return false;
}
TheExe.ProgressExit();
string strFixinfo = "Refining grid...";
TheExe.ProgressInit(strFixinfo.c_str());
TheExe.ProgressSetValue(0);
iobasictools.ClearFreeEdges(false, scale, firstloop, uMaxFaceIx, igesshape, uMaxSurfIx, hashVertIdx);
iobasictools.ClearFreeEdges(false, scale, firstloop, uMaxFaceIx, igesshape, uMaxSurfIx, hashVertIdx);
TheExe.ProgressSetValue(100);
TheExe.ProgressExit();
anApp->Close(doc);
DWORD t2 = GetTickCount();
double dWaittime = (double)(t2 - t1) / 1000;
/*QString strMsg = QObject::tr("Import file using %1 seconds").arg(dWaittime);
TheExe.PrintMessage(strMsg, apiExe::MSG_NORMAL);*/
return true;
}
//////////////////////////////////////////////////////////////////
//