19 #include <STEPCAFControl_Reader.hxx>
20 #include <IGESCAFControl_Reader.hxx>
21 #include <TopExp_Explorer.hxx>
22 #include <XSControl_WorkSession.hxx>
23 #include <XSControl_TransferReader.hxx>
24 #include <Interface_InterfaceModel.hxx>
25 #include <StlAPI_Writer.hxx>
26 #include <Transfer_TransientProcess.hxx>
27 #include <TransferBRep.hxx>
29 #include <TopoDS_Shape.hxx>
30 #include <TopoDS_Solid.hxx>
32 #include <BRepAlgoAPI_Fuse.hxx>
33 #include <BRepAlgoAPI_Cut.hxx>
34 #include <BRepAlgoAPI_Common.hxx>
36 #include <BRepPrimAPI_MakeBox.hxx>
37 #include <BRepPrimAPI_MakeCylinder.hxx>
38 #include <BRepPrimAPI_MakeSphere.hxx>
39 #include <BRepPrimAPI_MakeTorus.hxx>
40 #include <BRepPrimAPI_MakeCone.hxx>
41 #include <BRepPrimAPI_MakeHalfSpace.hxx>
43 #include <Bnd_Box.hxx>
44 #include <BRepBndLib.hxx>
46 #include <Geom_CylindricalSurface.hxx>
47 #include <Geom_ToroidalSurface.hxx>
48 #include <Geom_SphericalSurface.hxx>
49 #include <Geom_ConicalSurface.hxx>
50 #include <Geom_Plane.hxx>
52 #include <BRepBuilderAPI_MakeFace.hxx>
53 #include <BRepBuilderAPI_Transform.hxx>
55 #include <BRepBuilderAPI_MakeSolid.hxx>
56 #include <BRepOffsetAPI_Sewing.hxx>
57 #include <BRepBuilderAPI_Sewing.hxx>
58 #include <Geom_Curve.hxx>
61 #include <gp_Torus.hxx>
62 #include <gp_Cone.hxx>
63 #include <gp_Sphere.hxx>
64 #include <gp_Cylinder.hxx>
65 #include <gp_Trsf.hxx>
69 #include <TGeoSphere.h>
70 #include <TGeoTorus.h>
74 #include <TGeoHalfSpace.h>
75 #include <TGeoCompositeShape.h>
83 #include <EicStepMaterialReader.h>
92 #define SQR(x) ((x)*(x))
95 #define _ANGULAR_TOLERANCE_ (1E-10)
96 #define _SPATIAL_TOLERANCE_ (1E-10)
113 bool EicCadShapeEqual(
const std::pair<unsigned, EicOpenCascadeShape> &lh,
114 const std::pair<unsigned, EicOpenCascadeShape> &rh)
117 if (lh.second.sType != rh.second.sType)
return false;
121 if (lh.second.sType == EicOpenCascadeShape::_OCC_PLANE_)
123 const gp_Pln *pll = (gp_Pln *)lh.second.object, *plr = (gp_Pln *)rh.second.object;
129 assert(lh.first == rh.first);
134 gp_Dir anti(-plr->Axis().Direction().X(),
135 -plr->Axis().Direction().Y(),
136 -plr->Axis().Direction().Z());
143 assert(lh.first != rh.first);
153 switch (lh.second.sType) {
154 case EicOpenCascadeShape::_OCC_SPHERE_:
157 const gp_Sphere *spl = (gp_Sphere *)lh.second.object, *spr = (gp_Sphere *)rh.second.object;
162 double dc = sqrt(
SQR(spl->Location().X() - spr->Location().X()) +
163 SQR(spl->Location().Y() - spr->Location().Y()) +
164 SQR(spl->Location().Z() - spr->Location().Z()));
170 case EicOpenCascadeShape::_OCC_CYLINDER_:
172 const gp_Cylinder *cyl = (gp_Cylinder *)lh.second.object, *cyr = (gp_Cylinder *)rh.second.object;
182 gp_XYZ diff(cyl->Location().X() - cyr->Location().X(),
183 cyl->Location().Y() - cyr->Location().Y(),
184 cyl->Location().Z() - cyr->Location().Z());
188 gp_XYZ axis(cyl->Axis().Direction().X(),
189 cyl->Axis().Direction().Y(),
190 cyl->Axis().Direction().Z());
191 double pro = diff.Dot(axis);
192 gp_XYZ vpro = pro*axis, vnorm = diff - vpro;
198 case EicOpenCascadeShape::_OCC_TORUS_:
200 const gp_Torus *tol = (gp_Torus *)lh.second.object, *tor = (gp_Torus *)rh.second.object;
209 double dc = sqrt(
SQR(tol->Location().X() - tor->Location().X()) +
210 SQR(tol->Location().Y() - tor->Location().Y()) +
211 SQR(tol->Location().Z() - tor->Location().Z()));
217 case EicOpenCascadeShape::_OCC_CONE_:
219 const gp_Cone *col = (gp_Cone *)lh.second.object, *cor = (gp_Cone *)rh.second.object;
230 double dc = sqrt(
SQR(col->Location().X() - cor->Location().X()) +
231 SQR(col->Location().Y() - cor->Location().Y()) +
232 SQR(col->Location().Z() - cor->Location().Z()));
244 assert(lh.first == rh.first);
257 boundarySphereCenter = NULL;
258 boundarySphereRadius = 0.0;
291 Bool_t EicCadFile::elementaryFaceType(TopoDS_Face &face, FaceType fType)
293 Handle(Geom_Surface) SS = BRep_Tool::
Surface(face);
295 if (SS->IsKind(STANDARD_TYPE(Geom_Plane)) && fType != FaceTypeCurved)
return kTRUE;
297 if ((SS->IsKind(STANDARD_TYPE(Geom_CylindricalSurface))
298 || SS->IsKind(STANDARD_TYPE(Geom_ConicalSurface))
299 || SS->IsKind(STANDARD_TYPE(Geom_ToroidalSurface))
300 || SS->IsKind(STANDARD_TYPE(Geom_SphericalSurface))
302 fType != FaceTypeFlat)
312 Bool_t
EicCadFile::splitSolidByInfiniteFace(const TopoDS_Shape &
solid, const TopoDS_Face &face,
313 EicOpenCascadeShape &cuttingShape,
314 std::
vector<TopoDS_Shape> &commonSolids,
315 std::
vector<TopoDS_Shape> &cutSolids)
317 Handle(Geom_Surface) SS = BRep_Tool::
Surface(face);
319 if (SS->IsKind(STANDARD_TYPE(Geom_Plane))) {
320 Handle(Geom_Plane) es = Handle(Geom_Plane)::DownCast(SS);
324 TopoDS_Face aFace = BRepBuilderAPI_MakeFace(es->Pln());
325 gp_Dir dir = es->Pln().
Axis().Direction();
330 gp_Pnt x0(es->Location()), pt(x0.X() - t*dir.X(), x0.Y() - t*dir.Y(), x0.Z() - t*dir.Z());
333 cuttingShape.solid = new TopoDS_Shape(BRepPrimAPI_MakeHalfSpace(aFace, pt).Solid());
335 cuttingShape.sType = EicOpenCascadeShape::_OCC_PLANE_;
337 cuttingShape.
object = new gp_Pln(x0, dir);
342 if (SS->IsKind(STANDARD_TYPE(Geom_CylindricalSurface))) {
343 Handle(Geom_CylindricalSurface) es = Handle(Geom_CylindricalSurface)::DownCast(SS);
345 gp_Cylinder gcylinder = es->Cylinder();
346 double r = gcylinder.Radius();
349 gp_Dir dir =
ax1.Direction();
350 gp_Ax2 ax2 = gcylinder.Position().Ax2();
351 gp_Pnt x0 = gcylinder.Location();
352 double dist_to_sphere = es->Location().Distance(*boundarySphereCenter);
353 double safe_dimension = dist_to_sphere + boundarySphereRadius;
354 double t = safe_dimension;
355 gp_Pnt x1(x0.X() - t*dir.X(), x0.Y()- t*dir.Y(), x0.Z()- t*dir.Z());
357 ax2.SetDirection(dir);
359 new TopoDS_Shape(BRepPrimAPI_MakeCylinder(ax2, r, 2 * safe_dimension).Solid());
361 cuttingShape.sType = EicOpenCascadeShape::_OCC_CYLINDER_;
362 cuttingShape.
object = new gp_Cylinder(gp_Ax3(ax2), r);
363 cuttingShape.dimension = safe_dimension;
366 if (SS->IsKind(STANDARD_TYPE(Geom_SphericalSurface))) {
367 Handle(Geom_SphericalSurface) es = Handle(Geom_SphericalSurface)::DownCast(SS);
369 gp_Sphere sphere = es->Sphere();
372 cuttingShape.sType = EicOpenCascadeShape::_OCC_SPHERE_;
373 cuttingShape.
object = new gp_Sphere(es->Sphere());
375 new TopoDS_Shape(BRepPrimAPI_MakeSphere(sphere.Location(), sphere.Radius()).Solid());
378 if (SS->IsKind(STANDARD_TYPE(Geom_ToroidalSurface))) {
379 Handle(Geom_ToroidalSurface) es = Handle(Geom_ToroidalSurface)::DownCast(SS);
381 gp_Torus torus = es->Torus();
384 cuttingShape.sType = EicOpenCascadeShape::_OCC_TORUS_;
385 cuttingShape.
object = new gp_Torus(es->Torus());
387 new TopoDS_Shape(BRepPrimAPI_MakeTorus(torus.Position().Ax2(), torus.MajorRadius(),
388 torus.MinorRadius()).Solid());
391 if (SS->IsKind(STANDARD_TYPE(Geom_ConicalSurface))) {
392 Handle(Geom_ConicalSurface) es = Handle(Geom_ConicalSurface)::DownCast(SS);
394 gp_Cone cone = es->Cone();
395 gp_Pnt apex = cone.Apex();
396 gp_Ax1 axis = cone.Axis();
397 gp_Ax2 ax2(apex, axis.Direction());
399 double dist_to_sphere = apex.Distance(*boundarySphereCenter);
401 double safe_dimension = dist_to_sphere + boundarySphereRadius;
402 double semiAngle = cone.SemiAngle(), r0 = 0.0,
r1 = safe_dimension*tan(semiAngle);
404 cuttingShape.sType = EicOpenCascadeShape::_OCC_CONE_;
405 cuttingShape.
object = new gp_Cone(gp_Ax3(ax2), semiAngle, r0);
406 cuttingShape.dimension = safe_dimension;
408 new TopoDS_Shape(BRepPrimAPI_MakeCone(ax2, r0,
r1, safe_dimension).Solid());
418 TopoDS_Shape commonHalf = BRepAlgoAPI_Common(solid, *cuttingShape.solid);
421 for (TopExp_Explorer itq(commonHalf,TopAbs_SOLID); itq.More(); itq.Next())
422 commonSolids.push_back(itq.Current());
425 TopoDS_Shape cutHalf = BRepAlgoAPI_Cut(solid, *cuttingShape.solid);
427 for (TopExp_Explorer itq(cutHalf,TopAbs_SOLID); itq.More(); itq.Next())
428 cutSolids.push_back(itq.Current());
430 return (commonSolids.size() + cutSolids.size() >= 2);
436 void EicCadFile::DumpAsStlSolid(
const TopoDS_Shape &solid, TGeoMedium *medium)
438 char buffer[128], vname[128];
440 snprintf(buffer, 128-1,
"/tmp/%05d-%06d-step.stl", getpid(),
mStlVolumeCounter++);
443 StlAPI_Writer writer;
445 writer.ASCIIMode() =
true;
449 writer.SetCoefficient(
config()->GetStlQualityCoefficient());
451 writer.Write(solid, buffer);
461 stl_factory._ConstructGeometry(
mCave, TVector3());
469 gp_Pnt x0 = ax3.Location();
470 gp_Dir xDir = ax3.XDirection(), yDir = ax3.YDirection(), zDir = ax3.Axis().Direction();
472 double data[9] = {xDir.X(), yDir.X(), zDir.X(),
473 xDir.Y(), yDir.Y(), zDir.Y(),
474 xDir.Z(), yDir.Z(), zDir.Z()};
475 TGeoRotation *grr =
new TGeoRotation();
476 grr->SetMatrix(data);
478 return new TGeoCombiTrans(name,
479 x0.X() + offset * zDir.X(),
480 x0.Y() + offset * zDir.Y(),
481 x0.Z() + offset * zDir.Z(), grr);
486 void EicCadFile::DumpAsRootSolid(
const TopoDS_Shape &solid,
487 std::vector< std::pair<unsigned, EicOpenCascadeShape> > &facets,
488 TGeoMedium *medium,
double *
color)
493 static unsigned counter;
494 char vname[128], tname[128];
498 snprintf(vname, 128-1,
"VS%04d", counter);
499 snprintf(tname, 128-1,
"TS%04d", counter++);
501 cmd = vname + cmd + tname;
504 TGeoSphere *shell =
new TGeoSphere(vname, 0.0, boundarySphereRadius + 1E-10*counter);
505 TGeoCombiTrans *ts =
new TGeoCombiTrans(tname, boundarySphereCenter->X(),
506 boundarySphereCenter->Y(),
507 boundarySphereCenter->Z(), 0);
508 ts->RegisterYourself();
511 for(
unsigned fc=0; fc<facets.size(); fc++) {
512 EicOpenCascadeShape &cuttingShape = facets[fc].second;
515 static unsigned counter;
516 char vname[128], tname[128];
518 snprintf(vname, 128-1,
"V%04d", counter);
519 snprintf(tname, 128-1,
"T%04d", counter++);
521 TGeoCombiTrans *ts = 0;
522 switch (cuttingShape.sType) {
523 case EicOpenCascadeShape::_OCC_PLANE_:
525 const gp_Pln *
plane = (gp_Pln *)cuttingShape.object;
527 double x0[3] = {plane->Location().X(), plane->Location().Y(), plane->Location().Z()};
528 double n0[3] = {plane->Axis().Direction().X(), plane->Axis().Direction().Y(), plane->Axis().Direction().Z()};
530 printf(
"PL (%s) -> x0[]: %7.2f %7.2f %7.2f; n0[]: %7.2f %7.2f %7.2f\n",
531 facets[fc].first ?
"-" :
"*", x0[0], x0[1], x0[2], n0[0], n0[1], n0[2]);
533 TGeoHalfSpace *shell =
new TGeoHalfSpace(vname, x0, n0);
536 case EicOpenCascadeShape::_OCC_SPHERE_:
538 gp_Sphere *sphere = (gp_Sphere *)cuttingShape.object;
540 TGeoSphere *shell =
new TGeoSphere(vname, 0.0, sphere->Radius());
541 ts =
new TGeoCombiTrans(tname, sphere->Location().X(),
542 sphere->Location().Y(),
543 sphere->Location().Z(), 0);
544 printf(
"SP (%s) x0[]: %f %f %f, r = %f\n", facets[fc].first ?
"-" :
"*",
545 sphere->Location().X(),
546 sphere->Location().Y(), sphere->Location().Z(), sphere->Radius());
549 case EicOpenCascadeShape::_OCC_CYLINDER_:
551 gp_Cylinder *cylinder = (gp_Cylinder *)cuttingShape.object;
553 TGeoTube *shell =
new TGeoTube(vname, 0.0, cylinder->Radius(), cuttingShape.dimension);
555 ts =
Ax3ToCombiTrans(tname, cylinder->Position(), cuttingShape.dimension);
557 printf(
"CY (%s) x0[]: %f %f %f, r = %f\n", facets[fc].first ?
"-" :
"*",
558 cylinder->Location().X(),
559 cylinder->Location().Y(), cylinder->Location().Z(), cylinder->Radius());
562 case EicOpenCascadeShape::_OCC_CONE_:
564 gp_Cone *cone = (gp_Cone *)cuttingShape.object;
566 TGeoCone *shell =
new TGeoCone(vname, cuttingShape.dimension/2.,
568 0.0, cuttingShape.dimension*tan(cone->SemiAngle()));
570 ts =
Ax3ToCombiTrans(tname, cone->Position(), cuttingShape.dimension/2.);
572 printf(
"CO (%s) x0[]: %f %f %f\n", facets[fc].first ?
"-" :
"*",
573 cone->Location().X(), cone->Location().Y(), cone->Location().Z());
576 case EicOpenCascadeShape::_OCC_TORUS_:
578 gp_Torus *torus = (gp_Torus *)cuttingShape.object;
580 TGeoTorus *shell =
new TGeoTorus(vname, torus->MajorRadius(),
581 0.0, torus->MinorRadius(), 0.0, 360.0);
585 printf(
"TO (%s) x0[]: %f %f %f, rMa = %f, rMi = %f\n", facets[fc].first ?
"-" :
"*",
586 torus->Location().X(),
587 torus->Location().Y(), torus->Location().Z(),
588 torus->MajorRadius(), torus->MinorRadius());
595 if (ts) ts->RegisterYourself();
597 cmd =
"(" + cmd +
")" + (facets[fc].first ?
"-" :
"*") + vname + (ts ? TString(
":") + tname :
"");
602 printf(
"%s\n", cmd.Data());
604 TopoDS_Shape myShape =
solid;
606 BRepPrimAPI_MakeSphere(*boundarySphereCenter, boundarySphereRadius).Shape();
608 for(
unsigned fc=0; fc<facets.size(); fc++) {
609 EicOpenCascadeShape &cuttingShape = facets[fc].second;
611 if (facets[fc].first)
612 myShape = BRepAlgoAPI_Cut (myShape, *cuttingShape.solid);
614 myShape = BRepAlgoAPI_Common(myShape, *cuttingShape.solid);
619 static unsigned ccounter;
622 snprintf(cname, 128-1,
"C%04d", ccounter++);
628 TGeoVolume *vcomp =
new TGeoVolume(cname, comp, medium);
631 static unsigned current_color_id = 1000;
632 static std::vector<TColor*> colors;
635 for(
unsigned c=0;
c<colors.size();
c++)
637 TColor *clr = colors[
c];
639 if (clr->GetRed() == float(color[0]) &&
640 clr->GetGreen() == float(color[1]) &&
641 clr->GetBlue() == float(color[2]))
643 cid = clr->GetNumber();
650 TColor *clr =
new TColor(++current_color_id, color[0], color[1], color[2]);
651 colors.push_back(clr);
652 printf(
" @C@ %d, %f, %f, %f\n", current_color_id,
653 float(color[0]),
float(color[1]),
float(color[2]));
655 cid = current_color_id;
666 TColor *color =
new TColor(
id, 1., 0., 1.);
671 vcomp->SetLineColor(cid);
672 vcomp->SetFillColor(cid);
676 mCave->AddNode(vcomp, 0,
new TGeoCombiTrans(0, 0, 0, 0));
677 vcomp->RegisterYourself();
683 #include <Geom_BSplineCurve.hxx>
684 #include <Geom_Circle.hxx>
686 void EicCadFile::HandleThisSolid(
const TopoDS_Shape &solid, TGeoMedium *medium,
double *color)
690 printf(
"EicCadFile::HandleThisSolid() ...\n");
692 std::vector< std::pair<unsigned, EicOpenCascadeShape> > facets;
694 unsigned hasCurvedFaces = 0;
697 const Standard_Transient *shapeIds[] = {
698 STANDARD_TYPE(Geom_Plane),
699 STANDARD_TYPE(Geom_CylindricalSurface),
700 STANDARD_TYPE(Geom_SphericalSurface),
701 STANDARD_TYPE(Geom_ToroidalSurface),
702 STANDARD_TYPE(Geom_ConicalSurface)};
706 for(
unsigned pass=0; pass<(
sizeof(shapeIds)/
sizeof(shapeIds[0])); pass++) {
707 for (TopExp_Explorer itg(solid,TopAbs_FACE); itg.More(); itg.Next()) {
708 TopoDS_Face &face = (TopoDS_Face&)itg.Current();
712 Handle(Geom_Surface) SS = BRep_Tool::
Surface(face);
713 cout << " " << SS->DynamicType() << endl;
715 if (SS->IsKind((const Standard_Type*)shapeIds[pass])) {
716 EicOpenCascadeShape cuttingShape;
717 std::vector<TopoDS_Shape> commonSolids, cutSolids;
719 assert(elementaryFaceType(face));
721 printf(
"Splitting ...\n");
722 if (splitSolidByInfiniteFace(solid, face, cuttingShape,
723 commonSolids, cutSolids)) {
724 printf(
" Will now handle %d (common) & %d (cut) sub-solids ...\n",
725 commonSolids.size(), cutSolids.size());
727 for(
unsigned sh=0; sh<commonSolids.size(); sh++)
728 HandleThisSolid(commonSolids[sh], medium, color);
730 for(
unsigned sh=0; sh<cutSolids.size(); sh++)
731 HandleThisSolid(cutSolids[sh], medium, color);
736 assert(commonSolids.size() + cutSolids.size() == 1);
741 if (!SS->IsKind(STANDARD_TYPE(Geom_Plane))) hasCurvedFaces = 1;
748 std::pair<unsigned, EicOpenCascadeShape> qPair(cutSolids.size(), cuttingShape);
750 for(
unsigned fc=0; fc<facets.size(); fc++)
751 if (EicCadShapeEqual(qPair, facets[fc])) {
756 if (!exists) facets.push_back(qPair);
771 unsigned hasComplexFaces = 0;
773 for (TopExp_Explorer itg(solid,TopAbs_FACE); itg.More(); itg.Next()) {
774 TopoDS_Face &face = (TopoDS_Face&)itg.Current();
780 Handle(Geom_Surface) SS = BRep_Tool::
Surface(face);
781 cout << " " << SS->DynamicType() << endl;
782 if (SS->IsKind(STANDARD_TYPE(Geom_Plane))) {
786 for (TopExp_Explorer ith(itg.Current(),TopAbs_EDGE); ith.More(); ith.Next()) {
787 TopoDS_Edge &edge = (TopoDS_Edge&)ith.Current();
789 Handle(Geom_Curve) SC = BRep_Tool::Curve(edge, first, last);
792 cout << " --> " << SC->DynamicType() << endl;
793 if (SC->IsKind(STANDARD_TYPE(Geom_BSplineCurve))) {
794 Handle(Geom_BSplineCurve) spline = Handle(Geom_BSplineCurve)::DownCast(SC);
796 gp_Pnt
start = spline->StartPoint(), end = spline->EndPoint();
798 printf("%
d %
d\
n", spline->NbKnots(), spline->NbPoles());
799 printf("%
d %
d\n", spline->Degree(), spline->MaxDegree());
801 end.X(), end.Y(), end.Z());
804 if (SC->IsKind(STANDARD_TYPE(Geom_Circle))) {
805 Handle(Geom_Circle) circle = Handle(Geom_Circle)::DownCast(SC);
806 gp_Pnt center = circle->Location();
808 printf("%f %f %f; %f\n", center.X(), center.Y(), center.Z(), circle->Radius());
813 if (!elementaryFaceType(face)) hasComplexFaces = 1;
817 if (hasComplexFaces ) {
818 printf(
"Complex face found -> dump as STL!\n");
820 DumpAsStlSolid(solid, medium);
828 printf(
"Can decompose into basic ROOT shapes!\n");
829 DumpAsRootSolid(solid, facets, medium, color);
832 DumpAsStlSolid(solid, medium);
839 void EicCadFile::HandleThisSolidWrapper(
const TopoDS_Shape &solid, TGeoMedium *medium,
double *color)
842 double xMin, yMin, zMin, xMax, yMax, zMax;
846 Boundary.Get(xMin, yMin, zMin, xMax, yMax, zMax);
848 if (boundarySphereCenter)
delete boundarySphereCenter;
851 boundarySphereCenter =
new gp_Pnt((xMin+xMax)/2., (yMin+yMax)/2., (zMin+zMax)/2.);
852 boundarySphereRadius = sqrt(
SQR(xMax-xMin) +
SQR(yMax-yMin) +
SQR(zMax-zMin))/2.;
856 HandleThisSolid(solid, medium, color);
858 DumpAsStlSolid(solid, medium);
867 void EicCadFile::ConstructDummyStepGeometry()
869 gp_Pnt p0(-1,-1,-2), p1(1,1,2), p2(1, 0, 0), p3(0,0,-2);
871 TopoDS_Solid rect1 = BRepPrimAPI_MakeBox(p0, p1).Solid();
872 TopoDS_Solid sphere = BRepPrimAPI_MakeSphere(p3, 0.7).Solid();
874 gp_Pnt p000(0,0,0), p111(3,3,3);
875 TopoDS_Solid rect2 = BRepPrimAPI_MakeBox(p000, p111).Solid();
879 gp_Pnt q000(-2,-1,2), q111(1,1,6);
880 TopoDS_Solid rect3 = BRepPrimAPI_MakeBox(q000, q111).Solid();
882 gp_Pnt r000(-1,-1,-4), r111(1,1,-3);
883 TopoDS_Solid rect4 = BRepPrimAPI_MakeBox(r000, r111).Solid();
890 ax2.SetDirection(dir);
891 TopoDS_Solid cylinder = BRepPrimAPI_MakeCylinder(ax2, 3., 10.).Solid();
893 TopoDS_Solid torus = BRepPrimAPI_MakeTorus(ax2, 3., 2.).Solid();
895 TopoDS_Solid cone = BRepPrimAPI_MakeCone(ax2, 0.0, 5.0, 10.0).Solid();
900 TopoDS_Shape solid = BRepAlgoAPI_Fuse(rect1, sphere);
903 double color[3] = {0., 0., 0.};
918 void EicCadFile::ConstructStepGeometry()
921 EicStepMaterialReader stReader;
936 "given and StepCode support is not compiled in! \033[0m",
944 STEPControl_Reader cReader;
949 cReader.TransferRoots();
950 printf(
"%d root(s) in STEP file; %d shape(s)\n", cReader.NbRootsForTransfer(), cReader.NbShapes());
955 Handle(Transfer_TransientProcess) TP = cReader.WS()->TransferReader()->TransientProcess();
956 Handle(Interface_InterfaceModel) Model =cReader.Model();
957 printf("OpenCascade Model->NbEntities(): %
d\n", Model->NbEntities());
961 scaling.SetScaleFactor(
config()->scale() *
config()->units());
962 BRepBuilderAPI_Transform sc(scaling);
966 for(
unsigned sh=0; sh<cReader.NbShapes(); sh++) {
967 TopoDS_Shape shape = cReader.Shape(sh+1);
970 for (TopExp_Explorer itf(shape,TopAbs_SOLID); itf.More(); itf.Next()) {
980 for (
unsigned iqq=0; iqq<Model->NbEntities(); iqq++) {
981 Handle(Standard_Transient) enti = Model->Value(iqq+1);
985 if(enti->IsKind(STANDARD_TYPE(StepRepr_NextAssemblyUsageOccurrence))) {
986 Standard_Integer index = TP->MapIndex(enti);
989 Handle(Transfer_Binder) binder = TP->MapItem (index);
990 TopoDS_Shape S = TransferBRep::ShapeResult(binder);
991 if (S.IsEqual(itf.Current())) {
992 printf(
" .. found (%4d)!\n", iqq);
995 const char *mediumName = stReader.GetMediumName(iqq);
998 "in step file (and no medium given by hand)! \033[0m", iqq);
1005 "check media.geo file! \033[0m", mediumName);
1014 "given and StepCode support is not compiled in! \033[0m",
1019 double color[3] = {0., 0., 0.};
1024 TopExp_Explorer itg(itf.Current(),TopAbs_FACE);
1030 for (
unsigned iqq=0; iqq<Model->NbEntities(); iqq++) {
1031 Handle(Standard_Transient) enti = Model->Value(iqq+1);
1039 Standard_Integer index = TP->MapIndex(enti);
1042 Handle(Transfer_Binder) binder = TP->MapItem (index);
1043 TopoDS_Shape S = TransferBRep::ShapeResult(binder);
1044 if (S.IsEqual(itg.Current())) {
1047 if (!stReader.GetColor(iqq, color))
1055 const char *mediumName = stReader.GetMediumName(iqq);
1058 "in step file (and no medium given by hand)! \033[0m", iqq);
1060 medium = mediaHub->GetMedium(mediumName);
1065 "check media.geo file! \033[0m", mediumName);
1076 sc.Perform(itf.Current());
1078 static unsigned counter;
1081 printf(
"counter: %d\n", counter);
1091 HandleThisSolidWrapper(sc.ModifiedShape(itf.Current()), medium, color);
1102 #include <STEPControl_Writer.hxx>
1110 snprintf(fname, 1024-1,
"%s.root",
dname->
name().Data());
1116 STEPControl_Reader cReader;
1120 cReader.TransferRoots();
1121 printf(
"%d root(s) in STEP file; %d shape(s)\n", cReader.NbRootsForTransfer(), cReader.NbShapes());
1126 Handle(Transfer_TransientProcess) TP = cReader.WS()->TransferReader()->TransientProcess();
1127 Handle(Interface_InterfaceModel) Model = cReader.Model();
1128 printf("OpenCascade Model->NbEntities(): %
d\n", Model->NbEntities());
1133 gp_Pnt x0(0.0, 0.0, 0.0);
1136 gp_Dir n0(0.0, 1.0, 0.0);
1137 gp_Ax1 axis(x0, n0);
1139 scaling.SetRotation(axis,
config()->GetRotationAngle() * TMath::DegToRad());
1142 scaling.SetScaleFactor(
config()->scale() *
config()->units());
1143 BRepBuilderAPI_Transform sc(scaling);
1147 for(
unsigned sh=0; sh<cReader.NbShapes(); sh++) {
1148 TopoDS_Shape shape = cReader.Shape(sh+1);
1151 for (TopExp_Explorer itf(shape,TopAbs_SOLID); itf.More(); itf.Next()) {
1154 double color[3] = {0., 0., 0.};
1157 sc.Perform(itf.Current());
1160 static unsigned counter;
1161 printf(
"counter: %d\n", counter);
1162 const TopoDS_Shape &solid = sc.ModifiedShape(itf.Current());
1164 STEPControl_Writer cWriter;
1165 cWriter.Transfer(solid, STEPControl_ManifoldSolidBrep);
1168 cWriter.Write(name);
1174 HandleThisSolidWrapper(sc.ModifiedShape(itf.Current()), medium, color);
1178 const TopoDS_Shape &solid = sc.ModifiedShape(itf.Current());
1179 std::vector<TopoDS_Shape> parts;
1180 gp_Pnt x0(0.0, 0.0, 0.0);
1181 gp_Dir n0(0.0, 1.0, 0.0);
1183 TopoDS_Face face = BRepBuilderAPI_MakeFace(pl);
1185 gp_Pnt pt(x0.X() - t*n0.X(), x0.Y() - t*n0.Y(), x0.Z() - t*n0.Z());
1186 TopoDS_Shape *cut =
new TopoDS_Shape(BRepPrimAPI_MakeHalfSpace(face, pt).Solid());
1188 TopoDS_Shape commonHalf = BRepAlgoAPI_Common(solid, *cut);
1189 for (TopExp_Explorer itq(commonHalf,TopAbs_SOLID); itq.More(); itq.Next())
1190 parts.push_back(itq.Current());
1192 TopoDS_Shape cutHalf = BRepAlgoAPI_Cut(solid, *cut);
1193 for (TopExp_Explorer itq(cutHalf,TopAbs_SOLID); itq.More(); itq.Next())
1194 parts.push_back(itq.Current());
1196 unsigned rCounter = 0;
1197 printf(
"--> %d\n", parts.size());
1198 for(
unsigned pt=0; pt<parts.size(); pt++) {
1199 STEPControl_Writer cWriter;
1200 cWriter.Transfer(parts[pt], STEPControl_ManifoldSolidBrep);
1202 snprintf(name, 1024-1,
"test-%05d.stp", rCounter);
1203 cWriter.Write(name);
1216 const TopoDS_Shape &solid = itf.Current();
1219 double xMin, yMin, zMin, xMax, yMax, zMax;
1223 Boundary.Get(xMin, yMin, zMin, xMax, yMax, zMax);
1225 if (boundarySphereCenter)
delete boundarySphereCenter;
1228 boundarySphereCenter =
new gp_Pnt((xMin+xMax)/2., (yMin+yMax)/2., (zMin+zMax)/2.);
1229 boundarySphereRadius = sqrt(
SQR(xMax-xMin) +
SQR(yMax-yMin) +
SQR(zMax-zMin))/2.;
1232 unsigned faceCounter = 0;
1234 for (TopExp_Explorer itg(solid,TopAbs_FACE); itg.More(); itg.Next())
1236 printf(
"%3d faces total!\n", faceCounter);
1237 for (TopExp_Explorer itg(solid,TopAbs_FACE); itg.More(); itg.Next()) {
1238 TopoDS_Face &face = (TopoDS_Face&)itg.Current();
1243 Handle(Geom_Surface) SS = BRep_Tool::
Surface(face);
1244 cout << " " << SS->DynamicType() << endl;
1245 if (!elementaryFaceType(face)) continue;
1253 EicOpenCascadeShape cuttingShape;
1254 std::vector<TopoDS_Shape> commonSolids, cutSolids;
1255 std::vector<TopoDS_Shape> *arr[2] = {&commonSolids, &cutSolids};
1256 unsigned pCounter[2], fCounter[2] = {0, 0};
1258 splitSolidByInfiniteFace(solid, face, cuttingShape, commonSolids, cutSolids);
1261 for(
unsigned gr=0; gr<2; gr++) {
1262 std::vector<TopoDS_Shape> *group = arr[gr];
1264 pCounter[gr] = group->size();
1265 for(
unsigned pt=0; pt<group->size(); pt++) {
1266 const TopoDS_Shape &shape = (*group)[pt];
1268 for (TopExp_Explorer itx(shape,TopAbs_FACE); itx.More(); itx.Next())
1275 if (pCounter[0] + pCounter[1] >= 2) {
1276 for(
unsigned gr=0; gr<2; gr++)
1277 printf(
" gr#%d -> %3d solids & %4d faces\n", gr, pCounter[gr], fCounter[gr]);
1278 printf(
" --> %4d faces total!\n", fCounter[0] + fCounter[1]);
1281 if (pCounter[0] + pCounter[1] >= 2) {
1282 unsigned rCounter = 0;
1284 for(
unsigned gr=0; gr<2; gr++) {
1285 std::vector<TopoDS_Shape> *group = arr[gr];
1287 for(
unsigned pt=0; pt<group->size(); pt++) {
1288 const TopoDS_Shape &shape = (*group)[pt];
1290 STEPControl_Writer cWriter;
1293 cWriter.Transfer(shape, STEPControl_ManifoldSolidBrep);
1295 snprintf(name, 1024-1,
"test-%03d-%05d.stp", rfCounter, rCounter);
1296 cWriter.Write(name);
1333 void EicCadFile::ConstructIgesGeometry()
1336 IGESControl_Reader cReader;
1341 cReader.TransferRoots();
1345 BRepBuilderAPI_Sewing *Sew =
new BRepBuilderAPI_Sewing();
1349 for(
unsigned sh=0; sh<cReader.NbShapes(); sh++) {
1350 const TopoDS_Shape shape = cReader.Shape(sh+1);
1354 for (TopExp_Explorer itf(shape,TopAbs_EDGE); itf.More(); itf.Next()) {
1356 TopoDS_Edge &edge = (TopoDS_Edge&)itf.Current();
1357 Handle(Geom_Curve) SC = BRep_Tool::Curve(edge, first, last);
1359 cout << SC->DynamicType() << endl;
1364 for (TopExp_Explorer itf(shape,TopAbs_FACE); itf.More(); itf.Next()) {
1365 static unsigned counter;
1367 Sew->Add(itf.Current());
1370 TopoDS_Face &face = (TopoDS_Face&)itf.Current();
1371 if (!elementaryFaceType(face)) {
1372 printf(
" One more complex face: \n");
1373 Handle(Geom_Surface) SS = BRep_Tool::
Surface(face);
1374 cout << " " << SS->DynamicType() << endl;
1376 for (TopExp_Explorer ith(itg.Current(),TopAbs_EDGE); ith.More(); ith.Next()) {
1377 TopoDS_Edge &edge = (TopoDS_Edge&)ith.Current();
1379 Handle(Geom_Curve) SC = BRep_Tool::Curve(edge, first, last);
1382 cout << " --> " << SC->DynamicType() << endl;
1392 Sew->Perform(); Sew->Dump();
1393 TopoDS_Shape result = Sew->SewedShape();
1394 for (TopExp_Explorer itf(result,TopAbs_SHELL); itf.More(); itf.Next()) {
1395 TopoDS_Shell &shell = (TopoDS_Shell&)itf.Current();
1407 void EicCadFile::ConstructElmerGeometry()
1409 mElmerMesh =
new mesh_t();
1416 printf(
"@@@: %d elements\n", mElmerMesh->getElements());
1417 for(
unsigned el=0; el<mElmerMesh->getElements(); el++) {
1418 element_t *element = mElmerMesh->getElement(el);
1427 for(
unsigned nd=0; nd<element->
getNodes(); nd++) {
1439 snprintf(name, 128-1,
"%sTet%06d",
dname->
Name().Data(), el);
1447 TVector3 qpoints[4];
1449 snprintf(qname, 128-1,
"%sTet%06dxz",
dname->
Name().Data(), el);
1451 for(
unsigned pt=0; pt<4; pt++) {
1452 qpoints[pt] = points[pt];
1453 qpoints[pt].SetY(-qpoints[pt].
y());
1460 TVector3 qpoints[4];
1462 snprintf(qname, 128-1,
"%sTet%06dxy",
dname->
Name().Data(), el);
1464 for(
unsigned pt=0; pt<4; pt++) {
1465 qpoints[pt] = points[pt];
1466 qpoints[pt].SetZ(-qpoints[pt].
z());
1473 TVector3 qpoints[4];
1475 snprintf(qname, 128-1,
"%sTet%06dxyz",
dname->
Name().Data(), el);
1477 for(
unsigned pt=0; pt<4; pt++) {
1478 qpoints[pt] = points[pt];
1479 qpoints[pt].SetY(-qpoints[pt].
y());
1480 qpoints[pt].SetZ(-qpoints[pt].
z());
1503 mCave = gGeoManager->GetTopVolume();
1506 #ifdef _OPENCASCADE_
1508 ConstructStepGeometry();
1511 "support is not compiled in! \033[0m");
1515 #ifdef _OPENCASCADE_
1516 ConstructIgesGeometry();
1519 "support is not compiled in! \033[0m");
1523 std::vector<TGeoVolume*> volumes;
1528 for(
unsigned iq=0; iq<volumes.size(); iq++)
1537 ConstructElmerGeometry();
1540 "support is not compiled in! \033[0m");
1544 "allowed (and only if support is compiled in)! \033[0m");