Skip to content

Commit c50078d

Browse files
committed
Fem: Update constraint transform
1 parent 964cfd1 commit c50078d

16 files changed

+525
-488
lines changed

src/Mod/Fem/App/FemConstraintTransform.cpp

+135-44
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,10 @@
2323

2424
#include "PreCompiled.h"
2525

26-
#include "FemConstraintTransform.h"
26+
#include <Mod/Part/App/PartFeature.h>
2727

28+
#include "FemConstraintTransform.h"
29+
#include "FemTools.h"
2830

2931
using namespace Fem;
3032

@@ -34,9 +36,11 @@ static const char* TransformTypes[] = {"Cylindrical", "Rectangular", nullptr};
3436

3537
ConstraintTransform::ConstraintTransform()
3638
{
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");
4044
ADD_PROPERTY_TYPE(TransformType,
4145
(1),
4246
"ConstraintTransform",
@@ -78,54 +82,141 @@ const char* ConstraintTransform::getViewProviderName() const
7882
return "FemGui::ViewProviderFemConstraintTransform";
7983
}
8084

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
84115
{
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);
90130
}
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;
95136
}
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;
100142
}
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);
103166
}
167+
return Base::Rotation();
104168
}
105169

106-
void ConstraintTransform::onChanged(const App::Property* prop)
107-
{
108-
Constraint::onChanged(prop);
170+
} // namespace
109171

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();
129202
}
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);
130221
}
131222
}

src/Mod/Fem/App/FemConstraintTransform.h

+5-8
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,9 @@ class FemExport ConstraintTransform: public Fem::Constraint
4242
App::PropertyLinkList NameDispl;
4343
App::PropertyVector BasePoint;
4444
App::PropertyVector Axis;
45-
App::PropertyAngle X_rot;
46-
App::PropertyAngle Y_rot;
47-
App::PropertyAngle Z_rot;
45+
46+
App::PropertyRotation Rotation;
4847
App::PropertyEnumeration TransformType;
49-
// etc
50-
/* */
5148

5249
/// recalculate the object
5350
App::DocumentObjectExecReturn* execute() override;
@@ -56,9 +53,9 @@ class FemExport ConstraintTransform: public Fem::Constraint
5653
const char* getViewProviderName() const override;
5754

5855
protected:
59-
void handleChangedPropertyType(Base::XMLReader& reader,
60-
const char* TypeName,
61-
App::Property* prop) override;
56+
void handleChangedPropertyName(Base::XMLReader& reader,
57+
const char* typeName,
58+
const char* propName) override;
6259
void onChanged(const App::Property* prop) override;
6360
};
6461

src/Mod/Fem/Gui/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,7 @@ SET(FemGuiSymbol_IV
366366
Resources/symbols/ConstraintSectionPrint.iv
367367
Resources/symbols/ConstraintSpring.iv
368368
Resources/symbols/ConstraintTemperature.iv
369+
Resources/symbols/ConstraintTransform.iv
369370
Resources/symbols/ConstraintTie.iv
370371
)
371372

src/Mod/Fem/Gui/Command.cpp

-3
Original file line numberDiff line numberDiff line change
@@ -1002,9 +1002,6 @@ void CmdFemConstraintTransform::activated(int)
10021002
doCommand(Doc,
10031003
"App.activeDocument().addObject(\"Fem::ConstraintTransform\",\"%s\")",
10041004
FeatName.c_str());
1005-
doCommand(Doc, "App.activeDocument().%s.X_rot = 0.0", FeatName.c_str());
1006-
doCommand(Doc, "App.activeDocument().%s.Y_rot = 0.0", FeatName.c_str());
1007-
doCommand(Doc, "App.activeDocument().%s.Z_rot = 0.0", FeatName.c_str());
10081005
doCommand(Doc, "App.activeDocument().%s.Scale = 1", FeatName.c_str());
10091006
doCommand(Doc,
10101007
"App.activeDocument().%s.addObject(App.activeDocument().%s)",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
#Inventor V2.1 ascii
2+
3+
4+
Separator {
5+
6+
Separator {
7+
DEF REC_TRANSFORM Switch {
8+
Separator {
9+
10+
BaseColor {
11+
rgb 0.0 1.0 0.0
12+
13+
}
14+
DEF AXIS Separator {
15+
16+
Translation {
17+
translation 0 2.5 0
18+
19+
}
20+
Cone {
21+
bottomRadius 0.4
22+
height 1.0
23+
24+
}
25+
Translation {
26+
translation 0 -1.5 0
27+
28+
}
29+
Cylinder {
30+
radius 0.15
31+
height 2.0
32+
33+
}
34+
}
35+
Separator {
36+
37+
BaseColor {
38+
rgb 1.0 0.0 0.0
39+
40+
}
41+
Rotation {
42+
rotation 0 0 1 -1.5707964
43+
44+
}
45+
USE AXIS
46+
}
47+
48+
Separator {
49+
50+
BaseColor {
51+
rgb 0.0 0.0 1.0
52+
53+
}
54+
Rotation {
55+
rotation 1 0 0 1.5707964
56+
57+
}
58+
USE AXIS
59+
}
60+
}
61+
Separator {
62+
63+
BaseColor {
64+
rgb 1.0 0.0 0.0
65+
66+
}
67+
Translation {
68+
translation 0 2.5 0
69+
70+
}
71+
Cone {
72+
bottomRadius 0.4
73+
height 1.0
74+
75+
}
76+
Translation {
77+
translation 0 -1.5 0
78+
79+
}
80+
Cylinder {
81+
radius 0.15
82+
height 2.0
83+
84+
}
85+
}
86+
87+
}
88+
}
89+
Separator {
90+
91+
Switch {
92+
93+
Separator {
94+
95+
BaseColor {
96+
rgb 0.0 0.0 1.0
97+
98+
}
99+
Translation {
100+
translation 0 12.0 0
101+
102+
}
103+
Cone {
104+
bottomRadius 0.4
105+
height 1.0
106+
107+
}
108+
Translation {
109+
translation 0 -12.5 0
110+
111+
}
112+
Cylinder {
113+
radius 0.15
114+
height 24.0
115+
116+
}
117+
}
118+
}
119+
}
120+
}

0 commit comments

Comments
 (0)