Salome HOME
6dcea0b926494849e3da42a5aa6f87549148c1b0
[modules/shaper.git] / src / GeomAlgoAPI / GeomAlgoAPI_STEPExport.cpp
1 // Copyright (C) 2014-2023  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include <GeomAlgoAPI_STEPExport.h>
21
22 #include "GeomAlgoAPI_Tools.h"
23
24 #include <GeomAPI_Shape.h>
25 #include <ModelAPI_ResultBody.h>
26 #include <ModelAPI_AttributeIntArray.h>
27
28 #include <TDocStd_Document.hxx>
29 #include <TDataStd_Name.hxx>
30 #include <XCAFDoc_DocumentTool.hxx>
31 #include <XCAFDoc_ShapeTool.hxx>
32 #include <XCAFDoc_ColorTool.hxx>
33 #include <STEPCAFControl_Writer.hxx>
34 #include <Interface_Static.hxx>
35 #include <Quantity_Color.hxx>
36
37 #include <Basics_OCCTVersion.hxx>
38
39 // a structure to manage step document exported attributes
40 struct GeomAlgoAPI_STEPAttributes {
41   bool myHasColor; ///< true if color is defined
42   Quantity_Color myColor;
43   std::wstring myName;
44   Handle(XCAFDoc_ShapeTool) myShapeTool;
45   Handle(XCAFDoc_ColorTool) myColorTool;
46
47   GeomAlgoAPI_STEPAttributes(const TDF_Label& theMain) : myHasColor(false)
48   {
49     myShapeTool = XCAFDoc_DocumentTool::ShapeTool(theMain);
50     myShapeTool->SetAutoNaming(Standard_False);
51     myColorTool = XCAFDoc_DocumentTool::ColorTool(theMain);
52   }
53 };
54
55 static TDF_Label exportShape(const GeomShapePtr theShape, GeomAlgoAPI_STEPAttributes& theAttrs,
56   const GeomShapePtr theFatherShape, TDF_Label& theFaterID) {
57   TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
58   TDF_Label aShapeLab;
59   if (!theFaterID.IsNull()) { // make sub-component of father's assembly
60     gp_Trsf aFatherTrsf;
61     if (theFatherShape.get())
62       aFatherTrsf = theFatherShape->implPtr<TopoDS_Shape>()->Location().Transformation();
63     TopLoc_Location aLocation = aFatherTrsf.Inverted() * aShape.Location();
64     static const TopLoc_Location anEmptyLoc;
65     aShape.Location(anEmptyLoc);
66     aShapeLab = theAttrs.myShapeTool->AddShape(aShape, Standard_False);
67     TDF_Label aRetLabel = theAttrs.myShapeTool->AddComponent(theFaterID, aShapeLab, aLocation);
68     if (!aRetLabel.IsNull())
69       aRetLabel.ForgetAttribute(TDataStd_Name::GetID());
70   }
71   else { // make a single shape
72     aShapeLab = theAttrs.myShapeTool->AddShape(aShape, Standard_False);
73     TDF_Label aRefShapeLab;
74     if (theAttrs.myShapeTool->GetReferredShape(aShapeLab, aRefShapeLab))
75       aShapeLab = aRefShapeLab;
76   }
77   TDataStd_Name::Set(aShapeLab, TCollection_ExtendedString(theAttrs.myName.c_str()));
78   if (theAttrs.myHasColor) {
79     TDF_Label aColorLab = theAttrs.myColorTool->AddColor(theAttrs.myColor);
80     theAttrs.myColorTool->SetColor(aShapeLab, aColorLab, XCAFDoc_ColorGen);
81   }
82   return aShapeLab;
83 }
84
85 /// Returns the attributes of the result: names and color
86 static void getAttributes(const ResultPtr& theResult, GeomAlgoAPI_STEPAttributes& theAttrs)
87 {
88   theAttrs.myName = theResult->data()->name();
89   AttributeIntArrayPtr aColorAttr = theResult->data()->intArray(ModelAPI_Result::COLOR_ID());
90   if (aColorAttr.get() && aColorAttr->size() > 2) {
91     theAttrs.myHasColor = true;
92     theAttrs.myColor.SetValues(aColorAttr->value(0) / 255., aColorAttr->value(1) / 255.,
93                                aColorAttr->value(2) / 255., Quantity_TOC_RGB);
94   }
95   else
96     theAttrs.myHasColor = false;
97 }
98
99 /// Performs the whole result export: as an assembly or a single one, but with results attributes.
100 static void putResult(const ResultPtr& theResult, const GeomShapePtr theFatherShape,
101   TDF_Label& theFaterID, GeomAlgoAPI_STEPAttributes& theAttrs)
102 {
103   GeomShapePtr aShape = theResult->shape();
104   if (!aShape.get() || aShape->isNull())
105     return;
106   ResultBodyPtr aBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(theResult);
107   if (aBody.get() && aBody->numberOfSubs()) { // make an assembly
108     getAttributes(theResult, theAttrs);
109     TDF_Label aBodyID = exportShape(aShape, theAttrs, theFatherShape, theFaterID);
110     int aNumSubs = aBody->numberOfSubs();
111     for (int a = 0; a < aNumSubs; a++) {
112       ResultBodyPtr aSub = aBody->subResult(a);
113       if (!aSub->isDisabled())
114         putResult(aSub, aShape, aBodyID, theAttrs);
115     }
116   }
117   else { // a simple shape-body
118     getAttributes(theResult, theAttrs);
119     exportShape(aShape, theAttrs, theFatherShape, theFaterID);
120   }
121 }
122
123 bool STEPExport(const std::string& theFileName,
124                 const std::list<std::shared_ptr<GeomAPI_Shape> >& theShapes,
125                 const std::list<std::shared_ptr<ModelAPI_Result> >& theResults,
126                 std::string& theError)
127 {
128   theError = "";
129   // prepare XCAF document to store the whole data structure there
130   Handle(TDocStd_Document) aDoc = new TDocStd_Document("BinXCAF");
131
132   GeomAlgoAPI_STEPAttributes anAttrs(aDoc->Main());
133
134   std::list<std::shared_ptr<GeomAPI_Shape> >::const_iterator aShape = theShapes.cbegin();
135   std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aResult = theResults.cbegin();
136   for (; aShape != theShapes.cend(); aShape++, aResult++) {
137     TDF_Label aNullLab;
138     if (aResult->get() && !(*aShape)->isSame((*aResult)->shape()))
139     { // simple sub-shape
140       getAttributes(*aResult, anAttrs);
141       exportShape(*aShape, anAttrs, GeomShapePtr(), aNullLab);
142     }
143     else { // whole result selection
144       putResult(*aResult, GeomShapePtr(), aNullLab, anAttrs);
145     }
146   }
147   // store the XCAF document to STEP file
148   try {
149     GeomAlgoAPI_Tools::Localizer aLocalizer; // Set "C" numeric locale to save numbers correctly
150
151 #if OCC_VERSION_LARGE < 0x07070000
152     STEPCAFControl_Writer aWriter;
153     Interface_Static::SetCVal("xstep.cascade.unit", "M");
154     Interface_Static::SetIVal("write.step.nonmanifold", 0); // 1 don't allow to export assemly tree
155     Interface_Static::SetCVal("write.step.unit", "M");
156 #else
157     STEPCAFControl_Writer aWriterTmp;
158     Interface_Static::SetCVal("xstep.cascade.unit", "M");
159     Interface_Static::SetIVal("write.step.nonmanifold", 0); // 1 don't allow to export assemly tree
160     Interface_Static::SetCVal("write.step.unit", "M");
161     STEPCAFControl_Writer aWriter;
162 #endif
163
164     auto aStatus = aWriter.Transfer(aDoc, STEPControl_AsIs);
165     if (aStatus == IFSelect_RetDone)
166       aStatus = aWriter.Write(theFileName.c_str());
167     if (aStatus != IFSelect_RetDone)
168       theError = "STEP Export failed";
169   }
170   catch (Standard_Failure&) {
171     theError = "Exception catched in STEPExport";
172   }
173   return theError.empty();
174 }