App::DocumentObjectExecReturn *Revolution::execute(void)
{
App::DocumentObject* link = Sketch.getValue();
if (!link)
return new App::DocumentObjectExecReturn("No sketch linked");
if (!link->getTypeId().isDerivedFrom(Part::Part2DObject::getClassTypeId()))
return new App::DocumentObjectExecReturn("Linked object is not a Sketch or Part2DObject");
Part::Part2DObject* pcSketch=static_cast<Part::Part2DObject*>(link);
TopoDS_Shape shape = pcSketch->Shape.getShape()._Shape;
if (shape.IsNull())
return new App::DocumentObjectExecReturn("Linked shape object is empty");
// this is a workaround for an obscure OCC bug which leads to empty tessellations
// for some faces. Making an explicit copy of the linked shape seems to fix it.
// The error only happens when re-computing the shape.
if (!this->Shape.getValue().IsNull()) {
BRepBuilderAPI_Copy copy(shape);
shape = copy.Shape();
if (shape.IsNull())
return new App::DocumentObjectExecReturn("Linked shape object is empty");
}
TopExp_Explorer ex;
std::vector<TopoDS_Wire> wires;
for (ex.Init(shape, TopAbs_WIRE); ex.More(); ex.Next()) {
wires.push_back(TopoDS::Wire(ex.Current()));
}
if (wires.empty()) // there can be several wires
return new App::DocumentObjectExecReturn("Linked shape object is not a wire");
// get the Sketch plane
Base::Placement SketchPlm = pcSketch->Placement.getValue();
// get reference axis
App::DocumentObject *pcReferenceAxis = ReferenceAxis.getValue();
const std::vector<std::string> &subReferenceAxis = ReferenceAxis.getSubValues();
if (pcReferenceAxis && pcReferenceAxis == pcSketch) {
bool hasValidAxis=false;
Base::Axis axis;
if (subReferenceAxis[0] == "V_Axis") {
hasValidAxis = true;
axis = pcSketch->getAxis(Part::Part2DObject::V_Axis);
}
else if (subReferenceAxis[0] == "H_Axis") {
hasValidAxis = true;
axis = pcSketch->getAxis(Part::Part2DObject::H_Axis);
}
else if (subReferenceAxis[0].size() > 4 && subReferenceAxis[0].substr(0,4) == "Axis") {
int AxId = std::atoi(subReferenceAxis[0].substr(4,4000).c_str());
if (AxId >= 0 && AxId < pcSketch->getAxisCount()) {
hasValidAxis = true;
axis = pcSketch->getAxis(AxId);
}
}
if (hasValidAxis) {
axis *= SketchPlm;
Base::Vector3d base=axis.getBase();
Base::Vector3d dir=axis.getDirection();
Base.setValue(base.x,base.y,base.z);
Axis.setValue(dir.x,dir.y,dir.z);
}
}
// get revolve axis
Base::Vector3f b = Base.getValue();
gp_Pnt pnt(b.x,b.y,b.z);
Base::Vector3f v = Axis.getValue();
gp_Dir dir(v.x,v.y,v.z);
// get the support of the Sketch if any
App::DocumentObject* pcSupport = pcSketch->Support.getValue();
Part::Feature *SupportObject = 0;
if (pcSupport && pcSupport->getTypeId().isDerivedFrom(Part::Feature::getClassTypeId()))
SupportObject = static_cast<Part::Feature*>(pcSupport);
TopoDS_Shape aFace = makeFace(wires);
if (aFace.IsNull())
return new App::DocumentObjectExecReturn("Creating a face from sketch failed");
// Rotate the face by half the angle to get revolution symmetric to sketch plane
if (Midplane.getValue()) {
gp_Trsf mov;
mov.SetRotation(gp_Ax1(pnt, dir), Base::toRadians<double>(Angle.getValue()) * (-1.0) / 2.0);
TopLoc_Location loc(mov);
aFace.Move(loc);
}
this->positionBySketch();
TopLoc_Location invObjLoc = this->getLocation().Inverted();
pnt.Transform(invObjLoc.Transformation());
dir.Transform(invObjLoc.Transformation());
// Reverse angle if selected
double angle = Base::toRadians<double>(Angle.getValue());
if (Reversed.getValue() && !Midplane.getValue())
angle *= (-1.0);
try {
//.........这里部分代码省略.........
App::DocumentObjectExecReturn *Revolution::execute(void)
{
// Validate parameters
double angle = Angle.getValue();
if (angle < Precision::Confusion())
return new App::DocumentObjectExecReturn("Angle of revolution too small");
if (angle > 360.0)
return new App::DocumentObjectExecReturn("Angle of revolution too large");
angle = Base::toRadians<double>(angle);
// Reverse angle if selected
if (Reversed.getValue() && !Midplane.getValue())
angle *= (-1.0);
std::vector<TopoDS_Wire> wires;
try {
wires = getSketchWires();
} catch (const Base::Exception& e) {
return new App::DocumentObjectExecReturn(e.what());
}
TopoDS_Shape support;
try {
support = getSupportShape();
} catch (const Base::Exception&) {
// ignore, because support isn't mandatory
support = TopoDS_Shape();
}
// update Axis from ReferenceAxis
updateAxis();
// get revolve axis
Base::Vector3d b = Base.getValue();
gp_Pnt pnt(b.x,b.y,b.z);
Base::Vector3d v = Axis.getValue();
gp_Dir dir(v.x,v.y,v.z);
try {
TopoDS_Shape sketchshape = makeFace(wires);
if (sketchshape.IsNull())
return new App::DocumentObjectExecReturn("Creating a face from sketch failed");
// Rotate the face by half the angle to get Revolution symmetric to sketch plane
if (Midplane.getValue()) {
gp_Trsf mov;
mov.SetRotation(gp_Ax1(pnt, dir), Base::toRadians<double>(Angle.getValue()) * (-1.0) / 2.0);
TopLoc_Location loc(mov);
sketchshape.Move(loc);
}
this->positionBySketch();
TopLoc_Location invObjLoc = this->getLocation().Inverted();
pnt.Transform(invObjLoc.Transformation());
dir.Transform(invObjLoc.Transformation());
support.Move(invObjLoc);
sketchshape.Move(invObjLoc);
// Check distance between sketchshape and axis - to avoid failures and crashes
if (checkLineCrossesFace(gp_Lin(pnt, dir), TopoDS::Face(sketchshape)))
return new App::DocumentObjectExecReturn("Revolve axis intersects the sketch");
// revolve the face to a solid
BRepPrimAPI_MakeRevol RevolMaker(sketchshape, gp_Ax1(pnt, dir), angle);
if (RevolMaker.IsDone()) {
TopoDS_Shape result = RevolMaker.Shape();
result = refineShapeIfActive(result);
// set the additive shape property for later usage in e.g. pattern
this->AddShape.setValue(result);
// if the sketch has a support fuse them to get one result object (PAD!)
if (!support.IsNull()) {
// Let's call algorithm computing a fuse operation:
BRepAlgoAPI_Fuse mkFuse(support, result);
// Let's check if the fusion has been successful
if (!mkFuse.IsDone())
throw Base::Exception("Fusion with support failed");
result = mkFuse.Shape();
result = refineShapeIfActive(result);
}
this->Shape.setValue(result);
}
else
return new App::DocumentObjectExecReturn("Could not revolve the sketch!");
return App::DocumentObject::StdReturn;
}
catch (Standard_Failure) {
Handle_Standard_Failure e = Standard_Failure::Caught();
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.");
else
return new App::DocumentObjectExecReturn(e->GetMessageString());
}
catch (Base::Exception& e) {
return new App::DocumentObjectExecReturn(e.what());
}
//.........这里部分代码省略.........
App::DocumentObjectExecReturn *RuledSurface::execute(void)
{
try {
App::DocumentObjectExecReturn* ret;
// get the first input shape
TopoDS_Shape S1;
ret = getShape(Curve1, S1);
if (ret) return ret;
// get the second input shape
TopoDS_Shape S2;
ret = getShape(Curve2, S2);
if (ret) return ret;
// check for expected type
if (S1.IsNull() || S2.IsNull())
return new App::DocumentObjectExecReturn("Linked shapes are empty.");
if (S1.ShapeType() != TopAbs_EDGE && S1.ShapeType() != TopAbs_WIRE)
return new App::DocumentObjectExecReturn("Linked shape is neither edge nor wire.");
if (S2.ShapeType() != TopAbs_EDGE && S2.ShapeType() != TopAbs_WIRE)
return new App::DocumentObjectExecReturn("Linked shape is neither edge nor wire.");
// https://forum.freecadweb.org/viewtopic.php?f=8&t=24052
//
// if both shapes are sub-elements of one common shape then the fill algorithm
// leads to problems if the shape has set a placement
// The workaround is to reset the placement before calling BRepFill and then
// applying the placement to the output shape
TopLoc_Location Loc;
if (Curve1.getValue() == Curve2.getValue()) {
Loc = S1.Location();
if (!Loc.IsIdentity() && Loc == S2.Location()) {
S1.Location(TopLoc_Location());
S2.Location(TopLoc_Location());
}
}
// make both shapes to have the same type
Standard_Boolean isWire = Standard_False;
if (S1.ShapeType() == TopAbs_WIRE)
isWire = Standard_True;
if (isWire) {
if (S2.ShapeType() == TopAbs_EDGE)
S2 = BRepLib_MakeWire(TopoDS::Edge(S2));
}
else {
// S1 is an edge, if S2 is a wire convert S1 to a wire, too
if (S2.ShapeType() == TopAbs_WIRE) {
S1 = BRepLib_MakeWire(TopoDS::Edge(S1));
isWire = Standard_True;
}
}
if (Orientation.getValue() == 0) {
// Automatic
Handle(Adaptor3d_HCurve) a1;
Handle(Adaptor3d_HCurve) a2;
if (!isWire) {
BRepAdaptor_Curve adapt1(TopoDS::Edge(S1));
BRepAdaptor_Curve adapt2(TopoDS::Edge(S2));
a1 = new BRepAdaptor_HCurve(adapt1);
a2 = new BRepAdaptor_HCurve(adapt2);
}
else {
BRepAdaptor_CompCurve adapt1(TopoDS::Wire(S1));
BRepAdaptor_CompCurve adapt2(TopoDS::Wire(S2));
a1 = new BRepAdaptor_HCompCurve(adapt1);
a2 = new BRepAdaptor_HCompCurve(adapt2);
}
if (!a1.IsNull() && !a2.IsNull()) {
// get end points of 1st curve
Standard_Real first, last;
first = a1->FirstParameter();
last = a1->LastParameter();
if (S1.Closed())
last = (first + last)/2;
gp_Pnt p1 = a1->Value(first);
gp_Pnt p2 = a1->Value(last);
if (S1.Orientation() == TopAbs_REVERSED) {
std::swap(p1, p2);
}
// get end points of 2nd curve
first = a2->FirstParameter();
last = a2->LastParameter();
if (S2.Closed())
last = (first + last)/2;
gp_Pnt p3 = a2->Value(first);
gp_Pnt p4 = a2->Value(last);
if (S2.Orientation() == TopAbs_REVERSED) {
std::swap(p3, p4);
}
// Form two triangles (P1,P2,P3) and (P4,P3,P2) and check their normals.
// If the dot product is negative then it's assumed that the resulting face
//.........这里部分代码省略.........
App::DocumentObjectExecReturn *Groove::execute(void)
{
// Validate parameters
double angle = Angle.getValue();
if (angle < Precision::Confusion())
return new App::DocumentObjectExecReturn("Angle of groove too small");
if (angle > 360.0)
return new App::DocumentObjectExecReturn("Angle of groove too large");
angle = Base::toRadians<double>(angle);
// Reverse angle if selected
if (Reversed.getValue() && !Midplane.getValue())
angle *= (-1.0);
Part::Part2DObject* sketch = 0;
std::vector<TopoDS_Wire> wires;
TopoDS_Shape support;
try {
sketch = getVerifiedSketch();
wires = getSketchWires();
support = getSupportShape();
} catch (const Base::Exception& e) {
return new App::DocumentObjectExecReturn(e.what());
}
// get the Sketch plane
Base::Placement SketchPlm = sketch->Placement.getValue();
// get reference axis
App::DocumentObject *pcReferenceAxis = ReferenceAxis.getValue();
const std::vector<std::string> &subReferenceAxis = ReferenceAxis.getSubValues();
if (pcReferenceAxis && pcReferenceAxis == sketch) {
bool hasValidAxis=false;
Base::Axis axis;
if (subReferenceAxis[0] == "V_Axis") {
hasValidAxis = true;
axis = sketch->getAxis(Part::Part2DObject::V_Axis);
}
else if (subReferenceAxis[0] == "H_Axis") {
hasValidAxis = true;
axis = sketch->getAxis(Part::Part2DObject::H_Axis);
}
else if (subReferenceAxis[0].size() > 4 && subReferenceAxis[0].substr(0,4) == "Axis") {
int AxId = std::atoi(subReferenceAxis[0].substr(4,4000).c_str());
if (AxId >= 0 && AxId < sketch->getAxisCount()) {
hasValidAxis = true;
axis = sketch->getAxis(AxId);
}
}
if (hasValidAxis) {
axis *= SketchPlm;
Base::Vector3d base=axis.getBase();
Base::Vector3d dir=axis.getDirection();
Base.setValue(base.x,base.y,base.z);
Axis.setValue(dir.x,dir.y,dir.z);
}
}
// get revolve axis
Base::Vector3f b = Base.getValue();
gp_Pnt pnt(b.x,b.y,b.z);
Base::Vector3f v = Axis.getValue();
gp_Dir dir(v.x,v.y,v.z);
try {
TopoDS_Shape sketchshape = makeFace(wires);
if (sketchshape.IsNull())
return new App::DocumentObjectExecReturn("Creating a face from sketch failed");
// Rotate the face by half the angle to get Groove symmetric to sketch plane
if (Midplane.getValue()) {
gp_Trsf mov;
mov.SetRotation(gp_Ax1(pnt, dir), Base::toRadians<double>(Angle.getValue()) * (-1.0) / 2.0);
TopLoc_Location loc(mov);
sketchshape.Move(loc);
}
this->positionBySketch();
TopLoc_Location invObjLoc = this->getLocation().Inverted();
pnt.Transform(invObjLoc.Transformation());
dir.Transform(invObjLoc.Transformation());
support.Move(invObjLoc);
sketchshape.Move(invObjLoc);
// Check distance between sketchshape and axis - to avoid failures and crashes
if (checkLineCrossesFace(gp_Lin(pnt, dir), TopoDS::Face(sketchshape)))
return new App::DocumentObjectExecReturn("Revolve axis intersects the sketch");
// revolve the face to a solid
BRepPrimAPI_MakeRevol RevolMaker(sketchshape, gp_Ax1(pnt, dir), angle);
if (RevolMaker.IsDone()) {
TopoDS_Shape result = RevolMaker.Shape();
// set the subtractive shape property for later usage in e.g. pattern
this->SubShape.setValue(result);
// cut out groove to get one result object
BRepAlgoAPI_Cut mkCut(support, result);
// Let's check if the fusion has been successful
if (!mkCut.IsDone())
//.........这里部分代码省略.........
App::DocumentObjectExecReturn *Pad::execute(void)
{
// Validate parameters
double L = Length.getValue();
if ((std::string(Type.getValueAsString()) == "Length") && (L < Precision::Confusion()))
return new App::DocumentObjectExecReturn("Length of pad too small");
double L2 = Length2.getValue();
if ((std::string(Type.getValueAsString()) == "TwoLengths") && (L < Precision::Confusion()))
return new App::DocumentObjectExecReturn("Second length of pad too small");
Part::Part2DObject* sketch = 0;
std::vector<TopoDS_Wire> wires;
try {
sketch = getVerifiedSketch();
wires = getSketchWires();
} catch (const Base::Exception& e) {
return new App::DocumentObjectExecReturn(e.what());
}
TopoDS_Shape support;
try {
support = getSupportShape();
} catch (const Base::Exception&) {
// ignore, because support isn't mandatory
support = TopoDS_Shape();
}
// get the Sketch plane
Base::Placement SketchPos = sketch->Placement.getValue();
Base::Rotation SketchOrientation = SketchPos.getRotation();
Base::Vector3d SketchVector(0,0,1);
SketchOrientation.multVec(SketchVector,SketchVector);
this->positionBySketch();
TopLoc_Location invObjLoc = this->getLocation().Inverted();
try {
support.Move(invObjLoc);
gp_Dir dir(SketchVector.x,SketchVector.y,SketchVector.z);
dir.Transform(invObjLoc.Transformation());
TopoDS_Shape sketchshape = makeFace(wires);
if (sketchshape.IsNull())
return new App::DocumentObjectExecReturn("Pad: Creating a face from sketch failed");
sketchshape.Move(invObjLoc);
TopoDS_Shape prism;
std::string method(Type.getValueAsString());
if (method == "UpToFirst" || method == "UpToLast" || method == "UpToFace") {
TopoDS_Face supportface = getSupportFace();
supportface.Move(invObjLoc);
if (Reversed.getValue())
dir.Reverse();
// Find a valid face to extrude up to
TopoDS_Face upToFace;
if (method == "UpToFace") {
getUpToFaceFromLinkSub(upToFace, UpToFace);
upToFace.Move(invObjLoc);
}
getUpToFace(upToFace, support, supportface, sketchshape, method, dir);
// A support object is always required and we need to use BRepFeat_MakePrism
// Problem: For Pocket/UpToFirst (or an equivalent Pocket/UpToFace) the resulting shape is invalid
// because the feature does not add any material. This only happens with the "2" option, though
// Note: It might be possible to pass a shell or a compound containing multiple faces
// as the Until parameter of Perform()
BRepFeat_MakePrism PrismMaker;
PrismMaker.Init(support, sketchshape, supportface, dir, 2, 1);
PrismMaker.Perform(upToFace);
if (!PrismMaker.IsDone())
return new App::DocumentObjectExecReturn("Pad: Up to face: Could not extrude the sketch!");
prism = PrismMaker.Shape();
} else {
generatePrism(prism, sketchshape, method, dir, L, L2,
Midplane.getValue(), Reversed.getValue());
}
if (prism.IsNull())
return new App::DocumentObjectExecReturn("Pad: Resulting shape is empty");
// set the additive shape property for later usage in e.g. pattern
this->AddShape.setValue(prism);
// if the sketch has a support fuse them to get one result object
if (!support.IsNull()) {
// Let's call algorithm computing a fuse operation:
BRepAlgoAPI_Fuse mkFuse(support, prism);
// Let's check if the fusion has been successful
if (!mkFuse.IsDone())
return new App::DocumentObjectExecReturn("Pad: Fusion with support failed");
TopoDS_Shape result = mkFuse.Shape();
// we have to get the solids (fuse sometimes creates compounds)
TopoDS_Shape solRes = this->getSolid(result);
// lets check if the result is a solid
if (solRes.IsNull())
return new App::DocumentObjectExecReturn("Pad: Resulting shape is not a solid");
//.........这里部分代码省略.........
请发表评论