0031682: Visualization - Prs3d_ShadingAspect::SetTransparency() has no effect with...
[occt.git] / src / Expr / Expr_BinaryExpression.cxx
1 // Created on: 1991-04-12
2 // Created by: Arnaud BOUZY
3 // Copyright (c) 1991-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
18 #include <Expr_BinaryExpression.hxx>
19 #include <Expr_GeneralExpression.hxx>
20 #include <Expr_InvalidOperand.hxx>
21 #include <Expr_NamedUnknown.hxx>
22 #include <Standard_NumericError.hxx>
23 #include <Standard_OutOfRange.hxx>
24 #include <Standard_Type.hxx>
25
26 IMPLEMENT_STANDARD_RTTIEXT(Expr_BinaryExpression,Expr_GeneralExpression)
27
28 void Expr_BinaryExpression::SetFirstOperand (const Handle(Expr_GeneralExpression)& exp)
29 {
30   Handle(Expr_BinaryExpression) me;
31   me = this;
32   if (exp == me) {
33     throw Expr_InvalidOperand();
34   }
35   if (exp->Contains(me)) {
36     throw Expr_InvalidOperand();
37   }
38   myFirstOperand = exp;
39 }
40
41 void Expr_BinaryExpression::SetSecondOperand (const Handle(Expr_GeneralExpression)& exp)
42 {
43   Handle(Expr_BinaryExpression) me;
44   me = this;
45   if (exp == me) {
46     throw Expr_InvalidOperand();
47   }
48   if (exp->Contains(me)) {
49     throw Expr_InvalidOperand();
50   }
51   mySecondOperand = exp;
52 }
53
54 void Expr_BinaryExpression::CreateFirstOperand (const Handle(Expr_GeneralExpression)& exp)
55 {
56   myFirstOperand = exp;
57 }
58
59 void Expr_BinaryExpression::CreateSecondOperand (const Handle(Expr_GeneralExpression)& exp)
60 {
61   mySecondOperand = exp;
62 }
63
64 Standard_Integer Expr_BinaryExpression::NbSubExpressions () const
65 {
66   return 2;
67 }
68
69 const Handle(Expr_GeneralExpression)& Expr_BinaryExpression::SubExpression (const Standard_Integer I) const
70 {
71   if (I == 1) {
72     return myFirstOperand;
73   }
74   else {
75     if (I == 2) {
76       return mySecondOperand;
77     }
78     else {
79       throw Standard_OutOfRange();
80     }
81   }
82 }
83
84 Standard_Boolean Expr_BinaryExpression::ContainsUnknowns () const
85 {
86   if (myFirstOperand->IsKind(STANDARD_TYPE(Expr_NamedUnknown))) {
87     return Standard_True;
88   }
89   if (mySecondOperand->IsKind(STANDARD_TYPE(Expr_NamedUnknown))) {
90     return Standard_True;
91   }
92   if (myFirstOperand->ContainsUnknowns()) {
93     return Standard_True;
94   }
95   if (mySecondOperand->ContainsUnknowns()) {
96     return Standard_True;
97   }
98   return Standard_False;
99 }
100
101 Standard_Boolean Expr_BinaryExpression::Contains (const Handle(Expr_GeneralExpression)& exp) const
102 {
103   if (myFirstOperand == exp) {
104     return Standard_True;
105   }
106   if (mySecondOperand == exp) {
107     return Standard_True;
108   }
109   if (myFirstOperand->Contains(exp)) {
110     return Standard_True;
111   }
112   if (mySecondOperand->Contains(exp)) {
113     return Standard_True;
114   }
115   return Standard_False;
116 }
117
118 void Expr_BinaryExpression::Replace (const Handle(Expr_NamedUnknown)& var, const Handle(Expr_GeneralExpression)& with)
119 {
120   if (myFirstOperand == var) {
121     SetFirstOperand(with);
122   }
123   else {
124     if (myFirstOperand->Contains(var)) {
125       myFirstOperand->Replace(var,with);
126     }
127   }
128   if (mySecondOperand == var) {
129     SetSecondOperand(with);
130   }
131   else {
132     if (mySecondOperand->Contains(var)) {
133       mySecondOperand->Replace(var,with);
134     }
135   }
136 }
137
138
139 Handle(Expr_GeneralExpression) Expr_BinaryExpression::Simplified() const
140 {
141   Handle(Expr_BinaryExpression) cop = Handle(Expr_BinaryExpression)::DownCast(Copy());
142   Handle(Expr_GeneralExpression) op1 = cop->FirstOperand();
143   Handle(Expr_GeneralExpression) op2 = cop->SecondOperand();
144   cop->SetFirstOperand(op1->Simplified());
145   cop->SetSecondOperand(op2->Simplified());
146   return cop->ShallowSimplified();
147 }