File size: 10,994 Bytes
985c397 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 | /***************************************************************************
* Copyright (c) 2010 Juergen Riegel <FreeCAD@juergen-riegel.net> *
* *
* This file is part of the FreeCAD CAx development system. *
* *
* This library is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Library General Public *
* License as published by the Free Software Foundation; either *
* version 2 of the License, or (at your option) any later version. *
* *
* This library is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Library General Public License for more details. *
* *
* You should have received a copy of the GNU Library General Public *
* License along with this library; see the file COPYING.LIB. If not, *
* write to the Free Software Foundation, Inc., 59 Temple Place, *
* Suite 330, Boston, MA 02111-1307, USA *
* *
***************************************************************************/
#include <limits>
#include <BRepAlgo.hxx>
#include <BRepFilletAPI_MakeChamfer.hxx>
#include <TopExp.hxx>
#include <TopoDS.hxx>
#include <TopoDS_Edge.hxx>
#include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
#include <TopTools_ListOfShape.hxx>
#include <ShapeFix_Shape.hxx>
#include <ShapeFix_ShapeTolerance.hxx>
#include <Standard_Version.hxx>
#include <Base/Exception.h>
#include <Base/Reader.h>
#include <Base/Tools.h>
#include <Mod/Part/App/TopoShape.h>
#include "FeatureChamfer.h"
#include <Base/ProgramVersion.h>
using namespace PartDesign;
PROPERTY_SOURCE(PartDesign::Chamfer, PartDesign::DressUp)
const char* ChamferTypeEnums[] = {"Equal distance", "Two distances", "Distance and Angle", nullptr};
const App::PropertyQuantityConstraint::Constraints Chamfer::floatSize
= {0.0, std::numeric_limits<float>::max(), 0.1};
const App::PropertyAngle::Constraints Chamfer::floatAngle = {0.0, 180.0, 1.0};
static App::DocumentObjectExecReturn* validateParameters(
int chamferType,
double size,
double size2,
double angle
);
Chamfer::Chamfer()
{
ADD_PROPERTY_TYPE(ChamferType, (0L), "Chamfer", App::Prop_None, "Type of chamfer");
ChamferType.setEnums(ChamferTypeEnums);
ADD_PROPERTY_TYPE(Size, (1.0), "Chamfer", App::Prop_None, "Size of chamfer");
Size.setUnit(Base::Unit::Length);
Size.setConstraints(&floatSize);
ADD_PROPERTY_TYPE(Size2, (1.0), "Chamfer", App::Prop_None, "Second size of chamfer");
Size2.setUnit(Base::Unit::Length);
Size2.setConstraints(&floatSize);
ADD_PROPERTY_TYPE(Angle, (45.0), "Chamfer", App::Prop_None, "Angle of chamfer");
Angle.setUnit(Base::Unit::Angle);
Angle.setConstraints(&floatAngle);
ADD_PROPERTY_TYPE(FlipDirection, (false), "Chamfer", App::Prop_None, "Flip direction");
ADD_PROPERTY_TYPE(
UseAllEdges,
(false),
"Chamfer",
App::Prop_None,
"Chamfer all edges if true, else use only those edges in Base property.\n"
"If true, then this overrides any edge changes made to the Base property or in the "
"dialog.\n"
);
updateProperties();
}
short Chamfer::mustExecute() const
{
bool touched = false;
auto chamferType = ChamferType.getValue();
switch (chamferType) {
case 0: // "Equal distance"
touched = Size.isTouched() || ChamferType.isTouched();
break;
case 1: // "Two distances"
touched = Size.isTouched() || ChamferType.isTouched() || Size2.isTouched();
break;
case 2: // "Distance and Angle"
touched = Size.isTouched() || ChamferType.isTouched() || Angle.isTouched();
break;
}
if (Placement.isTouched() || touched) {
return 1;
}
return DressUp::mustExecute();
}
App::DocumentObjectExecReturn* Chamfer::execute()
{
if (onlyHaveRefined()) {
return App::DocumentObject::StdReturn;
}
// NOTE: Normally the Base property and the BaseFeature property should point to the same object.
// The only difference is that the Base property also stores the edges that are to be chamfered
Part::TopoShape TopShape;
try {
TopShape = getBaseTopoShape();
}
catch (Base::Exception& e) {
return new App::DocumentObjectExecReturn(e.what());
}
TopShape.setTransform(Base::Matrix4D());
auto edges = UseAllEdges.getValue() ? TopShape.getSubTopoShapes(TopAbs_EDGE)
: getContinuousEdges(TopShape);
if (edges.empty()) {
return new App::DocumentObjectExecReturn(QT_TRANSLATE_NOOP("Exception", "No edges specified"));
}
const int chamferType = ChamferType.getValue();
const double size = Size.getValue();
double size2 = Size2.getValue();
const double angle = Angle.getValue();
const bool flipDirection = FlipDirection.getValue();
auto res = validateParameters(chamferType, size, size2, angle);
if (res != App::DocumentObject::StdReturn) {
return res;
}
this->positionByBaseFeature();
if (static_cast<Part::ChamferType>(chamferType) == Part::ChamferType::distanceAngle) {
size2 = angle;
}
try {
TopoShape shape(0);
shape.makeElementChamfer(
TopShape,
edges,
static_cast<Part::ChamferType>(chamferType),
size,
size2,
nullptr,
flipDirection ? Part::Flip::flip : Part::Flip::none
);
if (shape.isNull()) {
return new App::DocumentObjectExecReturn(
QT_TRANSLATE_NOOP("Exception", "Failed to create chamfer")
);
}
TopTools_ListOfShape aLarg;
aLarg.Append(TopShape.getShape());
if (!BRepAlgo::IsValid(aLarg, shape.getShape(), Standard_False, Standard_False)) {
ShapeFix_ShapeTolerance aSFT;
aSFT.LimitTolerance(
shape.getShape(),
Precision::Confusion(),
Precision::Confusion(),
TopAbs_SHAPE
);
}
// store shape before refinement
this->rawShape = shape;
shape = refineShapeIfActive(shape);
if (!isSingleSolidRuleSatisfied(shape.getShape())) {
return new App::DocumentObjectExecReturn(QT_TRANSLATE_NOOP(
"Exception",
"Result has multiple solids: enable 'Allow Compound' in the active body."
));
}
shape = getSolid(shape);
this->Shape.setValue(shape);
return App::DocumentObject::StdReturn;
}
catch (Standard_Failure& e) {
return new App::DocumentObjectExecReturn(e.GetMessageString());
}
}
void Chamfer::Restore(Base::XMLReader& reader)
{
DressUp::Restore(reader);
migrateFlippedProperties(reader);
}
void Chamfer::handleChangedPropertyType(Base::XMLReader& reader, const char* TypeName, App::Property* prop)
{
if (prop && strcmp(TypeName, "App::PropertyFloatConstraint") == 0
&& strcmp(prop->getTypeId().getName(), "App::PropertyQuantityConstraint") == 0) {
App::PropertyFloatConstraint p;
p.Restore(reader);
static_cast<App::PropertyQuantityConstraint*>(prop)->setValue(p.getValue());
}
else {
DressUp::handleChangedPropertyType(reader, TypeName, prop);
}
}
bool Chamfer::requiresSizeSwapping(const Base::XMLReader& reader) const
{
return Base::getVersion(reader.ProgramVersion) < Base::Version::v1_0
&& (ChamferType.getValue() == 1 || ChamferType.getValue() == 2);
}
void Chamfer::migrateFlippedProperties(const Base::XMLReader& reader)
{
if (!requiresSizeSwapping(reader)) {
return;
}
Base::Console().warning(
"The 'FlipDirection' property of the chamfer of %s is being adjusted to maintain"
"the same geometry in this FreeCAD version. If the re-saved file is later opened "
"in FreeCAD 0.21.x the chamfer result may differ due to the changed parameter "
"interpretation.\n",
getFullName()
);
FlipDirection.setValue(!FlipDirection.getValue());
}
void Chamfer::onChanged(const App::Property* prop)
{
if (prop == &ChamferType) {
updateProperties();
}
DressUp::onChanged(prop);
}
void Chamfer::updateProperties()
{
auto chamferType = ChamferType.getValue();
auto disableproperty = [](App::Property* prop, bool on) {
prop->setStatus(App::Property::ReadOnly, on);
};
switch (chamferType) {
case 0: // "Equal distance"
disableproperty(&this->Angle, true);
disableproperty(&this->Size2, true);
break;
case 1: // "Two distances"
disableproperty(&this->Angle, true);
disableproperty(&this->Size2, false);
break;
case 2: // "Distance and Angle"
disableproperty(&this->Angle, false);
disableproperty(&this->Size2, true);
break;
}
}
static App::DocumentObjectExecReturn* validateParameters(
int chamferType,
double size,
double size2,
double angle
)
{
// Size is common to all chamfer types.
if (size <= 0) {
return new App::DocumentObjectExecReturn(
QT_TRANSLATE_NOOP("Exception", "Size must be greater than zero")
);
}
switch (chamferType) {
case 0: // Equal distance
// Nothing to do.
break;
case 1: // Two distances
if (size2 <= 0) {
return new App::DocumentObjectExecReturn(
QT_TRANSLATE_NOOP("Exception", "Size2 must be greater than zero")
);
}
break;
case 2: // Distance and angle
if (angle <= 0 || angle >= 180.0) {
return new App::DocumentObjectExecReturn(
QT_TRANSLATE_NOOP("Exception", "Angle must be greater than 0 and less than 180")
);
}
break;
}
return App::DocumentObject::StdReturn;
}
|