+ avoid code repetition in positioning of Sketch

git-svn-id: https://free-cad.svn.sourceforge.net/svnroot/free-cad/trunk@5254 e8eeb9e2-ec13-0410-a4a9-efa5cf37419d
This commit is contained in:
logari81 2011-12-10 15:14:56 +00:00
parent 6ed7ad3a15
commit fad7696ad4
4 changed files with 45 additions and 55 deletions

View File

@ -20,7 +20,7 @@
* *
***************************************************************************/
#include "PreCompiled.h"
#ifndef _PreComp_
# include <TopoDS_Shape.hxx>
@ -63,25 +63,36 @@ Part2DObject::Part2DObject()
App::DocumentObjectExecReturn *Part2DObject::execute(void)
{
return App::DocumentObject::StdReturn;
}
Base::Placement Part2DObject::positionBySupport(const TopoDS_Face &face, const Base::Placement &Place)
void Part2DObject::positionBySupport(void)
{
if (face.IsNull())
throw Base::Exception("Null Face in Part2DObject::positionBySupport()!");
// recalculate support:
Part::Feature *part = static_cast<Part::Feature*>(Support.getValue());
if (!part || !part->getTypeId().isDerivedFrom(Part::Feature::getClassTypeId()))
return;
bool Reverse = false;
if (face.Orientation() == TopAbs_REVERSED)
Reverse = true;
Base::Placement Place = part->Placement.getValue();
const std::vector<std::string> &sub = Support.getSubValues();
assert(sub.size()==1);
// get the selected sub shape (a Face)
const Part::TopoShape &shape = part->Shape.getShape();
if (shape._Shape.IsNull())
throw Base::Exception("Support shape is empty!");
TopoDS_Shape sh = shape.getSubShape(sub[0].c_str());
const TopoDS_Face &face = TopoDS::Face(sh);
if (face.IsNull())
throw Base::Exception("Null face in Part2DObject::positionBySupport()!");
BRepAdaptor_Surface adapt(face);
if (adapt.GetType() != GeomAbs_Plane)
throw Base::Exception("No planar Face in Part2DObject::positionBySupport()!");
throw Base::Exception("No planar face in Part2DObject::positionBySupport()!");
bool Reverse = false;
if (face.Orientation() == TopAbs_REVERSED)
Reverse = true;
gp_Pnt ObjOrg(Place.getPosition().x,Place.getPosition().y,Place.getPosition().z);
gp_Pln plane = adapt.Plane();
Standard_Boolean ok = plane.Direct();
if (!ok) {
@ -94,6 +105,8 @@ Base::Placement Part2DObject::positionBySupport(const TopoDS_Face &face, const B
if (Reverse)
Normal.Reverse();
gp_Pnt ObjOrg(Place.getPosition().x,Place.getPosition().y,Place.getPosition().z);
Handle (Geom_Plane) gPlane = new Geom_Plane(plane);
GeomAPI_ProjectPointOnSurf projector(ObjOrg,gPlane);
gp_Pnt SketchBasePoint = projector.NearestPoint();
@ -168,7 +181,7 @@ Base::Placement Part2DObject::positionBySupport(const TopoDS_Face &face, const B
// check the angle against the Z Axis
//Standard_Real a = Normal.Angle(gp_Ax1(gp_Pnt(0,0,0),gp_Dir(0,0,1)));
return Base::Placement(mtrx);
Placement.setValue(Base::Placement(mtrx));
}
bool Part2DObject::seekTrimPoints(const std::vector<Geometry *> &geomlist,

View File

@ -35,16 +35,16 @@ namespace Part
{
class Geometry;
/** 2D Shape
* This is a specialiced version of the PartShape for use with
/** 2D Shape
* This is a specialiced version of the PartShape for use with
* flat (2D) geometry. The Z direction has always to be 0.
* The position and orientation of the Plane this 2D geometry is
* referenced is defined by the Placement property. It also
* has a link to a supporting Face which defines the position
* has a link to a supporting Face which defines the position
* in space where it is located. If the support is changed the
* static methode positionBySupport() is used to calculate a
* static methode positionBySupport() is used to calculate a
* new position for the Part2DObject.
* This object can be used stand alone or for constraint
* This object can be used stand alone or for constraint
* geometry as its descend Sketcher::SketchObject .
*/
@ -58,15 +58,15 @@ public:
/// if the 2DObject lies on the Face of an other object this links to it
App::PropertyLinkSub Support;
/** calculate a position out of a support face
* this methode will calculate the position of the
* 2D shape on a supporting Face. The Normal(Orientation) get
/** calculate and update the Placement property based on the Support
* this methode will calculate the position of the
* 2D shape on a supporting Face. The Normal(Orientation) get
* extracted from the Face and for the position an educated guess is made,
* by examining the placement of the support object (not only the face).
* If the support is changed this methode is called do determine a new
* If the support is changed this methode is called do determine a new
* postion of the 2D shape on the supporting Face
*/
static Base::Placement positionBySupport(const TopoDS_Face &face, const Base::Placement &Place);
void positionBySupport(void);
/** calculate the points where a curve with index GeoId should be trimmed
* with respect to the rest of the curves contained in the list geomlist

View File

@ -57,28 +57,11 @@ SketchObject::SketchObject()
App::DocumentObjectExecReturn *SketchObject::execute(void)
{
// recalculate support:
Part::Feature *part = static_cast<Part::Feature*>(Support.getValue());
if (part && part->getTypeId().isDerivedFrom(Part::Feature::getClassTypeId()))
{
Base::Placement ObjectPos = part->Placement.getValue();
const std::vector<std::string> &sub = Support.getSubValues();
assert(sub.size()==1);
// get the selected sub shape (a Face)
const Part::TopoShape &shape = part->Shape.getShape();
if (shape._Shape.IsNull())
return new App::DocumentObjectExecReturn("Support shape is empty!");
TopoDS_Shape sh = shape.getSubShape(sub[0].c_str());
const TopoDS_Face &face = TopoDS::Face(sh);
assert(!face.IsNull());
BRepAdaptor_Surface adapt(face);
if (adapt.GetType() != GeomAbs_Plane)
return new App::DocumentObjectExecReturn("Sketch has no planar support!");
// set sketch position
Base::Placement placement = Part2DObject::positionBySupport(face,ObjectPos);
Placement.setValue(placement);
try {
this->positionBySupport();
}
catch (const Base::Exception& e) {
return new App::DocumentObjectExecReturn(e.what());
}
// setup and diagnose the sketch

View File

@ -54,7 +54,7 @@ using namespace Part;
DEF_STD_CMD_A(CmdSketcherNewSketch);
CmdSketcherNewSketch::CmdSketcherNewSketch()
:Command("Sketcher_NewSketch")
:Command("Sketcher_NewSketch")
{
sAppModule = "Sketcher";
sGroup = QT_TR_NOOP("Sketcher");
@ -103,19 +103,16 @@ void CmdSketcherNewSketch::activated(int iMsg)
QObject::tr("You need a planar face as support for a sketch!"));
return;
}
Base::Placement placement = Part2DObject::positionBySupport(face,ObjectPos);
double a,b,c;
placement.getRotation().getYawPitchRoll(a,b,c);
std::string supportString = FaceFilter.Result[0][0].getAsPropertyLinkSubString();
// create Sketch on Face
std::string FeatName = getUniqueObjectName("Sketch");
openCommand("Create a Sketch on Face");
doCommand(Doc,"App.activeDocument().addObject('Sketcher::SketchObject','%s')",FeatName.c_str());
doCommand(Gui,"App.activeDocument().%s.Placement = FreeCAD.Placement(FreeCAD.Vector(%f,%f,%f),FreeCAD.Rotation(%f,%f,%f))",FeatName.c_str(),placement.getPosition().x,placement.getPosition().y,placement.getPosition().z,a,b,c);
doCommand(Gui,"App.activeDocument().%s.Support = %s",FeatName.c_str(),supportString.c_str());
doCommand(Gui,"App.activeDocument().recompute()"); // recompute the sketch placement based on its support
//doCommand(Gui,"Gui.activeDocument().activeView().setCamera('%s')",cam.c_str());
doCommand(Gui,"Gui.activeDocument().setEdit('%s')",FeatName.c_str());
}
@ -158,7 +155,7 @@ void CmdSketcherNewSketch::activated(int iMsg)
doCommand(Gui,"Gui.activeDocument().activeView().setCamera('%s')",camstring.c_str());
doCommand(Gui,"Gui.activeDocument().setEdit('%s')",FeatName.c_str());
}
}
bool CmdSketcherNewSketch::isActive(void)
@ -237,15 +234,12 @@ void CmdSketcherMapSketch::activated(int iMsg)
qApp->translate(className(),"You need a planar face as support for a sketch!"));
return;
}
Base::Placement placement = Part2DObject::positionBySupport(face,ObjectPos);
double a,b,c;
placement.getRotation().getYawPitchRoll(a,b,c);
std::string supportString = FaceFilter.Result[0][0].getAsPropertyLinkSubString();
openCommand("Map a Sketch on Face");
doCommand(Gui,"App.activeDocument().%s.Placement = FreeCAD.Placement(FreeCAD.Vector(%f,%f,%f),FreeCAD.Rotation(%f,%f,%f))",featName.c_str(),placement.getPosition().x,placement.getPosition().y,placement.getPosition().z,a,b,c);
doCommand(Gui,"App.activeDocument().%s.Support = %s",featName.c_str(),supportString.c_str());
doCommand(Gui,"App.activeDocument().recompute()");
doCommand(Gui,"Gui.activeDocument().setEdit('%s')",featName.c_str());
}
else {