0033661: Data Exchange, Step Import - Tessellated GDTs are not imported
[occt.git] / src / PrsDim / PrsDim_Relation.cxx
1 // Created on: 1996-12-05
2 // Created by: Odile Olivier
3 // Copyright (c) 1996-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
5 //
6 // This file is part of Open CASCADE Technology software library.
7 //
8 // This library is free software; you can redistribute it and/or modify it under
9 // the terms of the GNU Lesser General Public License version 2.1 as published
10 // by the Free Software Foundation, with special exception defined in the file
11 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
12 // distribution for complete text of the license and disclaimer of any warranty.
13 //
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
16
17 #include <PrsDim_Relation.hxx>
18
19 #include <PrsDim.hxx>
20 #include <AIS_GraphicTool.hxx>
21 #include <BRep_Tool.hxx>
22 #include <BRepBuilderAPI_MakeEdge.hxx>
23 #include <BRepBuilderAPI_MakeVertex.hxx>
24 #include <ElCLib.hxx>
25 #include <Geom_Circle.hxx>
26 #include <Geom_Curve.hxx>
27 #include <Geom_Line.hxx>
28 #include <Graphic3d_ArrayOfSegments.hxx>
29 #include <gp_Pnt.hxx>
30 #include <Graphic3d_Group.hxx>
31 #include <Precision.hxx>
32 #include <Prs3d_DimensionAspect.hxx>
33 #include <Prs3d_Drawer.hxx>
34 #include <Prs3d_LineAspect.hxx>
35 #include <Prs3d_Presentation.hxx>
36 #include <Prs3d_TextAspect.hxx>
37 #include <Quantity_Color.hxx>
38 #include <StdPrs_Point.hxx>
39 #include <StdPrs_WFShape.hxx>
40 #include <TopExp.hxx>
41 #include <TopoDS.hxx>
42 #include <TopoDS_Edge.hxx>
43 #include <TopoDS_Shape.hxx>
44 #include <TopoDS_Vertex.hxx>
45
46 IMPLEMENT_STANDARD_RTTIEXT(PrsDim_Relation, AIS_InteractiveObject)
47
48 //=======================================================================
49 //function : PrsDim_Relation
50 //purpose  : 
51 //=======================================================================
52 PrsDim_Relation::PrsDim_Relation(const PrsMgr_TypeOfPresentation3d aTypeOfPresentation3d)
53 :AIS_InteractiveObject(aTypeOfPresentation3d),
54  myVal(1.),
55  myPosition(0.,0.,0.),
56  myArrowSize( myVal / 10. ),
57  myAutomaticPosition(Standard_True),
58  myExtShape(0),
59  myFirstOffset(0.),mySecondOffset(0.),
60  myIsSetBndBox( Standard_False ),
61  myArrowSizeIsDefined( Standard_False)
62 {
63 }
64
65 //=======================================================================
66 //function : ComputeProjEdgePresentation
67 //purpose  : 
68 //=======================================================================
69
70 void PrsDim_Relation::ComputeProjEdgePresentation(const Handle(Prs3d_Presentation)& aPrs, 
71                                                const TopoDS_Edge& anEdge,
72                                                const Handle(Geom_Curve)& ProjCurv, 
73                                                const gp_Pnt& FirstP, 
74                                                const gp_Pnt& LastP, 
75                                                const Quantity_NameOfColor aColor,
76                                                const Standard_Real width,
77                                                const Aspect_TypeOfLine aProjTOL,
78                                                const Aspect_TypeOfLine aCallTOL) const 
79 {
80   if (!myDrawer->HasOwnWireAspect()){
81     myDrawer->SetWireAspect(new Prs3d_LineAspect(aColor,aProjTOL,2.));}
82   else {
83     const Handle(Prs3d_LineAspect)& li = myDrawer->WireAspect();
84     li->SetColor(aColor);
85     li->SetTypeOfLine(aProjTOL);
86     li->SetWidth(width);
87   }
88
89   Standard_Real pf, pl;
90   TopLoc_Location loc;
91   Handle(Geom_Curve) curve;
92   Standard_Boolean isInfinite;
93   curve = BRep_Tool::Curve(anEdge,loc,pf,pl);
94   isInfinite = (Precision::IsInfinite(pf) || Precision::IsInfinite(pl));
95
96   TopoDS_Edge E;
97
98   // Calcul de la presentation de l'edge
99   if (ProjCurv->IsInstance(STANDARD_TYPE(Geom_Line)) ) {
100     Handle(Geom_Line) gl (Handle(Geom_Line)::DownCast (ProjCurv));
101     if ( !isInfinite) {
102       pf = ElCLib::Parameter(gl->Lin(),FirstP);
103       pl = ElCLib::Parameter(gl->Lin(),LastP);
104       BRepBuilderAPI_MakeEdge MakEd(gl->Lin(), pf, pl);
105       E = MakEd.Edge();
106     }
107     else {
108       BRepBuilderAPI_MakeEdge MakEd(gl->Lin());
109       E = MakEd.Edge();
110     }
111   }
112   else if (ProjCurv->IsInstance(STANDARD_TYPE(Geom_Circle)) ) {
113     Handle(Geom_Circle) gc (Handle(Geom_Circle)::DownCast (ProjCurv));
114     pf = ElCLib::Parameter(gc->Circ(),FirstP);
115     pl = ElCLib::Parameter(gc->Circ(),LastP);
116     BRepBuilderAPI_MakeEdge MakEd(gc->Circ(),pf, pl);
117     E = MakEd.Edge();
118   }
119   StdPrs_WFShape::Add (aPrs, E, myDrawer);
120
121   //Calcul de la presentation des lignes de raccord
122   myDrawer->WireAspect()->SetTypeOfLine(aCallTOL);
123   if (!isInfinite) {
124     gp_Pnt ppf, ppl;
125     ppf = BRep_Tool::Pnt( TopExp::FirstVertex(TopoDS::Edge(anEdge)));
126     ppl = BRep_Tool::Pnt( TopExp::LastVertex(TopoDS::Edge(anEdge)));
127     if (FirstP.Distance (ppf) > gp::Resolution())
128     {
129       BRepBuilderAPI_MakeEdge MakEd1 (FirstP, ppf);
130       StdPrs_WFShape::Add (aPrs, MakEd1.Edge(), myDrawer);
131     }
132     else
133     {
134       BRepBuilderAPI_MakeVertex MakVert1 (FirstP);
135       StdPrs_WFShape::Add (aPrs, MakVert1.Vertex(), myDrawer);
136     }
137     if (LastP.Distance (ppl) > gp::Resolution())
138     {
139       BRepBuilderAPI_MakeEdge MakEd2 (LastP, ppl);
140       StdPrs_WFShape::Add (aPrs, MakEd2.Edge(), myDrawer);
141     }
142     else
143     {
144       BRepBuilderAPI_MakeVertex MakVert2 (LastP);
145       StdPrs_WFShape::Add (aPrs, MakVert2.Vertex(), myDrawer);
146     }
147 /*
148     BRepBuilderAPI_MakeEdge MakEd1 (FirstP, ppf);
149     StdPrs_WFShape::Add (aPrs, MakEd1.Edge(), myDrawer);
150     BRepBuilderAPI_MakeEdge MakEd2 (LastP, ppl);
151     StdPrs_WFShape::Add (aPrs, MakEd2.Edge(), myDrawer);
152 */
153   }
154 }
155
156
157 //=======================================================================
158 //function : ComputeProjVertexPresentation
159 //purpose  : 
160 //=======================================================================
161
162 void PrsDim_Relation::ComputeProjVertexPresentation(const Handle(Prs3d_Presentation)& aPrs, 
163                                                  const TopoDS_Vertex& aVertex,
164                                                  const gp_Pnt& ProjPoint, 
165                                                  const Quantity_NameOfColor aColor,
166                                                  const Standard_Real width,
167                                                  const Aspect_TypeOfMarker aProjTOM,
168                                                  const Aspect_TypeOfLine aCallTOL) const 
169 {
170   if (!myDrawer->HasOwnPointAspect()){
171     myDrawer->SetPointAspect(new Prs3d_PointAspect(aProjTOM, aColor,1));}
172   else {
173     const Handle(Prs3d_PointAspect)& pa = myDrawer->PointAspect();
174     pa->SetColor(aColor);
175     pa->SetTypeOfMarker(aProjTOM);
176   }
177   
178   {
179     Handle(Graphic3d_Group) aGroup = aPrs->NewGroup();
180     Handle(Graphic3d_ArrayOfPoints) anArrayOfPoints = new Graphic3d_ArrayOfPoints (1);
181     anArrayOfPoints->AddVertex (ProjPoint);
182     aGroup->SetGroupPrimitivesAspect (myDrawer->PointAspect()->Aspect());
183     aGroup->AddPrimitiveArray (anArrayOfPoints);
184   }
185
186   if (!myDrawer->HasOwnWireAspect()){
187     myDrawer->SetWireAspect(new Prs3d_LineAspect(aColor,aCallTOL,2.));}
188   else {
189     const Handle(Prs3d_LineAspect)& li = myDrawer->WireAspect();
190     li->SetColor(aColor);
191     li->SetTypeOfLine(aCallTOL);
192     li->SetWidth(width);
193   }
194   
195   // Si les points ne sont pas confondus...
196   if (!ProjPoint.IsEqual (BRep_Tool::Pnt(aVertex),Precision::Confusion()))
197   {
198     Handle(Graphic3d_Group) aGroup = aPrs->NewGroup();
199     Handle(Graphic3d_ArrayOfSegments) anArrayOfLines = new Graphic3d_ArrayOfSegments (2);
200     anArrayOfLines->AddVertex (ProjPoint);
201     anArrayOfLines->AddVertex (BRep_Tool::Pnt(aVertex));
202     aGroup->SetGroupPrimitivesAspect (myDrawer->WireAspect()->Aspect());
203     aGroup->AddPrimitiveArray (anArrayOfLines);
204   }
205 }
206
207 //=======================================================================
208 //function : SetColor
209 //purpose  : 
210 //=======================================================================
211 void PrsDim_Relation::SetColor(const Quantity_Color &aCol)
212 {
213   if(hasOwnColor && myDrawer->Color() == aCol) return;
214
215   if (!myDrawer->HasOwnTextAspect()) myDrawer->SetTextAspect(new Prs3d_TextAspect());
216   hasOwnColor=Standard_True;
217   myDrawer->SetColor (aCol);
218   myDrawer->TextAspect()->SetColor(aCol);
219
220   Standard_Real WW = HasWidth()? Width(): myDrawer->HasLink() ?
221     AIS_GraphicTool::GetLineWidth(myDrawer->Link(),AIS_TOA_Line) : 1.;
222   if (!myDrawer->HasOwnLineAspect()) {
223     myDrawer->SetLineAspect(new Prs3d_LineAspect(aCol,Aspect_TOL_SOLID,WW));
224   }
225   if (!myDrawer->HasOwnDimensionAspect()) {
226      myDrawer->SetDimensionAspect(new Prs3d_DimensionAspect);
227   }
228
229   myDrawer->LineAspect()->SetColor(aCol);  
230   const Handle(Prs3d_DimensionAspect)& DIMENSION = myDrawer->DimensionAspect();
231   const Handle(Prs3d_LineAspect)&   LINE   = myDrawer->LineAspect();
232   const Handle(Prs3d_TextAspect)&   TEXT   = myDrawer->TextAspect();
233
234   DIMENSION->SetLineAspect(LINE);
235   DIMENSION->SetTextAspect(TEXT); 
236 }
237
238 //=======================================================================
239 //function : UnsetColor
240 //purpose  : 
241 //=======================================================================
242 void PrsDim_Relation::UnsetColor()
243 {
244   if (!hasOwnColor) return;
245   hasOwnColor = Standard_False;
246   const Handle(Prs3d_LineAspect)& LA = myDrawer->LineAspect();
247   Quantity_Color CC = Quantity_NOC_YELLOW;
248   if (myDrawer->HasLink())
249   {
250     AIS_GraphicTool::GetLineColor(myDrawer->Link(),AIS_TOA_Line,CC);
251     myDrawer->SetTextAspect(myDrawer->Link()->TextAspect());
252   }
253   LA->SetColor(CC);
254   myDrawer->DimensionAspect()->SetLineAspect(LA);
255 }