Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-09-17 09:22:04

0001 // Created on: 2000-08-15
0002 // Created by: Andrey BETENEV
0003 // Copyright (c) 2000-2014 OPEN CASCADE SAS
0004 //
0005 // This file is part of Open CASCADE Technology software library.
0006 //
0007 // This library is free software; you can redistribute it and/or modify it under
0008 // the terms of the GNU Lesser General Public License version 2.1 as published
0009 // by the Free Software Foundation, with special exception defined in the file
0010 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
0011 // distribution for complete text of the license and disclaimer of any warranty.
0012 //
0013 // Alternatively, this file may be used under the terms of Open CASCADE
0014 // commercial license or contractual agreement.
0015 
0016 #ifndef _STEPCAFControl_Writer_HeaderFile
0017 #define _STEPCAFControl_Writer_HeaderFile
0018 
0019 #include <TopoDS_Shape.hxx>
0020 #include <Standard_Transient.hxx>
0021 #include <TopTools_ShapeMapHasher.hxx>
0022 #include <NCollection_DataMap.hxx>
0023 #include <NCollection_DynamicArray.hxx>
0024 #include <Standard_DefineAlloc.hxx>
0025 #include <Standard_Handle.hxx>
0026 #include <TDF_Label.hxx>
0027 #include <STEPCAFControl_ExternFile.hxx>
0028 #include <STEPControl_Writer.hxx>
0029 #include <StepAP242_GeometricItemSpecificUsage.hxx>
0030 #include <DESTEP_Parameters.hxx>
0031 #include <StepData_Factors.hxx>
0032 #include <StepDimTol_Datum.hxx>
0033 #include <StepDimTol_GeometricTolerance.hxx>
0034 #include <StepDimTol_DatumSystemOrReference.hxx>
0035 #include <NCollection_Array1.hxx>
0036 #include <NCollection_HArray1.hxx>
0037 #include <StepRepr_ProductDefinitionShape.hxx>
0038 #include <StepVisual_DraughtingModel.hxx>
0039 #include <StepVisual_PresentationStyleAssignment.hxx>
0040 #include <NCollection_Sequence.hxx>
0041 #include <NCollection_Map.hxx>
0042 #include <XCAFDimTolObjects_GeomToleranceObject.hxx>
0043 
0044 class XSControl_WorkSession;
0045 class TDocStd_Document;
0046 class STEPCAFControl_ExternFile;
0047 class TopoDS_Shape;
0048 class StepShape_ShapeDefinitionRepresentation;
0049 
0050 //! Provides a tool to write DECAF document to the
0051 //! STEP file. Besides transfer of shapes (including
0052 //! assemblies) provided by STEPControl, supports also
0053 //! colors and part names
0054 //!
0055 //! Also supports multifile writing
0056 class STEPCAFControl_Writer
0057 {
0058 public:
0059   DEFINE_STANDARD_ALLOC
0060 
0061 public:
0062   //! Creates a writer with an empty
0063   //! STEP model and sets ColorMode, LayerMode, NameMode and
0064   //! PropsMode to true.
0065   Standard_EXPORT STEPCAFControl_Writer();
0066 
0067   //! Creates a reader tool and attaches it to an already existing Session
0068   //! Clears the session if it was not yet set for STEP
0069   //! Clears the internal data structures
0070   Standard_EXPORT STEPCAFControl_Writer(const occ::handle<XSControl_WorkSession>& theWS,
0071                                         const bool theScratch = true);
0072 
0073   //! Clears the internal data structures and attaches to a new session
0074   //! Clears the session if it was not yet set for STEP
0075   Standard_EXPORT void Init(const occ::handle<XSControl_WorkSession>& theWS,
0076                             const bool                                theScratch = true);
0077 
0078   //! Writes all the produced models into file
0079   //! In case of multimodel with extern references,
0080   //! filename will be a name of root file, all other files
0081   //! have names of corresponding parts
0082   //! Provided for use like single-file writer
0083   Standard_EXPORT IFSelect_ReturnStatus Write(const char* const theFileName);
0084 
0085   //! Writes all the produced models into the stream.
0086   //! Provided for use like single-file writer
0087   Standard_EXPORT IFSelect_ReturnStatus WriteStream(std::ostream& theStream);
0088 
0089   //! Transfers a document (or single label) to a STEP model
0090   //! The mode of translation of shape is AsIs
0091   //! If multi is not null pointer, it switches to multifile
0092   //! mode (with external refs), and string pointed by <multi>
0093   //! gives prefix for names of extern files (can be empty string)
0094   //! Returns True if translation is OK
0095   Standard_EXPORT bool Transfer(const occ::handle<TDocStd_Document>& theDoc,
0096                                 const STEPControl_StepModelType      theMode    = STEPControl_AsIs,
0097                                 const char* const                    theIsMulti = nullptr,
0098                                 const Message_ProgressRange& theProgress = Message_ProgressRange());
0099 
0100   //! Transfers a document (or single label) to a STEP model
0101   //! This method uses if need to set parameters avoiding
0102   //! initialization from Interface_Static
0103   //! @param theParams  configuration parameters
0104   //! @param theMode    mode of translation of shape is AsIs
0105   //! @param theIsMulti if multi is not null pointer, it switches to multifile
0106   //!                   mode (with external refs), and string pointed by <multi>
0107   //!                   gives prefix for names of extern files (can be empty string)
0108   //! @param theProgress progress indicator
0109   //! Returns True if translation is OK
0110   Standard_EXPORT bool Transfer(const occ::handle<TDocStd_Document>& theDoc,
0111                                 const DESTEP_Parameters&             theParams,
0112                                 const STEPControl_StepModelType      theMode    = STEPControl_AsIs,
0113                                 const char* const                    theIsMulti = nullptr,
0114                                 const Message_ProgressRange& theProgress = Message_ProgressRange());
0115 
0116   //! Method to transfer part of the document specified by label
0117   Standard_EXPORT bool Transfer(const TDF_Label&                theLabel,
0118                                 const STEPControl_StepModelType theMode    = STEPControl_AsIs,
0119                                 const char* const               theIsMulti = nullptr,
0120                                 const Message_ProgressRange& theProgress = Message_ProgressRange());
0121 
0122   //! Method to transfer part of the document specified by label
0123   //! This method uses if need to set parameters avoiding
0124   //! initialization from Interface_Static
0125   Standard_EXPORT bool Transfer(const TDF_Label&                theLabel,
0126                                 const DESTEP_Parameters&        theParams,
0127                                 const STEPControl_StepModelType theMode    = STEPControl_AsIs,
0128                                 const char* const               theIsMulti = nullptr,
0129                                 const Message_ProgressRange& theProgress = Message_ProgressRange());
0130 
0131   //! Method to writing sequence of root assemblies
0132   //! or part of the file specified by use by one label
0133   Standard_EXPORT bool Transfer(const NCollection_Sequence<TDF_Label>& theLabelSeq,
0134                                 const STEPControl_StepModelType        theMode = STEPControl_AsIs,
0135                                 const char* const                      theIsMulti = nullptr,
0136                                 const Message_ProgressRange& theProgress = Message_ProgressRange());
0137 
0138   //! Method to writing sequence of root assemblies
0139   //! or part of the file specified by use by one label.
0140   //! This method is utilized if there's a need to set parameters avoiding
0141   //! initialization from Interface_Static
0142   Standard_EXPORT bool Transfer(const NCollection_Sequence<TDF_Label>& theLabelSeq,
0143                                 const DESTEP_Parameters&               theParams,
0144                                 const STEPControl_StepModelType        theMode = STEPControl_AsIs,
0145                                 const char* const                      theIsMulti = nullptr,
0146                                 const Message_ProgressRange& theProgress = Message_ProgressRange());
0147 
0148   Standard_EXPORT bool Perform(const occ::handle<TDocStd_Document>& theDoc,
0149                                const TCollection_AsciiString&       theFileName,
0150                                const Message_ProgressRange& theProgress = Message_ProgressRange());
0151 
0152   //! Transfers a document and writes it to a STEP file
0153   //! Returns True if translation is OK
0154   Standard_EXPORT bool Perform(const occ::handle<TDocStd_Document>& theDoc,
0155                                const char* const                    theFileName,
0156                                const Message_ProgressRange& theProgress = Message_ProgressRange());
0157 
0158   //! Transfers a document and writes it to a STEP file
0159   //! This method is utilized if there's a need to set parameters avoiding
0160   //! initialization from Interface_Static
0161   //! Returns True if translation is OK
0162   Standard_EXPORT bool Perform(const occ::handle<TDocStd_Document>& theDoc,
0163                                const char* const                    theFileName,
0164                                const DESTEP_Parameters&             theParams,
0165                                const Message_ProgressRange& theProgress = Message_ProgressRange());
0166 
0167   //! Returns data on external files
0168   //! Returns Null handle if no external files are read
0169   const NCollection_DataMap<TCollection_AsciiString, occ::handle<STEPCAFControl_ExternFile>>&
0170     ExternFiles() const
0171   {
0172     return myFiles;
0173   };
0174 
0175   //! Returns data on external file by its original label
0176   //! Returns False if no external file with given name is read
0177   Standard_EXPORT bool ExternFile(const TDF_Label&                        theLabel,
0178                                   occ::handle<STEPCAFControl_ExternFile>& theExtFile) const;
0179 
0180   //! Returns data on external file by its name
0181   //! Returns False if no external file with given name is read
0182   Standard_EXPORT bool ExternFile(const char* const                       theName,
0183                                   occ::handle<STEPCAFControl_ExternFile>& theExtFile) const;
0184 
0185   //! Returns basic reader for root file
0186   STEPControl_Writer& ChangeWriter() { return myWriter; }
0187 
0188   //! Returns basic reader as const
0189   const STEPControl_Writer& Writer() const { return myWriter; }
0190 
0191   //! Set ColorMode for indicate write Colors or not.
0192   void SetColorMode(const bool theColorMode) { myColorMode = theColorMode; }
0193 
0194   bool GetColorMode() const { return myColorMode; }
0195 
0196   //! Set NameMode for indicate write Name or not.
0197   void SetNameMode(const bool theNameMode) { myNameMode = theNameMode; }
0198 
0199   bool GetNameMode() const { return myNameMode; }
0200 
0201   //! Set LayerMode for indicate write Layers or not.
0202   void SetLayerMode(const bool theLayerMode) { myLayerMode = theLayerMode; }
0203 
0204   bool GetLayerMode() const { return myLayerMode; }
0205 
0206   //! PropsMode for indicate write Validation properties or not.
0207   void SetPropsMode(const bool thePropsMode) { myPropsMode = thePropsMode; }
0208 
0209   bool GetPropsMode() const { return myPropsMode; }
0210 
0211   //! Set MetadataMode for indicate write metadata or not.
0212   void SetMetadataMode(const bool theMetadataMode) { myMetadataMode = theMetadataMode; }
0213 
0214   bool GetMetadataMode() const { return myMetadataMode; }
0215 
0216   //! Set SHUO mode for indicate write SHUO or not.
0217   void SetSHUOMode(const bool theSHUOMode) { mySHUOMode = theSHUOMode; }
0218 
0219   bool GetSHUOMode() const { return mySHUOMode; }
0220 
0221   //! Set dimtolmode for indicate write D&GTs or not.
0222   void SetDimTolMode(const bool theDimTolMode) { myGDTMode = theDimTolMode; };
0223 
0224   bool GetDimTolMode() const { return myGDTMode; }
0225 
0226   //! Set flag for indicate write material or not.
0227   void SetMaterialMode(const bool theMaterialMode) { myMatMode = theMaterialMode; }
0228 
0229   bool GetMaterialMode() const { return myMatMode; }
0230 
0231   //! Set flag for indicate write visual material or not.
0232   void SetVisualMaterialMode(const bool theVisualMaterialMode)
0233   {
0234     myVisMatMode = theVisualMaterialMode;
0235   }
0236 
0237   bool GetVisualMaterialMode() const { return myVisMatMode; }
0238 
0239   //! Set clean duplicates flag.
0240   //! If set to True, duplicates will be removed from the model.
0241   //! @param theCleanDuplicates the flag to set.
0242   void SetCleanDuplicates(const bool theCleanDuplicates)
0243   {
0244     myIsCleanDuplicates = theCleanDuplicates;
0245   }
0246 
0247   //! Returns the flag indicating whether duplicates should be removed from the model.
0248   //! @return the flag indicating whether duplicates should be removed from the model.
0249   bool GetCleanDuplicates() const { return myIsCleanDuplicates; }
0250 
0251   //! Sets parameters for shape processing.
0252   //! @param theParameters the parameters for shape processing.
0253   Standard_EXPORT void SetShapeFixParameters(
0254     const XSAlgo_ShapeProcessor::ParameterMap& theParameters);
0255 
0256   //! Sets parameters for shape processing.
0257   //! Parameters are moved from the input map.
0258   //! @param theParameters the parameters for shape processing.
0259   Standard_EXPORT void SetShapeFixParameters(XSAlgo_ShapeProcessor::ParameterMap&& theParameters);
0260 
0261   //! Sets parameters for shape processing.
0262   //! Parameters from @p theParameters are copied to the internal map.
0263   //! Parameters from @p theAdditionalParameters are copied to the internal map
0264   //! if they are not present in @p theParameters.
0265   //! @param theParameters the parameters for shape processing.
0266   //! @param theAdditionalParameters the additional parameters for shape processing.
0267   Standard_EXPORT void SetShapeFixParameters(
0268     const DE_ShapeFixParameters&               theParameters,
0269     const XSAlgo_ShapeProcessor::ParameterMap& theAdditionalParameters = {});
0270 
0271   //! Returns parameters for shape processing that was set by SetParameters() method.
0272   //! @return the parameters for shape processing. Empty map if no parameters were set.
0273   Standard_EXPORT const XSAlgo_ShapeProcessor::ParameterMap& GetShapeFixParameters() const;
0274 
0275   //! Sets flags defining operations to be performed on shapes.
0276   //! @param theFlags The flags defining operations to be performed on shapes.
0277   Standard_EXPORT void SetShapeProcessFlags(const ShapeProcess::OperationsFlags& theFlags);
0278 
0279   //! Returns flags defining operations to be performed on shapes.
0280   //! @return Pair of values defining operations to be performed on shapes and a boolean value
0281   //!         that indicates whether the flags were set.
0282   Standard_EXPORT const XSAlgo_ShapeProcessor::ProcessingFlags& GetShapeProcessFlags() const;
0283 
0284 protected:
0285   //! Transfers labels to a STEP model
0286   //! Returns True if translation is OK
0287   //! isExternFile setting from transferExternFiles method
0288   bool transfer(STEPControl_Writer&                    theWriter,
0289                 const NCollection_Sequence<TDF_Label>& theLabels,
0290                 const STEPControl_StepModelType        theMode      = STEPControl_AsIs,
0291                 const char* const                      theIsMulti   = nullptr,
0292                 const bool                             isExternFile = false,
0293                 const Message_ProgressRange&           theProgress  = Message_ProgressRange());
0294 
0295   //! Parses assembly structure of label L, writes all the simple
0296   //! shapes each to its own file named by name of its label plus
0297   //! prefix
0298   //! Returns shape representing that assembly structure
0299   //! in the form of nested empty compounds (and a sequence of
0300   //! labels which are newly written nodes of this assembly)
0301   TopoDS_Shape transferExternFiles(
0302     const TDF_Label&                 theLabel,
0303     const STEPControl_StepModelType  theMode,
0304     NCollection_Sequence<TDF_Label>& theLabelSeq,
0305     const StepData_Factors&          theLocalFactors = StepData_Factors(),
0306     const char* const                thePrefix       = "",
0307     const Message_ProgressRange&     theProgress     = Message_ProgressRange());
0308 
0309   //! Write external references to STEP
0310   bool writeExternRefs(const occ::handle<XSControl_WorkSession>& theWS,
0311                        const NCollection_Sequence<TDF_Label>&    theLabels) const;
0312 
0313   //! Write colors assigned to specified labels, to STEP model
0314   bool writeColors(const occ::handle<XSControl_WorkSession>& theWS,
0315                    const NCollection_Sequence<TDF_Label>&    theLabels);
0316 
0317   //! Write names assigned to specified labels, to STEP model
0318   bool writeNames(const occ::handle<XSControl_WorkSession>& theWS,
0319                   const NCollection_Sequence<TDF_Label>&    theLabels) const;
0320 
0321   //! Write metadata assigned to specified labels, to STEP model
0322   bool writeMetadata(const occ::handle<XSControl_WorkSession>& theWS,
0323                      const NCollection_Sequence<TDF_Label>&    theLabels) const;
0324 
0325   //! Write metadata assigned to specified label, to STEP model.
0326   // Also recursively writes metadata for children labels.
0327   bool writeMetadataForLabel(const occ::handle<XSControl_WorkSession>& theWS,
0328                              const TDF_Label&                          theLabel) const;
0329 
0330   //! Write metadata representation item to STEP model.
0331   //! @param theKey The key for metadata item.
0332   //! @param theModel The STEP model to write to.
0333   //! @param theShapeDefRep The shape definition representation.
0334   //! @param theProdDef The product definition.
0335   //! @param theItem The representation item to write.
0336   void writeMetadataRepresentationItem(
0337     const TCollection_AsciiString&                              theKey,
0338     const occ::handle<StepData_StepModel>&                      theModel,
0339     const occ::handle<StepShape_ShapeDefinitionRepresentation>& theShapeDefRep,
0340     const occ::handle<StepBasic_ProductDefinition>&             theProdDef,
0341     const occ::handle<StepRepr_RepresentationItem>&             theItem) const;
0342 
0343   //! Write D&GTs assigned to specified labels, to STEP model
0344   bool writeDGTs(const occ::handle<XSControl_WorkSession>& theWS,
0345                  const NCollection_Sequence<TDF_Label>&    theLabels) const;
0346 
0347   //! Write D&GTs assigned to specified labels, to STEP model, according AP242
0348   bool writeDGTsAP242(const occ::handle<XSControl_WorkSession>& theWS,
0349                       const NCollection_Sequence<TDF_Label>&    theLabels,
0350                       const StepData_Factors& theLocalFactors = StepData_Factors());
0351 
0352   //! Write materials assigned to specified labels, to STEP model
0353   bool writeMaterials(const occ::handle<XSControl_WorkSession>& theWS,
0354                       const NCollection_Sequence<TDF_Label>&    theLabels) const;
0355 
0356   //! Write validation properties assigned to specified labels,
0357   //! to STEP model
0358   bool writeValProps(const occ::handle<XSControl_WorkSession>& theWS,
0359                      const NCollection_Sequence<TDF_Label>&    theLabels,
0360                      const char* const                         theIsMulti) const;
0361 
0362   //! Write layers assigned to specified labels, to STEP model
0363   bool writeLayers(const occ::handle<XSControl_WorkSession>& theWS,
0364                    const NCollection_Sequence<TDF_Label>&    theLabels) const;
0365 
0366   //! Write SHUO assigned to specified component, to STEP model
0367   bool writeSHUOs(const occ::handle<XSControl_WorkSession>& theWS,
0368                   const NCollection_Sequence<TDF_Label>&    theLabels);
0369 
0370   //! Finds length units located in root of label
0371   //! If it exists, initializes local length unit from it
0372   //! Else initializes according to Cascade length unit
0373   void prepareUnit(const TDF_Label&                       theLabel,
0374                    const occ::handle<StepData_StepModel>& theModel,
0375                    StepData_Factors&                      theLocalFactors);
0376 
0377   occ::handle<StepRepr_ShapeAspect> writeShapeAspect(
0378     const occ::handle<XSControl_WorkSession>&          theWS,
0379     const TDF_Label                                    theLabel,
0380     const TopoDS_Shape&                                theShape,
0381     occ::handle<StepRepr_RepresentationContext>&       theRC,
0382     occ::handle<StepAP242_GeometricItemSpecificUsage>& theGISU);
0383 
0384   void writePresentation(const occ::handle<XSControl_WorkSession>&    theWS,
0385                          const TopoDS_Shape&                          thePresentation,
0386                          const occ::handle<TCollection_HAsciiString>& thePrsName,
0387                          const bool                                   theHasSemantic,
0388                          const bool                                   theHasPlane,
0389                          const gp_Ax2&                                theAnnotationPlane,
0390                          const gp_Pnt&                                theTextPosition,
0391                          const occ::handle<Standard_Transient>&       theDimension,
0392                          const StepData_Factors& theLocalFactors = StepData_Factors());
0393 
0394   occ::handle<StepDimTol_Datum> writeDatumAP242(
0395     const occ::handle<XSControl_WorkSession>& theWS,
0396     const NCollection_Sequence<TDF_Label>&    theShapeL,
0397     const TDF_Label&                          theDatumL,
0398     const bool                                isFirstDTarget,
0399     const occ::handle<StepDimTol_Datum>&      theWrittenDatum,
0400     const StepData_Factors&                   theLocalFactors = StepData_Factors());
0401 
0402   void writeToleranceZone(const occ::handle<XSControl_WorkSession>&                 theWS,
0403                           const occ::handle<XCAFDimTolObjects_GeomToleranceObject>& theObject,
0404                           const occ::handle<StepDimTol_GeometricTolerance>&         theEntity,
0405                           const occ::handle<StepRepr_RepresentationContext>&        theRC);
0406 
0407   void writeGeomTolerance(
0408     const occ::handle<XSControl_WorkSession>&                                  theWS,
0409     const NCollection_Sequence<TDF_Label>&                                     theShapeSeqL,
0410     const TDF_Label&                                                           theGeomTolL,
0411     const occ::handle<NCollection_HArray1<StepDimTol_DatumSystemOrReference>>& theDatumSystem,
0412     const occ::handle<StepRepr_RepresentationContext>&                         theRC,
0413     const StepData_Factors& theLocalFactors = StepData_Factors());
0414 
0415 private:
0416   STEPControl_Writer                                                                   myWriter;
0417   NCollection_DataMap<TCollection_AsciiString, occ::handle<STEPCAFControl_ExternFile>> myFiles;
0418   NCollection_Map<TDF_Label>                                                           myRootLabels;
0419   NCollection_DataMap<TDF_Label, TopoDS_Shape>                                         myLabels;
0420   NCollection_DataMap<TDF_Label, occ::handle<STEPCAFControl_ExternFile>>               myLabEF;
0421   NCollection_DataMap<TDF_Label, TopoDS_Shape> myPureRefLabels;
0422   bool                                         myColorMode;
0423   bool                                         myNameMode;
0424   bool                                         myLayerMode;
0425   bool                                         myPropsMode;
0426   bool                                         myMetadataMode;
0427   bool                                         mySHUOMode;
0428   NCollection_DataMap<TopoDS_Shape, occ::handle<Standard_Transient>, TopTools_ShapeMapHasher>
0429                                                                      myMapCompMDGPR;
0430   bool                                                               myGDTMode;
0431   bool                                                               myMatMode;
0432   bool                                                               myVisMatMode;
0433   bool                                                               myIsCleanDuplicates;
0434   NCollection_DynamicArray<occ::handle<StepRepr_RepresentationItem>> myGDTAnnotations;
0435   occ::handle<StepVisual_DraughtingModel>                            myGDTPresentationDM;
0436   occ::handle<NCollection_HArray1<occ::handle<StepVisual_PresentationStyleAssignment>>>
0437                                                myGDTPrsCurveStyle;
0438   occ::handle<StepRepr_ProductDefinitionShape> myGDTCommonPDS;
0439 };
0440 
0441 #endif // _STEPCAFControl_Writer_HeaderFile