23
23
24
24
#include " PreCompiled.h"
25
25
26
- #include " FemConstraintTransform.h "
26
+ #include < Mod/Part/App/PartFeature.h >
27
27
28
+ #include " FemConstraintTransform.h"
29
+ #include " FemTools.h"
28
30
29
31
using namespace Fem ;
30
32
@@ -34,9 +36,11 @@ static const char* TransformTypes[] = {"Cylindrical", "Rectangular", nullptr};
34
36
35
37
ConstraintTransform::ConstraintTransform ()
36
38
{
37
- ADD_PROPERTY (X_rot, (0.0 ));
38
- ADD_PROPERTY (Y_rot, (0.0 ));
39
- ADD_PROPERTY (Z_rot, (0.0 ));
39
+ ADD_PROPERTY_TYPE (Rotation,
40
+ (Base::Rotation (0.0 , 0.0 , 0.0 , 1.0 )),
41
+ " ConstraintTransform" ,
42
+ App::Prop_Output,
43
+ " Rectangular system transform" );
40
44
ADD_PROPERTY_TYPE (TransformType,
41
45
(1 ),
42
46
" ConstraintTransform" ,
@@ -78,54 +82,141 @@ const char* ConstraintTransform::getViewProviderName() const
78
82
return " FemGui::ViewProviderFemConstraintTransform" ;
79
83
}
80
84
81
- void ConstraintTransform::handleChangedPropertyType (Base::XMLReader& reader,
82
- const char * TypeName,
83
- App::Property* prop)
85
+ void ConstraintTransform::onChanged (const App::Property* prop)
86
+ {
87
+ if (prop == &References) {
88
+ std::string transform_type = TransformType.getValueAsString ();
89
+ if (transform_type == " Cylindrical" ) {
90
+ // Extract geometry from References
91
+ std::vector<App::DocumentObject*> ref = References.getValues ();
92
+ std::vector<std::string> subRef = References.getSubValues ();
93
+ if (ref.empty ()) {
94
+ return ;
95
+ }
96
+
97
+ Part::Feature* feat = static_cast <Part::Feature*>(ref.front ());
98
+ TopoDS_Shape sh = Tools::getFeatureSubShape (feat, subRef.front ().c_str (), true );
99
+
100
+ Base::Vector3d axis, base;
101
+ double height, radius;
102
+ if (!Tools::getCylinderParams (sh, base, axis, height, radius)) {
103
+ return ;
104
+ }
105
+
106
+ BasePoint.setValue (base);
107
+ Axis.setValue (axis);
108
+ }
109
+ }
110
+
111
+ Constraint::onChanged (prop);
112
+ }
113
+
114
+ namespace
84
115
{
85
- // properties _rot had App::PropertyFloat and were changed to App::PropertyAngle
86
- if (prop == &X_rot && strcmp (TypeName, " App::PropertyFloat" ) == 0 ) {
87
- App::PropertyFloat X_rotProperty;
88
- X_rotProperty.Restore (reader);
89
- X_rot.setValue (X_rotProperty.getValue ());
116
+
117
+ Base::Rotation anglesToRotation (double xAngle, double yAngle, double zAngle)
118
+ {
119
+ static Base::Vector3d a (1 , 0 , 0 );
120
+ static Base::Vector3d b (0 , 1 , 0 );
121
+ static int count = 0 ;
122
+ double xRad = xAngle * D_PI / 180.0 ;
123
+ double yRad = yAngle * D_PI / 180.0 ;
124
+ double zRad = zAngle * D_PI / 180.0 ;
125
+ if (xAngle != 0 ) {
126
+ a[1 ] = 0 ;
127
+ a[2 ] = 0 ;
128
+ b[1 ] = std::cos (xRad);
129
+ b[2 ] = -std::sin (xRad);
90
130
}
91
- else if (prop == &Y_rot && strcmp (TypeName, " App::PropertyFloat" ) == 0 ) {
92
- App::PropertyFloat Y_rotProperty;
93
- Y_rotProperty.Restore (reader);
94
- Y_rot.setValue (Y_rotProperty.getValue ());
131
+ if (yAngle != 0 ) {
132
+ a[0 ] = std::cos (yRad);
133
+ a[2 ] = std::sin (yRad);
134
+ b[0 ] = 0 ;
135
+ b[2 ] = 0 ;
95
136
}
96
- else if (prop == &Z_rot && strcmp (TypeName, " App::PropertyFloat" ) == 0 ) {
97
- App::PropertyFloat Z_rotProperty;
98
- Z_rotProperty.Restore (reader);
99
- Z_rot.setValue (Z_rotProperty.getValue ());
137
+ if (zAngle != 0 ) {
138
+ a[0 ] = std::cos (zRad);
139
+ a[1 ] = -std::sin (zRad);
140
+ b[0 ] = 0 ;
141
+ b[1 ] = 0 ;
100
142
}
101
- else {
102
- Constraint::handleChangedPropertyType (reader, TypeName, prop);
143
+
144
+ ++count;
145
+ count %= 3 ;
146
+ if (!count) {
147
+ Base::Vector3d X = a.Normalize ();
148
+ Base::Vector3d Y = b.Normalize ();
149
+ Base::Vector3d Z = X.Cross (Y);
150
+ Z.Normalize ();
151
+ Y = Z.Cross (X);
152
+
153
+ a.x = 1 ;
154
+ a.y = 0 ;
155
+ a.z = 0 ;
156
+ b.x = 0 ;
157
+ b.y = 1 ;
158
+ b.z = 0 ;
159
+
160
+ Base::Matrix4D m;
161
+ m.setCol (0 , X);
162
+ m.setCol (1 , Y);
163
+ m.setCol (2 , Z);
164
+
165
+ return Base::Rotation (m);
103
166
}
167
+ return Base::Rotation ();
104
168
}
105
169
106
- void ConstraintTransform::onChanged (const App::Property* prop)
107
- {
108
- Constraint::onChanged (prop);
170
+ } // namespace
109
171
110
- if (prop == &References) {
111
- std::vector<Base::Vector3d> points;
112
- std::vector<Base::Vector3d> normals;
113
- double scale = 1 ; // OvG: Enforce use of scale
114
- if (getPoints (points, normals, &scale)) {
115
- std::string transform_type = TransformType.getValueAsString ();
116
- if (transform_type == " Cylindrical" ) {
117
- // Find data of cylinder
118
- double radius, height;
119
- Base::Vector3d base, axis;
120
- if (!getCylinder (radius, height, base, axis)) {
121
- return ;
122
- }
123
- Axis.setValue (axis);
124
- // Update base point
125
- base = base + axis * height / 2 ;
126
- BasePoint.setValue (base);
127
- BasePoint.touch (); // This triggers ViewProvider::updateData()
128
- }
172
+
173
+ void ConstraintTransform::handleChangedPropertyName (Base::XMLReader& reader,
174
+ const char * typeName,
175
+ const char * propName)
176
+ {
177
+ if (strcmp (propName, " X_rot" ) == 0 ) {
178
+ double xAngle;
179
+ if (strcmp (typeName, " App::PropertyFloat" ) == 0 ) {
180
+ App::PropertyFloat X_rotProperty;
181
+ X_rotProperty.Restore (reader);
182
+ xAngle = X_rotProperty.getValue ();
183
+ }
184
+ else if (strcmp (typeName, " App::PropertyAngle" ) == 0 ) {
185
+ App::PropertyAngle X_rotProperty;
186
+ X_rotProperty.Restore (reader);
187
+ xAngle = X_rotProperty.getValue ();
188
+ }
189
+ anglesToRotation (xAngle, 0 , 0 );
190
+ }
191
+ else if (strcmp (propName, " Y_rot" ) == 0 ) {
192
+ double yAngle;
193
+ if (strcmp (typeName, " App::PropertyFloat" ) == 0 ) {
194
+ App::PropertyFloat Y_rotProperty;
195
+ Y_rotProperty.Restore (reader);
196
+ yAngle = Y_rotProperty.getValue ();
197
+ }
198
+ else if (strcmp (typeName, " App::PropertyAngle" ) == 0 ) {
199
+ App::PropertyAngle Y_rotProperty;
200
+ Y_rotProperty.Restore (reader);
201
+ yAngle = Y_rotProperty.getValue ();
129
202
}
203
+ anglesToRotation (0 , yAngle, 0 );
204
+ }
205
+ else if (strcmp (propName, " Z_rot" ) == 0 ) {
206
+ double zAngle;
207
+ if (strcmp (typeName, " App::PropertyFloat" ) == 0 ) {
208
+ App::PropertyFloat Z_rotProperty;
209
+ Z_rotProperty.Restore (reader);
210
+ zAngle = Z_rotProperty.getValue ();
211
+ }
212
+ else if (strcmp (typeName, " App::PropertyAngle" ) == 0 ) {
213
+ App::PropertyAngle Z_rotProperty;
214
+ Z_rotProperty.Restore (reader);
215
+ zAngle = Z_rotProperty.getValue ();
216
+ }
217
+ Rotation.setValue (anglesToRotation (0 , 0 , zAngle));
218
+ }
219
+ else {
220
+ Constraint::handleChangedPropertyName (reader, typeName, propName);
130
221
}
131
222
}
0 commit comments