From: vsv Date: Wed, 22 Jun 2022 16:24:17 +0000 (+0300) Subject: #54769: isCylinder with parameters generates UNKNOWN_UserException X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=9ff61f5119dc80365a9e335d25b1d121a31bdf9c;p=modules%2Fshaper.git #54769: isCylinder with parameters generates UNKNOWN_UserException - Check that given direction values are valid Direction - Provide output parameters of surfaces for all cases --- diff --git a/src/GeomAlgoAPI/GeomAlgoAPI_CanonicalRecognition.cpp b/src/GeomAlgoAPI/GeomAlgoAPI_CanonicalRecognition.cpp index 430e326e4..074a5342c 100644 --- a/src/GeomAlgoAPI/GeomAlgoAPI_CanonicalRecognition.cpp +++ b/src/GeomAlgoAPI/GeomAlgoAPI_CanonicalRecognition.cpp @@ -27,6 +27,11 @@ #include #include +static bool isValidDirection(const std::vector& theDir) +{ + return (theDir.size() == 3) && (gp_Vec(theDir[0], theDir[1], theDir[2]).Magnitude() > 0.); +} + bool GeomAlgoAPI_CanonicalRecognition::isPlane(const GeomShapePtr& theShape, double theTolerance, std::vector& theNormal, std::vector& theOrigin) { @@ -37,7 +42,7 @@ bool GeomAlgoAPI_CanonicalRecognition::isPlane(const GeomShapePtr& theShape, dou if (aShape.IsNull()) return false; - bool aIsValidNormal = theNormal.size() == 3; + bool aIsValidNormal = isValidDirection(theNormal); bool aIsValidOrigin = theOrigin.size() == 3; gp_Pln aPln; if (aIsValidNormal && aIsValidOrigin) { @@ -53,18 +58,21 @@ bool GeomAlgoAPI_CanonicalRecognition::isPlane(const GeomShapePtr& theShape, dou catch (...) { return false; } - if (aResult && aIsValidNormal && aIsValidOrigin) - { - gp_Pnt aOrig = aPln.Location(); - theOrigin[0] = aOrig.X(); - theOrigin[1] = aOrig.Y(); - theOrigin[2] = aOrig.Z(); - - gp_Dir aNorm = aPln.Axis().Direction(); - theNormal[0] = aNorm.X(); - theNormal[1] = aNorm.Y(); - theNormal[2] = aNorm.Z(); - } + + gp_Pnt aOrig = aPln.Location(); + if (theOrigin.size() != 3) + theOrigin.resize(3); + theOrigin[0] = aOrig.X(); + theOrigin[1] = aOrig.Y(); + theOrigin[2] = aOrig.Z(); + + gp_Dir aNorm = aPln.Axis().Direction(); + if (theNormal.size() != 3) + theNormal.resize(3); + theNormal[0] = aNorm.X(); + theNormal[1] = aNorm.Y(); + theNormal[2] = aNorm.Z(); + return aResult; } @@ -94,14 +102,15 @@ bool GeomAlgoAPI_CanonicalRecognition::isSphere(const GeomShapePtr& theShape, do catch (...) { return false; } - if (aResult && aIsValidOrigin && aIsValidRadius) - { - gp_Pnt aLoc = aSphere.Location(); - theOrigin[0] = aLoc.X(); - theOrigin[1] = aLoc.Y(); - theOrigin[2] = aLoc.Z(); - theRadius = aSphere.Radius(); - } + + gp_Pnt aLoc = aSphere.Location(); + if (theOrigin.size() != 3) + theOrigin.resize(3); + theOrigin[0] = aLoc.X(); + theOrigin[1] = aLoc.Y(); + theOrigin[2] = aLoc.Z(); + theRadius = aSphere.Radius(); + return aResult; } @@ -116,15 +125,15 @@ bool GeomAlgoAPI_CanonicalRecognition::isCone(const GeomShapePtr& theShape, doub if (aShape.IsNull()) return false; - bool aIsValidAxis = theAxis.size() == 3; + bool aIsValidAxis = isValidDirection(theAxis); bool aIsValidApex = theApex.size() == 3; bool aIsValidAngle = theHalfAngle > 0; gp_Cone aCone; if (aIsValidAxis && aIsValidApex && aIsValidAngle) { gp_Pnt aLoc(theApex[0], theApex[1], theApex[2]); - aCone.SetLocation(aLoc); - aCone.SetAxis(gp_Ax1(aLoc, gp_Dir(theAxis[0], theAxis[1], theAxis[2]))); + gp_Ax3 aAx3(aLoc, gp_Dir(theAxis[0], theAxis[1], theAxis[2])); + aCone.SetPosition(aAx3); } ShapeAnalysis_CanonicalRecognition aRecognition(aShape); bool aResult = false; @@ -134,20 +143,23 @@ bool GeomAlgoAPI_CanonicalRecognition::isCone(const GeomShapePtr& theShape, doub catch (...) { return false; } - if (aResult && aIsValidAxis && aIsValidApex && aIsValidAngle) - { - gp_Dir aDir = aCone.Axis().Direction(); - theAxis[0] = aDir.X(); - theAxis[1] = aDir.Y(); - theAxis[2] = aDir.Z(); - gp_Pnt aApex = aCone.Apex(); - theApex[0] = aApex.X(); - theApex[1] = aApex.Y(); - theApex[2] = aApex.Z(); + gp_Dir aDir = aCone.Axis().Direction(); + if (theAxis.size() != 3) + theAxis.resize(3); + theAxis[0] = aDir.X(); + theAxis[1] = aDir.Y(); + theAxis[2] = aDir.Z(); + + gp_Pnt aApex = aCone.Apex(); + if (theApex.size() != 3) + theApex.resize(3); + theApex[0] = aApex.X(); + theApex[1] = aApex.Y(); + theApex[2] = aApex.Z(); + + theHalfAngle = aCone.SemiAngle(); - theHalfAngle = aCone.SemiAngle(); - } return aResult; } @@ -162,15 +174,15 @@ bool GeomAlgoAPI_CanonicalRecognition::isCylinder(const GeomShapePtr& theShape, if (aShape.IsNull()) return false; - bool aIsValidAxis = theAxis.size() == 3; + bool aIsValidAxis = isValidDirection(theAxis); bool aIsValidOrigin = theOrigin.size() == 3; bool aIsValidRadius = theRadius > 0; gp_Cylinder aCylinder; if (aIsValidAxis && aIsValidOrigin && aIsValidRadius) { gp_Pnt aLoc(theOrigin[0], theOrigin[0], theOrigin[0]); - aCylinder.SetLocation(aLoc); - aCylinder.SetAxis(gp_Ax1(aLoc, gp_Dir(theAxis[0], theAxis[1], theAxis[2]))); + gp_Ax3 aAx3(aLoc, gp_Dir(theAxis[0], theAxis[1], theAxis[2])); + aCylinder.SetPosition(aAx3); aCylinder.SetRadius(theRadius); } ShapeAnalysis_CanonicalRecognition aRecognition(aShape); @@ -181,20 +193,23 @@ bool GeomAlgoAPI_CanonicalRecognition::isCylinder(const GeomShapePtr& theShape, catch (...) { return false; } - if (aResult && aIsValidAxis && aIsValidOrigin && aIsValidRadius) - { - gp_Dir aDir = aCylinder.Axis().Direction(); - theAxis[0] = aDir.X(); - theAxis[1] = aDir.Y(); - theAxis[2] = aDir.Z(); - gp_Pnt aLoc = aCylinder.Location(); - theOrigin[0] = aLoc.X(); - theOrigin[1] = aLoc.Y(); - theOrigin[2] = aLoc.Z(); + gp_Dir aDir = aCylinder.Axis().Direction(); + if (theAxis.size() != 3) + theAxis.resize(3); + theAxis[0] = aDir.X(); + theAxis[1] = aDir.Y(); + theAxis[2] = aDir.Z(); + + gp_Pnt aLoc = aCylinder.Location(); + if (theOrigin.size() != 3) + theOrigin.resize(3); + theOrigin[0] = aLoc.X(); + theOrigin[1] = aLoc.Y(); + theOrigin[2] = aLoc.Z(); + + theRadius = aCylinder.Radius(); - theRadius = aCylinder.Radius(); - } return aResult; } @@ -208,7 +223,7 @@ bool GeomAlgoAPI_CanonicalRecognition::isLine(const GeomShapePtr& theEdge, doubl if (aShape.IsNull()) return false; - bool aIsValidDir = theDir.size() == 3; + bool aIsValidDir = isValidDirection(theDir); bool aIsValidOrigin = theOrigin.size() == 3; gp_Lin aLine; if (aIsValidDir && aIsValidOrigin) @@ -224,18 +239,21 @@ bool GeomAlgoAPI_CanonicalRecognition::isLine(const GeomShapePtr& theEdge, doubl catch (...) { return false; } - if (aResult && aIsValidDir && aIsValidOrigin) - { - gp_Pnt aLoc = aLine.Location(); - theOrigin[0] = aLoc.X(); - theOrigin[1] = aLoc.Y(); - theOrigin[2] = aLoc.Z(); - - gp_Dir aDir = aLine.Direction(); - theDir[0] = aDir.X(); - theDir[1] = aDir.Y(); - theDir[2] = aDir.Z(); - } + + gp_Pnt aLoc = aLine.Location(); + if (theOrigin.size() != 3) + theOrigin.resize(3); + theOrigin[0] = aLoc.X(); + theOrigin[1] = aLoc.Y(); + theOrigin[2] = aLoc.Z(); + + gp_Dir aDir = aLine.Direction(); + if (theDir.size() != 3) + theDir.resize(3); + theDir[0] = aDir.X(); + theDir[1] = aDir.Y(); + theDir[2] = aDir.Z(); + return aResult; } @@ -250,14 +268,15 @@ bool GeomAlgoAPI_CanonicalRecognition::isCircle(const GeomShapePtr& theEdge, dou if (aShape.IsNull()) return false; - bool aIsValidNormal = theNormal.size() == 3; + bool aIsValidNormal = isValidDirection(theNormal); bool aIsValidOrigin = theOrigin.size() == 3; bool aIsValidRadius = theRadius > 0; gp_Circ aCircle; if (aIsValidNormal && aIsValidOrigin && aIsValidRadius) { - aCircle.SetAxis(gp_Ax1(gp_Pnt(theOrigin[0], theOrigin[1], theOrigin[2]), - gp_Dir(theNormal[0], theNormal[1], theNormal[2]))); + gp_Ax2 aAx2(gp_Pnt(theOrigin[0], theOrigin[1], theOrigin[2]), + gp_Dir(theNormal[0], theNormal[1], theNormal[2])); + aCircle.SetPosition(aAx2); aCircle.SetRadius(theRadius); } ShapeAnalysis_CanonicalRecognition aRecognition(aShape); @@ -268,19 +287,21 @@ bool GeomAlgoAPI_CanonicalRecognition::isCircle(const GeomShapePtr& theEdge, dou catch (...) { return false; } - if (aResult && aIsValidNormal && aIsValidOrigin && aIsValidRadius) - { - gp_Pnt aLoc = aCircle.Location(); - theOrigin[0] = aLoc.X(); - theOrigin[1] = aLoc.Y(); - theOrigin[2] = aLoc.Z(); - - gp_Dir aDir = aCircle.Axis().Direction(); - theNormal[0] = aDir.X(); - theNormal[1] = aDir.Y(); - theNormal[2] = aDir.Z(); - theRadius = aCircle.Radius(); - } + + gp_Pnt aLoc = aCircle.Location(); + if (theOrigin.size() != 3) + theOrigin.resize(3); + theOrigin[0] = aLoc.X(); + theOrigin[1] = aLoc.Y(); + theOrigin[2] = aLoc.Z(); + + gp_Dir aDir = aCircle.Axis().Direction(); + if (theNormal.size() != 3) + theNormal.resize(3); + theNormal[0] = aDir.X(); + theNormal[1] = aDir.Y(); + theNormal[2] = aDir.Z(); + theRadius = aCircle.Radius(); return aResult; } @@ -296,9 +317,9 @@ bool GeomAlgoAPI_CanonicalRecognition::isEllipse(const GeomShapePtr& theEdge, do if (aShape.IsNull()) return false; - bool aIsValidNormal = theNormal.size() == 3; + bool aIsValidNormal = isValidDirection(theNormal); bool aIsValidOrigin = theOrigin.size() == 3; - bool aIsValidDirX = theDirX.size() == 3; + bool aIsValidDirX = isValidDirection(theDirX); bool aIsValidRad1 = (theMajorRadius > 0) && (theMajorRadius > theMinorRadius); bool aIsValidRad2 = (theMinorRadius > 0) && (theMajorRadius > theMinorRadius); @@ -318,25 +339,30 @@ bool GeomAlgoAPI_CanonicalRecognition::isEllipse(const GeomShapePtr& theEdge, do catch (...) { return false; } - if (aResult && aIsValidNormal && aIsValidOrigin && aIsValidDirX && aIsValidRad1 && aIsValidRad2) - { - gp_Pnt aLoc = aElips.Position().Location(); - theOrigin[0] = aLoc.X(); - theOrigin[1] = aLoc.Y(); - theOrigin[2] = aLoc.Z(); - - gp_Dir aNorm = aElips.Position().Direction(); - theNormal[0] = aNorm.X(); - theNormal[1] = aNorm.Y(); - theNormal[2] = aNorm.Z(); - - gp_Dir aDirX = aElips.Position().XDirection(); - theDirX[0] = aDirX.X(); - theDirX[1] = aDirX.Y(); - theDirX[2] = aDirX.Z(); - - theMajorRadius = aElips.MajorRadius(); - theMinorRadius = aElips.MinorRadius(); - } + + gp_Pnt aLoc = aElips.Position().Location(); + if (theOrigin.size() != 3) + theOrigin.resize(3); + theOrigin[0] = aLoc.X(); + theOrigin[1] = aLoc.Y(); + theOrigin[2] = aLoc.Z(); + + gp_Dir aNorm = aElips.Position().Direction(); + if (theNormal.size() != 3) + theNormal.resize(3); + theNormal[0] = aNorm.X(); + theNormal[1] = aNorm.Y(); + theNormal[2] = aNorm.Z(); + + gp_Dir aDirX = aElips.Position().XDirection(); + if (theDirX.size() != 3) + theDirX.resize(3); + theDirX[0] = aDirX.X(); + theDirX[1] = aDirX.Y(); + theDirX[2] = aDirX.Z(); + + theMajorRadius = aElips.MajorRadius(); + theMinorRadius = aElips.MinorRadius(); + return aResult; } diff --git a/src/PythonAPI/geom/CanonicalRecognition.py b/src/PythonAPI/geom/CanonicalRecognition.py index 725a103e5..f7510762b 100644 --- a/src/PythonAPI/geom/CanonicalRecognition.py +++ b/src/PythonAPI/geom/CanonicalRecognition.py @@ -1,32 +1,77 @@ +# 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, 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 +# Lesser General Public License for more details. +# +# 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 +# +# See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com +# + from GeomAlgoAPI import GeomAlgoAPI_CanonicalRecognition class CanonicalRecognition: "The class provides recognition of canonical shapes" def isPlane(self, shape, tolerance, normal = [], origin = []): - "Check if shape is planar" + """ + Check if shape is planar + Usage: + > CR.isPlane(shape, tolerance, normal, origin) + """ return GeomAlgoAPI_CanonicalRecognition.isPlane(shape, tolerance, normal, origin) def isSphere(self, shape, tolerance, origin = [], radius = 0.0): - "Check if shape is spherical" + """ + Check if shape is spherical + Usage: + > CR.isSphere(shape, tolerance, origin, radius) + """ return GeomAlgoAPI_CanonicalRecognition.isSphere(shape, tolerance, origin, radius) def isCone(self, shape, tolerance, axis = [], apex = [], halfAngle = 0.0): - "Check if shape is conical" + """ + Check if shape is conical + Usage: + > CR.isCone(shape, tolerance, axis, apex, halfAngle) + """ return GeomAlgoAPI_CanonicalRecognition.isCone(shape, tolerance, axis, apex, halfAngle) def isCylinder(self, shape, tolerance, axis = [], origin = [], radius = 0.0): - "Check if shape is cylinder" + """ + Check if shape is cylinder + Usage: + > CR.isCylinder(shape, tolerance, axis, origin, radius) + """ return GeomAlgoAPI_CanonicalRecognition.isCylinder(shape, tolerance, axis, origin, radius) def isLine(self, edge, tolerance, direction = [], origin = []): - "Check if edge/wire is line" + """ + Check if edge/wire is line + Usage: + > CR.isLine(edge, tolerance, direction, origin) + """ return GeomAlgoAPI_CanonicalRecognition.isLine(edge, tolerance, direction, origin) def isCircle(self, edge, tolerance, normal = [], origin = [], radius = 0.0): - "Check if edge/wire is circle" + """ + Check if edge/wire is circle + Usage: + > CR.isCircle(edge, tolerance, normal, origin, radius) + """ return GeomAlgoAPI_CanonicalRecognition.isCircle(edge, tolerance, normal, origin, radius) def isEllipse(self, edge, tolerance, normal = [], dirX = [], origin = [], majorRadius = 0.0, minorRadius = 0.0): - "Check if edge/wire is ellipse" + """ + Check if edge/wire is ellipse + Usage: + > CR.isEllipse(edge, tolerance, normal, dirX, origin, majorRadius, minorRadius) + """ return GeomAlgoAPI_CanonicalRecognition.isEllipse(edge, tolerance, normal, dirX, origin, majorRadius, minorRadius)