Modernize code

This commit is contained in:
André Althaus
2024-03-25 11:21:02 +01:00
parent 067c774292
commit a93e94dabb
3 changed files with 91 additions and 112 deletions

View File

@@ -52,10 +52,11 @@ void MultiTransform::positionBySupport()
PartDesign::Transformed::positionBySupport();
std::vector<App::DocumentObject*> transFeatures = Transformations.getValues();
for (auto f : transFeatures) {
if (!(f->isDerivedFrom<PartDesign::Transformed>())) {
auto transFeature = Base::freecad_dynamic_cast<PartDesign::Transformed>(f);
if (!transFeature) {
throw Base::TypeError("Transformation features must be subclasses of Transformed");
}
PartDesign::Transformed* transFeature = static_cast<PartDesign::Transformed*>(f);
transFeature->Placement.setValue(this->Placement.getValue());
// To avoid that a linked transform feature stays touched after a recompute
@@ -95,22 +96,19 @@ MultiTransform::getTransformations(const std::vector<App::DocumentObject*> origi
std::list<gp_Trsf> result;
std::list<gp_Pnt> cogs;
std::vector<App::DocumentObject*>::const_iterator f;
for (f = transFeatures.begin(); f != transFeatures.end(); ++f) {
if (!((*f)->isDerivedFrom<PartDesign::Transformed>())) {
for (auto const& f : transFeatures) {
auto transFeature = Base::freecad_dynamic_cast<PartDesign::Transformed>(f);
if (!transFeature) {
throw Base::TypeError("Transformation features must be subclasses of Transformed");
}
PartDesign::Transformed* transFeature = static_cast<PartDesign::Transformed*>(*f);
std::list<gp_Trsf> newTransformations = transFeature->getTransformations(originals);
std::list<gp_Trsf> newTransformations = transFeature->getTransformations(originals);
if (result.empty()) {
// First transformation Feature
result = newTransformations;
for (std::list<gp_Trsf>::const_iterator nt = newTransformations.begin();
nt != newTransformations.end();
++nt) {
cogs.push_back(cog.Transformed(*nt));
for (auto nt : newTransformations) {
cogs.push_back(cog.Transformed(nt));
}
}
else {
@@ -121,7 +119,7 @@ MultiTransform::getTransformations(const std::vector<App::DocumentObject*> origi
std::list<gp_Pnt> oldCogs;
cogs.swap(oldCogs); // empty cogs to receive new cogs
if ((*f)->is<PartDesign::Scaled>()) {
if (transFeature->is<PartDesign::Scaled>()) {
// Diagonal method
// Multiply every element in the old transformations' slices with the corresponding
// element in the newTransformations. Example:
@@ -141,15 +139,13 @@ MultiTransform::getTransformations(const std::vector<App::DocumentObject*> origi
}
unsigned sliceLength = oldTransformations.size() / newTransformations.size();
std::list<gp_Trsf>::const_iterator ot = oldTransformations.begin();
std::list<gp_Pnt>::const_iterator oc = oldCogs.begin();
auto ot = oldTransformations.begin();
auto oc = oldCogs.begin();
for (std::list<gp_Trsf>::const_iterator nt = newTransformations.begin();
nt != newTransformations.end();
++nt) {
for (auto const& nt : newTransformations) {
for (unsigned s = 0; s < sliceLength; s++) {
gp_Trsf trans;
double factor = nt->ScaleFactor(); // extract scale factor
double factor = nt.ScaleFactor(); // extract scale factor
if (factor > Precision::Confusion()) {
trans.SetScale(*oc, factor); // recreate the scaled transformation to
@@ -158,8 +154,8 @@ MultiTransform::getTransformations(const std::vector<App::DocumentObject*> origi
cogs.push_back(*oc); // Scaling does not affect the COG
}
else {
trans = (*nt) * (*ot);
cogs.push_back(oc->Transformed(*nt));
trans = nt * (*ot);
cogs.push_back(oc->Transformed(nt));
}
result.push_back(trans);
++ot;
@@ -174,16 +170,12 @@ MultiTransform::getTransformations(const std::vector<App::DocumentObject*> origi
// a11 a12 b1 a11*b1 a12*b1 a11*b2 a12*b2 a11*b3 a12*b3
// a21 a22 mul b2 = a21*b1 a22*b1 a21*b2 a22*b2 a21*b3 a22*b3
// b3
for (std::list<gp_Trsf>::const_iterator nt = newTransformations.begin();
nt != newTransformations.end();
++nt) {
std::list<gp_Pnt>::const_iterator oc = oldCogs.begin();
for (auto const& nt : newTransformations) {
auto oc = oldCogs.begin();
for (std::list<gp_Trsf>::const_iterator ot = oldTransformations.begin();
ot != oldTransformations.end();
++ot) {
result.push_back((*nt) * (*ot));
cogs.push_back(oc->Transformed(*nt));
for (auto const& ot : oldTransformations) {
result.push_back(nt * ot);
cogs.push_back(oc->Transformed(nt));
++oc;
}
}

View File

@@ -57,11 +57,11 @@ short Scaled::mustExecute() const
const std::list<gp_Trsf>
Scaled::getTransformations(const std::vector<App::DocumentObject*> originals)
{
double factor = Factor.getValue();
double const factor = Factor.getValue();
if (factor < Precision::Confusion()) {
throw Base::ValueError("Scaling factor too small");
}
int occurrences = Occurrences.getValue();
int const occurrences = Occurrences.getValue();
if (occurrences < 2) {
throw Base::ValueError("At least two occurrences required");
}

View File

@@ -109,10 +109,8 @@ Part::Feature* Transformed::getBaseObject(bool silent) const
// first
App::DocumentObject* firstOriginal = originals.empty() ? nullptr : originals.front();
if (firstOriginal) {
if (firstOriginal->isDerivedFrom(Part::Feature::getClassTypeId())) {
rv = static_cast<Part::Feature*>(firstOriginal);
}
else {
rv = Base::freecad_dynamic_cast<Part::Feature>(firstOriginal);
if (!rv) {
err = QT_TRANSLATE_NOOP("Exception",
"Transformation feature Linked object is not a Part object");
}
@@ -131,30 +129,25 @@ Part::Feature* Transformed::getBaseObject(bool silent) const
App::DocumentObject* Transformed::getSketchObject() const
{
std::vector<DocumentObject*> originals = Originals.getValues();
if (!originals.empty() && originals.front()->isDerivedFrom<PartDesign::ProfileBased>()) {
return (static_cast<PartDesign::ProfileBased*>(originals.front()))->getVerifiedSketch(true);
DocumentObject const* firstOriginal = !originals.empty() ? originals.front() : nullptr;
if (auto feature = Base::freecad_dynamic_cast<PartDesign::ProfileBased>(firstOriginal)) {
return feature->getVerifiedSketch(true);
}
else if (!originals.empty() && originals.front()->isDerivedFrom<PartDesign::FeatureAddSub>()) {
if (Base::freecad_dynamic_cast<PartDesign::FeatureAddSub>(firstOriginal)) {
return nullptr;
}
else if (this->isDerivedFrom<LinearPattern>()) {
// if Originals is empty then try the linear pattern's Direction property
const LinearPattern* pattern = static_cast<const LinearPattern*>(this);
if (auto pattern = Base::freecad_dynamic_cast<LinearPattern>(this)) {
return pattern->Direction.getValue();
}
else if (this->isDerivedFrom<PolarPattern>()) {
// if Originals is empty then try the polar pattern's Axis property
const PolarPattern* pattern = static_cast<const PolarPattern*>(this);
if (auto pattern = Base::freecad_dynamic_cast<PolarPattern>(this)) {
return pattern->Axis.getValue();
}
else if (this->isDerivedFrom<Mirrored>()) {
// if Originals is empty then try the mirror pattern's MirrorPlane property
const Mirrored* pattern = static_cast<const Mirrored*>(this);
if (auto pattern = Base::freecad_dynamic_cast<Mirrored>(this)) {
return pattern->MirrorPlane.getValue();
}
else {
return nullptr;
}
return nullptr;
}
void Transformed::Restore(Base::XMLReader& reader)
@@ -185,13 +178,13 @@ void Transformed::handleChangedPropertyType(Base::XMLReader& reader,
// The property 'Angle' of PolarPattern has changed from PropertyFloat
// to PropertyAngle and the property 'Length' has changed to PropertyLength.
Base::Type inputType = Base::Type::fromName(TypeName);
if (prop->isDerivedFrom<App::PropertyFloat>()
&& inputType.isDerivedFrom(App::PropertyFloat::getClassTypeId())) {
if (auto property = Base::freecad_dynamic_cast<App::PropertyFloat>(prop);
property != nullptr && inputType.isDerivedFrom(App::PropertyFloat::getClassTypeId())) {
// Do not directly call the property's Restore method in case the implementation
// has changed. So, create a temporary PropertyFloat object and assign the value.
App::PropertyFloat floatProp;
floatProp.Restore(reader);
static_cast<App::PropertyFloat*>(prop)->setValue(floatProp.getValue());
property->setValue(floatProp.getValue());
}
else {
PartDesign::Feature::handleChangedPropertyType(reader, TypeName, prop);
@@ -260,7 +253,7 @@ App::DocumentObjectExecReturn* Transformed::execute()
}
// Get the support
Part::Feature* supportFeature;
Part::Feature* supportFeature = nullptr;
try {
supportFeature = getBaseObject();
@@ -287,7 +280,7 @@ App::DocumentObjectExecReturn* Transformed::execute()
TopTools_ListOfShape shapeTools;
std::vector<TopoDS_Shape> shapes;
std::vector<gp_Trsf>::const_iterator transformIter = transformations.begin();
auto transformIter = transformations.cbegin();
// First transformation is skipped since it should not be part of the toolShape.
++transformIter;
@@ -328,55 +321,52 @@ App::DocumentObjectExecReturn* Transformed::execute()
Part::TopoShape fuseShape;
Part::TopoShape cutShape;
if (original->isDerivedFrom<PartDesign::FeatureAddSub>()) {
PartDesign::FeatureAddSub* feature =
static_cast<PartDesign::FeatureAddSub*>(original);
feature->getAddSubShape(fuseShape, cutShape);
if (fuseShape.isNull() && cutShape.isNull()) {
return new App::DocumentObjectExecReturn(
QT_TRANSLATE_NOOP("Exception",
"Shape of additive/subtractive feature is empty"));
}
gp_Trsf trsf = feature->getLocation().Transformation().Multiplied(trsfInv);
#ifdef FC_USE_TNP_FIX
if (!fuseShape.isNull()) {
fuseShape = fuseShape.makeElementTransform(trsf);
}
if (!cutShape.isNull()) {
cutShape = cutShape.makeElementTransform(trsf);
}
#else
if (!fuseShape.isNull()) {
fuseShape = fuseShape.makeTransform(trsf);
}
if (!cutShape.isNull()) {
cutShape = cutShape.makeTransform(trsf);
}
#endif
}
else {
auto feature = Base::freecad_dynamic_cast<PartDesign::FeatureAddSub>(original);
if (!feature) {
return new App::DocumentObjectExecReturn(QT_TRANSLATE_NOOP(
"Exception",
"Only additive and subtractive features can be transformed"));
}
feature->getAddSubShape(fuseShape, cutShape);
if (fuseShape.isNull() && cutShape.isNull()) {
return new App::DocumentObjectExecReturn(
QT_TRANSLATE_NOOP("Exception",
"Shape of additive/subtractive feature is empty"));
}
gp_Trsf trsf = feature->getLocation().Transformation().Multiplied(trsfInv);
#ifdef FC_USE_TNP_FIX
if (!fuseShape.isNull()) {
fuseShape = fuseShape.makeElementTransform(trsf);
}
if (!cutShape.isNull()) {
cutShape = cutShape.makeElementTransform(trsf);
}
#else
if (!fuseShape.isNull()) {
fuseShape = fuseShape.makeTransform(trsf);
}
if (!cutShape.isNull()) {
cutShape = cutShape.makeTransform(trsf);
}
#endif
TopoDS_Shape current = support;
if (!fuseShape.isNull()) {
TopTools_ListOfShape shapeArguments;
shapeArguments.Append(current);
TopTools_ListOfShape shapeTools = getTransformedCompShape(fuseShape.getShape());
if (!shapeTools.IsEmpty()) {
std::unique_ptr<BRepAlgoAPI_BooleanOperation> mkBool(
new BRepAlgoAPI_Fuse());
mkBool->SetArguments(shapeArguments);
mkBool->SetTools(shapeTools);
mkBool->Build();
if (!mkBool->IsDone()) {
BRepAlgoAPI_Fuse mkBool;
mkBool.SetArguments(shapeArguments);
mkBool.SetTools(shapeTools);
mkBool.Build();
if (!mkBool.IsDone()) {
return new App::DocumentObjectExecReturn(
QT_TRANSLATE_NOOP("Exception", "Boolean operation failed"));
}
current = mkBool->Shape();
current = mkBool.Shape();
}
}
if (!cutShape.isNull()) {
@@ -384,15 +374,15 @@ App::DocumentObjectExecReturn* Transformed::execute()
shapeArguments.Append(current);
TopTools_ListOfShape shapeTools = getTransformedCompShape(cutShape.getShape());
if (!shapeTools.IsEmpty()) {
std::unique_ptr<BRepAlgoAPI_BooleanOperation> mkBool(new BRepAlgoAPI_Cut());
mkBool->SetArguments(shapeArguments);
mkBool->SetTools(shapeTools);
mkBool->Build();
if (!mkBool->IsDone()) {
BRepAlgoAPI_Cut mkBool;
mkBool.SetArguments(shapeArguments);
mkBool.SetTools(shapeTools);
mkBool.Build();
if (!mkBool.IsDone()) {
return new App::DocumentObjectExecReturn(
QT_TRANSLATE_NOOP("Exception", "Boolean operation failed"));
}
current = mkBool->Shape();
current = mkBool.Shape();
}
}
@@ -404,15 +394,15 @@ App::DocumentObjectExecReturn* Transformed::execute()
shapeArguments.Append(support);
TopTools_ListOfShape shapeTools = getTransformedCompShape(support);
if (!shapeTools.IsEmpty()) {
std::unique_ptr<BRepAlgoAPI_BooleanOperation> mkBool(new BRepAlgoAPI_Fuse());
mkBool->SetArguments(shapeArguments);
mkBool->SetTools(shapeTools);
mkBool->Build();
if (!mkBool->IsDone()) {
BRepAlgoAPI_Fuse mkBool;
mkBool.SetArguments(shapeArguments);
mkBool.SetTools(shapeTools);
mkBool.Build();
if (!mkBool.IsDone()) {
return new App::DocumentObjectExecReturn(
QT_TRANSLATE_NOOP("Exception", "Boolean operation failed"));
}
support = mkBool->Shape();
support = mkBool.Shape();
}
break;
}
@@ -449,9 +439,9 @@ TopoDS_Shape Transformed::refineShapeIfActive(const TopoDS_Shape& oldShape) cons
return oldShape;
}
void Transformed::divideTools(const std::vector<TopoDS_Shape>& toolsIn,
void divideTools(const std::vector<TopoDS_Shape>& toolsIn,
std::vector<TopoDS_Shape>& individualsOut,
TopoDS_Compound& compoundOut) const
TopoDS_Compound& compoundOut)
{
using ShapeBoundPair = std::pair<TopoDS_Shape, Bnd_Box>;
using PairList = std::list<ShapeBoundPair>;
@@ -459,8 +449,7 @@ void Transformed::divideTools(const std::vector<TopoDS_Shape>& toolsIn,
PairList pairList;
std::vector<TopoDS_Shape>::const_iterator it;
for (it = toolsIn.begin(); it != toolsIn.end(); ++it) {
for (auto it = toolsIn.begin(); it != toolsIn.end(); ++it) {
Bnd_Box bound;
BRepBndLib::Add(*it, bound);
bound.SetGap(0.0);
@@ -475,11 +464,10 @@ void Transformed::divideTools(const std::vector<TopoDS_Shape>& toolsIn,
PairVector currentGroup;
currentGroup.push_back(pairList.front());
pairList.pop_front();
PairList::iterator it = pairList.begin();
auto it = pairList.begin();
while (it != pairList.end()) {
PairVector::const_iterator groupIt;
bool found(false);
for (groupIt = currentGroup.begin(); groupIt != currentGroup.end(); ++groupIt) {
for (auto groupIt = currentGroup.begin(); groupIt != currentGroup.end(); ++groupIt) {
if (!(*it).second.IsOut((*groupIt).second)) { // touching means is out.
found = true;
break;
@@ -498,8 +486,7 @@ void Transformed::divideTools(const std::vector<TopoDS_Shape>& toolsIn,
builder.Add(compoundOut, currentGroup.front().first);
}
else {
PairVector::const_iterator groupIt;
for (groupIt = currentGroup.begin(); groupIt != currentGroup.end(); ++groupIt) {
for (auto groupIt = currentGroup.cbegin(); groupIt != currentGroup.end(); ++groupIt) {
individualsOut.push_back((*groupIt).first);
}
}
@@ -523,7 +510,7 @@ TopoDS_Shape Transformed::getRemainingSolids(const TopoDS_Shape& shape)
builder.Add(compShape, xp.Current());
}
return TopoDS_Shape(std::move(compShape));
return {std::move(compShape)};
}
} // namespace PartDesign