]> SALOME platform Git repositories - modules/shaper.git/commitdiff
Salome HOME
Sketch fix asozinov/DXF 25/head
authorasozinov <alexey.sozinov@opencascade.com>
Mon, 8 Apr 2024 17:14:49 +0000 (18:14 +0100)
committerasozinov <alexey.sozinov@opencascade.com>
Tue, 9 Apr 2024 17:18:45 +0000 (18:18 +0100)
Error license message moved to console

src/DXFPlugin/DXFPlugin_Import.cpp
src/DXFPlugin/DXFPlugin_ImportAlgoAPI.cpp
src/DXFPlugin/DXFPlugin_ImportAlgoAPI.h
src/DXFPlugin/plugin-DXF.xml
src/ModuleBase/ModuleBase_IModule.cpp

index cbe44388b71c246c8c779a4ff4d2be8562b079b6..d8a20823e2f1a9c74868098745cd1ebe5bb21ba9 100644 (file)
 #include <ModelAPI_AttributeString.h>
 #include <ModelAPI_ResultBody.h>
 #include <ModelAPI_Tools.h>
+#include <ModelAPI_Session.h>
 #include <ModelAPI_AttributeInteger.h>>
 #include <ModelAPI_AttributeDouble.h>>
 #include <ModelAPI_AttributeReference.h>
 #include <ModelAPI_AttributeBoolean.h>
+#include <ModelAPI_Validator.h>
 #include <Locale_Convert.h>
 #include <Events_InfoMessage.h>
+#include <SketchPlugin_Sketch.h>
 
 #include <string>
 #include <sstream>
@@ -90,6 +93,10 @@ void DXFPlugin_Import::initAttributes()
   data()->addAttribute(DXFPlugin_Import::BLOCKS_ID(), ModelAPI_AttributeBoolean::typeId());
   data()->addAttribute(DXFPlugin_Import::IMPORT_COLORS_ID(), ModelAPI_AttributeBoolean::typeId());
   data()->addAttribute(DXFPlugin_Import::EDGES_WITH_WIDTH_ID(), ModelAPI_AttributeBoolean::typeId());
+
+
+  ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), SKETCH_TO_JOIN_ID());
+  ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), ATTACH_TO_SKETCH_ID());
 }
 
 bool DXFPlugin_Import::checkLicense()
@@ -178,6 +185,11 @@ void DXFPlugin_Import::execute()
 
   Handle(TDocStd_Document) aDoc = new TDocStd_Document("XDE");
   Standard_Boolean aBool = aReader.Transfer(aDoc);
+  if (!aBool)
+  {
+    setError("Error: Failed transfer OCAF document from DXF file.");
+    return;
+  }
   DXFPlugin_ImportAlgoAPI anAlgo(aDoc);
 
   GeomShapePtr aResShape(new GeomAPI_Shape());
@@ -196,7 +208,36 @@ void DXFPlugin_Import::execute()
     if (isOk)
       // Convert shape to sketch
     {
-      auto aShapes = anAlgo.ConvertToSketchElements(aShape);
+      CompositeFeaturePtr aSketch;
+      if (boolean(DXFPlugin_Import::ATTACH_TO_SKETCH_ID())->value())
+      {
+        auto anObj = reference(DXFPlugin_Import::SKETCH_TO_JOIN_ID())->value();
+        aSketch =
+          std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(anObj);
+      }
+      else
+      {
+        DocumentPtr aPartSet = ModelAPI_Session::get()->moduleDocument();
+        DocumentPtr aPart = ModelAPI_Session::get()->activeDocument();
+        FeaturePtr aSkFeature = aPart->addFeature("Sketch");
+        std::shared_ptr<GeomDataAPI_Point> anOrigin = std::dynamic_pointer_cast<GeomDataAPI_Point>(
+          aSkFeature->data()->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
+
+        anOrigin->setX(0);
+        anOrigin->setY(0);
+        anOrigin->setZ(0);
+
+        std::shared_ptr<GeomDataAPI_Dir> aDirX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
+          aSkFeature->data()->attribute(SketchPlugin_Sketch::DIRX_ID()));
+        aDirX->setValue(1, 0, 0);
+
+        std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
+          aSkFeature->data()->attribute(SketchPlugin_Sketch::NORM_ID()));
+        aNorm->setValue(0, 0, 1);
+
+        aSketch = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aSkFeature);
+      }
+      auto aShapes = anAlgo.ConvertToSketchElements(aShape, aSketch);
       isOk = false;
     }
     // From list of edges create sketch primitives
index 1adad70c12cd0446f9938f515b29182e72db84ff..7879058e64d73239a98603c111db6d5196128b3d 100644 (file)
 #include <SketchPlugin_MacroEllipticArc.h>
 #include <SketchPlugin_Sketch.h>
 
+#define _DEBUG
+
+#ifdef _DEBUG
+#include <GeomAPI_Pnt2d.h>
+#endif // _DEBUG
+
 DXFPlugin_ImportAlgoAPI::DXFPlugin_ImportAlgoAPI(Handle(TDocStd_Document) theDoc)
 {
   myDoc = Handle(TDocStd_Document)(theDoc);
@@ -127,23 +133,8 @@ std::vector<int> DXFPlugin_ImportAlgoAPI::color(const GeomShapePtr theShape) con
   return std::vector<int>();
 }
 
-std::list<TopoDS_Shape> DXFPlugin_ImportAlgoAPI::ConvertToSketchElements(const TopoDS_Shape theShape) const
+std::list<TopoDS_Shape> DXFPlugin_ImportAlgoAPI::ConvertToSketchElements(const TopoDS_Shape theShape, CompositeFeaturePtr theSketch) const
 {
-  CompositeFeaturePtr aSketch;
-  auto aSession = ModelAPI_Session::get();
-  auto aPartSet = aSession->moduleDocument();
-  auto aPart = aSession->activeDocument();
-  for (auto feat : aPart->allFeatures())
-    if (feat->getKind() == "Sketch")
-    {
-      aSketch =
-        std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(feat);
-      break;
-    }
-
-  if (!aSketch)
-    return std::list<TopoDS_Shape>();
-
   int aNb = 0;
   std::list<TopoDS_Shape> aShapes;
   for (TopExp_Explorer anExp(theShape, TopAbs_EDGE); anExp.More(); anExp.Next())
@@ -162,7 +153,7 @@ std::list<TopoDS_Shape> DXFPlugin_ImportAlgoAPI::ConvertToSketchElements(const T
         auto aStartP = anEdge->firstPoint();
         auto anEndP = anEdge->lastPoint();
         auto aCenter = aCircle->center();
-        auto anArcFeature = aSketch->addFeature(SketchPlugin_Arc::ID());
+        auto anArcFeature = theSketch->addFeature(SketchPlugin_Arc::ID());
 
         std::shared_ptr<GeomDataAPI_Point2D> aStartAttr = std::dynamic_pointer_cast<
           GeomDataAPI_Point2D>(anArcFeature->attribute(SketchPlugin_Arc::START_ID()));
@@ -176,6 +167,7 @@ std::list<TopoDS_Shape> DXFPlugin_ImportAlgoAPI::ConvertToSketchElements(const T
         aCenterAttr->setValue(aCenter->x(), aCenter->y());
         anArcFeature->execute();
       }
+      // BSpline importred incorrect
       if (anEdge->isBSpline())
       {
         const TopoDS_Shape& aShape = anEdge->impl<TopoDS_Shape>();
@@ -187,7 +179,8 @@ std::list<TopoDS_Shape> DXFPlugin_ImportAlgoAPI::ConvertToSketchElements(const T
         auto aWeights = aBSpl->Weights();
         auto aDegree = aBSpl->Degree();
         auto aPoles = aBSpl->Poles();
-        auto aBSplineFeature = aSketch->addFeature(SketchPlugin_BSpline::ID());
+
+        auto aBSplineFeature = theSketch->addFeature(SketchPlugin_BSpline::ID());
 
         AttributePoint2DArrayPtr aPolesArray =
           std::dynamic_pointer_cast<GeomDataAPI_Point2DArray>(aBSplineFeature->attribute(SketchPlugin_BSpline::POLES_ID()));
@@ -196,21 +189,67 @@ std::list<TopoDS_Shape> DXFPlugin_ImportAlgoAPI::ConvertToSketchElements(const T
         AttributeIntArrayPtr aMultsArray = aBSplineFeature->data()->intArray(SketchPlugin_BSpline::MULTS_ID());
         AttributeIntegerPtr aDegreeAttr = aBSplineFeature->data()->integer(SketchPlugin_BSpline::DEGREE_ID());
         aWeightsArray->setSize(aPoles.Size());
-        aKnotsArray->setSize(aPoles.Size());
-        aMultsArray->setSize(aPoles.Size());
         aPolesArray->setSize(aPoles.Size());
 
         aDegreeAttr->setValue(aDegree);
         for (int i = 0; i < aPoles.Size(); ++i)
         {
           aPolesArray->setPnt(i, aPoles[i + 1].X(), aPoles[i + 1].Y());
+        }
+
+        for (int i = 0; i < aPoles.Size(); ++i)
+        {
           if (aWeights == NULL)
             aWeightsArray->setValue(i, 1);
           else
             aWeightsArray->setValue(i, aWeights->Value(i + 1));
-          aKnotsArray->setValue(i, aKnots.Value(i + 1));
-          aMultsArray->setValue(i, aMulti.Value(i + 1));
         }
+
+        aKnotsArray->setSize(aPoles.Size() - 2);
+        aKnotsArray->setValue(0, aKnots.Value(aKnots.Lower()));
+        aKnotsArray->setValue(aKnotsArray->size() - 1, aKnots.Value(aKnots.Upper()));
+        for (int i = 1; i < aKnotsArray->size() - 1; ++i)
+        {
+          aKnotsArray->setValue(i, (aKnots.Last() - aKnots.First()) / (aKnotsArray->size() - 1) * i);
+        }
+
+        aMultsArray->setSize(aPoles.Size() - 2);
+        aMultsArray->setValue(0, 4 /*aMulti.Value(aMulti.Lower())*/);
+        aMultsArray->setValue(aMultsArray->size() - 1, 4 /*aMulti.Value(aMulti.Upper())*/);
+        for (int i = 1; i < aMultsArray->size() - 1; ++i)
+        {
+          aMultsArray->setValue(i, 1 /*aMulti.Value(aMulti.Lower() + 1)*/);
+        }
+
+#ifdef _DEBUG
+        std::cout << "\n\nPoles\n";
+        for (int i = 0; i < aPolesArray->size(); ++i)
+        {
+          std::cout << "(" << aPolesArray->pnt(i)->x() << ", " << aPolesArray->pnt(i)->y() << ")," << std::endl;
+        }
+
+        std::cout << "\nWeight\n";
+        for (int i = 0; i < aWeightsArray->size(); ++i)
+        {
+          std::cout << aWeightsArray->value(i) << ", ";
+        }
+        std::cout << std::endl;
+
+        std::cout << "\nKnot:\n";
+        for (int i = 0; i < aKnotsArray->size(); ++i)
+        {
+          std::cout << aKnotsArray->value(i) << ", ";
+        }
+        std::cout << std::endl;
+
+        std::cout << "\nMulti:\n";
+        for (int i = 0; i < aMultsArray->size() - 1; ++i)
+        {
+          std::cout << aMultsArray->value(i) << ", ";
+        }
+        std::cout << aMultsArray->value(aMultsArray->size() - 1) << std::endl;
+#endif
+
         std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
           aBSplineFeature->attribute(SketchPlugin_BSpline::START_ID()))->setValue(aPolesArray->pnt(0));
         std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
@@ -221,7 +260,7 @@ std::list<TopoDS_Shape> DXFPlugin_ImportAlgoAPI::ConvertToSketchElements(const T
       {
         auto aCircle = anEdge->circle();
         auto aCenter = aCircle->center();
-        auto aCircleFeature = aSketch->addFeature(SketchPlugin_Circle::ID());
+        auto aCircleFeature = theSketch->addFeature(SketchPlugin_Circle::ID());
 
         std::shared_ptr<GeomDataAPI_Point2D> aStartAttr = std::dynamic_pointer_cast<
           GeomDataAPI_Point2D>(aCircleFeature->attribute(SketchPlugin_Circle::CENTER_ID()));
@@ -238,7 +277,7 @@ std::list<TopoDS_Shape> DXFPlugin_ImportAlgoAPI::ConvertToSketchElements(const T
         auto aMinorR = anEllipse->minorRadius();
         auto aCenter = anEllipse->center();
 
-        auto anEllipseFeature = aSketch->addFeature(SketchPlugin_Ellipse::ID());
+        auto anEllipseFeature = theSketch->addFeature(SketchPlugin_Ellipse::ID());
 
         std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr = std::dynamic_pointer_cast<
           GeomDataAPI_Point2D>(anEllipseFeature->attribute(SketchPlugin_Ellipse::CENTER_ID()));
@@ -259,7 +298,7 @@ std::list<TopoDS_Shape> DXFPlugin_ImportAlgoAPI::ConvertToSketchElements(const T
         auto aStartP = anEdge->firstPoint();
         auto anEndP = anEdge->lastPoint();
 
-        auto aLineFeature = aSketch->addFeature(SketchPlugin_Line::ID());
+        auto aLineFeature = theSketch->addFeature(SketchPlugin_Line::ID());
 
         std::shared_ptr<GeomDataAPI_Point2D> aStartAttr = std::dynamic_pointer_cast<
           GeomDataAPI_Point2D>(aLineFeature->attribute(SketchPlugin_Line::START_ID()));
index 548bff84bbd470a14b95de5e558079ddf02569d9..f8023f8b41bf2cc467d5f24d961491c6aa23cbba 100644 (file)
@@ -25,6 +25,7 @@
 #include <GeomAPI_Shape.h>
 #include <TopoDS_Shape.hxx>
 
+class ModelAPI_CompositeFeature;
 /**
  * \class DXFPlugin_ImportAlgoAPI
  * \ingroup DataAlgo
@@ -53,7 +54,7 @@ public:
   /// Get shape color
   std::vector<int> color(const GeomShapePtr theShape) const;
 
-  std::list<TopoDS_Shape> ConvertToSketchElements(const TopoDS_Shape theShape) const;
+  std::list<TopoDS_Shape> ConvertToSketchElements(const TopoDS_Shape theShape, std::shared_ptr<ModelAPI_CompositeFeature> theSketch) const;
 
 private:
   Handle(TDocStd_Document) myDoc; ///< Handle to the document for transferring
index c75d721f1f962254d1c5ec9f80e7113280110ced..0db518089e98eae0e54f01f1f75852ee499a843d 100644 (file)
           <radio id="Sketch"
                  title="Sketch"
                  tooltip="Import all as sketch.">
-            <boolvalue id="attach_to_sketch"
-                       label="Join to sketch"
-                       default="false"
-                       tooltip="Join to sketch"
-                       change_visual_attributes="true"/>
-            <shape_selector id="sktech_to_join"
-                            label="Sketch"
-                            tooltip="Select an edge for direction"
-                            shape_types="compound"
-                            geometrical_selection="true">
-            </shape_selector>
+            <optionalbox id="attach_to_sketch" title="Join to sketch">
+              <shape_selector id="sktech_to_join"
+                              label="Selected sketch"
+                              tooltip="Sketch to import"
+                              shape_types="compound"
+                              geometrical_selection="true">
+              </shape_selector>
+            </optionalbox>
           </radio>
         </radiobox>
       </feature>
index dd9746a539d457fa3f3f5ad9b7cdc8e44c327b7f..9b6af677b5a18e0588416a76d304e5c22bd90b81 100644 (file)
@@ -247,9 +247,9 @@ void ModuleBase_IModule::loadProprietaryPlugins()
     if (!ModelAPI_Session::get()->checkLicense(*itP))
       // Check if exist OCCLICENSE_FILE env
       if (!getenv("OCCLICENSE_FILE"))
-        Events_InfoMessage(*itP, "Environment variable %1 for %2 plugin is not set!").arg("OCCLICENSE_FILE").arg(*itP).send();
+        std::cout << "Environment variable " << "OCCLICENSE_FILE" << " for " << *itP << " plugin is not set!" << std::endl;
       else
-        Events_InfoMessage(*itP, "License of %1 plugin is not valid or not exist!").arg(*itP).send();
+        std::cout << "License of " << *itP << " plugin is not valid or not exist!" << std::endl;
   }
 }