1
0
mirror of https://git.dev.opencascade.org/repos/occt.git synced 2025-09-18 14:27:39 +03:00

0032821: DEWrapper - Implementation of a common toolkit for importing and exporting CAD files

Implementing DE_Wrapper and its formats
This commit is contained in:
atychini
2022-02-16 13:22:23 +03:00
committed by afokin
parent b1970c8a47
commit 6d1a049be7
141 changed files with 12406 additions and 277 deletions

View File

@@ -2,3 +2,7 @@ RWStl.cxx
RWStl.hxx
RWStl_Reader.cxx
RWStl_Reader.hxx
RWStl_ConfigurationNode.cxx
RWStl_ConfigurationNode.hxx
RWStl_Provider.cxx
RWStl_Provider.hxx

View File

@@ -0,0 +1,185 @@
// Copyright (c) 2022 OPEN CASCADE SAS
//
// This file is part of Open CASCADE Technology software library.
//
// This library is free software; you can redistribute it and/or modify it under
// the terms of the GNU Lesser General Public License version 2.1 as published
// by the Free Software Foundation, with special exception defined in the file
// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
// distribution for complete text of the license and disclaimer of any warranty.
//
// Alternatively, this file may be used under the terms of Open CASCADE
// commercial license or contractual agreement.
#include <RWStl_ConfigurationNode.hxx>
#include <DE_ConfigurationContext.hxx>
#include <NCollection_Buffer.hxx>
#include <RWStl_Provider.hxx>
IMPLEMENT_STANDARD_RTTIEXT(RWStl_ConfigurationNode, DE_ConfigurationNode)
static const TCollection_AsciiString THE_CONFIGURATION_SCOPE = "provider";
//=======================================================================
// function : STEPCAFControl_ConfigurationNode
// purpose :
//=======================================================================
RWStl_ConfigurationNode::RWStl_ConfigurationNode() :
DE_ConfigurationNode()
{
UpdateLoad();
}
//=======================================================================
// function : STEPCAFControl_ConfigurationNode
// purpose :
//=======================================================================
RWStl_ConfigurationNode::RWStl_ConfigurationNode(const Handle(RWStl_ConfigurationNode)& theNode)
:DE_ConfigurationNode(theNode)
{
InternalParameters = theNode->InternalParameters;
UpdateLoad();
}
//=======================================================================
// function : Load
// purpose :
//=======================================================================
bool RWStl_ConfigurationNode::Load(const Handle(DE_ConfigurationContext)& theResource)
{
TCollection_AsciiString aScope = THE_CONFIGURATION_SCOPE + "." + GetFormat() + "." + GetVendor();
InternalParameters.ReadMergeAngle =
theResource->RealVal("read.merge.angle", InternalParameters.ReadMergeAngle, aScope);
InternalParameters.ReadBRep =
theResource->BooleanVal("read.brep", InternalParameters.ReadBRep, aScope);
InternalParameters.WriteAscii =
theResource->BooleanVal("write.ascii", InternalParameters.WriteAscii, aScope);
return true;
}
//=======================================================================
// function : Save
// purpose :
//=======================================================================
TCollection_AsciiString RWStl_ConfigurationNode::Save() const
{
TCollection_AsciiString aResult;
aResult += "!*****************************************************************************\n";
aResult = aResult + "!Configuration Node " + " Vendor: " + GetVendor() + " Format: " + GetFormat() + "\n";
TCollection_AsciiString aScope = THE_CONFIGURATION_SCOPE + "." + GetFormat() + "." + GetVendor() + ".";
aResult += "!\n";
aResult += "!Read parameters:\n";
aResult += "!\n";
aResult += "!\n";
aResult += "!Input merge angle value\n";
aResult += "!Default value (in degrees): 90.0. Angle should be within [0.0, 90.0] range\n";
aResult += aScope + "read.merge.angle :\t " + InternalParameters.ReadMergeAngle + "\n";
aResult += "!\n";
aResult += "!\n";
aResult += "!Setting up Boundary Representation flag\n";
aResult += "!Default value: false. Available values: \"on\", \"off\"\n";
aResult += aScope + "read.brep :\t " + InternalParameters.ReadBRep + "\n";
aResult += "!\n";
aResult += "!\n";
aResult += "!Write parameters:\n";
aResult += "!\n";
aResult += "!\n";
aResult += "!Setting up writing mode (Ascii or Binary)\n";
aResult += "!Default value: 1(Binary). Available values: 0(Ascii), 1(Binary)\n";
aResult += aScope + "write.ascii :\t " + InternalParameters.WriteAscii + "\n";
aResult += "!\n";
aResult += "!*****************************************************************************\n";
return aResult;
}
//=======================================================================
// function : Copy
// purpose :
//=======================================================================
Handle(DE_ConfigurationNode) RWStl_ConfigurationNode::Copy() const
{
return new RWStl_ConfigurationNode(*this);
}
//=======================================================================
// function : BuildProvider
// purpose :
//=======================================================================
Handle(DE_Provider) RWStl_ConfigurationNode::BuildProvider()
{
return new RWStl_Provider(this);
}
//=======================================================================
// function : IsImportSupported
// purpose :
//=======================================================================
bool RWStl_ConfigurationNode::IsImportSupported() const
{
return true;
}
//=======================================================================
// function : IsExportSupported
// purpose :
//=======================================================================
bool RWStl_ConfigurationNode::IsExportSupported() const
{
return true;
}
//=======================================================================
// function : GetFormat
// purpose :
//=======================================================================
TCollection_AsciiString RWStl_ConfigurationNode::GetFormat() const
{
return TCollection_AsciiString("STL");
}
//=======================================================================
// function : GetVendor
// purpose :
//=======================================================================
TCollection_AsciiString RWStl_ConfigurationNode::GetVendor() const
{
return TCollection_AsciiString("OCC");
}
//=======================================================================
// function : GetExtensions
// purpose :
//=======================================================================
TColStd_ListOfAsciiString RWStl_ConfigurationNode::GetExtensions() const
{
TColStd_ListOfAsciiString anExt;
anExt.Append("stl");
return anExt;
}
//=======================================================================
// function : CheckContent
// purpose :
//=======================================================================
bool RWStl_ConfigurationNode::CheckContent(const Handle(NCollection_Buffer)& theBuffer) const
{
if (theBuffer.IsNull() || theBuffer->Size() < 7)
{
return false;
}
const char* aBytes = (const char*)theBuffer->Data();
if (!(::strncmp(aBytes, "solid", 5) || ::strncmp(aBytes, "SOLID", 5)) && isspace(aBytes[5]))
{
return true;
}
// binary STL has no header for identification - format can be detected only by file extension
return false;
}

View File

@@ -0,0 +1,99 @@
// Copyright (c) 2022 OPEN CASCADE SAS
//
// This file is part of Open CASCADE Technology software library.
//
// This library is free software; you can redistribute it and/or modify it under
// the terms of the GNU Lesser General Public License version 2.1 as published
// by the Free Software Foundation, with special exception defined in the file
// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
// distribution for complete text of the license and disclaimer of any warranty.
//
// Alternatively, this file may be used under the terms of Open CASCADE
// commercial license or contractual agreement.
#ifndef _RWStl_ConfigurationNode_HeaderFile
#define _RWStl_ConfigurationNode_HeaderFile
#include <DE_ConfigurationNode.hxx>
//! The purpose of this class is to configure the transfer process for STL format
//! Stores the necessary settings for RWStl_Provider.
//! Configures and creates special provider to transfer STL files.
//!
//! Nodes grouped by Vendor name and Format type.
//! The Vendor name is "OCC"
//! The Format type is "STL"
//! The supported CAD extension is ".stl"
//! The import process is supported.
//! The export process is supported.
class RWStl_ConfigurationNode : public DE_ConfigurationNode
{
DEFINE_STANDARD_RTTIEXT(RWStl_ConfigurationNode, DE_ConfigurationNode)
public:
//! Initializes all field by default
Standard_EXPORT RWStl_ConfigurationNode();
//! Copies values of all fields
//! @param[in] theNode object to copy
Standard_EXPORT RWStl_ConfigurationNode(const Handle(RWStl_ConfigurationNode)& theNode);
//! Updates values according the resource
//! @param[in] theResource input resource to use
//! @return true if theResource loading has ended correctly
Standard_EXPORT virtual bool Load(const Handle(DE_ConfigurationContext)& theResource) Standard_OVERRIDE;
//! Writes configuration to the string
//! @return result resource string
Standard_EXPORT virtual TCollection_AsciiString Save() const Standard_OVERRIDE;
//! Copies values of all fields
//! @return new object with the same field values
Standard_EXPORT virtual Handle(DE_ConfigurationNode) Copy() const Standard_OVERRIDE;
//! Creates new provider for the own format
//! @return new created provider
Standard_EXPORT virtual Handle(DE_Provider) BuildProvider() Standard_OVERRIDE;
public:
//! Checks the import supporting
//! @return true if import is supported
Standard_EXPORT virtual bool IsImportSupported() const Standard_OVERRIDE;
//! Checks the export supporting
//! @return true if export is supported
Standard_EXPORT virtual bool IsExportSupported() const Standard_OVERRIDE;
//! Gets CAD format name of associated provider
//! @return provider CAD format
Standard_EXPORT virtual TCollection_AsciiString GetFormat() const Standard_OVERRIDE;
//! Gets provider's vendor name of associated provider
//! @return provider's vendor name
Standard_EXPORT virtual TCollection_AsciiString GetVendor() const Standard_OVERRIDE;
//! Gets list of supported file extensions
//! @return list of extensions
Standard_EXPORT virtual TColStd_ListOfAsciiString GetExtensions() const Standard_OVERRIDE;
//! Checks the file content to verify a format
//! @param[in] theBuffer read stream buffer to check content
//! @return Standard_True if file is supported by a current provider
Standard_EXPORT virtual bool CheckContent(const Handle(NCollection_Buffer)& theBuffer) const Standard_OVERRIDE;
public:
struct RWStl_InternalSection
{
// Read
double ReadMergeAngle = 90.; //!< Input merge angle value
bool ReadBRep = false; //!< Setting up Boundary Representation flag
// Write
bool WriteAscii = true; //!< Setting up writing mode (Ascii or Binary)
} InternalParameters;
};
#endif // _RWStl_ConfigurationNode_HeaderFile

View File

@@ -0,0 +1,251 @@
// Copyright (c) 2022 OPEN CASCADE SAS
//
// This file is part of Open CASCADE Technology software library.
//
// This library is free software; you can redistribute it and/or modify it under
// the terms of the GNU Lesser General Public License version 2.1 as published
// by the Free Software Foundation, with special exception defined in the file
// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
// distribution for complete text of the license and disclaimer of any warranty.
//
// Alternatively, this file may be used under the terms of Open CASCADE
// commercial license or contractual agreement.
#include <RWStl_Provider.hxx>
#include <BinXCAFDrivers.hxx>
#include <BRep_Builder.hxx>
#include <Message.hxx>
#include <RWStl.hxx>
#include <RWStl_ConfigurationNode.hxx>
#include <StlAPI.hxx>
#include <StlAPI_Writer.hxx>
#include <TDocStd_Document.hxx>
#include <XCAFDoc_ShapeTool.hxx>
#include <XCAFDoc_DocumentTool.hxx>
IMPLEMENT_STANDARD_RTTIEXT(RWStl_Provider, DE_Provider)
//=======================================================================
// function : RWStl_Provider
// purpose :
//=======================================================================
RWStl_Provider::RWStl_Provider()
{}
//=======================================================================
// function : RWStl_Provider
// purpose :
//=======================================================================
RWStl_Provider::RWStl_Provider(const Handle(DE_ConfigurationNode)& theNode)
:DE_Provider(theNode)
{}
//=======================================================================
// function : Read
// purpose :
//=======================================================================
bool RWStl_Provider::Read(const TCollection_AsciiString& thePath,
const Handle(TDocStd_Document)& theDocument,
Handle(XSControl_WorkSession)& theWS,
const Message_ProgressRange& theProgress)
{
(void)theWS;
return Read(thePath, theDocument, theProgress);
}
//=======================================================================
// function : Write
// purpose :
//=======================================================================
bool RWStl_Provider::Write(const TCollection_AsciiString& thePath,
const Handle(TDocStd_Document)& theDocument,
Handle(XSControl_WorkSession)& theWS,
const Message_ProgressRange& theProgress)
{
(void)theWS;
return Write(thePath, theDocument, theProgress);
}
//=======================================================================
// function : Read
// purpose :
//=======================================================================
bool RWStl_Provider::Read(const TCollection_AsciiString& thePath,
const Handle(TDocStd_Document)& theDocument,
const Message_ProgressRange& theProgress)
{
if (theDocument.IsNull())
{
Message::SendFail() << "Error in the RWStl_Provider during reading the file " <<
thePath << "\t: theDocument shouldn't be null";
return false;
}
TopoDS_Shape aShape;
if (!Read(thePath, aShape, theProgress))
{
return false;
}
Handle(XCAFDoc_ShapeTool) aShapeTool = XCAFDoc_DocumentTool::ShapeTool(theDocument->Main());
aShapeTool->AddShape(aShape);
return true;
}
//=======================================================================
// function : Write
// purpose :
//=======================================================================
bool RWStl_Provider::Write(const TCollection_AsciiString& thePath,
const Handle(TDocStd_Document)& theDocument,
const Message_ProgressRange& theProgress)
{
TopoDS_Shape aShape;
TDF_LabelSequence aLabels;
Handle(XCAFDoc_ShapeTool) aSTool = XCAFDoc_DocumentTool::ShapeTool(theDocument->Main());
aSTool->GetFreeShapes(aLabels);
if (aLabels.Length() <= 0)
{
Message::SendFail() << "Error in the RWStl_Provider during writing the file " <<
thePath << "\t: Document contain no shapes";
return false;
}
if (aLabels.Length() == 1)
{
aShape = aSTool->GetShape(aLabels.Value(1));
}
else
{
TopoDS_Compound aComp;
BRep_Builder aBuilder;
aBuilder.MakeCompound(aComp);
for (Standard_Integer anIndex = 1; anIndex <= aLabels.Length(); anIndex++)
{
TopoDS_Shape aS = aSTool->GetShape(aLabels.Value(anIndex));
aBuilder.Add(aComp, aS);
}
aShape = aComp;
}
return Write(thePath, aShape, theProgress);
}
//=======================================================================
// function : Read
// purpose :
//=======================================================================
bool RWStl_Provider::Read(const TCollection_AsciiString& thePath,
TopoDS_Shape& theShape,
Handle(XSControl_WorkSession)& theWS,
const Message_ProgressRange& theProgress)
{
(void)theWS;
return Read(thePath, theShape, theProgress);
}
//=======================================================================
// function : Write
// purpose :
//=======================================================================
bool RWStl_Provider::Write(const TCollection_AsciiString& thePath,
const TopoDS_Shape& theShape,
Handle(XSControl_WorkSession)& theWS,
const Message_ProgressRange& theProgress)
{
(void)theWS;
return Write(thePath, theShape, theProgress);
}
//=======================================================================
// function : Read
// purpose :
//=======================================================================
bool RWStl_Provider::Read(const TCollection_AsciiString& thePath,
TopoDS_Shape& theShape,
const Message_ProgressRange& theProgress)
{
Message::SendWarning() << "OCCT Stl reader does not support model scaling according to custom length unit";
if (!GetNode()->IsKind(STANDARD_TYPE(RWStl_ConfigurationNode)))
{
Message::SendFail() << "Error in the RWStl_Provider during reading the file " <<
thePath << "\t: Incorrect or empty Configuration Node";
return true;
}
Handle(RWStl_ConfigurationNode) aNode = Handle(RWStl_ConfigurationNode)::DownCast(GetNode());
double aMergeAngle = aNode->InternalParameters.ReadMergeAngle * M_PI / 180.0;
if(aMergeAngle != M_PI_2)
{
if (aMergeAngle < 0.0 || aMergeAngle > M_PI_2)
{
Message::SendFail() << "Error in the RWStl_Provider during reading the file " <<
thePath << "\t: The merge angle is out of the valid range";
return false;
}
}
if (!aNode->InternalParameters.ReadBRep)
{
Handle(Poly_Triangulation) aTriangulation = RWStl::ReadFile(thePath.ToCString(), aMergeAngle, theProgress);
TopoDS_Face aFace;
BRep_Builder aB;
aB.MakeFace(aFace);
aB.UpdateFace(aFace, aTriangulation);
theShape = aFace;
}
else
{
Standard_DISABLE_DEPRECATION_WARNINGS
if (!StlAPI::Read(theShape, thePath.ToCString()))
{
Message::SendFail() << "Error in the RWStl_Provider during reading the file " << thePath;
return false;
}
Standard_ENABLE_DEPRECATION_WARNINGS
}
return true;
}
//=======================================================================
// function : Write
// purpose :
//=======================================================================
bool RWStl_Provider::Write(const TCollection_AsciiString& thePath,
const TopoDS_Shape& theShape,
const Message_ProgressRange& theProgress)
{
Message::SendWarning() << "OCCT Stl writer does not support model scaling according to custom length unit";
if (GetNode().IsNull() || !GetNode()->IsKind(STANDARD_TYPE(RWStl_ConfigurationNode)))
{
Message::SendFail() << "Error in the RWStl_Provider during reading the file " <<
thePath << "\t: Incorrect or empty Configuration Node";
return false;
}
Handle(RWStl_ConfigurationNode) aNode = Handle(RWStl_ConfigurationNode)::DownCast(GetNode());
StlAPI_Writer aWriter;
aWriter.ASCIIMode() = aNode->InternalParameters.WriteAscii;
if (!aWriter.Write(theShape, thePath.ToCString(), theProgress))
{
Message::SendFail() << "Error in the RWStl_Provider during reading the file " <<
thePath << "\t: Mesh writing has been failed";
return false;
}
return true;
}
//=======================================================================
// function : GetFormat
// purpose :
//=======================================================================
TCollection_AsciiString RWStl_Provider::GetFormat() const
{
return TCollection_AsciiString("STL");
}
//=======================================================================
// function : GetVendor
// purpose :
//=======================================================================
TCollection_AsciiString RWStl_Provider::GetVendor() const
{
return TCollection_AsciiString("OCC");
}

View File

@@ -0,0 +1,136 @@
// Copyright (c) 2022 OPEN CASCADE SAS
//
// This file is part of Open CASCADE Technology software library.
//
// This library is free software; you can redistribute it and/or modify it under
// the terms of the GNU Lesser General Public License version 2.1 as published
// by the Free Software Foundation, with special exception defined in the file
// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
// distribution for complete text of the license and disclaimer of any warranty.
//
// Alternatively, this file may be used under the terms of Open CASCADE
// commercial license or contractual agreement.
#ifndef _RWStl_Provider_HeaderFile
#define _RWStl_Provider_HeaderFile
#include <DE_Provider.hxx>
//! The class to transfer STL files.
//! Reads and Writes any STL files into/from OCCT.
//! Each operation needs configuration node.
//!
//! Providers grouped by Vendor name and Format type.
//! The Vendor name is "OCC"
//! The Format type is "STL"
//! The import process is supported.
//! The export process is supported.
class RWStl_Provider : public DE_Provider
{
public:
DEFINE_STANDARD_RTTIEXT(RWStl_Provider, DE_Provider)
public:
//! Default constructor
//! Configure translation process with global configuration
Standard_EXPORT RWStl_Provider();
//! Configure translation process
//! @param[in] theNode object to copy
Standard_EXPORT RWStl_Provider(const Handle(DE_ConfigurationNode)& theNode);
public:
//! Reads a CAD file, according internal configuration
//! @param[in] thePath path to the import CAD file
//! @param[out] theDocument document to save result
//! @param[in] theWS current work session
//! @param theProgress[in] progress indicator
//! @return true if Read operation has ended correctly
Standard_EXPORT virtual Standard_Boolean Read(const TCollection_AsciiString& thePath,
const Handle(TDocStd_Document)& theDocument,
Handle(XSControl_WorkSession)& theWS,
const Message_ProgressRange& theProgress = Message_ProgressRange()) Standard_OVERRIDE;
//! Writes a CAD file, according internal configuration
//! @param[in] thePath path to the export CAD file
//! @param[out] theDocument document to export
//! @param[in] theWS current work session
//! @param theProgress[in] progress indicator
//! @return true if Write operation has ended correctly
Standard_EXPORT virtual Standard_Boolean Write(const TCollection_AsciiString& thePath,
const Handle(TDocStd_Document)& theDocument,
Handle(XSControl_WorkSession)& theWS,
const Message_ProgressRange& theProgress = Message_ProgressRange()) Standard_OVERRIDE;
//! Reads a CAD file, according internal configuration
//! @param[in] thePath path to the import CAD file
//! @param[out] theDocument document to save result
//! @param theProgress[in] progress indicator
//! @return true if Read operation has ended correctly
Standard_EXPORT virtual Standard_Boolean Read(const TCollection_AsciiString& thePath,
const Handle(TDocStd_Document)& theDocument,
const Message_ProgressRange& theProgress = Message_ProgressRange()) Standard_OVERRIDE;
//! Writes a CAD file, according internal configuration
//! @param[in] thePath path to the export CAD file
//! @param[out] theDocument document to export
//! @param theProgress[in] progress indicator
//! @return true if Write operation has ended correctly
Standard_EXPORT virtual Standard_Boolean Write(const TCollection_AsciiString& thePath,
const Handle(TDocStd_Document)& theDocument,
const Message_ProgressRange& theProgress = Message_ProgressRange()) Standard_OVERRIDE;
//! Reads a CAD file, according internal configuration
//! @param[in] thePath path to the import CAD file
//! @param[out] theShape shape to save result
//! @param[in] theWS current work session
//! @param theProgress[in] progress indicator
//! @return true if Read operation has ended correctly
Standard_EXPORT virtual Standard_Boolean Read(const TCollection_AsciiString& thePath,
TopoDS_Shape& theShape,
Handle(XSControl_WorkSession)& theWS,
const Message_ProgressRange& theProgress = Message_ProgressRange()) Standard_OVERRIDE;
//! Writes a CAD file, according internal configuration
//! @param[in] thePath path to the export CAD file
//! @param[out] theShape shape to export
//! @param[in] theWS current work session
//! @param theProgress[in] progress indicator
//! @return true if Write operation has ended correctly
Standard_EXPORT virtual Standard_Boolean Write(const TCollection_AsciiString& thePath,
const TopoDS_Shape& theShape,
Handle(XSControl_WorkSession)& theWS,
const Message_ProgressRange& theProgress = Message_ProgressRange()) Standard_OVERRIDE;
//! Reads a CAD file, according internal configuration
//! @param[in] thePath path to the import CAD file
//! @param[out] theShape shape to save result
//! @param theProgress[in] progress indicator
//! @return true if Read operation has ended correctly
Standard_EXPORT virtual Standard_Boolean Read(const TCollection_AsciiString& thePath,
TopoDS_Shape& theShape,
const Message_ProgressRange& theProgress = Message_ProgressRange()) Standard_OVERRIDE;
//! Writes a CAD file, according internal configuration
//! @param[in] thePath path to the export CAD file
//! @param[out] theShape shape to export
//! @param theProgress[in] progress indicator
//! @return true if Write operation has ended correctly
Standard_EXPORT virtual Standard_Boolean Write(const TCollection_AsciiString& thePath,
const TopoDS_Shape& theShape,
const Message_ProgressRange& theProgress = Message_ProgressRange()) Standard_OVERRIDE;
public:
//! Gets CAD format name of associated provider
//! @return provider CAD format
Standard_EXPORT virtual TCollection_AsciiString GetFormat() const Standard_OVERRIDE;
//! Gets provider's vendor name of associated provider
//! @return provider's vendor name
Standard_EXPORT virtual TCollection_AsciiString GetVendor() const Standard_OVERRIDE;
};
#endif // _RWStl_Provider_HeaderFile