]> OCCT Git - occt-copy.git/commitdiff
0026302: New functionality. Converting the assembly to compound. CR26302
authorink <ink@opencascade.com>
Wed, 19 Aug 2015 09:13:17 +0000 (12:13 +0300)
committerink <ink@opencascade.com>
Thu, 12 Nov 2015 11:30:01 +0000 (14:30 +0300)
Added new functionality for converting the assembly to compaund.
Added new drw command XCompact.
Changed DumpShape and DumpAssembly
Added test.

src/XCAFDoc/XCAFDoc_Editor.cxx
src/XCAFDoc/XCAFDoc_Editor.hxx
src/XCAFDoc/XCAFDoc_ShapeTool.cxx
src/XCAFDoc/XCAFDoc_ShapeTool.hxx
src/XDEDRAW/XDEDRAW_Common.cxx
tests/bugs/xde/bug26302 [new file with mode: 0644]

index 89ce9da326326872714b6b7a0e8e995b136a62e5..cd1f96cbd565a20205b88e64dbd2f5efb3ce17ea 100644 (file)
@@ -31,6 +31,7 @@
 #include <XCAFDoc_ShapeMapTool.hxx>
 #include <XCAFDoc_Location.hxx>
 #include <TNaming_NamedShape.hxx>
+#include <BRep_Builder.hxx>
 
 //=======================================================================
 //function : Expand
@@ -213,4 +214,137 @@ Standard_Boolean XCAFDoc_Editor::setParams (const TDF_Label& Doc, const TDF_Labe
     TDataStd_Name::Set(Label, TCollection_ExtendedString(aName));
   }
   return Standard_True;
-}
\ No newline at end of file
+}
+
+//=======================================================================
+//function : Compress
+//purpose  : Convert assembly to compound
+//=======================================================================
+
+Standard_Boolean XCAFDoc_Editor::Compact (const TDF_Label& Doc, const TDF_Label& Shape)
+{
+  if(Doc.IsNull() || Shape.IsNull())
+    return Standard_False;
+
+  Handle(XCAFDoc_ShapeTool) aShapeTool = XCAFDoc_DocumentTool::ShapeTool(Doc);
+
+  TopoDS_Shape aS = aShapeTool->GetShape(Shape);
+  if (!aS.IsNull() && aS.ShapeType() == TopAbs_COMPOUND && aShapeTool->IsAssembly(Shape))
+  {
+    Shape.ForgetAttribute(XCAFDoc::AssemblyGUID());
+
+    TopoDS_Compound Comp;//new compound for Shape label
+    BRep_Builder B;
+    B.MakeCompound(Comp);
+
+    //convert assembly to compound 
+    TDF_ChildIterator anIter(Shape);
+    for(; anIter.More(); anIter.Next())
+    {
+      TDF_Label aChild = anIter.Value();
+      TopoDS_Shape aChildShape = aShapeTool->GetShape(aChild);
+
+      TDF_Label aPart;
+      if(aShapeTool->GetReferredShape(aChild, aPart))
+      {
+        if (aChildShape.ShapeType() == TopAbs_COMPOUND && aShapeTool->IsAssembly(aPart))
+        {
+          //iterate next level if it needed
+          Compact(Doc, aPart);
+        }
+        //get location
+        Handle(XCAFDoc_Location) LocationAttribute;
+        aChild.FindAttribute(XCAFDoc_Location::GetID(), LocationAttribute);
+
+        TopLoc_Location aLoc;
+        if(!LocationAttribute.IsNull())
+        {
+          aLoc = LocationAttribute->Get();
+        }
+
+        if(aChildShape.ShapeType() != TopAbs_COMPOUND)
+        {
+          TDF_LabelSequence aLayers;
+          TDF_LabelSequence aColors;
+          Handle(TDataStd_Name) aName;
+          getParams(Doc, aPart, aColors, aLayers, aName);
+
+          aChild.ForgetAllAttributes(Standard_False);
+          //move shape
+          aShapeTool->SetShape(aChild, aChildShape.Located(aLoc), Standard_False);
+
+          aChildShape.Free(Standard_True);
+          B.Add(Comp, aChildShape.Located(aLoc));
+
+          setParams(Doc, aChild, aColors, aLayers, aName);
+        }
+        else
+        {
+          aChild.ForgetAllAttributes(Standard_False);
+        }
+
+        //move subshapes
+        TDF_LabelSequence aSub;
+        aShapeTool->GetSubShapes(aPart,aSub);
+        for(Standard_Integer i = 1; i <= aSub.Length(); i++)
+        {
+          TopoDS_Shape aShapeSub = aShapeTool->GetShape(aSub(i));
+
+          TDF_LabelSequence aLayers;
+          TDF_LabelSequence aColors;
+          Handle(TDataStd_Name) aName;
+          getParams(Doc, aSub(i), aColors, aLayers, aName);
+
+          aSub(i).ForgetAllAttributes(Standard_False);
+
+          TDF_TagSource aTag;
+          TDF_Label aSubC = aTag.NewChild(Shape);
+
+          TopLoc_Location loc = aLoc;
+          if(aChildShape.ShapeType() == TopAbs_COMPOUND)
+          {
+            loc = aShapeSub.Location().Multiplied(aLoc);
+          }
+
+          //set shape
+          aShapeTool->SetShape(aSubC, aShapeSub.Located(loc), Standard_False);
+          aSubC.ForgetAttribute(XCAFDoc_ShapeMapTool::GetID() );
+          if(aChildShape.ShapeType() == TopAbs_COMPOUND)
+          {
+            aShapeSub.Free(Standard_True);
+            B.Add(Comp, aShapeSub.Located(loc));
+          }
+          setParams(Doc, aSubC, aColors, aLayers, aName);
+        }
+        //remove part
+        aPart.ForgetAllAttributes();
+      }
+    }
+    aShapeTool->SetShape(Shape, Comp);
+    return Standard_True;
+  }
+  return Standard_False;
+}
+
+//=======================================================================
+//function : Compress
+//purpose  : Convert all assambly in Doc to compound
+//=======================================================================
+
+Standard_Boolean XCAFDoc_Editor::Compact (const TDF_Label& Doc)
+{
+  Standard_Boolean result = Standard_False;
+  TDF_LabelSequence aLabels;
+  Handle(XCAFDoc_ShapeTool) aShapeTool = XCAFDoc_DocumentTool::ShapeTool(Doc);
+  aShapeTool->GetFreeShapes(aLabels);
+  for(Standard_Integer i = 1; i <= aLabels.Length(); i++)
+  {
+    TopoDS_Shape aS = aShapeTool->GetShape(aLabels(i));
+    if (!aS.IsNull() && aS.ShapeType() == TopAbs_COMPOUND && aShapeTool->IsAssembly(aLabels(i)))
+      if (Compact(Doc, aLabels(i)))
+      {
+        result = Standard_True;
+      }
+  }
+  return result;
+}
index 341faeed04ee6e470ab17948e928df5067884977..796cd30c87702ab40fc55bcd21aa747fae9afef4 100644 (file)
@@ -41,6 +41,11 @@ public:
   //! Convert all compounds in Doc to assembly
   Standard_EXPORT static   Standard_Boolean Expand (const TDF_Label& Doc, const Standard_Boolean recursively = Standard_True) ;
 
+  //! Convert Shape(assembly) to compound
+  Standard_EXPORT static   Standard_Boolean Compact (const TDF_Label& Doc, const TDF_Label& Shape) ;
+  
+  //! Convert all assembly in Doc to compounds
+  Standard_EXPORT static   Standard_Boolean Compact (const TDF_Label& Doc) ;
 
 
 
index 0ce4c13fe7e44a593ab6e63cd124c9b29ad5b381..b3b5e110cffa09c0f84b179807cd86b12934c803 100644 (file)
@@ -399,9 +399,10 @@ TDF_Label XCAFDoc_ShapeTool::NewShape() const
 //purpose  : 
 //=======================================================================
 
-void XCAFDoc_ShapeTool::SetShape (const TDF_Label& L, const TopoDS_Shape& S)
+void XCAFDoc_ShapeTool::SetShape (const TDF_Label& L, const TopoDS_Shape& S,
+                                  const Standard_Boolean theProtection)
 { 
-  if(IsReference(L) || !IsTopLevel(L) || /*IsAssembly(L) ||*/ !S.Location().IsIdentity())
+  if(IsReference(L) || ((!IsTopLevel(L) ||!S.Location().IsIdentity()) && theProtection))
     return;
 
   TDF_LabelSequence aSubShapes;
@@ -1936,4 +1937,4 @@ void XCAFDoc_ShapeTool::makeSubShape (const TDF_Label& Part, const TopoDS_Shape&
     }
     makeSubShape(Part, aChildShape);
   }
-}
\ No newline at end of file
+}
index 48ebcc3c16f93d380820fc38a004817266eb8bd3..c3fac4d72f1b7465fa24421d01fa9c9fb4eccc68 100644 (file)
@@ -203,12 +203,13 @@ public:
   Standard_EXPORT TDF_Label NewShape() const;
   
   //! Sets representation (TopoDS_Shape) for top-level shape.
+  //! If you set theProtection = Standard_False you can set shape for subshapes(not top-level)
   //! If S has location(location.IsIdentity() is false),
   //! command will be skipped. Sub-shapes of S which is
   //! subshape of old shape, will be stored ( all atributes will be stored).
   //! If a sub-label of L is not a sub-shape of the new shape,
   //! it will be removed.
-  Standard_EXPORT void SetShape (const TDF_Label& L, const TopoDS_Shape& S);
+  Standard_EXPORT void SetShape (const TDF_Label& L, const TopoDS_Shape& S, const Standard_Boolean theProtection = Standard_True);
   
   //! Adds a new top-level (creates and returns a new label)
   //! If makeAssembly is True, treats TopAbs_COMPOUND shapes
index ce5535748575bafb263ff559ac299ff00f691c54..5c0d7514ab8a86cc05e2f1b862c5157da5a962c5 100644 (file)
@@ -519,6 +519,51 @@ static Standard_Integer Expand (Draw_Interpretor& di, Standard_Integer argc, con
   return 0;
 }
 
+
+static Standard_Integer Compact (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
+{
+  if (argc < 2) {
+    di<<"Use: "<<argv[0]<<" Doc or Doc label1 label2 ... or Doc shape1 shape2 ..."<<"\n";
+    return 1;
+  }
+  Handle(TDocStd_Document) Doc;   
+  DDocStd::GetDocument(argv[1], Doc);
+  if ( Doc.IsNull() ) { di << argv[1] << " is not a document" << "\n"; return 1; }
+
+  Handle(XCAFDoc_ShapeTool) aShapeTool = XCAFDoc_DocumentTool::ShapeTool(Doc->Main());
+
+  if (argc == 2)
+  {
+    if(!XCAFDoc_Editor::Compact(Doc->Main())){
+      di << "The shape is  not assembly" << "\n";
+      return 1;
+    }
+  }
+  else 
+  {
+    for (Standard_Integer i = 2; i < argc; i++)
+    {
+      TDF_Label aLabel;
+      TDF_Tool::Label(Doc->GetData(), argv[i], aLabel);
+      if(aLabel.IsNull()){
+        TopoDS_Shape aShape;
+        aShape = DBRep::Get(argv[i]);
+        aLabel = aShapeTool->FindShape(aShape);
+      }
+
+      if (!aLabel.IsNull()){
+        if(!XCAFDoc_Editor::Compact(Doc->Main(), aLabel)){
+          di << "The shape is not assembly" << "\n";
+          return 1;
+        }
+      }
+      else
+      { di << argv[i] << " is not a shape" << "\n"; return 1; }
+    }
+  }
+  return 0;
+}
+
 void XDEDRAW_Common::InitCommands(Draw_Interpretor& di) {
 
   static Standard_Boolean initactor = Standard_False;
@@ -539,4 +584,6 @@ void XDEDRAW_Common::InitCommands(Draw_Interpretor& di) {
   di.Add("XExpand", "XExpand Doc recursively(0/1) or XExpand Doc recursively(0/1) label1 abel2 ..."  
           "or XExpand Doc recursively(0/1) shape1 shape2 ...",__FILE__, Expand, g);
 
+  di.Add("XCompact", "XCompact Doc or XCompact Doc label1 abel2 ..."  
+          "or XCompact Doc shape1 shape2 ...",__FILE__, Compact, g);
 }
diff --git a/tests/bugs/xde/bug26302 b/tests/bugs/xde/bug26302
new file mode 100644 (file)
index 0000000..224ec49
--- /dev/null
@@ -0,0 +1,83 @@
+puts "========"
+puts "OCC26302"
+puts "========"
+puts ""
+#######################################################################
+# Convert assembly to compound
+#######################################################################
+
+pload ALL
+box b1 0 0 0 10 10 10 
+box b2 10 0 0 10 10 10 
+box b3 20 0 0 10 10 10 
+explode b1
+explode b2
+explode b3
+explode b1_1
+explode b2_1
+explode b3_1
+compound b1 b2 c1
+compound c1 b3 c2
+NewDocument D
+XAddShape D c2 1
+XSetColor D b1 1 0 0 
+XSetColor D b2 0 1 0 
+XSetColor D b3 0 0 1 
+XSetColor D b1_1_4 1 1 0 
+XSetColor D b2_1_4 0 1 1 
+XSetColor D b3_1_4 1 0 1
+XSetColor D b1_1_3 1 0 1 
+XSetColor D b2_1_3 1 1 0 
+XSetColor D b3_1_3 0 1 1
+XCompact D 
+
+if { [regexp "PART COMPOUND 0:1:1:1 \"ASSEMBLY\"" [Xdump D]] != 1 } {
+ puts "ERROR: Structure of document is wrong.1"
+} else {
+  if { [regexp "BLUE" [XGetShapeColor D 0:1:1:1:2]] != 1 } {
+    puts "ERROR: Structure of document is wrong."
+  } else {
+    if { [regexp "RED" [XGetShapeColor D 0:1:1:1:3]] != 1 } {
+      puts "ERROR: Structure of document is wrong."
+    } else {
+      if { [regexp "GREEN" [XGetShapeColor D 0:1:1:1:4]] != 1 } {
+        puts "ERROR: Structure of document is wrong."
+      } else {
+        if { [regexp "YELLOW" [XGetShapeColor D 0:1:1:1:5]] != 1 } {
+          puts "ERROR: Structure of document is wrong."
+        } else {
+          if { [regexp "MAGENTA" [XGetShapeColor D 0:1:1:1:6]] != 1 } {
+            puts "ERROR: Structure of document is wrong."
+          } else {
+            if { [regexp "CYAN" [XGetShapeColor D 0:1:1:1:7]] != 1 } {
+              puts "ERROR: Structure of document is wrong."
+            } else {
+              if { [regexp "YELLOW" [XGetShapeColor D 0:1:1:1:8]] != 1 } {
+                puts "ERROR: Structure of document is wrong."
+              } else {
+                if { [regexp "MAGENTA" [XGetShapeColor D 0:1:1:1:9]] != 1 } {
+                  puts "ERROR: Structure of document is wrong."
+                } else {
+                  if { [regexp "CYAN" [XGetShapeColor D 0:1:1:1:10]] != 1 } {
+                    puts "ERROR: Structure of document is wrong."
+                  }
+                }
+              }
+            }
+          }
+        }
+      }
+    }
+  }
+}
+
+
+
+
+
+
+
+
+
+
+