]> OCCT Git - occt.git/commitdiff
Foundation Classes - TopoDS_Shape accept scaling by default #239
authordpasukhi <dpasukhi@opencascade.com>
Sat, 4 Jan 2025 22:28:01 +0000 (22:28 +0000)
committerdpasukhi <dpasukhi@opencascade.com>
Sat, 4 Jan 2025 23:47:53 +0000 (23:47 +0000)
Refactor Location and Move methods to validate transformations.
New default value to raise exception is false.

src/TopoDS/TopoDS_Shape.hxx

index 4933134650bf7b67d649904b7e9fafd0f2b10706..7f3dfcbf987683cc4cc97f0fbcf3442eb4fbe6a4 100644 (file)
@@ -85,23 +85,24 @@ public:
   const TopLoc_Location& Location() const { return myLocation; }
 
   //! Sets the shape local coordinate system.
-  void Location (const TopLoc_Location& theLoc, const Standard_Boolean theRaiseExc = Standard_True)
+  //! @param theLoc the new local coordinate system.
+  //! @param theRaiseExc flag to raise exception in case of transformation with scale or negative.
+  void Location (const TopLoc_Location& theLoc, const Standard_Boolean theRaiseExc = Standard_False)
   {
     const gp_Trsf& aTrsf = theLoc.Transformation();
-    if ((Abs(Abs(aTrsf.ScaleFactor()) - 1.) > TopLoc_Location::ScalePrec() || aTrsf.IsNegative()) && theRaiseExc)
+    if (theRaiseExc)
     {
-      //Exception
-      throw Standard_DomainError("Location with scaling transformation is forbidden");
-    }
-    else
-    {
-      myLocation = theLoc;
+      validateTransformation(aTrsf);
     }
+    myLocation = theLoc;
   }
 
-  //! Returns a  shape  similar to <me> with   the local
+  //! Returns a  shape  similar to <me> with the local
   //! coordinate system set to <Loc>.
-  TopoDS_Shape Located (const TopLoc_Location& theLoc, const Standard_Boolean theRaiseExc = Standard_True) const
+  //! @param theLoc the new local coordinate system.
+  //! @param theRaiseExc flag to raise exception in case of transformation with scale or negative.
+  //! @return the located shape.
+  TopoDS_Shape Located (const TopLoc_Location& theLoc, const Standard_Boolean theRaiseExc = Standard_False) const
   {
     TopoDS_Shape aShape (*this);
     aShape.Location (theLoc, theRaiseExc);
@@ -182,22 +183,23 @@ public:
   void Convex (Standard_Boolean theIsConvex) { myTShape->Convex (theIsConvex); }
 
   //! Multiplies the Shape location by thePosition.
-  void Move(const TopLoc_Location& thePosition, const Standard_Boolean theRaiseExc = Standard_True)
+  //! @param thePosition the transformation to apply.
+  //! @param theRaiseExc flag to raise exception in case of transformation with scale or negative.
+  void Move(const TopLoc_Location& thePosition, const Standard_Boolean theRaiseExc = Standard_False)
   {
     const gp_Trsf& aTrsf = thePosition.Transformation();
-    if ((Abs(Abs(aTrsf.ScaleFactor()) - 1.) > TopLoc_Location::ScalePrec() || aTrsf.IsNegative()) && theRaiseExc)
+    if (theRaiseExc)
     {
-      //Exception
-      throw Standard_DomainError("Moving with scaling transformation is forbidden");
-    }
-    else
-    {
-      myLocation = thePosition * myLocation;
+      validateTransformation(aTrsf);
     }
+    myLocation = thePosition * myLocation;
    }
 
   //! Returns a shape similar to <me> with a location multiplied by thePosition.
-  TopoDS_Shape Moved (const TopLoc_Location& thePosition, const Standard_Boolean theRaiseExc = Standard_True) const
+  //! @param thePosition the transformation to apply.
+  //! @param theRaiseExc flag to raise exception in case of transformation with scale or negative.
+  //! @return the moved shape.
+  TopoDS_Shape Moved (const TopLoc_Location& thePosition, const Standard_Boolean theRaiseExc = Standard_False) const
   {
     TopoDS_Shape aShape (*this);
     aShape.Move (thePosition, theRaiseExc);
@@ -300,6 +302,20 @@ public:
   //! Dumps the content of me into the stream
   Standard_EXPORT void DumpJson (Standard_OStream& theOStream, Standard_Integer theDepth = -1) const;
 
+protected:
+
+  //! Checks if the transformation contains scaling or negative values.
+  //! Raises an exception if the transformation is invalid.
+  //! @param theTrsf transformation to validate
+  void validateTransformation(const gp_Trsf& theTrsf) const
+  {
+    if (Abs(Abs(theTrsf.ScaleFactor()) - 1.) > TopLoc_Location::ScalePrec() || theTrsf.IsNegative())
+    {
+      //Exception
+      throw Standard_DomainError("Transformation with scaling transformation is forbidden");
+    }
+  }
+
 private:
 
   Handle(TopoDS_TShape) myTShape;