| 1 | // Created on: 1998-12-16 |
| 2 | // Created by: Roman LYGIN |
| 3 | // Copyright (c) 1998-1999 Matra Datavision |
| 4 | // Copyright (c) 1999-2012 OPEN CASCADE SAS |
| 5 | // |
| 6 | // The content of this file is subject to the Open CASCADE Technology Public |
| 7 | // License Version 6.5 (the "License"). You may not use the content of this file |
| 8 | // except in compliance with the License. Please obtain a copy of the License |
| 9 | // at http://www.opencascade.org and read it completely before using this file. |
| 10 | // |
| 11 | // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its |
| 12 | // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France. |
| 13 | // |
| 14 | // The Original Code and all software distributed under the License is |
| 15 | // distributed on an "AS IS" basis, without warranty of any kind, and the |
| 16 | // Initial Developer hereby disclaims all such warranties, including without |
| 17 | // limitation, any warranties of merchantability, fitness for a particular |
| 18 | // purpose or non-infringement. Please see the License for the specific terms |
| 19 | // and conditions governing the rights and limitations under the License. |
| 20 | |
| 21 | |
| 22 | #include <IGESToBRep_IGESBoundary.ixx> |
| 23 | #include <TColStd_HSequenceOfTransient.hxx> |
| 24 | #include <Geom2d_Curve.hxx> |
| 25 | #include <Geom_Curve.hxx> |
| 26 | #include <Geom_Plane.hxx> |
| 27 | #include <GeomAdaptor_Curve.hxx> |
| 28 | #include <Precision.hxx> |
| 29 | #include <TopoDS_Edge.hxx> |
| 30 | #include <TopoDS_Wire.hxx> |
| 31 | #include <BRep_Builder.hxx> |
| 32 | #include <BRep_Tool.hxx> |
| 33 | #include <ShapeBuild_Edge.hxx> |
| 34 | #include <IGESToBRep.hxx> |
| 35 | #include <IGESToBRep_TopoCurve.hxx> |
| 36 | #include <ShapeFix_Wire.hxx> |
| 37 | |
| 38 | //======================================================================= |
| 39 | //function : IGESToBRep_IGESBoundary |
| 40 | //purpose : |
| 41 | //======================================================================= |
| 42 | |
| 43 | IGESToBRep_IGESBoundary::IGESToBRep_IGESBoundary() |
| 44 | { |
| 45 | } |
| 46 | |
| 47 | //======================================================================= |
| 48 | //function : IGESToBRep_IGESBoundary |
| 49 | //purpose : |
| 50 | //======================================================================= |
| 51 | |
| 52 | IGESToBRep_IGESBoundary::IGESToBRep_IGESBoundary(const IGESToBRep_CurveAndSurface& CS) |
| 53 | : myCS (CS) |
| 54 | { |
| 55 | } |
| 56 | |
| 57 | //======================================================================= |
| 58 | //function : Init |
| 59 | //purpose : |
| 60 | //======================================================================= |
| 61 | |
| 62 | void IGESToBRep_IGESBoundary::Init(const IGESToBRep_CurveAndSurface& CS, |
| 63 | const Handle(IGESData_IGESEntity)& entity, |
| 64 | const TopoDS_Face& face, |
| 65 | const gp_Trsf2d& trans, |
| 66 | const Standard_Real uFact, |
| 67 | const Standard_Integer filepreference) |
| 68 | { |
| 69 | myCS = CS; |
| 70 | myentity = entity; |
| 71 | myface = face; |
| 72 | mytrsf = trans; |
| 73 | myuFact = uFact; |
| 74 | myfilepreference = filepreference; |
| 75 | } |
| 76 | |
| 77 | //======================================================================= |
| 78 | //function : Transfer |
| 79 | //purpose : |
| 80 | //======================================================================= |
| 81 | |
| 82 | Standard_Boolean IGESToBRep_IGESBoundary::Transfer(Standard_Boolean& okCurve, |
| 83 | Standard_Boolean& okCurve3d, |
| 84 | Standard_Boolean& okCurve2d, |
| 85 | const Handle(IGESData_IGESEntity)& curve3d, |
| 86 | const Standard_Boolean toreverse3d, |
| 87 | const Handle(IGESData_HArray1OfIGESEntity)& curves2d, |
| 88 | const Standard_Integer number) |
| 89 | { |
| 90 | Handle(ShapeExtend_WireData) scurve3d, lsewd; //temporary objects |
| 91 | return Transfer (okCurve, okCurve3d, okCurve2d, |
| 92 | curve3d, scurve3d, Standard_False, toreverse3d, |
| 93 | curves2d, Standard_False, |
| 94 | number, lsewd); |
| 95 | } |
| 96 | |
| 97 | //======================================================================= |
| 98 | //function : Transfer |
| 99 | //purpose : |
| 100 | //======================================================================= |
| 101 | |
| 102 | Standard_Boolean IGESToBRep_IGESBoundary::Transfer(Standard_Boolean& okCurve, |
| 103 | Standard_Boolean& okCurve3d, |
| 104 | Standard_Boolean& okCurve2d, |
| 105 | const Handle(ShapeExtend_WireData)& curve3d, |
| 106 | const Handle(IGESData_HArray1OfIGESEntity)& curves2d, |
| 107 | const Standard_Boolean toreverse2d, |
| 108 | const Standard_Integer number, |
| 109 | Handle(ShapeExtend_WireData)& lsewd) |
| 110 | { |
| 111 | Handle(IGESData_IGESEntity) icurve3d; //temporary object |
| 112 | return Transfer (okCurve, okCurve3d, okCurve2d, |
| 113 | icurve3d, curve3d, Standard_True, Standard_False, |
| 114 | curves2d, toreverse2d, |
| 115 | number, lsewd); |
| 116 | } |
| 117 | |
| 118 | //======================================================================= |
| 119 | //function : Check |
| 120 | //purpose : |
| 121 | //======================================================================= |
| 122 | |
| 123 | void IGESToBRep_IGESBoundary::Check(const Standard_Boolean,const Standard_Boolean,const Standard_Boolean,const Standard_Boolean) |
| 124 | { |
| 125 | // Implemented in Advanced Data Exchange |
| 126 | } |
| 127 | |
| 128 | //======================================================================= |
| 129 | //function : Transfer |
| 130 | //purpose : |
| 131 | //======================================================================= |
| 132 | |
| 133 | Standard_Boolean IGESToBRep_IGESBoundary::Transfer(Standard_Boolean&, |
| 134 | Standard_Boolean&, |
| 135 | Standard_Boolean&, |
| 136 | const Handle(IGESData_IGESEntity)& icurve3d, |
| 137 | const Handle(ShapeExtend_WireData)& scurve3d, |
| 138 | const Standard_Boolean usescurve, |
| 139 | const Standard_Boolean toreverse3d, |
| 140 | const Handle(IGESData_HArray1OfIGESEntity)& curves2d, |
| 141 | const Standard_Boolean toreverse2d, |
| 142 | const Standard_Integer number, |
| 143 | Handle(ShapeExtend_WireData)& Gsewd) |
| 144 | { |
| 145 | Gsewd = new ShapeExtend_WireData;//local translation (for mysewd) |
| 146 | Handle(ShapeExtend_WireData) Gsewd3d = new ShapeExtend_WireData;//local translation (for mysewd3d) |
| 147 | Handle(ShapeExtend_WireData) Gsewd2d = new ShapeExtend_WireData;//local translation (for mysewd2d) |
| 148 | |
| 149 | Standard_Boolean GTranslate3d = Standard_True, GTranslate2d = Standard_True, |
| 150 | Preferred3d = Standard_True, Preferred2d = Standard_True; |
| 151 | |
| 152 | Standard_Integer len3d = 0, len2d = 0; |
| 153 | Handle(TColStd_HSequenceOfTransient) seq3d, seq2d; |
| 154 | if (usescurve) |
| 155 | len3d = scurve3d->NbEdges(); |
| 156 | else { |
| 157 | IGESToBRep::IGESCurveToSequenceOfIGESCurve (icurve3d, seq3d); |
| 158 | len3d = seq3d->Length(); |
| 159 | } |
| 160 | if (!curves2d.IsNull()) { |
| 161 | for (Standard_Integer i = 1; i <= curves2d->Length(); i++) |
| 162 | IGESToBRep::IGESCurveToSequenceOfIGESCurve (curves2d->Value (i), seq2d); |
| 163 | len2d = seq2d->Length(); |
| 164 | } |
| 165 | |
| 166 | if (len3d == 0) |
| 167 | GTranslate3d = Standard_False; |
| 168 | else if (len2d == 0) |
| 169 | GTranslate2d = Standard_False; |
| 170 | |
| 171 | if (GTranslate3d && GTranslate2d) { |
| 172 | //Setting preference in the case of inconsitency between 3D and 2D |
| 173 | if (myfilepreference == 2) Preferred3d = Standard_False; |
| 174 | else if (myfilepreference == 3) Preferred2d = Standard_False; |
| 175 | else Preferred3d = Standard_False; |
| 176 | } |
| 177 | if (GTranslate3d && GTranslate2d && len3d != len2d) { |
| 178 | GTranslate3d = Preferred3d; |
| 179 | GTranslate2d = Preferred2d; |
| 180 | } |
| 181 | |
| 182 | IGESToBRep_TopoCurve TC (myCS); |
| 183 | |
| 184 | if (GTranslate3d && !GTranslate2d) { |
| 185 | if (usescurve) |
| 186 | Gsewd->Add (scurve3d->Wire()); |
| 187 | else { |
| 188 | TopoDS_Shape Sh = TC.TransferTopoCurve (icurve3d); |
| 189 | if (!Sh.IsNull()) { |
| 190 | Gsewd3d->Add (Sh); |
| 191 | if (toreverse3d) { |
| 192 | ReverseCurves3d (Gsewd3d); |
| 193 | Gsewd->Add (Gsewd3d->Wire()); |
| 194 | } |
| 195 | else Gsewd->Add (Sh);//Gsewd = Gsewd3d is impossible to avoid sharing of sewd (UK1.igs entity 7) |
| 196 | } |
| 197 | } |
| 198 | } |
| 199 | else if (!GTranslate3d && GTranslate2d) { |
| 200 | for (Standard_Integer i = curves2d->Lower(); i <= curves2d->Upper(); i++) { |
| 201 | TopoDS_Shape Sh = TC.Transfer2dTopoCurve (Handle(IGESData_IGESEntity)::DownCast (curves2d->Value (i)), |
| 202 | myface, mytrsf, myuFact); |
| 203 | if (!Sh.IsNull()) Gsewd2d->Add (Sh); |
| 204 | } |
| 205 | if (toreverse2d) |
| 206 | ReverseCurves2d (Gsewd2d, myface); |
| 207 | Gsewd->Add (Gsewd2d->Wire()); |
| 208 | } |
| 209 | else if (GTranslate3d && GTranslate2d) { |
| 210 | //Translate both curves 3D and 2D |
| 211 | //Suppose that i-th segment in 2D curve corresponds to i-th segment in 3D curve |
| 212 | for (Standard_Integer i = 1; i <= len3d; i++) { |
| 213 | Standard_Boolean LTranslate3d = Standard_True, LTranslate2d = Standard_True; |
| 214 | |
| 215 | Handle(ShapeExtend_WireData) Lsewd3d = new ShapeExtend_WireData; |
| 216 | TC.SetBadCase (Standard_False); //:27 |
| 217 | if (usescurve) |
| 218 | Lsewd3d->Add (scurve3d->Edge (i)); |
| 219 | else { |
| 220 | TopoDS_Shape shape3d = TC.TransferTopoCurve (Handle(IGESData_IGESEntity)::DownCast (seq3d->Value (i))); |
| 221 | if (!shape3d.IsNull()) { |
| 222 | Lsewd3d->Add (shape3d); |
| 223 | if (toreverse3d) |
| 224 | ReverseCurves3d (Lsewd3d); |
| 225 | } |
| 226 | else LTranslate3d = Standard_False; |
| 227 | } |
| 228 | Gsewd3d->Add (Lsewd3d->Wire()); |
| 229 | |
| 230 | Handle(ShapeExtend_WireData) Lsewd2d = new ShapeExtend_WireData; |
| 231 | TopoDS_Shape shape2d = TC.Transfer2dTopoCurve (Handle(IGESData_IGESEntity)::DownCast (seq2d->Value (i)), |
| 232 | myface, mytrsf, myuFact); |
| 233 | if (!shape2d.IsNull()) { |
| 234 | Lsewd2d->Add (shape2d); |
| 235 | if (toreverse2d) |
| 236 | ReverseCurves2d (Lsewd2d, myface); |
| 237 | Gsewd2d->Add (Lsewd2d->Wire()); |
| 238 | } |
| 239 | else LTranslate2d = Standard_False; |
| 240 | |
| 241 | if (LTranslate3d && LTranslate2d && Lsewd3d->NbEdges() != Lsewd2d->NbEdges()) { |
| 242 | LTranslate3d = Preferred3d; |
| 243 | LTranslate2d = Preferred2d; |
| 244 | } |
| 245 | Handle(ShapeExtend_WireData) Lsewd;//Lsewd3d or Lsewd2d or Lsewd3d+pcurve |
| 246 | if ( LTranslate3d && !LTranslate2d) Lsewd = Lsewd3d; |
| 247 | else if (!LTranslate3d && LTranslate2d) Lsewd = Lsewd2d; |
| 248 | else { |
| 249 | Lsewd = Lsewd3d; |
| 250 | //copying pcurve to edge with 3D curve |
| 251 | for (Standard_Integer iedge = 1; iedge <= Lsewd3d->NbEdges(); iedge++) { |
| 252 | TopoDS_Edge edge3d = Lsewd3d->Edge (iedge), edge2d = Lsewd2d->Edge (iedge); |
| 253 | if (!IGESToBRep::TransferPCurve (edge2d, edge3d, myface)) continue; |
| 254 | } |
| 255 | } |
| 256 | Gsewd->Add (Lsewd->Wire()); |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | if (number > 1) { |
| 261 | mysewd ->Add (Gsewd ->Wire()); |
| 262 | mysewd3d->Add (Gsewd3d->Wire()); |
| 263 | mysewd2d->Add (Gsewd2d->Wire()); |
| 264 | } |
| 265 | else { |
| 266 | mysewd = Gsewd; |
| 267 | mysewd3d = Gsewd3d; |
| 268 | mysewd2d = Gsewd2d; |
| 269 | } |
| 270 | return Standard_True; |
| 271 | } |
| 272 | |
| 273 | //======================================================================= |
| 274 | //function : ReverseCurves3d |
| 275 | //purpose : Reverses 3D curves of the edges in the wire and reverses |
| 276 | // the order of edges in the wire. |
| 277 | // Orientation of each edge is not changed |
| 278 | //======================================================================= |
| 279 | |
| 280 | void IGESToBRep_IGESBoundary::ReverseCurves3d (const Handle(ShapeExtend_WireData)& sewd) |
| 281 | { |
| 282 | sewd->Reverse(); |
| 283 | BRep_Builder B; |
| 284 | TopoDS_Wire W; |
| 285 | B.MakeWire(W); |
| 286 | for (Standard_Integer i = 1; i <= sewd->NbEdges(); i++) { |
| 287 | TopoDS_Edge oldedge = sewd->Edge (i), newedge; |
| 288 | TopLoc_Location L; |
| 289 | Standard_Real p1, p2; |
| 290 | Handle(Geom_Curve) curve = BRep_Tool::Curve (oldedge, L, p1, p2); |
| 291 | if (curve->IsPeriodic()) //#21 |
| 292 | ShapeBuild_Edge().MakeEdge (newedge, curve->Reversed(), L, |
| 293 | curve->ReversedParameter (p2), |
| 294 | curve->ReversedParameter (p1)); |
| 295 | else ShapeBuild_Edge().MakeEdge (newedge, curve->Reversed(), L, |
| 296 | Max (curve->ReversedParameter(curve->LastParameter()), curve->ReversedParameter (p2)), |
| 297 | Min (curve->ReversedParameter(curve->FirstParameter()), curve->ReversedParameter (p1))); |
| 298 | newedge.Orientation(TopAbs::Reverse (oldedge.Orientation())); |
| 299 | //sewd->Set (newedge, i); |
| 300 | B.Add(W,newedge); |
| 301 | } |
| 302 | Handle(ShapeFix_Wire) sfw = new ShapeFix_Wire(); |
| 303 | sfw->Load(W); |
| 304 | sfw->FixConnected(); |
| 305 | sewd->Init(sfw->Wire()); |
| 306 | } |
| 307 | |
| 308 | //======================================================================= |
| 309 | //function : ReverseCurves2d |
| 310 | //purpose : Reverses pcurves of the edges in the wire and reverses |
| 311 | // the order of edges in the wire. |
| 312 | // Orientation of each edge is also changed |
| 313 | //======================================================================= |
| 314 | |
| 315 | void IGESToBRep_IGESBoundary::ReverseCurves2d (const Handle(ShapeExtend_WireData)& sewd, const TopoDS_Face& face) |
| 316 | { |
| 317 | sewd->Reverse(face); |
| 318 | for (Standard_Integer i = 1; i <= sewd->NbEdges(); i++) { |
| 319 | TopoDS_Edge oldedge = sewd->Edge (i), newedge; |
| 320 | Standard_Real p1, p2; |
| 321 | Handle(Geom2d_Curve) curve = BRep_Tool::CurveOnSurface (oldedge, face, p1, p2); |
| 322 | |
| 323 | // skl 24.04.2002 for OCC314 |
| 324 | if(curve->IsPeriodic()) |
| 325 | ShapeBuild_Edge().MakeEdge (newedge, curve->Reversed(), face, |
| 326 | curve->ReversedParameter (p2), |
| 327 | curve->ReversedParameter (p1)); |
| 328 | else |
| 329 | ShapeBuild_Edge().MakeEdge (newedge, curve->Reversed(), face, |
| 330 | Max (curve->FirstParameter(), curve->ReversedParameter (p2)),//BUC50001 entity 936 2DForced |
| 331 | Min (curve->LastParameter(), curve->ReversedParameter (p1))); |
| 332 | newedge.Orientation(oldedge.Orientation()); |
| 333 | sewd->Set (newedge, i); |
| 334 | } |
| 335 | } |
| 336 | |