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 #include <Basics_OCCTVersion.hxx>
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;
44 Handle(XCAFDoc_ShapeTool) myShapeTool;
45 Handle(XCAFDoc_ColorTool) myColorTool;
47 GeomAlgoAPI_STEPAttributes(const TDF_Label& theMain) : myHasColor(false)
49 myShapeTool = XCAFDoc_DocumentTool::ShapeTool(theMain);
50 myShapeTool->SetAutoNaming(Standard_False);
51 myColorTool = XCAFDoc_DocumentTool::ColorTool(theMain);
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>();
59 if (!theFaterID.IsNull()) { // make sub-component of father's assembly
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());
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;
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);
85 /// Returns the attributes of the result: names and color
86 static void getAttributes(const ResultPtr& theResult, GeomAlgoAPI_STEPAttributes& theAttrs)
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);
96 theAttrs.myHasColor = false;
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)
103 GeomShapePtr aShape = theResult->shape();
104 if (!aShape.get() || aShape->isNull())
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);
117 else { // a simple shape-body
118 getAttributes(theResult, theAttrs);
119 exportShape(aShape, theAttrs, theFatherShape, theFaterID);
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)
129 // prepare XCAF document to store the whole data structure there
130 Handle(TDocStd_Document) aDoc = new TDocStd_Document("BinXCAF");
132 GeomAlgoAPI_STEPAttributes anAttrs(aDoc->Main());
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++) {
138 if (aResult->get() && !(*aShape)->isSame((*aResult)->shape()))
139 { // simple sub-shape
140 getAttributes(*aResult, anAttrs);
141 exportShape(*aShape, anAttrs, GeomShapePtr(), aNullLab);
143 else { // whole result selection
144 putResult(*aResult, GeomShapePtr(), aNullLab, anAttrs);
147 // store the XCAF document to STEP file
149 GeomAlgoAPI_Tools::Localizer aLocalizer; // Set "C" numeric locale to save numbers correctly
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");
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;
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";
170 catch (Standard_Failure&) {
171 theError = "Exception catched in STEPExport";
173 return theError.empty();