1 // Copyright (C) 2014-2022 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include <GeomAlgoAPI_STEPExport.h>
22 #include "GeomAlgoAPI_Tools.h"
24 #include <GeomAPI_Shape.h>
25 #include <ModelAPI_ResultBody.h>
26 #include <ModelAPI_AttributeIntArray.h>
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>
37 // a structure to manage step document exported attributes
38 struct GeomAlgoAPI_STEPAttributes {
39 bool myHasColor; ///< true if color is defined
40 Quantity_Color myColor;
42 Handle(XCAFDoc_ShapeTool) myShapeTool;
43 Handle(XCAFDoc_ColorTool) myColorTool;
45 GeomAlgoAPI_STEPAttributes(const TDF_Label& theMain) : myHasColor(false)
47 myShapeTool = XCAFDoc_DocumentTool::ShapeTool(theMain);
48 myShapeTool->SetAutoNaming(Standard_False);
49 myColorTool = XCAFDoc_DocumentTool::ColorTool(theMain);
53 static TDF_Label exportShape(const GeomShapePtr theShape, GeomAlgoAPI_STEPAttributes& theAttrs,
54 const GeomShapePtr theFatherShape, TDF_Label& theFaterID) {
55 TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
57 if (!theFaterID.IsNull()) { // make sub-component of father's assembly
59 if (theFatherShape.get())
60 aFatherTrsf = theFatherShape->implPtr<TopoDS_Shape>()->Location().Transformation();
61 TopLoc_Location aLocation = aFatherTrsf.Inverted() * aShape.Location();
62 static const TopLoc_Location anEmptyLoc;
63 aShape.Location(anEmptyLoc);
64 aShapeLab = theAttrs.myShapeTool->AddShape(aShape, Standard_False);
65 TDF_Label aRetLabel = theAttrs.myShapeTool->AddComponent(theFaterID, aShapeLab, aLocation);
66 if (!aRetLabel.IsNull())
67 aRetLabel.ForgetAttribute(TDataStd_Name::GetID());
69 else { // make a single shape
70 aShapeLab = theAttrs.myShapeTool->AddShape(aShape, Standard_False);
71 TDF_Label aRefShapeLab;
72 if (theAttrs.myShapeTool->GetReferredShape(aShapeLab, aRefShapeLab))
73 aShapeLab = aRefShapeLab;
75 TDataStd_Name::Set(aShapeLab, TCollection_ExtendedString(theAttrs.myName.c_str()));
76 if (theAttrs.myHasColor) {
77 TDF_Label aColorLab = theAttrs.myColorTool->AddColor(theAttrs.myColor);
78 theAttrs.myColorTool->SetColor(aShapeLab, aColorLab, XCAFDoc_ColorGen);
83 /// Returns the attributes of the result: names and color
84 static void getAttributes(const ResultPtr& theResult, GeomAlgoAPI_STEPAttributes& theAttrs)
86 theAttrs.myName = theResult->data()->name();
87 AttributeIntArrayPtr aColorAttr = theResult->data()->intArray(ModelAPI_Result::COLOR_ID());
88 if (aColorAttr.get() && aColorAttr->size() > 2) {
89 theAttrs.myHasColor = true;
90 theAttrs.myColor.SetValues(aColorAttr->value(0) / 255., aColorAttr->value(1) / 255.,
91 aColorAttr->value(2) / 255., Quantity_TOC_RGB);
94 theAttrs.myHasColor = false;
97 /// Performs the whole result export: as an assembly or a single one, but with results attributes.
98 static void putResult(const ResultPtr& theResult, const GeomShapePtr theFatherShape,
99 TDF_Label& theFaterID, GeomAlgoAPI_STEPAttributes& theAttrs)
101 GeomShapePtr aShape = theResult->shape();
102 if (!aShape.get() || aShape->isNull())
104 ResultBodyPtr aBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(theResult);
105 if (aBody.get() && aBody->numberOfSubs()) { // make an assembly
106 getAttributes(theResult, theAttrs);
107 TDF_Label aBodyID = exportShape(aShape, theAttrs, theFatherShape, theFaterID);
108 int aNumSubs = aBody->numberOfSubs();
109 for (int a = 0; a < aNumSubs; a++) {
110 ResultBodyPtr aSub = aBody->subResult(a);
111 if (!aSub->isDisabled())
112 putResult(aSub, aShape, aBodyID, theAttrs);
115 else { // a simple shape-body
116 getAttributes(theResult, theAttrs);
117 exportShape(aShape, theAttrs, theFatherShape, theFaterID);
121 bool STEPExport(const std::string& theFileName,
122 const std::list<std::shared_ptr<GeomAPI_Shape> >& theShapes,
123 const std::list<std::shared_ptr<ModelAPI_Result> >& theResults,
124 std::string& theError)
127 // prepare XCAF document to store the whole data structure there
128 Handle(TDocStd_Document) aDoc = new TDocStd_Document("BinXCAF");
130 GeomAlgoAPI_STEPAttributes anAttrs(aDoc->Main());
132 std::list<std::shared_ptr<GeomAPI_Shape> >::const_iterator aShape = theShapes.cbegin();
133 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aResult = theResults.cbegin();
134 for (; aShape != theShapes.cend(); aShape++, aResult++) {
136 if (aResult->get() && !(*aShape)->isSame((*aResult)->shape()))
137 { // simple sub-shape
138 getAttributes(*aResult, anAttrs);
139 exportShape(*aShape, anAttrs, GeomShapePtr(), aNullLab);
141 else { // whole result selection
142 putResult(*aResult, GeomShapePtr(), aNullLab, anAttrs);
145 // store the XCAF document to STEP file
147 GeomAlgoAPI_Tools::Localizer aLocalizer; // Set "C" numeric locale to save numbers correctly
148 STEPCAFControl_Writer aWriter;
149 Interface_Static::SetCVal("xstep.cascade.unit", "M");
150 Interface_Static::SetIVal("write.step.nonmanifold", 0); // 1 don't allow to export assemly tree
151 Interface_Static::SetCVal("write.step.unit", "M");
153 auto aStatus = aWriter.Transfer(aDoc, STEPControl_AsIs);
154 if (aStatus == IFSelect_RetDone)
155 aStatus = aWriter.Write(theFileName.c_str());
156 if (aStatus != IFSelect_RetDone)
157 theError = "STEP Export failed";
159 catch (Standard_Failure exception) {
160 theError = "Exception catched in STEPExport";
162 return theError.empty();