PartDesign, 758, 766, 773, 775: several improvements, fixes and code refactoring for Pad and Pocket

This commit is contained in:
jrheinlaender
2012-11-03 23:07:48 +01:00
committed by logari81
parent 83cca85c1c
commit 13d68e99aa
12 changed files with 795 additions and 462 deletions

View File

@@ -26,7 +26,7 @@
# include <BRep_Builder.hxx>
# include <BRep_Tool.hxx>
# include <BRepBndLib.hxx>
# include <BRepPrimAPI_MakePrism.hxx>
# include <BRepFeat_MakePrism.hxx>
# include <BRepBuilderAPI_MakeFace.hxx>
# include <Handle_Geom_Surface.hxx>
# include <TopoDS.hxx>
@@ -58,7 +58,7 @@ Pad::Pad()
Type.setEnums(TypeEnums);
ADD_PROPERTY(Length,(100.0));
ADD_PROPERTY(Length2,(100.0));
ADD_PROPERTY(FaceName,(""));
ADD_PROPERTY_TYPE(UpToFace,(0),"Pad",(App::PropertyType)(App::Prop_None),"Face where feature will end");
}
short Pad::mustExecute() const
@@ -66,7 +66,7 @@ short Pad::mustExecute() const
if (Placement.isTouched() ||
Length.isTouched() ||
Length2.isTouched() ||
FaceName.isTouched())
UpToFace.isTouched())
return 1;
return Additive::mustExecute();
}
@@ -108,171 +108,57 @@ App::DocumentObjectExecReturn *Pad::execute(void)
TopLoc_Location invObjLoc = this->getLocation().Inverted();
try {
// TopoDS::Face is not strictly necessary, but it will through an exception for
// invalid wires e.g. intersections or multiple separate wires
TopoDS_Shape aFace = TopoDS::Face(makeFace(wires));
if (aFace.IsNull())
return new App::DocumentObjectExecReturn("Creating a face from sketch failed");
support.Move(invObjLoc);
// extrude the face to a solid
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;
bool isSolid = false; // support is a solid?
bool isSolidChecked = false; // not checked yet
std::string method(Type.getValueAsString());
if (method == "UpToFirst" || method == "UpToLast" || method == "UpToFace") {
TopoDS_Face supportface = getSupportFace();
supportface.Move(invObjLoc);
if ((std::string(Type.getValueAsString()) == "UpToLast") ||
(std::string(Type.getValueAsString()) == "UpToFirst") ||
(std::string(Type.getValueAsString()) == "UpToFace"))
{
// Find a valid face to extrude up to
TopoDS_Face upToFace;
gp_Dir dir(SketchVector.x,SketchVector.y,SketchVector.z);
if ((std::string(Type.getValueAsString()) == "UpToLast") ||
(std::string(Type.getValueAsString()) == "UpToFirst"))
{
// Check for valid support object
if (support.IsNull())
return new App::DocumentObjectExecReturn("Cannot extrude up to face: No valid support in Sketch");
TopExp_Explorer xp (support, TopAbs_SOLID);
if (!xp.More())
return new App::DocumentObjectExecReturn("Cannot extrude up to face: Support shape is not a solid");
isSolid = true;
isSolidChecked = true;
TopoDS_Shape origFace = makeFace(wires); // original sketch face before moving one unit
std::vector<Part::cutFaces> cfaces = Part::findAllFacesCutBy(support, origFace, dir);
if (cfaces.empty())
return new App::DocumentObjectExecReturn("No faces found in this direction");
// Find nearest/furthest face
std::vector<Part::cutFaces>::const_iterator it, it_near, it_far;
it_near = it_far = cfaces.begin();
for (it = cfaces.begin(); it != cfaces.end(); it++)
if (it->distsq > it_far->distsq)
it_far = it;
else if (it->distsq < it_near->distsq)
it_near = it;
upToFace = (std::string(Type.getValueAsString()) == "UpToLast" ? it_far->face : it_near->face);
} else {
if (FaceName.isEmpty())
return new App::DocumentObjectExecReturn("Cannot extrude up to face: No face selected");
// Get active object, this is the object that the user referenced when he clicked on the face!
App::DocumentObject* baseLink = this->getDocument()->getActiveObject();
if (!baseLink)
return new App::DocumentObjectExecReturn("Cannot extrude up to face: No object linked");
if (!baseLink->getTypeId().isDerivedFrom(Part::Feature::getClassTypeId()))
return new App::DocumentObjectExecReturn("Cannot extrude up to face: Linked object is not a Part object");
Part::Feature *base = static_cast<Part::Feature*>(baseLink);
const Part::TopoShape& baseShape = base->Shape.getShape();
if (baseShape._Shape.IsNull())
return new App::DocumentObjectExecReturn("Cannot extrude up to face: Cannot work on invalid shape");
TopoDS_Shape sub = baseShape.getSubShape(FaceName.getValue());
if (!sub.IsNull() && sub.ShapeType() == TopAbs_FACE)
upToFace = TopoDS::Face(sub);
else
return new App::DocumentObjectExecReturn("Cannot extrude up to face: Selection is not a face");
// Validate face
// TODO: This would also exclude faces that are valid but not cut by the line
// So for now we trust to the intelligence of the user when picking the face
/*std::vector<cutFaces> cfaces = findAllFacesCutBy(upToFace, origFace, dir);
if (cfaces.empty())
return new App::DocumentObjectExecReturn("No faces found in this direction");*/
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);
// Create semi-infinite prism from sketch in direction dir
// Hack, because the two lines commented out below do NOT work!!!
SketchVector *= 1E6;
gp_Vec vec(SketchVector.x,SketchVector.y,SketchVector.z);
vec.Transform(invObjLoc.Transformation());
BRepPrimAPI_MakePrism PrismMaker(aFace.Moved(invObjLoc),vec,0,1); // very long, but finite prism
//dir.Transform(invObjLoc.Transformation());
//BRepPrimAPI_MakePrism PrismMaker(aFace.Moved(invObjLoc),dir,0,0,1);
if (!PrismMaker.IsDone())
return new App::DocumentObjectExecReturn("Cannot extrude up to face: Could not extrude the sketch!");
// Cut off the prism at the face we found
// Grab any point from the sketch
TopExp_Explorer exp;
exp.Init(aFace, TopAbs_VERTEX);
if (!exp.More())
return new App::DocumentObjectExecReturn("Cannot extrude up to face: Sketch without points?");
gp_Pnt aPnt = BRep_Tool::Pnt(TopoDS::Vertex(exp.Current()));
// Create a halfspace from the face, extending in direction of sketch plane
BRepPrimAPI_MakeHalfSpace mkHalfSpace(upToFace, aPnt);
if (!mkHalfSpace.IsDone())
return new App::DocumentObjectExecReturn("Cannot extrude up to face: HalfSpace creation failed");
// Find common material between halfspace and prism
BRepAlgoAPI_Common mkCommon(PrismMaker.Shape(), mkHalfSpace.Solid().Moved(invObjLoc));
if (!mkCommon.IsDone())
return new App::DocumentObjectExecReturn("Cannot extrude up to face: Common creation failed");
prism = this->getSolid(mkCommon.Shape());
if (prism.IsNull())
return new App::DocumentObjectExecReturn("Cannot extrude up to face: Resulting shape is not a solid");
} else if ((std::string(Type.getValueAsString()) == "Length") ||
(std::string(Type.getValueAsString()) == "TwoLengths")) {
if (std::string(Type.getValueAsString()) == "Length") {
if (Midplane.getValue()) {
// Move face by half the extrusion distance to get pad symmetric to sketch plane
gp_Trsf mov;
mov.SetTranslation(gp_Vec(SketchVector.x,SketchVector.y,SketchVector.z) * (-1.0) * L/2.0);
TopLoc_Location loc(mov);
aFace.Move(loc);
} else if (Reversed.getValue()) { // negative direction
SketchVector *= -1.0;
}
// lengthen the vector
SketchVector *= L;
} else {
// Move face by the second length to get pad extending to both sides of sketch plane
gp_Trsf mov;
mov.SetTranslation(gp_Vec(SketchVector.x,SketchVector.y,SketchVector.z) * (-1.0) * L2);
TopLoc_Location loc(mov);
aFace.Move(loc);
// lengthen the vector
SketchVector *= (L + L2);
}
// create the extrusion
gp_Vec vec(SketchVector.x,SketchVector.y,SketchVector.z);
vec.Transform(invObjLoc.Transformation());
BRepPrimAPI_MakePrism PrismMaker(aFace.Moved(invObjLoc),vec,0,1); // finite prism
if (!PrismMaker.IsDone())
return new App::DocumentObjectExecReturn("Could not extrude the sketch!");
return new App::DocumentObjectExecReturn("Pad: Up to face: Could not extrude the sketch!");
prism = PrismMaker.Shape();
} else {
return new App::DocumentObjectExecReturn("Internal error: Unknown type for Pad feature");
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()) {
if (!isSolidChecked) { // we haven't checked for solid, yet
if (!support.IsNull()) {
TopExp_Explorer xp;
xp.Init(support,TopAbs_SOLID);
for (;xp.More(); xp.Next()) {
isSolid = true;
break;
}
}
if (!isSolid)
return new App::DocumentObjectExecReturn("Support is not a solid");
}
// Let's call algorithm computing a fuse operation:
BRepAlgoAPI_Fuse mkFuse(support.Moved(invObjLoc), prism);
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");
@@ -283,12 +169,8 @@ App::DocumentObjectExecReturn *Pad::execute(void)
if (solRes.IsNull())
return new App::DocumentObjectExecReturn("Pad: Resulting shape is not a solid");
this->Shape.setValue(solRes);
}
else {
TopoDS_Shape result = this->getSolid(prism);
// set the additive shape property for later usage in e.g. pattern
this->AddShape.setValue(result);
this->Shape.setValue(result);
} else {
this->Shape.setValue(prism);
}
return App::DocumentObject::StdReturn;