PD: move duplicated code to common base class

This commit is contained in:
wmayer
2021-11-20 11:45:13 +01:00
parent 9ee60fd585
commit ab480df745
6 changed files with 66 additions and 163 deletions

View File

@@ -78,3 +78,54 @@ short FeatureExtrude::mustExecute() const
return 1;
return ProfileBased::mustExecute();
}
Base::Vector3d FeatureExtrude::computeDirection(const Base::Vector3d& sketchVector)
{
Base::Vector3d extrudeDirection;
if (!UseCustomVector.getValue()) {
if (!ReferenceAxis.getValue()) {
// use sketch's normal vector for direction
extrudeDirection = sketchVector;
AlongSketchNormal.setReadOnly(true);
}
else {
// update Direction from ReferenceAxis
App::DocumentObject* pcReferenceAxis = ReferenceAxis.getValue();
const std::vector<std::string>& subReferenceAxis = ReferenceAxis.getSubValues();
Base::Vector3d base;
Base::Vector3d dir;
getAxis(pcReferenceAxis, subReferenceAxis, base, dir, false);
switch (addSubType) {
case Type::Additive:
extrudeDirection = dir;
break;
case Type::Subtractive:
extrudeDirection = -dir;
break;
}
}
}
else {
// use the given vector
// if null vector, use sketchVector
if ((fabs(Direction.getValue().x) < Precision::Confusion())
&& (fabs(Direction.getValue().y) < Precision::Confusion())
&& (fabs(Direction.getValue().z) < Precision::Confusion())) {
Direction.setValue(sketchVector);
}
extrudeDirection = Direction.getValue();
}
// disable options of UseCustomVector
Direction.setReadOnly(!UseCustomVector.getValue());
ReferenceAxis.setReadOnly(UseCustomVector.getValue());
// UseCustomVector allows AlongSketchNormal but !UseCustomVector does not forbid it
if (UseCustomVector.getValue())
AlongSketchNormal.setReadOnly(false);
// explicitly set the Direction so that the dialog shows also the used direction
// if the sketch's normal vector was used
Direction.setValue(extrudeDirection);
return extrudeDirection;
}

View File

@@ -53,6 +53,9 @@ public:
//@{
short mustExecute() const;
//@}
protected:
Base::Vector3d computeDirection(const Base::Vector3d& sketchVector);
};
} //namespace PartDesign

View File

@@ -58,7 +58,7 @@ using namespace PartDesign;
const char* Pad::TypeEnums[]= {"Length", "UpToLast", "UpToFirst", "UpToFace", "TwoLengths", NULL};
PROPERTY_SOURCE(PartDesign::Pad, PartDesign::ProfileBased)
PROPERTY_SOURCE(PartDesign::Pad, PartDesign::FeatureExtrude)
Pad::Pad()
{
@@ -82,23 +82,7 @@ Pad::Pad()
Length2.setConstraints(nullptr);
}
short Pad::mustExecute() const
{
if (Placement.isTouched() ||
Type.isTouched() ||
Length.isTouched() ||
Length2.isTouched() ||
UseCustomVector.isTouched() ||
Direction.isTouched() ||
ReferenceAxis.isTouched() ||
AlongSketchNormal.isTouched() ||
Offset.isTouched() ||
UpToFace.isTouched())
return 1;
return ProfileBased::mustExecute();
}
App::DocumentObjectExecReturn *Pad::execute(void)
App::DocumentObjectExecReturn *Pad::execute()
{
// Validate parameters
double L = Length.getValue();
@@ -133,8 +117,6 @@ App::DocumentObjectExecReturn *Pad::execute(void)
base = TopoDS_Shape();
}
// get the Sketch plane
Base::Placement SketchPos = obj->Placement.getValue();
// get the normal vector of the sketch
Base::Vector3d SketchVector = getProfileNormal();
@@ -144,53 +126,11 @@ App::DocumentObjectExecReturn *Pad::execute(void)
base.Move(invObjLoc);
Base::Vector3d paddingDirection;
if (!UseCustomVector.getValue()) {
if (!ReferenceAxis.getValue()) {
// use sketch's normal vector for direction
paddingDirection = SketchVector;
AlongSketchNormal.setReadOnly(true);
}
else {
// update Direction from ReferenceAxis
try {
App::DocumentObject* pcReferenceAxis = ReferenceAxis.getValue();
const std::vector<std::string>& subReferenceAxis = ReferenceAxis.getSubValues();
Base::Vector3d base;
Base::Vector3d dir;
getAxis(pcReferenceAxis, subReferenceAxis, base, dir, false);
paddingDirection = dir;
}
catch (const Base::Exception& e) {
return new App::DocumentObjectExecReturn(e.what());
}
}
}
else {
// use the given vector
// if null vector, use SketchVector
if ( (fabs(Direction.getValue().x) < Precision::Confusion())
&& (fabs(Direction.getValue().y) < Precision::Confusion())
&& (fabs(Direction.getValue().z) < Precision::Confusion()) ) {
Direction.setValue(SketchVector);
}
paddingDirection = Direction.getValue();
}
// disable options of UseCustomVector
Direction.setReadOnly(!UseCustomVector.getValue());
ReferenceAxis.setReadOnly(UseCustomVector.getValue());
// UseCustomVector allows AlongSketchNormal but !UseCustomVector does not forbid it
if (UseCustomVector.getValue())
AlongSketchNormal.setReadOnly(false);
Base::Vector3d paddingDirection = computeDirection(SketchVector);
// create vector in padding direction with length 1
gp_Dir dir(paddingDirection.x, paddingDirection.y, paddingDirection.z);
// store the finally used direction to display it in the dialog
Direction.setValue(dir.X(), dir.Y(), dir.Z());
// The length of a gp_Dir is 1 so the resulting pad would have
// the length L in the direction of dir. But we want to have its height in the
// direction of the normal vector.
@@ -212,10 +152,6 @@ App::DocumentObjectExecReturn *Pad::execute(void)
L2 = L2 / factor;
}
// explicitly set the Direction so that the dialog shows also the used direction
// if the sketch's normal vector was used
Direction.setValue(paddingDirection);
dir.Transform(invObjLoc.Transformation());
if (sketchshape.IsNull())
@@ -394,7 +330,6 @@ App::DocumentObjectExecReturn *Pad::execute(void)
return App::DocumentObject::StdReturn;
}
catch (Standard_Failure& e) {
if (std::string(e.GetMessageString()) == "TopoDS::Face")
return new App::DocumentObjectExecReturn("Could not create face from sketch.\n"
"Intersecting sketch entities or multiple faces in a sketch are not allowed.");

View File

@@ -29,22 +29,13 @@
namespace PartDesign
{
class PartDesignExport Pad : public ProfileBased
class PartDesignExport Pad : public FeatureExtrude
{
PROPERTY_HEADER(PartDesign::Pad);
public:
Pad();
App::PropertyEnumeration Type;
App::PropertyLength Length;
App::PropertyLength Length2;
App::PropertyBool UseCustomVector;
App::PropertyVector Direction;
App::PropertyBool AlongSketchNormal;
App::PropertyLength Offset;
App::PropertyLinkSub ReferenceAxis;
/** @name methods override feature */
//@{
/** Recalculate the feature
@@ -60,17 +51,15 @@ public:
* If Reversed is true then the direction of revolution will be reversed.
* The created material will be fused with the sketch support (if there is one)
*/
App::DocumentObjectExecReturn *execute(void);
short mustExecute() const;
App::DocumentObjectExecReturn *execute();
/// returns the type name of the view provider
const char* getViewProviderName(void) const {
const char* getViewProviderName() const {
return "PartDesignGui::ViewProviderPad";
}
//@}
private:
static const char* TypeEnums[];
//static const char* SideEnums[];
};
} //namespace PartDesign

View File

@@ -57,7 +57,7 @@ using namespace PartDesign;
const char* Pocket::TypeEnums[]= {"Length","ThroughAll","UpToFirst","UpToFace","TwoLengths",NULL};
PROPERTY_SOURCE(PartDesign::Pocket, PartDesign::ProfileBased)
PROPERTY_SOURCE(PartDesign::Pocket, PartDesign::FeatureExtrude)
Pocket::Pocket()
{
@@ -81,23 +81,7 @@ Pocket::Pocket()
Length2.setConstraints(nullptr);
}
short Pocket::mustExecute() const
{
if (Placement.isTouched() ||
Type.isTouched() ||
Length.isTouched() ||
Length2.isTouched() ||
Offset.isTouched() ||
UseCustomVector.isTouched() ||
Direction.isTouched() ||
ReferenceAxis.isTouched() ||
AlongSketchNormal.isTouched() ||
UpToFace.isTouched())
return 1;
return ProfileBased::mustExecute();
}
App::DocumentObjectExecReturn *Pocket::execute(void)
App::DocumentObjectExecReturn *Pocket::execute()
{
// Handle legacy features, these typically have Type set to 3 (previously NULL, now UpToFace),
// empty FaceName (because it didn't exist) and a value for Length
@@ -136,10 +120,8 @@ App::DocumentObjectExecReturn *Pocket::execute(void)
return new App::DocumentObjectExecReturn(text);
}
// get the Sketch plane
Base::Placement SketchPos = obj->Placement.getValue();
// get the normal vector of the sketch
Base::Vector3d SketchVector = getProfileNormal();
Base::Vector3d SketchVector = getProfileNormal();
// turn around for pockets
SketchVector *= -1;
@@ -150,53 +132,11 @@ App::DocumentObjectExecReturn *Pocket::execute(void)
base.Move(invObjLoc);
Base::Vector3d pocketDirection;
if (!UseCustomVector.getValue()) {
if (!ReferenceAxis.getValue()) {
// use sketch's normal vector for direction
pocketDirection = SketchVector;
AlongSketchNormal.setReadOnly(true);
}
else {
// update Direction from ReferenceAxis
try {
App::DocumentObject* pcReferenceAxis = ReferenceAxis.getValue();
const std::vector<std::string>& subReferenceAxis = ReferenceAxis.getSubValues();
Base::Vector3d base;
Base::Vector3d dir;
getAxis(pcReferenceAxis, subReferenceAxis, base, dir, false);
pocketDirection = -dir;
}
catch (const Base::Exception& e) {
return new App::DocumentObjectExecReturn(e.what());
}
}
}
else {
// use the given vector
// if null vector, use SketchVector
if ((fabs(Direction.getValue().x) < Precision::Confusion())
&& (fabs(Direction.getValue().y) < Precision::Confusion())
&& (fabs(Direction.getValue().z) < Precision::Confusion())) {
Direction.setValue(SketchVector);
}
pocketDirection = Direction.getValue();
}
// disable options of UseCustomVector
Direction.setReadOnly(!UseCustomVector.getValue());
ReferenceAxis.setReadOnly(UseCustomVector.getValue());
// UseCustomVector allows AlongSketchNormal but !UseCustomVector does not forbid it
if (UseCustomVector.getValue())
AlongSketchNormal.setReadOnly(false);
Base::Vector3d pocketDirection = computeDirection(SketchVector);
// create vector in pocketing direction with length 1
gp_Dir dir(pocketDirection.x, pocketDirection.y, pocketDirection.z);
// store the finally used direction to display it in the dialog
Direction.setValue(dir.X(), dir.Y(), dir.Z());
// The length of a gp_Dir is 1 so the resulting pocket would have
// the length L in the direction of dir. But we want to have its height in the
// direction of the normal vector.
@@ -218,10 +158,6 @@ App::DocumentObjectExecReturn *Pocket::execute(void)
L2 = L2 / factor;
}
// explicitly set the Direction so that the dialog shows also the used direction
// if the sketch's normal vector was used
Direction.setValue(pocketDirection);
dir.Transform(invObjLoc.Transformation());
if (profileshape.IsNull())

View File

@@ -29,22 +29,13 @@
namespace PartDesign
{
class PartDesignExport Pocket : public ProfileBased
class PartDesignExport Pocket : public FeatureExtrude
{
PROPERTY_HEADER(PartDesign::Pocket);
public:
Pocket();
App::PropertyEnumeration Type;
App::PropertyLength Length;
App::PropertyLength Length2;
App::PropertyLength Offset;
App::PropertyBool UseCustomVector;
App::PropertyVector Direction;
App::PropertyBool AlongSketchNormal;
App::PropertyLinkSub ReferenceAxis;
/** @name methods override feature */
//@{
/** Recalculate the feature
@@ -57,8 +48,7 @@ public:
* If Midplane is true, then the extrusion will extend for half of the length on both sides of the sketch plane
* The created material will be cut out of the sketch support
*/
App::DocumentObjectExecReturn *execute(void);
short mustExecute() const;
App::DocumentObjectExecReturn *execute();
/// returns the type name of the view provider
const char* getViewProviderName(void) const {
return "PartDesignGui::ViewProviderPocket";
@@ -66,7 +56,6 @@ public:
//@}
private:
static const char* TypeEnums[];
};
} //namespace PartDesign