geom/src/SKETCHER/Sketcher_Profile.cxx

515 lines
15 KiB
C++
Raw Normal View History

2013-04-01 18:25:01 +06:00
// Copyright (C) 2007-2013 CEA/DEN, EDF R&D, OPEN CASCADE
2004-06-16 21:24:55 +06:00
//
2012-08-09 13:58:02 +06:00
// Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
// CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
//
2012-08-09 13:58:02 +06:00
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License.
//
2012-08-09 13:58:02 +06:00
// 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
// Lesser General Public License for more details.
//
2012-08-09 13:58:02 +06:00
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
2012-08-09 13:58:02 +06:00
// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
2004-06-16 21:24:55 +06:00
//
2012-08-09 13:58:02 +06:00
2009-02-13 17:16:39 +05:00
// GEOM SKETCHER : basic sketcher
2004-06-16 21:24:55 +06:00
// File : Sketcher_Profile.cxx
// Author : Damien COQUERET
// Module : GEOM
2012-08-09 13:58:02 +06:00
2005-08-11 10:43:57 +06:00
#include <Standard_Stream.hxx>
#include <Sketcher_Profile.hxx>
2004-06-16 21:24:55 +06:00
#include <TopoDS_Vertex.hxx>
#include <TopoDS_Face.hxx>
#include <BRepLib.hxx>
#include <BRepBuilderAPI_MakeVertex.hxx>
#include <BRepBuilderAPI_MakeEdge.hxx>
#include <BRepBuilderAPI_MakeWire.hxx>
#include <BRepBuilderAPI_MakeFace.hxx>
#include <GeomAPI.hxx>
2004-06-16 21:24:55 +06:00
#include <Geom2d_Line.hxx>
#include <Geom2d_Circle.hxx>
#include <Geom_Surface.hxx>
2004-06-16 21:24:55 +06:00
#include <Precision.hxx>
#include <gp_Pln.hxx>
#include <gp_Ax2.hxx>
2004-06-16 21:24:55 +06:00
#include <TCollection_AsciiString.hxx>
#include <TColStd_Array1OfAsciiString.hxx>
2004-06-16 21:24:55 +06:00
2004-12-01 15:39:14 +05:00
#include "utilities.h"
2004-06-16 21:24:55 +06:00
//=======================================================================
// profile
// command to build a profile
//=======================================================================
Sketcher_Profile::Sketcher_Profile()
{
}
//=======================================================================
// profile
// command to build a profile
//=======================================================================
Sketcher_Profile::Sketcher_Profile(const char* aCmd)
{
enum {line, circle, point, none} move;
Standard_Integer i = 1;
Standard_Real x0, y0, x, y, dx, dy;
x0 = y0 = x = y = dy = 0;
dx = 1;
Standard_Boolean first, stayfirst, face, close;
first = Standard_True;
stayfirst = face = close = Standard_False;
2012-08-09 13:58:02 +06:00
Standard_Integer reversed = 0;
Standard_Integer control_Tolerance = 0;
2004-06-16 21:24:55 +06:00
TopoDS_Shape S;
TopoDS_Vertex MP;
BRepBuilderAPI_MakeWire MW;
gp_Ax3 DummyHP(gp::XOY());
gp_Pln P(DummyHP);
TopLoc_Location TheLocation;
Handle(Geom_Surface) Surface;
myOK = Standard_False;
2012-08-09 13:58:02 +06:00
myError = 0;
2004-06-16 21:24:55 +06:00
//TCollection_AsciiString aCommand(CORBA::string_dup(aCmd));
TCollection_AsciiString aCommand ((char*)aCmd);
2004-06-16 21:24:55 +06:00
TCollection_AsciiString aToken = aCommand.Token(":", 1);
int n = 0;
2005-08-11 10:43:57 +06:00
// porting to WNT
TColStd_Array1OfAsciiString aTab (0, aCommand.Length() - 1);
2005-08-11 10:43:57 +06:00
if ( aCommand.Length() )
{
2004-06-16 21:24:55 +06:00
while(aToken.Length() != 0) {
2005-08-11 10:43:57 +06:00
if(aCommand.Token(":", n + 1).Length() > 0)
aTab(n) = aCommand.Token(":", n + 1);
2005-08-11 10:43:57 +06:00
aToken = aCommand.Token(":", ++n);
2004-06-16 21:24:55 +06:00
}
2005-08-11 10:43:57 +06:00
n = n - 1;
}
if ( aTab.Length() && aTab(0).Length() )
2005-08-11 10:43:57 +06:00
while(i < n) {
Standard_Real length = 0, radius = 0, angle = 0;
move = point;
2005-08-11 10:43:57 +06:00
int n1 = 0;
TColStd_Array1OfAsciiString a (0, aTab(0).Length());
aToken = aTab(i).Token(" ", 1);
while (aToken.Length() != 0) {
if (aTab(i).Token(" ", n1 + 1).Length() > 0)
a(n1) = aTab(i).Token(" ", n1 + 1);
aToken = aTab(i).Token(" ", ++n1);
2004-06-16 21:24:55 +06:00
}
2005-08-11 10:43:57 +06:00
n1 = n1 - 1;
switch(a(0).Value(1))
2004-06-16 21:24:55 +06:00
{
2005-08-11 10:43:57 +06:00
case 'F':
{
if (n1 != 3) goto badargs;
if (!first) {
MESSAGE("profile : The F instruction must precede all moves");
return;
}
x0 = x = a(1).RealValue();
y0 = y = a(2).RealValue();
2005-08-11 10:43:57 +06:00
stayfirst = Standard_True;
break;
}
case 'O':
{
if (n1 != 4) goto badargs;
P.SetLocation(gp_Pnt(a(1).RealValue(), a(2).RealValue(), a(3).RealValue()));
2005-08-11 10:43:57 +06:00
stayfirst = Standard_True;
break;
}
case 'P':
{
if (n1 != 7) goto badargs;
gp_Vec vn(a(1).RealValue(), a(2).RealValue(), a(3).RealValue());
gp_Vec vx(a(4).RealValue(), a(5).RealValue(), a(6).RealValue());
2005-08-11 10:43:57 +06:00
if (vn.Magnitude() <= Precision::Confusion() || vx.Magnitude() <= Precision::Confusion()) {
MESSAGE("profile : null direction");
return;
}
gp_Ax2 ax(P.Location(), vn, vx);
P.SetPosition(ax);
stayfirst = Standard_True;
break;
}
case 'X':
{
if (n1 != 2) goto badargs;
length = a(1).RealValue();
if (a(0) == "XX")
2005-08-11 10:43:57 +06:00
length -= x;
dx = 1; dy = 0;
move = line;
break;
}
case 'Y':
{
if (n1 != 2) goto badargs;
length = a(1).RealValue();
if (a(0) == "YY")
2005-08-11 10:43:57 +06:00
length -= y;
dx = 0; dy = 1;
move = line;
break;
}
case 'L':
{
if (n1 != 2) goto badargs;
length = a(1).RealValue();
2005-08-11 10:43:57 +06:00
if (Abs(length) > Precision::Confusion())
move = line;
else
move = none;
break;
}
case 'T':
{
if (n1 != 3) goto badargs;
Standard_Real vx = a(1).RealValue();
Standard_Real vy = a(2).RealValue();
if (a(0) == "TT") {
2005-08-11 10:43:57 +06:00
vx -= x;
vy -= y;
}
length = Sqrt(vx * vx + vy * vy);
if (length > Precision::Confusion()) {
move = line;
dx = vx / length;
dy = vy / length;
}
else
move = none;
break;
}
case 'R':
{
if (n1 != 2) goto badargs;
2012-08-09 13:58:02 +06:00
angle = a(1).RealValue() * M_PI / 180.;
if (a(0) == "RR") {
2005-08-11 10:43:57 +06:00
dx = Cos(angle);
dy = Sin(angle);
}
else {
Standard_Real c = Cos(angle);
Standard_Real s = Sin(angle);
Standard_Real t = c * dx - s * dy;
dy = s * dx + c * dy;
dx = t;
}
break;
}
case 'D':
{
if (n1 != 3) goto badargs;
Standard_Real vx = a(1).RealValue();
Standard_Real vy = a(2).RealValue();
2005-08-11 10:43:57 +06:00
length = Sqrt(vx * vx + vy * vy);
if (length > Precision::Confusion()) {
dx = vx / length;
dy = vy / length;
}
else
move = none;
break;
}
case 'C':
{
if (n1 != 3) goto badargs;
radius = a(1).RealValue();
2005-08-11 10:43:57 +06:00
if (Abs(radius) > Precision::Confusion()) {
2012-08-09 13:58:02 +06:00
angle = a(2).RealValue() * M_PI / 180.;
2005-08-11 10:43:57 +06:00
move = circle;
}
else
move = none;
break;
}
2012-08-09 13:58:02 +06:00
case 'A': // TAngential arc by end point
{
if (n1 != 3) goto badargs;
Standard_Real vx = a(1).RealValue();
Standard_Real vy = a(2).RealValue();
if (a(0) == "AA") {
vx -= x;
vy -= y;
}
Standard_Real det = dx * vy - dy * vx;
if ( Abs(det) > Precision::Confusion()) {
// Cosine of alpha = arc of angle / 2 , alpha in [0,Pi]
Standard_Real c = (dx * vx + dy * vy) / Sqrt((dx * dx + dy * dy) * (vx * vx + vy * vy));
// radius = distance between start and end point / 2 * sin(alpha)
// radius is > 0 or < 0
radius = (vx * vx + vy * vy)* Sqrt(dx * dx + dy * dy) / (2.0 * det);
if (Abs(radius) > Precision::Confusion()) {
angle = 2.0 * acos(c); // angle in [0,2Pi]
move = circle;
}
else
move = none;
break;
}
else
move = none;
break;
}
case 'U': // Arc by end point and radiUs
{
if (n1 != 5) goto badargs;
Standard_Real vx = a(1).RealValue();
Standard_Real vy = a(2).RealValue();
radius = a(3).RealValue();
reversed = a(4).IntegerValue();
if (a(0) == "UU") { // Absolute
vx -= x;
vy -= y;
}
Standard_Real length = Sqrt(vx * vx + vy * vy);
if ( (4.0 - (vx * vx + vy * vy) / (radius * radius) >= 0.0 ) && (length > Precision::Confusion()) ) {
// Cosine of alpha = arc angle / 2 , alpha in [0,Pi/2]
Standard_Real c = 0.5 * Sqrt(4.0 - (vx * vx + vy * vy) / (radius * radius));
angle = 2.0 * acos(c); // angle in [0,Pi]
if ( reversed == 2 )
angle = angle - 2 * M_PI;
dx = 0.5 * ( vy * 1.0/radius
+ vx * Sqrt(4.0 / (vx * vx + vy * vy) - 1.0 / (radius * radius)));
dy = - 0.5 * ( vx * 1.0/radius
- vy * Sqrt(4.0 / (vx * vx + vy * vy) - 1.0 / (radius * radius)));
move = circle;
}
else{
move = none;
}
break;
}
case 'E': // Arc by end point and cEnter
{
if (n1 != 7) goto badargs;
Standard_Real vx = a(1).RealValue();
Standard_Real vy = a(2).RealValue();
Standard_Real vxc = a(3).RealValue();
Standard_Real vyc = a(4).RealValue();
reversed = a(5).IntegerValue();
control_Tolerance = a(6).IntegerValue();
if (a(0) == "EE") { // Absolute
vx -= x;
vy -= y;
vxc -= x;
vyc -= y;
}
radius = Sqrt( vxc * vxc + vyc * vyc );
Standard_Real det = vx * vyc - vy * vxc;
Standard_Real length = Sqrt(vx * vx + vy * vy);
Standard_Real length2 = Sqrt((vx-vxc) * (vx-vxc) + (vy-vyc) * (vy-vyc));
Standard_Real length3 = Sqrt(vxc * vxc + vyc * vyc);
Standard_Real error = Abs(length2 - radius);
myError = error;
if ( error > Precision::Confusion() ){
MESSAGE("Warning : The specified end point is not on the Arc, distance = "<<error);
}
if ( error > Precision::Confusion() && control_Tolerance == 1) // Don't create the arc if the end point
move = none; // is too far from it
else if ( (length > Precision::Confusion()) &&
(length2 > Precision::Confusion()) &&
(length3 > Precision::Confusion()) ) {
Standard_Real c = ( radius * radius - (vx * vxc + vy * vyc) )
/ ( radius * Sqrt((vx-vxc) * (vx-vxc) + (vy-vyc) * (vy-vyc)) ) ; // Cosine of arc angle
angle = acos(c); // angle in [0,Pi]
if ( reversed == 2 )
angle = angle - 2 * M_PI;
if (det < 0)
angle = -angle;
dx = vyc / radius;
dy = -vxc / radius;
move = circle;
}
else {
move = none;
}
break;
}
2005-08-11 10:43:57 +06:00
case 'I':
{
if (n1 != 2) goto badargs;
length = a(1).RealValue();
if (a(0) == "IX") {
2005-08-11 10:43:57 +06:00
if (Abs(dx) < Precision::Confusion()) {
MESSAGE("profile : cannot intersect, arg "<<i-1);
return;
}
length = (length - x) / dx;
}
else if (a(0) == "IY") {
2005-08-11 10:43:57 +06:00
if (Abs(dy) < Precision::Confusion()) {
MESSAGE("profile : cannot intersect, arg "<<i-1);
return;
}
length = (length - y) / dy;
}
if (Abs(length) > Precision::Confusion())
move = line;
else
move = none;
break;
}
case 'W':
{
if (a(0) == "WW")
2005-08-11 10:43:57 +06:00
close = Standard_True;
else if(a(0) == "WF") {
2005-08-11 10:43:57 +06:00
close = Standard_True;
face = Standard_True;
}
i = n - 1;
break;
}
default:
{
MESSAGE("profile : unknown code " << a(i));
2005-08-11 10:43:57 +06:00
return;
}
2004-06-16 21:24:55 +06:00
}
2005-08-11 10:43:57 +06:00
again :
switch (move)
2004-06-16 21:24:55 +06:00
{
case line :
{
2005-08-11 10:43:57 +06:00
if (length < 0) {
length = -length;
dx = -dx;
dy = -dy;
}
Handle(Geom2d_Line) l = new Geom2d_Line(gp_Pnt2d(x,y),gp_Dir2d(dx,dy));
2004-12-01 15:39:14 +05:00
BRepBuilderAPI_MakeEdge ME (GeomAPI::To3d(l,P),0,length);
if (!ME.IsDone())
return;
2005-08-11 10:43:57 +06:00
MW.Add(ME);
x += length*dx;
y += length*dy;
break;
2004-06-16 21:24:55 +06:00
}
case circle :
{
2005-08-11 10:43:57 +06:00
Standard_Boolean sense = Standard_True;
if (radius < 0) {
radius = -radius;
sense = !sense;
dx = -dx;
dy = -dy;
}
gp_Ax2d ax(gp_Pnt2d(x-radius*dy,y+radius*dx),gp_Dir2d(dy,-dx));
if (angle < 0) {
angle = -angle;
sense = !sense;
}
Handle(Geom2d_Circle) c = new Geom2d_Circle(ax,radius,sense);
2004-12-01 15:39:14 +05:00
BRepBuilderAPI_MakeEdge ME (GeomAPI::To3d(c,P),0,angle);
if (!ME.IsDone())
return;
2005-08-11 10:43:57 +06:00
MW.Add(ME);
gp_Pnt2d p;
gp_Vec2d v;
c->D1(angle,p,v);
x = p.X();
y = p.Y();
dx = v.X() / radius;
dy = v.Y() / radius;
break;
2004-06-16 21:24:55 +06:00
}
case point:
{
2005-08-11 10:43:57 +06:00
MP = BRepBuilderAPI_MakeVertex(gp_Pnt(x, y, 0.0));
break;
2004-06-16 21:24:55 +06:00
}
case none:
{
2005-08-11 10:43:57 +06:00
i = n - 1;
break;
2004-06-16 21:24:55 +06:00
}
}
2004-06-16 21:24:55 +06:00
// update first
first = stayfirst;
stayfirst = Standard_False;
2004-06-16 21:24:55 +06:00
if(!(dx == 0 && dy == 0))
myLastDir.SetCoord(dx, dy, 0.0);
else
return;
myLastPoint.SetX(x);
myLastPoint.SetY(y);
2004-06-16 21:24:55 +06:00
// next segment....
i++;
if ((i == n) && close) {
// the closing segment
dx = x0 - x;
dy = y0 - y;
length = Sqrt(dx * dx + dy * dy);
move = line;
if (length > Precision::Confusion()) {
2005-08-11 10:43:57 +06:00
dx = dx / length;
dy = dy / length;
goto again;
2004-06-16 21:24:55 +06:00
}
}
}
2004-06-16 21:24:55 +06:00
// get the result, face or wire
2004-12-01 15:39:14 +05:00
if (move == none) {
2004-06-16 21:24:55 +06:00
return;
2004-12-01 15:39:14 +05:00
} else if (move == point) {
2004-06-16 21:24:55 +06:00
S = MP;
2004-12-01 15:39:14 +05:00
} else if (face) {
if (!MW.IsDone()) {
return;
}
BRepBuilderAPI_MakeFace MF (P, MW.Wire());
if (!MF.IsDone()) {
return;
}
S = MF;
} else {
if (!MW.IsDone()) {
return;
}
2004-06-16 21:24:55 +06:00
S = MW;
2004-12-01 15:39:14 +05:00
}
2004-06-16 21:24:55 +06:00
if(!TheLocation.IsIdentity())
S.Move(TheLocation);
myShape = S;
myOK = true;
return;
badargs :
2004-12-01 15:39:14 +05:00
MESSAGE("profile : bad number of arguments");
2004-06-16 21:24:55 +06:00
return;
}