Salome HOME
Optimization of SketchPrs objects parameters
[modules/shaper.git] / src / SketcherPrs / SketcherPrs_Perpendicular.cpp
1 // Copyright (C) 2014-2017  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #include "SketcherPrs_Perpendicular.h"
22 #include "SketcherPrs_Tools.h"
23 #include "SketcherPrs_PositionMgr.h"
24
25 #include <SketchPlugin_Constraint.h>
26
27 #include <Graphic3d_AspectLine3d.hxx>
28 #include <Prs3d_Root.hxx>
29
30 #include <GeomAPI_Curve.h>
31 #include <GeomAPI_Edge.h>
32 #include <GeomAPI_Lin.h>
33
34
35 IMPLEMENT_STANDARD_RTTIEXT(SketcherPrs_Perpendicular, SketcherPrs_SymbolPrs);
36
37 static Handle(Image_AlienPixMap) MyPixMap;
38
39 SketcherPrs_Perpendicular::SketcherPrs_Perpendicular(ModelAPI_Feature* theConstraint,
40   SketchPlugin_Sketch* theSketcher)
41  : SketcherPrs_SymbolPrs(theConstraint, theSketcher)
42 {
43 }
44
45 bool SketcherPrs_Perpendicular::IsReadyToDisplay(ModelAPI_Feature* theConstraint,
46                                                  const std::shared_ptr<GeomAPI_Ax3>&/* thePlane*/)
47 {
48   bool aReadyToDisplay = false;
49
50   ObjectPtr aObj1 =
51     SketcherPrs_Tools::getResult(theConstraint, SketchPlugin_Constraint::ENTITY_A());
52   ObjectPtr aObj2 =
53     SketcherPrs_Tools::getResult(theConstraint, SketchPlugin_Constraint::ENTITY_B());
54
55   aReadyToDisplay = SketcherPrs_Tools::getShape(aObj1).get() != NULL &&
56                     SketcherPrs_Tools::getShape(aObj2).get() != NULL;
57   return aReadyToDisplay;
58 }
59
60 bool SketcherPrs_Perpendicular::updateIfReadyToDisplay(double theStep, bool withColor) const
61 {
62   if (!IsReadyToDisplay(myConstraint, myPlane))
63     return false;
64
65   ObjectPtr aObj1 =
66     SketcherPrs_Tools::getResult(myConstraint, SketchPlugin_Constraint::ENTITY_A());
67   ObjectPtr aObj2 =
68     SketcherPrs_Tools::getResult(myConstraint, SketchPlugin_Constraint::ENTITY_B());
69
70   GeomShapePtr aShp1 = SketcherPrs_Tools::getShape(aObj1);
71   GeomShapePtr aShp2 = SketcherPrs_Tools::getShape(aObj2);
72
73   GeomEdgePtr aEdge1(new GeomAPI_Edge(aShp1));
74   GeomEdgePtr aEdge2(new GeomAPI_Edge(aShp2));
75
76   std::shared_ptr<GeomAPI_Lin> aLin1 = aEdge1->line();
77   std::shared_ptr<GeomAPI_Lin> aLin2 = aEdge2->line();
78
79   std::shared_ptr<GeomAPI_Pnt> aPnt = aLin1->intersect(aLin2);
80   double aParam1 = aLin1->projParam(aPnt);
81   double aParam2 = aLin2->projParam(aPnt);
82
83   GeomAPI_Curve aCurve1(aShp1);
84   GeomAPI_Curve aCurve2(aShp2);
85   bool isInside1 = (aParam1 >= (aCurve1.startParam() - Precision::Confusion())) &&
86     (aParam1 <= (aCurve1.endParam() + Precision::Confusion()));
87   bool isInside2 = (aParam2 >= (aCurve2.startParam() - Precision::Confusion())) &&
88     (aParam2 <= (aCurve2.endParam() + Precision::Confusion()));
89
90   if (!(isInside1 && isInside2))
91     aPnt = std::shared_ptr<GeomAPI_Pnt>();
92
93   // Compute position of symbols
94   SketcherPrs_PositionMgr* aMgr = SketcherPrs_PositionMgr::get();
95   gp_Pnt aP1 = aMgr->getPosition(aObj1, this, theStep, aPnt);
96   myPntArray = new Graphic3d_ArrayOfPoints(aPnt.get()? 1 : 2, withColor);
97   myPntArray->AddVertex(aP1);
98   if (!aPnt.get()) {
99     gp_Pnt aP2 = aMgr->getPosition(aObj2, this, theStep);
100     myPntArray->AddVertex(aP2);
101   }
102   return true;
103 }
104
105
106 void SketcherPrs_Perpendicular::drawLines(const Handle(Prs3d_Presentation)& thePrs,
107                                           Quantity_Color theColor) const
108 {
109   Handle(Graphic3d_Group) aGroup = Prs3d_Root::CurrentGroup(thePrs);
110
111   Handle(Graphic3d_AspectLine3d) aLineAspect =
112     new Graphic3d_AspectLine3d(theColor, Aspect_TOL_SOLID, 2);
113   aGroup->SetPrimitivesAspect(aLineAspect);
114
115   // Draw constrained lines
116   addLine(aGroup, SketchPlugin_Constraint::ENTITY_A());
117   addLine(aGroup, SketchPlugin_Constraint::ENTITY_B());
118 }
119